2016-09-14 2 views
3

Windows 10에서 간단한 개념 증명 스크립트를 작성하여 작업 관리자 메모리 창에 죄 곡선의 절대 값을 그려 보겠습니다. Image메모리에 사인 곡선 그리기 - Windows 10에서 작업 관리자보기 사용?

는 내가 원하는 것은 이것이다 : 다음과 같이 내가 얻고 것은

import time 
import math 
import gc 
import sys 

x = 1 
string_drawer = [] 

while True: 

    #Formula for the eqaution (sin curve) 
    y = (abs(math.sin(math.radians(100*x))))*512000000 
    print (y, type(y)) 

    #Making y type 'int' so that it can be used to append 
    y = int(round(y)) 
    print (y, type(y)) 

    #Checking the size of string_drawer for debugging 
    print(sys.getsizeof(string_drawer)) 

    #Loop used for appending 
    if sys.getsizeof(string_drawer) < y: #If y is bigger, find the difference and append 
     y = y - sys.getsizeof(string_drawer) 
     string_drawer.append(' ' *y) 
    elif sys.getsizeof(string_drawer) > y: #If y is smaller, delete the variable and make a new one 
     string_drawer = [] *y 
    else: #If y is the same size as string_drawer, do nothing 
     string_drawer = string_drawer 

    #Call the Python gerbage colector 
    gc.collect() 

    #Sleep to make sure Task Manager catches the change in RAM usage 
    time.sleep(0.5) 

    #Increment x 
    x += 1 
    print(x, type(x)) 

다음과 같이

내 코드는 Image 2

당신의 아이디어를 가지고 있습니까 무엇을 내가 잘못하고있는거야? 내 생각 엔 if 루프 또는 가비지 컬렉터와 관련된 것입니다.

도움을 주시면 감사하겠습니다. 감사합니다 :)

답변

1
sys.getsizeof(string_drawer) 

반환 바이트 객체의 크기입니다. 객체는 모든 유형의 객체 일 수 있습니다. 모든 내장 객체는 올바른 결과를 반환하지만 구현 특정이므로이 은 타사 확장을 위해 true 일 필요가 없습니다.

개체에 직접 기인 한 메모리 소비 만이 참조되는 개체의 메모리 소비가 아니라 설명됩니다.

그리고 getsizeof는 참조하는 공백 문자열의 크기가 아닌 목록에 할당 된 메모리를 반환하도록 문자열 목록에 추가합니다. 나중에는 가비지 컬렉터의 크기를 추가하기 때문에 문자열

string_drawer = '' 
string_drawer += ' ' * y 

의 목록을 변경 하지만 당신과 sys.getsizeof 대신 len를 사용해야 할 수 있습니다 (비록 문자열은 무시할 정도로 큰 경우)도 경우,

else: #If y is the same size as string_drawer, do nothing 
    string_drawer = string_drawer 

하고 목록 string_drawer = [] * y

로하지, string_drawer = ''를 않는 문자열을 재설정 : 당신은 다음, 아무것도하지 않고 아무것도 할 줄을 제거하려면 업데이트 : 파이썬 3.6.2 sys.getsizeof이 참조 된 개체의 크기도 고려하여 변경되었습니다.

bpo-12414 : 코드 객체의 sys.getsizeof()는 코드 구조체와 참조하는 객체의 크기를 포함하는 크기를 반환합니다. Na Dong-Hee 님이 패치했습니다.