MurpheyLab / trep

Trep: Mechanical Simulation and Optimal Control Software
https://murpheylab.github.io/trep/
GNU General Public License v3.0
18 stars 11 forks source link

Memory leak when creating system #17

Open adwilson10 opened 10 years ago

adwilson10 commented 10 years ago

From vlad.seg...@gmail.com on February 28, 2013 16:20:21

Suppose I create a system object and then overwrite it. The memory allocated to the first object will not be freed and extra memory will be allocated for the second system object, even though no references to the first object exist. Forcing a garbage collection by running gc.collect() does not work. The large objects that are left behind are almost certainly the third and fourth derivatives of g and vb as they appear in _structure_changed() in frames.py

Original issue: http://code.google.com/p/trep/issues/detail?id=17

adwilson10 commented 10 years ago

From elliot.r...@gmail.com on February 28, 2013 14:41:10

Can you attach an example script that demonstrates this?

Owner: elliot.r...@gmail.com

adwilson10 commented 10 years ago

From vlad.seg...@gmail.com on February 28, 2013 15:16:54

Here is one. Adjust the number of links in case what I used overloads your machine.

Attachment: memory_leak.py

adwilson10 commented 10 years ago

From elliot.r...@gmail.com on February 28, 2013 20:01:33

The system isn't being recovered by the garbage collector, most likely because of the cyclic references between child frames and their parents, and between the frames and the system. Python provides a C interface that addresses this problem at http://docs.python.org/2.7/c-api/gcsupport.html . I won't have time to fix this for a while, but hopefully the changes made for issue #18 will keep this from being a problem.

adwilson10 commented 10 years ago

From elliot.r...@gmail.com on February 28, 2013 20:04:54

Also, the way that you are measuring memory usage in that script is not correct for this. maxrss reports the max usages of a process over its entire lifetime, so it cannot go down even when the memory is freed properly. This code from stack overflow ( http://stackoverflow.com/questions/897941/python-equivalent-of-phps-memory-get-usage ) seems to work:

def memory_usage(): """Memory usage of the current process in kilobytes.""" status = None result = {'peak': 0, 'rss': 0} try:

This will only work on systems with a /proc file system

    # (like Linux).
    status = open('/proc/self/status')
    for line in status:
        parts = line.split()
        key = parts[0][2:-1].lower()
        if key in result:
            result[key] = int(parts[1])
finally:
    if status is not None:
        status.close()
return result
adwilson10 commented 10 years ago

From vlad.seg...@gmail.com on March 04, 2013 08:17:30

This is not a huge issue for now, since for what I'm doing there really is no need to delete and rebuild the system. Memory measurement method appreciated as well.