Design Issue 3?Constrained Memory Size

When we say that the Palm is a constrained memory environment, what does that mean to an application developer? First of all, let's have a look at how memory is organized in Palm OS.

Memory Organization

To understand memory organization on MIDP on Palm OS, start a MIDP application. Select the Options menu and then select Memory Info. A dialog similar to that of Figure 4.2 will appear.

Figure 4.2. A dialog showing memory information

graphics/04fig02.gif

The types of memory displayed in Figure 4.2 are explained in Table 4.1. Note that these descriptions are specific to the KVM and Sun's implementation of MIDP for Palm OS.

Table 4.1. Types of Memory

Type

Description

rom

The size of the ROM image on this device.

ram

The combined size of RAM on this device. Palm OS divides RAM into two logical areas: dynamic and storage. Both areas remain intact when the device is switched off, but a reset will clear out the dynamic area.

freeram

The combined size of available storage heap and dynamic memory heap on this device.

"Dynamic heap" refers to the area of RAM implemented as a single heap that is used for dynamic allocations such as application stacks, application dynamic allocations, and system dynamic allocations. In a Palm OS 3.5 device with 4 MB or more, the dynamic heap space is 256 KB. This amount of memory is set aside whether or not it is currently used for dynamic allocations.

"Storage heap" refers to the remainder of RAM. There can be one or more storage heaps. Storage heaps are used to store nonvolatile data, such as databases.

In all versions of Palm OS to date, the maximum chunk of storage RAM that can be obtained by an application is slightly less than 64 KB.[a]

freeheap

The size of the available memory in the storage heap.

maxheapchunk

The size of the largest chunk in the dynamic heap.

javafreeheap

The amount of memory available in the Java heap. The Java heap is the area of the dynamic heap that is used by Java when allocating new objects as well as objects on the application's stack. As it is a chunk of memory from the dynamic heap, its maximum value is slightly less than 64 KB.

permanent

The amount of memory allocated internally by the KVM.

[a] The 64 KB limit is increased in Palm OS 5.0.

Memory Usage

To see how much memory is available to the developer, we can write a simple application. There are two methods in the Runtime class that are useful for working out available memory. They are totalMemory and freeMemory. The following code fragment shows how to determine the memory available inside a MIDlet:

Runtime runtime = Runtime.getRuntime();
runtime.gc();
System.out.println("Total: " + runtime.totalMemory() +
  " Free: " + runtime.freeMemory());

Note that we use the Runtime method gc before calling the freeMemory method. The purpose of this call is to prompt the virtual machine to clean up unused objects that may not have been garbage collected because the amount of memory is not sufficiently low to trigger a routine garbage collection.

The amount of memory returned by freeMemory can be different from the amount of memory reported by javafreeheap in the Options | Memory Info dialog. This is because javafreeheap refers to the internal Java dynamic heap used for stack and new Java objects, while freeMemory returns an approximation of the amount of memory available for future allocated objects.

The MIDlets MemoryMIDlet, StaticMemoryMIDlet, and MultipleClassMIDlet each perform a different memory allocation. MemoryMIDlet allocates a new array of 1000 bytes.

Runtime runtime = Runtime.getRuntime();
runtime.gc();
beforeFree = runtime.freeMemory();
largeArray = new byte[1000];
runtime.gc();
afterFree = runtime.freeMemory();
System.out.println("Total: " + runtime.totalMemory() +
  " Free (before): " + beforeFree +
  " Free (after): " + afterFree);

StaticMemoryMIDlet does the same thing, but also has a static array of 2000 bytes:

static byte[] largeStaticArray = new byte[2000];

Finally, MultipleClassMIDlet creates an instance of LargeClass, a class that has an attribute that references an array of 1000 bytes:

LargeClass largeClass = new LargeClass();

where LargeClass is defined as:

package com.javaonpdas.small;

public class LargeClass {

  private byte[] byteArray = new byte[1000];

  public String method1(String s1, String s2) {
    return s1+s2;
  }

  public String method2(String s1, String s2) {
    return s1+s2;
  }
}

The number of bytes returned by the freeMemory() method were recorded before and after the dynamic allocation, and are collated in Table 4.2.

From the table, note that a static array object takes up space from the same Java heap that is used to allocate dynamic objects. That is, statically and dynamically allocated objects use the same heap space. As noted previously, the heap space is restricted to 64 KB in Palm OS 3.X and 4.X.

Table 4.2. Memory Consumed by Static and Dynamic Allocations

MIDlet

Bytes Consumed by Loading the MIDlet

Bytes Further Consumed by Processing the "Allocate" Command

Allocation Performed

MemoryMIDlet

4396

1016

Allocate an array of 1000 bytes

StaticMemoryMIDlet

6412

1016

Allocate an array of 1000 bytes

MultipleClassMIDlet

4392

1032

Allocate a new instance of LargeClass