Python Forum
Drawing a sine curve in memory view of Task Manager - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Drawing a sine curve in memory view of Task Manager (/thread-97.html)



Drawing a sine curve in memory view of Task Manager - Lukiewookie - Sep-19-2016

Hi guys [Image: icon_e_smile.gif]

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

        pass

    #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 this:
[Image: ahEsx.png]

What I want is this:
[Image: UXPYc.png]

I am not entirely sure what I am doing wrong, but I suspect it is something to do with the garbage collection in Python. I've extensively searched for the answer, but I didn't find anything that worked...

I hope you guys can help me. Thanks!


RE: Drawing a sine curve in memory view of Task Manager - Ofnuts - Sep-19-2016

See my answer to your answer on the other forum?

This forum is the newer version of the other one, the same people answer...


RE: Drawing a sine curve in memory view of Task Manager - Kebap - Sep-19-2016

The garbace will not be collected immediately when you call it, but at some arbitrary time, which can't be influenced directly. It depends on many things, maybe that is why you see the slow build-up and sharp decline there. By the way, which concept are you proofing? :D


RE: Drawing a sine curve in memory view of Task Manager - Ofnuts - Sep-19-2016

Doh... Now I understand what you want... Highly unlikely to work. Even assuming that the GC actually does something when you call it, it won't always return te recovered memory to the system.


RE: Drawing a sine curve in memory view of Task Manager - Lukiewookie - Sep-20-2016

(Sep-19-2016, 02:02 PM)Kebap Wrote: The garbace will not be collected immediately when you call it, but at some arbitrary time, which can't be influenced directly. It depends on many things, maybe that is why you see the slow build-up and sharp decline there. By the way, which concept are you proofing? :D

We started doing something about memory in my CS class, and then I had maths where we had some graphing exercises. I was wondering if what I want to do is possible. That's really it :P

(Sep-19-2016, 07:00 PM)Ofnuts Wrote: Doh... Now I understand what you want... Highly unlikely to work. Even assuming that the GC actually does something when you call it, it won't always return te recovered memory to the system.

Ok, I seem to understand. I've read here(https://stackoverflow.com/questions/15455048/releasing-memory-in-python) that if I spawn child processes, it shoudl release the memory back to the system. Another alternative I have thought of is making a 2D array, where each row is a diffferent length to correspond to how much memory must be used, and then deleting the rows in such a way that they create a sine curve. However, I don't know if I could get around the garbage collector issue that way :/


RE: Drawing a sine curve in memory view of Task Manager - Ofnuts - Sep-20-2016

Another solution:

Create many subprocesses:
  • All subprocesses will try to grab a visible chunk of memory (say 50M).
  • You start the processes at regular intervals, and each process exits after some delay, that is specific to it, with processes started first having longer lifespans.
  • So the first process grabs memory for 20 seconds, the seconds starts one second later but has a lifespan of 18 secs, and the third has a life span of 16 seconds, etc.
  • Once you detect the end of the first process, do it again.
This should create a (stepped) triangular wave. By adjusting the delays and the life spans, you can approximate your curve.