Note that there are some explanatory texts on larger screens.

plurals
  1. POMonoTouch: Memory leak when drawing PDF on a custom graphics context
    primarykey
    data
    text
    <p>I have an app that's basically a fancy PDF reader. I download a PDF from the internet and generate thumbnails for that PDF. However, it seems that when I generate these thumbnails a <em>lot</em> of memory is being allocated (checked using Instruments), sometimes parts of this is collected by the GC but in the end, my app gives up. I've had memory usage of up to 38Mb when generating thumbnails for a single PDF (100x100 thumbs, ~60 pages).</p> <p>I generate one thumbnail at a time, store it and then repeat the process, so under any circumstance there should only be one thumbnail in memory (while generating them, at least). My code for generating thumbnails looks like this:</p> <pre><code>public UIImage GetPageThumbnail(int pageNumber, SizeF size) { //If using retina display, make sure to scale-up the thumbnail as well. size.Width = size.Width * UIScreen.MainScreen.Scale; size.Height = size.Height * UIScreen.MainScreen.Scale; UIGraphics.BeginImageContext(size); CGContext tempContext = UIGraphics.GetCurrentContext(); CGPDFPage page = Document.GetPage(pageNumber); RectangleF drawArea = new RectangleF(new PointF(0f, 0f), size); CGAffineTransform transform = page.GetDrawingTransform( CGPDFBox.Crop, drawArea, 180, true); //fit PDF to context transform.xx = -transform.xx; // } transform.x0 = 0; // }flip horizontally //Console.WriteLine("XX: " + transform.xx + ", YX:" + transform.yx + ", XY:" + transform.xy + ", YY:" + transform.yy + ", X0:" + transform.x0 + ", Y0:" + transform.y0); tempContext.ConcatCTM(transform); tempContext.DrawPDFPage (page); UIImage returnImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return returnImage; } </code></pre> <p>I've tried explicitly disposing the context and PDF page, but that had no effect (actually it seemed worse, but take that with a pinch of salt).</p> <p>I've seen some posts about memory leakage with MonoTouch and PDF (basically <a href="https://stackoverflow.com/questions/2289174/monotouch-coregraphics-pdf-memory-issues-with-cgpdfdocument-and-cgpdfpage">this post</a>), but that's pretty old. I'm using the newest MonoTouch (5.0.2).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload