Re: [Pyobjc-dev] Garbage collection in Python/Pyobjc
Brought to you by:
ronaldoussoren
From: James E. <jam...@te...> - 2012-01-02 22:30:32
|
Hi Peter, You’re right, this is standard Objective-C memory management issue, but thankfully its easy to fix. Just create a new NSAutoreleasePool at the top of your loop and del it at the bottom. As an example, here’s the processing loop from one of my projects : for image in images: pool = NSAutoreleasePool.alloc().init() try: buffers.append( self.processImage_(image) ) finally: del pool Alternatively, with the following category hack, you can use a with statement : # Add context manager methods to NSAutoreleasePool as a category hack class NSAutoreleasePool(objc.Category(NSAutoreleasePool)): def __enter__(self): return self def __exit__(self, ext, exv, extb): del self return False James Le 2 janv. 2012 à 21:14, Peter Bernheim a écrit : > I'm working on a script to convert each page of a large directory of pdfs to jpgs, using a version of this script: > http://files.macscripter.net/joy/files/pdflib.py > > I am walking through a directory in Python, and calling the below function on each file. It seems that there is no garbage collection being performed on any of the objects being created below, and my searches through the docs don't seem to show an explicit way of performing garbage collection on these rather large objects. How to ensure that the below code, if called thousands of times within a Python loop isn't going to consume the entire system's memory? > > > def pdf2jpgs(pdfpath, pages_dir, resolution=72): > """I am converting all pages of a PDF file to JPG images.""" > > pdfdata = NSData.dataWithContentsOfFile_(pdfpath) > pdfrep = NSPDFImageRep.imageRepWithData_(pdfdata) > pagecount = pdfrep.pageCount() > for i in range(0, pagecount): > pdfrep.setCurrentPage_(i) > pdfimage = NSImage.alloc().init() > pdfimage.addRepresentation_(pdfrep) > origsize = pdfimage.size() > width, height = origsize > pdfimage.setScalesWhenResized_(YES) > rf = resolution / 72.0 > pdfimage.setSize_((width*rf, height*rf)) > > tiffimg = pdfimage.TIFFRepresentation() > bmpimg = NSBitmapImageRep.imageRepWithData_(tiffimg) > data = bmpimg.representationUsingType_properties_(NSJPEGFileType, {NSImageCompressionFactor: 1.0}) > pagenum = i + 1 > jpgpath = "%s/pg%d.jpg" % (pages_dir, pagenum) > if not os.path.exists(jpgpath): > data.writeToFile_atomically_(jpgpath, False) > return '' > ------------------------------------------------------------------------------ > Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex > infrastructure or vast IT resources to deliver seamless, secure access to > virtual desktops. With this all-in-one solution, easily deploy virtual > desktops for less than the cost of PCs and save 60% on VDI infrastructure > costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox > _______________________________________________ > Pyobjc-dev mailing list > Pyo...@li... > https://lists.sourceforge.net/lists/listinfo/pyobjc-dev -- James R. Eagan Télécom–ParisTech www.telecom-paristech.fr/~eagan |