What is CG Raster Data?
Asked Answered
W

6

26

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...

Whisper answered 19/9, 2012 at 21:30 Comment(1)
Did you ever figure this one out?I am having this issue still in iOS 7.Flavin
A
12

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.

Autogenous answered 29/9, 2012 at 4:41 Comment(0)
E
3

I had the same issue with CG Raster Data memory increasing by simply pushing and popping a view controller repeatedly. I spent a while thinking it was an issue with some drawing code. I finally tracked it down to a delegate not weakly referencing the view controller that was being pushed and popped, so when I popped the view controller, it wasn't being deallocated. The CG Raster Data happened to be the biggest part of that view controller's footprint, so I mistakenly attributed the problem to that initially, when it was really the view controller itself that wasn't being released (therefore, not releasing its views, some of which had CG Raster Data).

In short: if you're seeing memory leaks with CG Raster Data, look at view controllers that might have views with them, and make sure that they are being released.

Elboa answered 5/3, 2015 at 13:11 Comment(0)
E
2

This is not much of an answer, but just so someone gets the investigation started...

I think CG Raster Data is new with iOS 6, but was present in iOS 5 as CG Image. I tested on both simulators, and on iOS 5, CG Raster Data wasn't present, but if you compare the total amounts on iOS 6 and iOS 5, CG Image is just about equal to CG Raster Data, and CG Image doesn't show up on iOS 6. So I'm pretty sure they just renamed CG Image to CG Raster Data.

As for what CG Image really is, well I've been trying to figure that out for months. I think it's just view layout related things handled by the system, that you don't really control, because my app doesn't really have any UIImages or CG Images, and my CG Image memory is pretty high, so its probably something to do with Core Animation and view layout.

Erick answered 29/9, 2012 at 3:8 Comment(2)
“CG Image” is still present is iOS 6, at least in my app.Autogenous
Ya, for me too, though extremely small traces of it..whereas in iOS 5 it's around 20 mb, in iOS 6 its 32 kb max. So whatever CG Image is, it doesn't play that large of a role in iOS 6Erick
G
1

If you're concerned about the memory usage (89MB) is a bit. Have you tried simulating a memory warning in the Simulator (Simulator > Hardware > Simulate Memory Warning)?

My guess is it's memory being used up by picture images.

Chances are you may need to deallocate your images by observing the memory warning message:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];

Clear all your image objects in handleMemoryWarning method.

Graver answered 2/9, 2014 at 7:14 Comment(1)
how to clear all images? means image = nil? or self.view = nil?Bouzoun
V
1

When I had an issue with this, the "CG Raster Data" was coming from an image I created from a call to UIGraphicsGetImageFromCurrentImageContext from within a UIGraphicsBeginImageContextWithOptions. It took me a day to track down the problem, and in the end it wasn't related to how the image was made at all. In my case I had inadvertently stuck the image into an NSCache of my own in another part of code, not realizing it.

If you are having a problem with CG Raster Data not being released, you should consider that the source of where the data was created may very well not have anything to do with the real problem. It could be that the image data is simply being retained when you're not expecting it to be, and the "CG Raster Data" label you're seeing in Instruments is just referring to where the data originated. You should check to make sure you're not doing something like multiple addSubviews while failing to removeFromSuperview (with UIImageViews for example), or putting the image into a cache, an array, a strong variable, etc.

Vereeniging answered 25/3, 2016 at 23:28 Comment(1)
I appear to be having a very similar issue. Can you elaborate on the problem with placing an image into an NSCache?Potted
N
0

Not really an answer. But some more hints that I figured out while solving my CG-raster-data-leak:

  • the cg-raster-data holds the memory of CGIMageRefs (at least created from CGBitmapContextCreateImage() from a context created by CGBitmapContextCreate(NULL, ...)
  • my leak was a missing CGImageRelease(). I used the image to pass as contents of a CALayer and assumed that this property holds the reference. But it seems that this assignment copies the image. At least I have no problem with calling CGImageRelease right after the assignment.

Hope that helps someone...

Nattie answered 9/9, 2015 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.