Python: Ctypes how to check memory management

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.