[reportlab-users] Releasing memory used by Report Lab

Robin Becker robin at reportlab.com
Thu May 6 05:25:04 EDT 2010

On 06/05/2010 09:09, Jerry Casiano wrote:

> Hello,


> I have a PyGTK application which uses Report Lab to create a "Sample

> Sheet" from a directory of fonts. Unfortunately it seems to "keep" the

> memory used by Report Lab as long as my application is running. :-(


> I'm a newbie, so more than likely the problem is with my code, something

> I did or didn't do. But I was hoping someone here could point me in the

> right direction, so I don't have to resort to restarting the application

> or worse calling a script.


> Anyways, I've attached the relevant portions in a standalone version.



> Thanks in advance for any suggestions.

actually tried out your script and it ran on my arch system :)

Anyhow I suspect you may have some preconceptions about how this stuff works.
The rl toolkit doesn't control any memory directly it's all controlled by
python. However, we do maintain some global variables that are used to store
fonts and they may be the problem you're looking at. You can reset the state of
the toolkit as much as we know how by calling the reportlab.rl_config._reset()
function when you've finished with reportlab. In this case that's either at the
end of the BuildPDF.__init__ or in the _build_pdf callback. So you need to do
something like

def _build_pdf(unused_widget):
from reportlab.rl_config import _reset
#if exists(OUTPUT):

that should clean up the stuff that reportlab is holding onto. However, that
doesn't guarantee that all the memory will be returned to the system; that's up
to python and the way it & the system manage memory. You may be able to get some
information about memory usage by looking at python's garbage collector output.
One thing I know this doesn't do is return any space used up by imported modules.

If you really want to free up this memory a better way to do this is to make
your gui execute the document generation as a child process (see the subprocess
library module documentation). That way all of the memory used by the subprocess
will automatically be reclaimed when the process dies.
Robin Becker

More information about the reportlab-users mailing list