What is CG Raster Data?

chicabella picture chicabella · Sep 19, 2012 · Viewed 9.6k times · Source

I'm trying to find another memory leak in my code, and I can't seem to figure out what CG Raster Data is. While going through the VM Tracker with automatic snapshots enabled, the CG Raster Data seems to be the only part that increases. These also increase steadily without the allocations increasing.

I'm not entirely sure what the CG Raster Data is, nor how I would fix it, but at this point the increasing footprint eventually causes a memory error and crash, so it's not good! I do my own text rendering (using CoreText), so I'm thinking that has something to do with it. I also am loading pictures?

Below is an image of the footprint: Peaks in the allocations when the pages load (the app loads pages with images and text discretely), the dirty memory always increases though.

UPDATE: This problem still persists, but interestingly enough I can correlate it to a leak within UIFoundations to something called "NSConcreteGlyphGenerator." It seems to happen only when I call a "boundingRectWithSize:" method on an attributed string in the CoreText method that actually draws. The line, specifically, is:

[displayString boundingRectWithSize:CGSizeMake( self.frame.size.width, self.frame.size.height ) options:0 context:nil];

Slowly tracking it down...

Answer

rob mayoff picture rob mayoff · Sep 29, 2012

I don't know everything that “CG raster data” might contain, but one thing I know for sure it contains is... memory allocated by Core Graphics to store raster data, aka bitmaps.

Specifically, in my app, I create two 256x256 bitmap contexts using CGBitmapContextCreate. I pass NULL as the data parameter, so that Core Graphics allocates the bitmap memory for me. A 256x256 bitmap with 32 bits (4 bytes) per pixel takes 256 KiB = 64 pages of 4 KiB each. In Instruments, I get two “CG raster data” blocks of 65 pages each. If I comment out one of those bitmap contexts, I get just one 65-page “CG raster data” block in Instruments.

On the other hand, I also have a CATiledLayer in my app. The CATiledLayer sets up its own graphics contexts for me to draw into, and I believe it creates those contexts using shared memory that the window server (springboard on iOS 5, backboard on iOS 6) also directly accesses. I don't see any “CG raster data” blocks corresponding to those graphics contexts.