9

So I'm using Python as a front end GUI that interacts with some C files for storage and memory management as a backend. Whenever the GUI's window is closed or exited, I call all the destructor methods for my allocated variables.

Is there anyway to check memory leaks or availability, like a C Valgrind check, right before exiting the whole program to make sure there wasn't any memory leaks?

Example exit:

from tkinter import *
root = Tk()  # New GUI
# some code here

def destructorMethods:
    myFunctions.destructorLinkedList()  # Destructor method of my allocated memory in my C file
    # Here is where I would want to run a Valgrind/Memory management check before closing
    root.destroy()  # close the program

root.protocol("WM_DELETE_WINDOW", destructorMethods)  # When the close window option is pressed call destructorMethods function

notMyName
  • 690
  • 2
  • 6
  • 17
  • Did you write the C code? Because the easiest option here by far is to just fix your memory leaks. Does the python code have knowledge of all C allocations? If so, you could create a basic garbage collector in python to track your blocks. – Joe Finn Jun 28 '20 at 17:10

3 Answers3

4

If you want to use Valgrind, then this readme might be helpful. Probably, this could be another good resource to make Valgrind friendly python and use it in your program.

But if you consider something else like tracemalloc, then you can easily get some example usage of it here. The examples are pretty easy to interpret. For example according to their doc,

  import tracemalloc
  tracemalloc.start()

  # ... run your application ...
  snapshot = tracemalloc.take_snapshot()
  top_stats = snapshot.statistics('lineno')
  print("[ Top 10 ]")
  for stat in top_stats[:10]:
  print(stat)

This will output something like.

 <frozen importlib._bootstrap>:716: size=4855 KiB, count=39328, average=126 B
 <frozen importlib._bootstrap>:284: size=521 KiB, count=3199, average=167 > 

You can either parse this to plot memory usage for your investigation or you may use the reference doc to get a more concrete idea.

In this case your program could be something like the following:

 from tkinter import *
 import tracemalloc
 root = Tk()  # New GUI
 # some code here

 def destructorMethods:
     tracemalloc.start()
     myFunctions.destructorLinkedList()  # Destructor method of my allocated memory in my C file
     # Here is where I would want to run a Valgrind/Memory management check before closing
     snapshot = tracemalloc.take_snapshot()
     top_stats = snapshot.statistics('lineno')
     print("[ Top 10 ]")
     for stat in top_stats[:10]:
         print(stat)
     
     root.destroy()  # close the program

 root.protocol("WM_DELETE_WINDOW", destructorMethods)  

Another option is, you can use a memory profiler to see memory usage at a variable time. The package is available here. After the installation of this package, you can probably use the following command in your script to get the memory usage over time in a png file.

 mprof run --include-children python your_filename.py
 mprof plot --output timelyplot.png

or you may use different functions available on memory_profiler package according to your need. Maybe this tutorial can be an interesting one for you.

moyeen52
  • 425
  • 3
  • 13
0

I think you could use a debug memory allocator, which supports leak detection.

Something like tcmalloc would do. You just LD_PRELOAD it and all your calls to malloc , free etc will debug for leaks .

rostamn739
  • 323
  • 3
  • 8
0

To trace python memory leaks have a look at this article and - for more details - to these questions, here on SO:

andreagalle
  • 620
  • 6
  • 17