What should I do when I receive a memory warning? [duplicate] - iphone

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What to do when my app receives memory warning?
I developed an iPad App where I must display a lot of media (photos, videos, pdfs). My app uses the TabBar Application model. I found all of my memory leaks and now I am leaks-free but my App still crashes. I don't put any images in the cache with imageWithContentsOfFile, I release everything, the movieplayer, UIImage, UIWebView. However, I still receive memory warnings. So what can I do with these warnings ?

You need to keep your images in memory in such a way that you can go back and purge them when the device asks for memory. And then you need a way of knowing what you've purged so you can reload it when you need it again.

Related

Need more memory for my iPad app

I am working on a mail client on iPad (similar to that of the default app client) and using core data framework as a cache to increase performance . My app uses around 4.5 - 5 MB of heap memory and then it crashes because of memory overflow (detected this using allocation instrument). If I try to reduce memory my performance becomes very slow and sluggish because I am not able to cache my views, data structure (which store folders and all the mails) and tableviews.
I have checked my crashLogs and I see jettisoned written in front of my App which confirms that OS has forcefully closed my App!
I have used instrument to detect these limits. Please find the attached image here
This is a snapshot my recordings just before the app crashes.
I have tested my app on simulator and it stabilizes itself at 6 - 7 MB of heap memory.
Is there any way so that I can ask OS for more memory or avoid crash with a little redesign in my code.
Any suggestions or help would be highly appreciated.
6-8MB of memory should never be a problem. Likely you are either trashing memory or if you are running a debug version and have Zombies turned on, the default is to never delete the zombies. NSZombiesEnabled=YES and NSDeallocateZombies=NO will appear to leak memory as nothing is ever deleted.

Is there a way to send Memory Warning to iPhone device manually? [duplicate]

This question already has answers here:
iOS Development: How can I induce low memory warnings on device?
(10 answers)
Closed 9 years ago.
I got one problem these days. I'm using an image-cache library, it works well but eventually i met memory issue and the app just quit itself (I guess it's because it just runs out of memory). After read the source code from the image-cache library, i found it's said that when there's memory warning event, it would release all images cached (the images are huge). Is there anyway for me to send Memory warning event to the device manually and directly ? I'm using xcode instrument tool to evaluate the memory usage.
You can manually simulate in the simulator:
Hardware -> Simulate Memory Warning
You can also simulate it programmatically:
- (void)simulateMemoryWarning
{
#if TARGET_IPHONE_SIMULATOR
#ifdef DEBUG
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)#"UISimulatedMemoryWarningNotification", NULL, NULL, true);
#endif
#endif
}
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)#"UISimulatedMemoryWarningNotification", NULL, NULL, true);
Memory warning can be produced by calling an private method of UIApplication. It works fine on iOS 6.1 and below
[[UIApplication sharedApplication]performSelector:#selector(_performMemoryWarning)];
NOTE: Remove that selector call before submitting app to iTunes, otherwise it will be rejected.

what is a memory warning, how do we respond to it [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
iPhone OS Memory Warnings. What Do The Different Levels Mean?
what is a memory warning, how do we respond to it
Check this SO question
iPhone OS Memory Warnings. What Do The Different Levels Mean?
Check Kenny's answer
Memory level warnings are logged by SpringBoard. As an app developer you don't need to care about it. Just responding to -{application}didReceiveMemoryWarning is enough.
There are 4 levels of warnings (0 to 3). These are set from the kernel memory watcher, and can be obtained by the not-so-public function OSMemoryNotificationCurrentLevel().
typedef enum {
OSMemoryNotificationLevelAny = -1,
OSMemoryNotificationLevelNormal = 0,
OSMemoryNotificationLevelWarning = 1,
OSMemoryNotificationLevelUrgent = 2,
OSMemoryNotificationLevelCritical = 3
} OSMemoryNotificationLevel;
How the levels are triggered is not documented. SpringBoard is configured to do the following in each memory level:
Warning (not-normal) — Relaunch, or delay auto relaunch of nonessential background apps e.g. Mail.
Urgent — Quit all background apps, e.g. Safari and iPod.
Critical and beyond — The kernel will take over, probably killing SpringBoard or even reboot.
Killing the active app (jetsam) is not handled by SpringBoard, but launchd.
UPDATE
Pls go through the Apple Reference documents on Memory Management also

iphone device memory issue [duplicate]

This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
Memory Issues on iphone
Hello Everybody,
I was developed one small business iphone application...Actually whenever the program is running it stores in application memory.. In same manner in device it stores device memory...
But in mac some times we can store the data like USE SECURE VIRTUAL MEMORY OPTION......
So in the same manner is any possibilities are available to store data in iphone device location for security purpose .... Any Ideas?
You can take help from Memory Management.

How can I check if there is enough memory to process a picture captured by UIImagePickerController when taking multiple pics with the camera?

I am writing an application that uses UIImagePickerController to take multiple pictures with the camera as fast as iOS allows. My application has to run on iOS 3.13 on all versions of iPhone hardware (v1 through 4). I am using UIImagePickerController with a cameraOverlayView.
My question is, how can I determine programmatically how many photos can be processed at once based on available memory? See below for more details. Any help is appreciated!
When the imagePicker delegate recieves imagePicker:didFinishPickingMediaWithInfo:, the delegate saves the full size image in the savedPhotosAlbum with UIImageWriteToSavedPhotosAlbum(). Once I have this problem solved, the app will be doing more things with the full size image.
As a test, I take pictures as fast as I can. Eventually, as expected, the app receives memory warning due to the large number of photos in memory while saving to the photo album. If I ignore the memory warnings and continue taking pictures, eventually the app is killed by iOS b/c of this.
I have no problem with setting a limit on the number of concurrent photos I allow the app to process at a time. Each iPhone HW version has different memory capacities and different camera resolutions. With iOS 4, multi-tasking may also effect the available memory. I'd rather do this programmatically than hard-code a limit based on HW version.
A test application that shows this behavior is at Source Code
It's easier to hard-code the limit.
There's no easy way to find out what the "available memory" is, since more memory may suddenly become free if there's a memory warning, or if the phone decides to kill various background apps (namely Safari). There's NSRealMemoryAvailable(), but that might just return the installed RAM (and is probably what [NSProcessInfo physicalMemory] uses).
You could try saving images in a queue — saving one image at a time is likely to use less memory. You can also add markers to your queue when you receive a memory warning, and disable image-saving when there's more than one (ideally you want to stop taking pictures if you get "level 2" or "level 3" memory warnings, but while these are printed to the console, I don't know any easy way of checking in code. Presumably you want to resume on a memory un-warning, but these don't exist either).
Also note that you can get the raw JPEG data (on some OS versions) through a notification; I forget what the name is, but the userInfo key is #"AVCaptureNotificationInfo_JPEGData". If you save the raw JPEG data, you might be able to process it later.
I think UIImageWriteToSavedPhotosAlbum() stupidly decompresses the JPEG returned by the camera and recompresses it, but I could be wrong. Camera images are only around 1-2 MB, so you ought to be able to take plenty without it crashing, but in my tests that's not the case.