Python embedded in C & memory releasing

lebo leonardbocock at yahoo.com
Tue Sep 23 22:19:58 EDT 2003


Hi

I'm trying to understand how Python handles memory usage and dynamic
object loading and unloading.  Problem to solve? Build a very low
memory footprint (non-GUI) Python application for Unix/Windows. Why
use Python? End user flexibility.

So far it seems that (on Windows):
(1) memory increases when you import <module>. Expected behaviour.
(2) memory does not release when you del <module>. Why not?  Is Python
not releasing the memory or is it the OS not releasing memory?

So then I tried embedded Python in C:
(1) c application base 1.6MBytes (a good number)
(2) Call Python function from c application - memory up to 2.6MByte
(OK number)
(3) Python function exits but memory usage stays at 2.6MByte

Why is memory not releasing? How can I tell if this Python behaviour
or OS behaviour?

C code snippet:
#include <Python.h>

int main(int argc, char *argv[])
{
	int y = 0;
	printf("Started...\n");
	for (int count=0;count < 999999999;count++) {
		y += 1;
	}
	printf("Initing Py\n");
	Py_Initialize();
	PyRun_SimpleString("from time import sleep\n"
				"print 'I will sleep for a while'\n"
				"sleep(10)\n"
				"print 'Exiting Py'\n");
	Py_Finalize();

	for (int count=0;count < 999999999;count++) {
		y += 1;
	}
  return 0;
}

Thanks, Leonard




More information about the Python-list mailing list