Free Memory in Android

A few students of mine have been wondering about checking how much memory is being used on Android (using Processing) for their games, so I came up with a simple sketch that uses some built-in Android Debug features (reference here):

<span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">void setup()</span>
<pre>{
  size(480, 800, A3D);
  orientation(PORTRAIT);

  long heapStart = android.os.Debug.getNativeHeapAllocatedSize();

  println("HEAP START:" + heapStart);
  println("FREE: " + android.os.Debug.getNativeHeapFreeSize() );

  PImage p = loadImage("spore.png");

  long heapSize1 = android.os.Debug.getNativeHeapAllocatedSize();

  println("HEAP START[spore]:" + heapSize1 + " : " + (heapSize1-heapStart));
  println("FREE: " + android.os.Debug.getNativeHeapFreeSize() );

  // load another image into the old one
  p = loadImage("scissors.png");

  long heapSize2 = android.os.Debug.getNativeHeapAllocatedSize();

  println("HEAP START[spore,scissors]:" + heapSize2 + " : " + (heapSize2-heapSize1));
  println("FREE: " + android.os.Debug.getNativeHeapFreeSize() );

  // NOTE that memory still increases - the 1st is not garbage collected!

  // remove images - both should be garbage collected and removed from heap
  p = null;

  long heapSize3 = android.os.Debug.getNativeHeapAllocatedSize();

  println("HEAP START[spore]:" + heapSize3 + " : " + (heapSize3-heapSize2));

  println("FREE: " + android.os.Debug.getNativeHeapFreeSize() );

  noLoop();
}

void draw()
{

}