Python Forum
How to I make my program release memory?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to I make my program release memory?
#1
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
Reply
#2
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
Reply
#3
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?
Reply
#4
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/...Scope.html
Reply
#5
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 :(
Reply
#6
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
Reply
#7
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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 :(
Reply
#9
/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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,152 Feb-25-2022, 06:31 PM
Last Post: jao
  How to make a Vocal synthesizer program on Python? Seadust 3 3,548 Jan-28-2021, 06:26 PM
Last Post: gonnach
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,307 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
  I try to make Heron formula program abcd 7 3,389 Oct-22-2020, 12:48 AM
Last Post: abcd
  Make a Python program executable in Windows Pedroski55 1 2,101 Sep-26-2020, 12:34 AM
Last Post: bowlofred
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,729 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  how to make a program with a certain number of multiples? syafiq14 3 2,756 Jan-01-2020, 02:39 PM
Last Post: syafiq14
  How to get mouse coordinates on click and release OhNoSegFaultAgain 1 2,987 May-17-2019, 06:56 PM
Last Post: buran
  How to make a program run "online"? DanielTkach 2 2,535 Apr-09-2019, 11:59 PM
Last Post: DanielTkach
  Python 3.4: the only release to create .EXE standalone without .dll samsonite 7 5,784 Feb-28-2019, 09:20 AM
Last Post: samsonite

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020