Python Forum
How to I make my program release memory? - 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: How to I make my program release memory? (/thread-1251.html)

Pages: 1 2


How to I make my program release memory? - Plecto - Dec-18-2016

Sample code is as follows:

import numpy as np
import time
import sounddevice as sd


def FindFreq_Resp(f_num, Amp):
    print "Calculating frequency response..."
    Freq=np.logspace(1.3, 4.3, num=f_num)
    for f in Freq:
            xx=np.linspace(0.0, 1, 48000)
            y=Amp*np.sin(f*2.0*np.pi*xx)
            myrecording = sd.playrec(y, 48000, channels=1, blocking=True)        

Fs=48000
FindFreq_Resp(100,0.1)
print "Some memory has been eaten"
FindFreq_Resp(100,0.1)
print "Even more memory has been eaten"
FindFreq_Resp(100,0.1)
print "Not much memory left :("
If I run the function enough times, it will use up all of the memory and the program will freeze :( I'm coding on a raspberry PI 1


RE: How to I make my program release memory? - Larz60+ - Dec-18-2016

Memory will be released as soon as it goes completely out of scope
That said, it doesn't actually get released until garbage collection takes place
or until needed by the program.
suggest reading https://docs.python.org/3/c-api/memory.html


RE: How to I make my program release memory? - Plecto - Dec-18-2016

I read the article you linked, but couldn't quite understand all of it. You said that memory will be released as soon as it goes out of scope or a different program will need it, but that clearly doesn't happen with the code I showed. What do I do?


RE: How to I make my program release memory? - Larz60+ - Dec-18-2016

Quote:it doesn't actually get released until garbage collection takes place

or until needed by the program.

It will be cleaned eventually.

Out of scope - read this http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html


RE: How to I make my program release memory? - Plecto - Dec-18-2016

If this is not a memory issue, then it's something else. When swap memory reaches 0, the program freezes indefinitely. I'm working on an amplifier analyzer, but my whole project has come to a stop because of this issue :(


RE: How to I make my program release memory? - Larz60+ - Dec-18-2016

You're right, that sounds like your Linux swap partition is too small.
I believe there are ways to adjust partition size on an existing system
I think the program that does this is called gparted or similar. I'm not an expert
on the subject, so perhaps someone else can comment


RE: How to I make my program release memory? - wavic - Dec-18-2016

Hello!

# First, create a file
sudo dd if=/dev/zero of=/path/swap.img bs=2048 count=1M

#Format it
sudo mkswap /path/swap.img

# Open and add an entry to /etc/fstab file like this. As root. Save it.
/path/swap.img swap swap sw    0   0

# Make it active
sudo swapon /path/swap.img
This will create 2GB swap file. 

To open /etc/fstab as root into the default editor

sudo xdg-open /etc/fstab



RE: How to I make my program release memory? - Plecto - Dec-18-2016

Why is this happening though? Why does it insist of storing these variables in memory despite crashing the program doing so?

Quote:pi@raspberrypi:~ $ sudo dd if=/dev/zero of=/path/swap.img bs=2048 count=1M

dd: failed to open '/path/swap.img': No such file or directory

:(

Thanks for the replies by the way, I would really want to fix this issue :(


RE: How to I make my program release memory? - wavic - Dec-18-2016

/path/swap.img is just an exapmle of PATH to an IMG file you are creating. Chose a directory and name for the file and use them in place of /path/swap.img.


RE: How to I make my program release memory? - micseydel - Dec-18-2016

Increasing swap won't necessarily increase the speed of the program - in fact, it very well slow things down, if your script needs to use swap, since then you'll be using disk instead of RAM when disk is so much slower. It might only help if other programs are put in swap, but that can still cause problems if those programs have to get pulled out and your computer generally slows down.

This also isn't a scope issue. the sounddevice.playrec call, from what I can tell, is keeping what you passed to it and it's essentially a memory leak from your perspective. The docs for that function say that it records what you pass it, and I tried just using sounddevice.play (and dropped the channel argument) which unfortunately still didn't help. So, your problem lies either with how you're using the module or in the module itself (potentially as a bug).

I tried a few workarounds, like using gc.collect() and reloading the module, and can't get it to do the right thing. After looking over the docs closely, I recommend you contact the author or look for a more specialized forum.