I was targeting Windows Phone and using managed language but it’s quite possible I’d have encountered similar experience with other platforms.
Development of a mobile app poses challenges and in this post I’m writing about one specific challenge I encountered.
The amount of memory an app can use is limited and it depends on the memory size of the device. If the device has 512 MB RAM the app can use only 185 MB of memory. If the app hits the memory limit it will crash.
One might assume if the app is written in managed language the unused memory will be regularly freed up and the app won’t hit the memory limit.
In my experience, it was easy to introduce a memory leak that gradually consumed the memory till the point the app hit the memory limit and so it crashed.
When the app is running it already using certain amount of memory. In one scenario the available memory was less than the amount of memory the app tried to allocate. As a result, the app hit the memory limit and so it crashed.
That point, I felt like being in the demoscene back in late ’90s when there were the competitions with specific limits. I knew what I wanted to do but I needed to make sure what I do doesn't violate the limits.
A photo app stores each pixel of the photo in the memory. A single pixel can occupy four bytes describing the color. So an image of 3552x2000 requires 27 MB of memory.
The calculation looks like this: ((3552*2000*4)÷1024)÷1024 = 27
One might think that 27 MB of memory in this example is little taking into account that 185 MB is the limit. It’s not that simple.
The photo app might need to keep multiple copies of the photo in the memory. For example, the app keeps both the original and edited photos in the memory.
The photo app might need to keep multiple photos in the memory. For example, the app might insert one photo on the top of another. The photo app might need to show a different image to the user than it holds in the memory. So both photos needs to keep in the memory.
Other than that, the photo app needs to keep other things in the memory like temporary buffers, pages of app, history, etc. When these are all add up it’s fairly easy to hit the memory limit.
The memory consumption of the app might not be exactly the same between two devices even when the devices have the same amount of RAM. So the memory consumption of the photo app not that just has to be lower than the limit but it’s advisable to leave a safety margin to be on the safe side.
Different devices have different amount of RAM and unless you want to optimize your code to the low-end device you need to deal with devices with varying amount of RAM.
I used Visual Studio’s built-in Performance and Diagnostics Tools to ensure there are no memory leaks. I also made a measurement how much memory is needed for certain resolution of photos. Then, I set a limit that the app won’t open large images that would lead to excessive memory usage and ultimately to crash. I wrote a memory manager class that checks the amount of memory available and adjusts the internal configuration of the app accordingly. As a consequence, certain functionalities — such as zooming into the photo — can take the advantage of more memory.
No comments:
Post a Comment