CoreImage very high memory usage

一笑奈何 提交于 2019-12-24 02:23:42

问题


When working with CIImage my memory jumps quite drastically by 50MB-60MB when creating the CIIMage and then my program crashes when I'm trying to create the UIImage result.

CIImage *image = [CIImage imageWithData:data]; // +50MB memory increase (data is jpeg data)

It doesn't happen all the time but it happens more frequently for larger images (3264x2448) and while the app is in background mode.

It leads to frequent crashes. Any ideas?


回答1:


Core Image has severe limits on image size. iOS 8 lifts those limits to a very large extent; if you're on an earlier system, don't do that (i.e. confine yourself to smaller images). In iOS 7 and before, the GPU is available only for images less than 4K in size, which is very small indeed.

Moreover, use of core image in the background is severely limited, because, after all, how could your app dare to try to reach into the GPU, a small and busy place, from the background, when some foreground app may be using it? You are forced into using the CPU (though again, to some extent this restriction is lifted in iOS 8).

In all probability, therefore, your issues are partly memory related (you're trying to use core image for an image that's too big) and partly threading related (if you're using the CPU, you need to make sure you're on a background thread, because this is going to take a LOOOOONG time and the watchdog will kill you dead if you hang up the main thread).

Another thing to consider is how you're managing memory yourself. You haven't shown any relevant code so it's hard to say, but it may be that you are hanging on to data you don't need. You should show your code, and ideally you should analyze your app with Instruments so you know exactly where the problem lies.

For example, the only line of code you cite is

CIImage *image = [CIImage imageWithData:data]

This suggests you are holding the data in memory. That's totally unnecessary. If you play your cards right, you can get the CIImage straight from the image file on disk and create the output straight to disk without occupying any extra memory. If you are generating large memory just because you formed a CIImage, you might be doing it wrong (a CIImage is just a pointer, not a bitmap); the only moment where any memory is needed is the final rendering, where the bitmap is formed, and you can use ImageIO to get that out to disk without holding it in memory.



来源:https://stackoverflow.com/questions/25431317/coreimage-very-high-memory-usage

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