Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing sine curve in Memory-Used view of Task Manager on Windows 10?

I am trying to write a simple proof-of-concept script on Windows 10 that let's me draw the absolute of a sin curve in the task manager memory window.

My code is as follows:

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))

What I am getting is as follows: Image

What I want is this: Image 2

Do you have an idea of what I am doing wrong? My guess is that is is something within the if loop or something regarding the garbage collector.

Any help is appreciated. Thanks :)

like image 542
Lukas Hejcman Avatar asked Nov 24 '25 17:11

Lukas Hejcman


1 Answers

sys.getsizeof(string_drawer)

Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

And you are appending to a list of strings so the getsizeof will return the memory attributed to the list not the size of the strings of spaces it refers to. Change the list for a string

string_drawer = ''
string_drawer += ' ' * y

but then you can and should use len instead of sys.getsizeof because the later adds the size of the garbage collector (although if the string is large enough it is negligible), also if you want to do nothing then do nothing, remove the lines:

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

and to reset the string do string_drawer = '', not as you do with the list string_drawer = [] * y

Update: Since Python 3.6.2 sys.getsizeof was changed to account for the size of the referenced objects too.

bpo-12414: sys.getsizeof() on a code object now returns the sizes which includes the code struct and sizes of objects which it references. Patch by Dong-hee Na.

like image 91
Kronen Avatar answered Nov 27 '25 06:11

Kronen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!