How to get reliable memory information on mobile devices?

I am currently working on an application that runs on iOS and Android. The core of the application is written in C ++ and over time allocates more and more memory. The fact is that I want at the same time it is possible to use as much memory as possible and ensure the stability of the application.

Of course, for this I will need to know how much memory I can use. Thus, if after a while I see that I need more than is available, I can stop the allocation instead of being killed by the operating system or crashing.

The problem is that after reading and testing various solutions, my feeling is that the information you dynamically receive is not reliable enough. For example, on iOS:

[NSProcessInfo processInfo].physicalMemory 

This is one of the typical examples / answers I read that seem unreliable. It seems that you cannot get enough information dynamically to make sure that you still have enough memory, because the OS will kill your application at some point if it uses too much memory and sends warnings earlier. But it can also kill other applications between them, so stopping when I get the first one seems not the optimal solution.

After reading a lot of posts, I am a little confused on this topic. Is there a way to find out dynamically and reliably how much memory is left for my iOS / Android application? Or is memory management from these OSs too unpredictable for this?

Thanks for the help!

+6
source share
1 answer

By design, you are prohibited from an operating system that ever uses all the physical memory on the device; Moreover, the requirements of the operating system and other applications running on it mean that the amount of memory that your application can use conveniently will change over time.

Therefore, asking how much memory you have left to use is actually unreasonable. You must design your application to use as little memory as possible, and then optimize it until you use less.

  • Be careful about caching things to disk.
  • Listen to the notifications provided by the OS that your memory is under pressure.
  • Create the application in such a way that you can restore its last state of the user interface from a new start: launching applications and task switches are displayed to the user in the same way, and they do not need and do not need to know the difference.

If you use more and more memory, the OS will try and terminate other applications to make room for you. This means goodbye, Facebook, goodbye Twitter, bye bye bye Mail, bye bye Contacts. Your users will notice, and they decide to run your application less often or otherwise evaluate it less profitably.

+2
source

Source: https://habr.com/ru/post/988081/


All Articles