| [ directory ] |
|
Design Issue 3桟onstrained Memory SizeWhen 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 OrganizationTo 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
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.
Memory UsageTo 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.
|
| [ directory ] |
|