The blog continues at suszter.com/WindowsMobile

Sunday, October 30, 2016

The Challenges of Developing a Photo App for Mobile Devices: Memory Limits

Couple of months ago I started developing a photo app as a side project. The kick-off was ambitious but then I needed to admit I underestimated the complexity of the project as a casual mobile developer.

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.

Thursday, January 7, 2016

Smartwatch from Casio with Dual Display

"There will only be chances later for those who take action now." -- Kazuo Kashio, 2008
I love Casio watches, heck Casio electronics, since my childhood which was 25+ years back. These days there are four Casio watches in my watch box that I regularly wear according to where to go. It's difficult to convey my opinion in an objective way about Casio WSD-F10 that will be reportedly released in March and April later this year.

I just saw the Casio press conference on YouTube. Casio say WSD-F10 created and designed specifically for outdoors and it's 50m water resistant. I would not hesitate to wear it outsize or in sea knowing it's built by Casio the maker of G-SHOCK watches. I just haven't had considerable problem with any of my Casio watches. Perhaps the worst thing was that I needed to replace the strap, once in 90's and once in 2000's I guess.

I saw the photo and the watch just looks so cool taking into account it's a smart watch. I still love my Rangeman, and I'm keeping an eye on the Bluetooth watches hoping they will support Windows Phone one day soon (I'm honestly sad about this but have hopes).

WSD-F10 has got DUAL DISPLAY. This is a big thing because the monochrome display can provide continuous time display. Also when you use the monochrome display rather than the color the battery can last many days or weeks. Casio say the battery can extend till 1 month -- I guess if you don't use the apps.

It has got sensors like compass, accelerometer, gyroscope, barometer. No thermometer? That would be useful, too. I got barometer in my Rangeman and it's pretty practical as you can predict weather changes. In Singapore, for example, I know if the pressure is going up high, then dropping fast it's likely will be raining soon, say, in the next hours.

It's got Android Wear OS on it that I'm not so happy about but Casio can't develop their own OS from scratch as they need to keep their focus on the watch.

Pre-installed apps like map can be really useful I guess specially with the use of compass.

It seems it doesn't have GPS built-in, but whether it can use to the phone's GPS via bluetooth is not clear.

It has got WiFi and bluetooth built in and it's used for notifications when the watch is linked with the smartphone. If it could be linked with Windows Phone that would be even better.

You can remotely operate Casio camera which means Casio take smartwatch seriously. At the moment, my heart however goes for the conservative Bluetooth G-SHOCK that sadly needs update to work with Windows Phone.