473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

releasing memory to malloc

Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.

I'd like step 2 to return memory to the C allocator so that it
is available to the extension in step 3 (which uses malloc).

Regards,
Iker Arizmendi

Sep 27 '06 #1
12 3121

ik************@gmail.com wrote:
Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.

I'd like step 2 to return memory to the C allocator so that it
is available to the extension in step 3 (which uses malloc).
Huh-uh. Other way up is better.

If you have access to the source of the extension, change it so that it
uses PyMem_Malloc, PyMem_Free, etc

Read this; it tells you why:

http://docs.python.org/api/memoryOverview.html

Cheers,
John

Sep 27 '06 #2
At Tuesday 26/9/2006 21:34, ik************@gmail.com wrote:
>Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.

I'd like step 2 to return memory to the C allocator so that it
is available to the extension in step 3 (which uses malloc).
Can you modify the C source? If you can, use the Python memory
allocation functions PyMem_Malloc/PyMem_Realloc/PyMem_Free.

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Sep 27 '06 #3
I can, but the extension is only a thin wrapper around a general
purpose C library which is also used independently of Python.

Iker

Gabriel Genellina wrote:
Can you modify the C source? If you can, use the Python memory
allocation functions PyMem_Malloc/PyMem_Realloc/PyMem_Free.

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
Sep 27 '06 #4

ik************@gmail.com wrote:
I can, but the extension is only a thin wrapper around a general
purpose C library which is also used independently of Python.
So change the library to use xmalloc etc and add something like this to
the .h file:

#ifdef PYMEM
#define xmalloc PyMem_Malloc
etc
#else
#define xmalloc malloc
etc
#endif

Sep 27 '06 #5
At Tuesday 26/9/2006 22:17, ik************@gmail.com wrote:
>I can, but the extension is only a thin wrapper around a general
purpose C library which is also used independently of Python.
If you can recompile a specific version for using with Python, you
can play with a few macros like

#ifdef USE_PYTHON_ALLOCATOR
#define my_malloc(s) PyMem_Malloc(s)
#else
#define my_malloc(s) malloc(s)
#endif

and change all bare malloc/realloc/free along the code to
my_malloc/etc. (Chances are the code is using a special malloc
spelling already, C guys tend to do such things...)
Then you can recompile it for use with or without Python.

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Sep 27 '06 #6
I happen to have the code for the C library in question, but I don't
think this is the way to go in general. If there's a way to get Python
to give memory back to the C allocator I can avoid touching the
library at all.

Regards,
Iker

John Machin wrote:
ik************@gmail.com wrote:
I can, but the extension is only a thin wrapper around a general
purpose C library which is also used independently of Python.

So change the library to use xmalloc etc and add something like this to
the .h file:

#ifdef PYMEM
#define xmalloc PyMem_Malloc
etc
#else
#define xmalloc malloc
etc
#endif
Sep 27 '06 #7
Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.
...

I happen to have the code for the C library in question, but I
don't think this is the way to go in general. If there's a way to
get Python to give memory back to the C allocator I can avoid
touching the library at all.

A cave-man approach might be to fork a new process after step 1, pass
the small data structure to it, and have the old process exit
(releasing all its memory back to the OS). The new process then
carries out the remaining steps.
Sep 27 '06 #8
Paul Rubin wrote:
Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.
...

I happen to have the code for the C library in question, but I
don't think this is the way to go in general. If there's a way to
get Python to give memory back to the C allocator I can avoid
touching the library at all.

A cave-man approach might be to fork a new process after step 1, pass
the small data structure to it, and have the old process exit
(releasing all its memory back to the OS). The new process then
carries out the remaining steps.
I think I see what you're doing, but fork() after step 1 will
create a child process with the same memory allocated.

I think it would make more sense to do step 1 in a subprocess.
Use the subprocess module or one of the older popen()s to create
a process that builds the target object, pickles it and pipes
it back to the main process, then exits.
--
--Bryan
Sep 28 '06 #9
Bryan Olson <fa*********@nowhere.orgwrites:
I think I see what you're doing, but fork() after step 1 will
create a child process with the same memory allocated.

I think it would make more sense to do step 1 in a subprocess.
Use the subprocess module or one of the older popen()s to create
a process that builds the target object, pickles it and pipes
it back to the main process, then exits.
Sorry, yes, that's what I meant. I shouldn't have used the word fork
without further qualification, which specifically means the child
process has all the same data. What's needed would traditionally have
been done by a fork followed by an exec, and popen would have done
something like that. These days I think there's some more streamlined
ways to do it.
Sep 28 '06 #10
The workaround I've settled for uses the shelve module and calls
to gc.collect() to put a cap on the amount of memory the Python
allocator consumes. A bit more intrusive but it gets the job done.

Would a gc method that released memory to malloc be something
worth adding to Python? Or are there reasons why allowing this is
a bad idea?

Iker

ik************@gmail.com wrote:
Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.

I'd like step 2 to return memory to the C allocator so that it
is available to the extension in step 3 (which uses malloc).

Regards,
Iker Arizmendi
Sep 28 '06 #11
The workaround I went with made use of the shelve module and
calls to gc.collect() to cap the memory consumed by the Python
allocator. It was a bit intrusive but it got the job done.

Would a method in the gc module that released memory to malloc
be something that could get added to Python? Or are there some
reasons why allowing that would be a bad idea?

Regards,
Iker

P.S.
This may be a repeat of an earlier message - it seems that
google groups may have discarded my earlier post.
ik************@gmail.com wrote:
Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.

I'd like step 2 to return memory to the C allocator so that it
is available to the extension in step 3 (which uses malloc).

Regards,
Iker Arizmendi
Sep 29 '06 #12
The memory manager in the latest Python release 2.5 does return freed
memory to the underlying system, if possible. For more details, see the
5th bullet on this page

<http://docs.python.org/whatsnew/ports.html>.

/Jean Brouwers

ik************@gmail.com wrote:
The workaround I went with made use of the shelve module and
calls to gc.collect() to cap the memory consumed by the Python
allocator. It was a bit intrusive but it got the job done.

Would a method in the gc module that released memory to malloc
be something that could get added to Python? Or are there some
reasons why allowing that would be a bad idea?

Regards,
Iker

P.S.
This may be a repeat of an earlier message - it seems that
google groups may have discarded my earlier post.
ik************@gmail.com wrote:
Is there any way to get Python to release memory back to the
C allocator? I'm currently running a script that goes through
the following steps:

1) Creates a very large number of Python objects to produce
a relatively small data structure that sits in a C extension.
The Python objects consume quite a bit of memory.

2) Releases all the Python objects.

3) Invokes a function of said C extension for further
processing. This step needs as much memory as possible.

I'd like step 2 to return memory to the C allocator so that it
is available to the extension in step 3 (which uses malloc).

Regards,
Iker Arizmendi
Oct 5 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lebo | last post by:
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...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
7
by: trialproduct2004 | last post by:
Hi all I am having application in c# where i am loading one table of database into dataset. My table is of large size. so whenever i am loading that into dataset my memory size is getting...
7
by: mangesh | last post by:
Is there any function in standard library that can forcefully release memory associated with a process(application) ? Regards Mangesh .
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
9
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It...
1
by: skip | last post by:
I am starting to experiment with ctypes. I have a function which returns a pointer to a struct allocated in heap memory. There is a corresponding free function for that sort of struct, e.g.: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.