Java Reference
In-Depth Information
v2[i] = o;
v = v2;
v.setElementAt (o, i); v [i] = obj;
Using Record Stores Instead of Heap Memory
An additional opportunity for saving heap space is to store application data in a record store instead of
consuming heap memory. The space available for persistent storage is often significantly larger than
the heap memory, so in situations where heap memory is really rare, it may make sense to shift some of
the memory consumption from the heap to persistent storage. However, accessing persistent storage
may be significantly slower than heap access. Thus, the price for having more heap may be a
significant performance tradeoff.
In order to demonstrate how to store objects in a record store instead of a Vector , let's create a sample
implementation of a StringVectorRms class that stores a list of String s in a record store. In
contrast to an ordinary Vector , only a small amount of heap is consumed. The StringVectorRms
implementation provides the access methods listed in Table 8.2 .
Table 8.2. Meth o ds of the StringVectorRms Class
Method Description
StringVectorRms() Constructs an instance and creates the underlying
record store if needed.
addString (String text) Adds a String to the end of the Vector .
String stringAt (int index) Returns the String at the given index.
removeStringAt (int index) Removes the String at the given index and shifts the
entries starting at index +1 down in order to fill the
resulting gap.
SetStringAt (String
newText, int newIndex)
Replaces the String at position index with the new
String that is contained in the variable newText .
int size()
Returns the number of String s that are currently
stored.
Listing 8.1 shows the corresponding StringVectorRms implementation. It handles the
conversation between Strings and the byte arrays stored in the record store. It also maps the
Vector indices starting with 0 to RMS indices starting with 1 and maps all RmsException s to
RuntimeException s. Finally, it takes care of shifting the remaining elements to their new index if
an element is deleted.
Note
Especially for flash memory, write operations might take seconds. Thus, the removeStringAt()
method should be used with special caution; all remaining items are moved to their new index position,
requiring a lot of write operations.
Listing 8.1 StringVectorRMS.java —The StringVectorRMS Class for Storing
String s in a Record Store During Application Runtime
import javax.microedition.rms.*;
 
 
 
Search WWH ::




Custom Search