What is CG Raster Data?

安稳与你 提交于 2019-12-03 09:32:55

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.

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.

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!