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:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MobileLikeSmallHeapDuringTestsEnforce {
public static final long MB = 1024 * 1024;
public static final long ANDROID_TYPICAL_MAX_HEAP_MB = 24;
public static final String XMX_ARGUMENT = "-Xmx" + ANDROID_TYPICAL_MAX_HEAP_MB + "m";
@Test
public void assertHeapIsMobileLikeSmall() {
long maxMemory = Runtime.getRuntime().maxMemory();
assertTrue("Max heap is too high for mobile!\n" +
"This JUnit test JVM heap max memory (" + maxMemory / MB + "MB) "
+ "is higher than the max heap size on a typical Android device (" + ANDROID_TYPICAL_MAX_HEAP_MB + "MB).\n" +
"Lowering max heap size will allow you to catch code flows that use too much memory.\n" +
"To correct: Add " + XMX_ARGUMENT + " as a VM argument.\n" +
"Correct in eclipse: menu > Run > Run Configuration... > Arguments tab > VM arguments > " + XMX_ARGUMENT,
maxMemory <= ANDROID_TYPICAL_MAX_HEAP_MB * MB);
}
}

Tuesday, January 15, 2013

Quickest way for a one-off XML sort - OR - Learning to keep the heavytools in the shed

What's the quickest way/tool to sort a 1000 entries xml file?
Your requirements: The xml is parked on your desktop. You only need to sort it just once so you can manually examine it. Sort by the tag "relevance:score".

How would you go about it? Would you:
A) Craft a pipe stream of shell utils?
B) Use the heavy tools - a Java main() that with uses JDom?
C) Refresh the XSLT skills you never had?
D) Try your luck with a Python script?
E) search for an online XML editor tool?
F) Or, my pick at the bottom.

Example xml document to sort:
<feed>
    <entry>
        <title>Bibi</title>
        <score>0.21000001</score>
    </entry>
    <entry>
        <title>Lapid</title>
        <score>0.42000002</score>
    </entry>
    <entry>
        <title>Yechimovich</title>
        <score>0.235</score>
    </entry>
    <!--- 997 more entries -->
</feed>

My Pick: considered all the above but it sounded like a headache for a simple sort operation. So I've .... thrown the file at MS Excel, turns out it can digest it rather well, and then I sorted by the score column. Yes! Surprising. But crappy MS Excel did the job (the original schema had more nesting than the document in the example above).

Life-saver lesson: Spend time picking the right tool for the job, than on the job itself.

One click to sort