Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Ctypes how to check memory management

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

like image 392
notMyName Avatar asked May 13 '20 15:05

notMyName


People also ask

Is there Memory management in Python?

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager.

How does Python handle the memory management?

Memory allocation can be defined as allocating a block of space in the computer memory to a program. In Python memory allocation and deallocation method is automatic as the Python developers created a garbage collector for Python so that the user does not have to do manual garbage collection.

How does Python track memory usage?

You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript. You'll see line-by-line memory usage once your script exits.


Video Answer


1 Answers

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.

like image 152
moyeen52 Avatar answered Oct 19 '22 17:10

moyeen52