Question

Supposed my app alloc memory every ViewDidLoad and not release. And my app does some work that cause memory fragmented.

Does iOS retrieve that leak memory, or that leak memory is untouched until next reboot?

Does iOS fix that fragmented memory, or memory is fragmented until next reboot?

What I want to know is whether iOS can handle leak and fragmented memory, so that other apps can run smoothly ?

Does this behavior happen the same for other mobile OS (Android, Windows Phone, BlackBerry) ?

From what I read Finding and fixing memory leak

All memory you allocate during execution of your app is freed by the system when your app shuts down. This includes any leaks you introduce

This seems true. But since many people say the opposite, which makes me confused

Was it helpful?

Solution

In iOS and Android(don't know about Windows Phone), your app can use a limited amount of memory which is allocated for each app that is running on the device.

On iOS, if one app will try to use more memory than the OS allocated, it will receive a memoryWarning this memoryWarnings have, I think 3 levels, so that the app can free memory without a force close. If the memory is not deallocated for the first two levels, on the next memoryWarning the app will be force closed and the memory freed. Also the OS will try to free memory from those application with low priority (apps in background).

So in iOS, if you don't free the memory used by your application, the OS will free it for you with the price of a force close

On Andorid, the situation is a little bit different and I don't agree with borrrden, Android uses garbage collection but memory leaks are still present, not as offen as on iOS but the appear and they are harder to find and fix.

So on Android each app has allocated a heap with an initial size, which can increase to a certain point, if the heap try to increase the heap above the system limit for the heap, the app will receive the famous OutOfMemoryError without any previous warnings and the app will be force closed and the memory freed.

Also one other nasty thing on Andorid is, almost every version of Android has a different max heap size. So it's possible that on some devices the app crashes with OOM and on other with a different Android version will run properly. And the last nasty thing on Android, only on the latest version of Android the heap size will be decreased when the memory is freed, this can cause an OOM because the system will increase the heap size before the heap is fully used (ironic to prevent OOM) and because the heap size is ont decreased it will exceed the max heap size.

Also Android has a method onLowMemory that is called when the whole system dosen't have enough memory but this method is rely rare called.

In conclusion, both Andorid and iOS will free the memory in case the OS will need it but this can force close your app. You should take in consideration the memory usage best practice and don't let the OS free the memory by himself just create your app so that it will use as few resources as possible and the app should free all the unused memory.

OTHER TIPS

All of the memory from your process will be returned to the OS when the process is terminated. I'm not sure what you mean by you are fragmenting memory, but all of that memory will be returned and the OS will continue dishing it out to other processes. The state of the memory is irrelevant (it is the programmer's responsibility to properly zero their allocations if they need to).

Windows Phone and Android are garbage collected, so memory leaks are largely irrelevant. I don't know about Blackberry.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top