Last active
December 27, 2015 02:49
-
-
Save gilinachum/7255058 to your computer and use it in GitHub Desktop.
Max heap enforcer. Makes sure your unit tests heap size, on the PC, is not larger than the heap size of the Android device your developing for (24m-64m). Otherwise your code will rock on the PC, but get an OOME on the device.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment