Thursday, October 31, 2013

A unit test to enforce max heap when running Android UT on the PC

2013's mobile devices come with 1-2GB RAM, yet Android still enforces a very small heap size of 24MB-64MB only (though it keeps increasing with time).
It's pretty easy to write an Android app that drains the heap. For example: Caching images w/o an LRU cache, reading whole files into memory instead of working with streams.

-- Your code will always use up as much memory as the system has (My spin on Parkinson's law).

I'm developing an Android app with a big UT suite that I run on Eclipse in the PC. I noticed that my default heap size is 256MB, huge compared to mobile, meaning my tests could pass on the PC, but still cause an OOME on an actual device.

So, I created MobileLikeSmallHeapDuringTestsEnforce, a new unit test to enforce a small heap size during Junit tests execution. Just make sure you throw it in to any test project you have and you're safe.

Created as a GitHubGist, you're welcome to make it better:


  1. Awesome stuff Gili. Things like this tend to get overlooked (which is probably why small heaps like that are still enforced) so it's always nice when someone steps up and develops something to fix it.

    Makbule N.

  2. Thus, with wide presence and popularity of Android app businesses having an application on Android platform can widen their business reach and eventually find prospective customers.

  3. Insightful post! This is additional thoughts over subject that helped a lot to me for working. reflective vest
    reflective fabric

  4. Uncharted 5 will be unbelievable if Cassie came in as Nate's replacement! As the daughter of Elena and Nathan, she has all the resources she will ever need to become the next treasure hunter in the family. And having her as the protagonist will ensure that we get to see Nate, Elena, and everyone else in at least some capacity. Uncharted 5 Release Date