How to handle low memory warning - ios

In my app I am getting low memory warning after few files download.I stops current download process. Even after that also app crashes and I am assuming that app crashes due to low memory issue. Can any one please let me know how can I free up memory in running app.

It sounds like you're downloading the files straight to memory. That will be an issue because you wont have enough memory to download the entire file.
The strategy you need to take is, as you download the file continue to write chunks of it out to disk, freeing up memory in doing so.
If you are using NSURLSession there is a parameter you can use to do this for you with little effort. Please post your code for in depth help.

Related

Memory Leak Found by Leaks Instrument (iOS)

I am having trouble understanding the leaks found by the Leaks instrument right after build. Is this being caused by my own code or a library?
Let me know if you need more information, and thanks!
libSystem.dylib is a system library. If you run your app on a device you shouldn't encounter this leak anymore.
The leak is related to the way the simulator works.
looks like it some system code and it's probably allocating a permanent memory chunk. It's a very small allocation and there's only two of them. I don't think its a concern--all apps "leak" some memory.
what to watch out for:
many repeated small allocations... For example: an NSString created and leaked every time you adjust a volume slider.
larger chunks that are meant to be used once and thrown away. For example, an image loaded for a window background that isn't released when the window is closed

What might be the reasons for Performance tool data is high. [ showing in Xcode VM tracker]

I recently profiled my app using Xcode VM tracker instrument.I found that app has lot of dirty memory especially performance tool data. So i want to know what are the reasons of the huge dirty memory and performance tool data.
Any help would be appreciated.
Your app takes 51MB to store, when it is suspended. The performance tool itself adds an overhead of 30MB. Which leaves 20MB for your app.
From the listed items, it looks like your app is graphics heavy. In fact, it looks very similar to this post. Which makes me wonder if these objects are still processing or waiting to be released, when the app is suspended.
Alternatively, I wonder if you could free a lot of those animations and images when entering background, and reconstruct them when entering foreground.
Finally, note that Apple recommends removing strong references to images, data from disk and media to reduce dirty memory.
Since I just had the same problem, here is what I found:
The "Performance tool data" entries were from libBacktraceRecording.dylib.
You can disable backtrace recording in the scheme editor.
See the related question Memory leak with “libBacktraceRecording.dylib” in React Native ios application.

How can I address an app that started earlier, but is "Terminated due to Memory Pressure" now?

I am working on an iOS app in Xcode. Earlier I got it to start and run, up to a limited level of functionality. Then there were compilation failures claiming untouched boilerplate generated code had syntax errors. Copying the source code into a new project gets a different problem.
Right now, I can compile and start running, but it states before even the launch image shows up that the application was closed due to memory pressure. The total visual assets is around 272M, which could be optimized some without hurting graphical richness, and is so far the only area of the program expected to be large. (The assets may or may not be kept in memory; for instance every current loading image is populated and my code never accesses any loading image programmatically.) And it crashes before the loading image has itself loaded.
How can I address this memory issue? I may be able to slim down the way images are handled, but I suspect there is another root cause. Or is this excessive memory consumption?
Thanks,
Review the Performance Tuning section of Apple's iOS Programming documentation. Use Apple's Instruments application to determine how, when, and how much memory your app is using.
One approach you should consider is to disconnect the graphics resources from your application, and add them back one-by-one once you feel they meet the requirements and limitations of iOS.
Now, this part of my answer is opinion: it sounds like your app is a high risk for being rejected from the App Store, in case that is your intended destination for this app.

load 5MB of images crashed iPad

I need to load 5MB of HD images (for iPad) before my game starts up. It crashes somewhere in the loading progress. Well, to confirm it was the 5MB problem, I then tried to load 2.5MB of LD images (iPhone quality) instead. It ran OK, no crashes.
Does it mean that I can't reserve 5MB or more memory? There should be a way to do it.
This happens more frequently when more apps pending in the background. And it doesn't crash if I run my game immediately after a clean reboot. Therefore, it must be a memory problem crashing my game.
Any idea how I can handle this?
5MB of compressed image data can easily explose to many (4x-10x) the memory requirements to have them uncompressed.
Any idea how I can handle this?
Use Apple's Instruments and look at memory usage.
Next ensure iOS itself is not caching images (look at the API you're using and their documentation, some will cache, others won't).
Then look if you can dispose of the image earlier. Images can be quite big (and a lot of memory for them is internal to iOS, which is why using Instruments will give you a better picture than MonoTouch's own HeapShot profiler) so disposing it manually (instead of waiting for the GC is generally a good idea).
Also look if you can delay load some images (or at least their decompressed versions), e.g. after the first one(s) are disposed.

iPhone memory warnings and crashes - but Instruments showing lowish memory use

I have a strange memory issue I'm having problems resolving and would appreciate some advice as to where else to look.
The program I have (iPhone App) has a function whereby it basically downloads loads of files, processes those that are JSON, and stores the rest to disk. The JSON processing is CPU intensive and can take several seconds per file, so I have a NSOperationQueue with maxConcurrency limited to 1 that handles all the heavy lifting, and a queue that manages the multiple files to download.
Ever since iOS5 came out, the App has had problems completing the download sequence without crashing and so far what I have tried is;
1) Changed the performSelectorOnBackgroundThread JSON processing to use a single NSOperationQueue so as to limit the number of background threads working with large objects.
2) Added NSAutoReleasePools inside loops that create multiple, large, transient objects.
3) Flushed the sharedURLCache to ensure the files aren't hanging around in the system cache.
4) Stored the JSON objects to disk using NSKeyedArchiver and passed the filenames between threads rather than the actual objects, to again try to mitigate the number and size of retained objects currently in use.
All of these at first seemed to make a difference, and when I look at the memory allocations, I've now got the peak usage down from just over 20MB (hence no wonder it was crashing) to under 10MB, and yet the app is still crashing with low memory as before.
I'm trying to trace what is eating the memory causing the app to crash and on this occasion I'm having real problems persuading Instruments to tell me anything useful.
Here's a typical trace (on an iPhone 3GS running iOS 4.3.5)
You can see that the PEAK usage was a tad over 7MB and yet shortly after, you can see the 2 flags pertaining to low memory, and then low memory urgent, followed by the app terminating shortly thereafter.
If I use the memory monitor, the cause of the crash seems clear enough - physical memory is being exhausted - look at the light green trace below. The low memory warnings co-incide (not surprisingly) with the physical memory running out.
There are no leaks showing FWIW either (I've done that in other runs).
It's not image caches or NSURLConnection caches and the only thing I can think of is that perhaps there are some bad leaks that aren't being detected ... but I'm having issues identifying them because if I click into all allocations to see the objects that are live, and then do a command-A to select them all (in order to paste them into a spreadsheet to see where the memory seems to be), at the point I hit command-C to copy them, Instruments beachballs and never recovers.
I really cant figure out what's going on. Does anyone have some advice on how to persuade instruments to show me some more useful information about what is using this memory?
Sorry I can't post any meaningful code fragments ... hopefully the instruments screenshots at least give you an idea about where I'm coming from.
The Leaks instrument isn't terribly useful for figuring out anything but the obvious leaks in your app.
What you are describing is an ideal candidate for heapshot analysis.
tl;dr Heapshot analysis allows you to see exactly how the heap of your application grows between any two points of time (where you determine the points).

Resources