fred basset
fred basset

Reputation: 10092

How to diagnose potential memory leak in Python program?

I have an embedded Linux system with 256MB of RAM. There is a large codebase running on it, most in C++ but some utilities are in Python. We have a Python "package manager" that handles updates to the system using the Python apt module (we distribute our updates as .deb files). When running, this application uses a large portion of the system RAM. I am monitoring RAM usage with top, looking at the RSS for the Python process (maybe this is not a valid way to analyze process memory usage? Open to suggestions).

The line

    cache = apt.Cache() 

which is called periodically to check on the status of the system seems to consume about 20MB each time it is called, and it doesn't look like this memory is being returned. I tried deleting the cache object at the end of the function and running gc.collect(), that didn't seem to help much.

How can I reduce the memory usage of this program?

The platform is ARM Cortex A8, running Linux 3.2, Debian Wheezy and Python 2.7.3.

Upvotes: 3

Views: 4315

Answers (2)

jsalonen
jsalonen

Reputation: 30531

Garbage Collector interface module (gc) is exactly what you could use here to further debug this problem.

To debug a leaking program call:

gc.set_debug(gc.DEBUG_LEAK)

This will output debugging information about garbage collection into your console.

For further debugging, gc module provides more facilities, including ability to list out all objects tracked by the garbage collector, disable/enable collector or manually trigger garbage collection.

If you don't have a leak and want to just lower memory usage, try using gc.set_threshold to trigger garbage collection at a lower memory usage point.

Upvotes: 7

GitaarLAB
GitaarLAB

Reputation: 14655

The clean command is used to free up the disk space by cleaning retrieved (downloaded) .deb files (packages) from the local repository.

$ sudo apt-get clean

Hope this helps!

Upvotes: -1

Related Questions