1. Field of the Invention
The present invention relates to techniques for increasing the efficiency of software on mobiles devices. More specifically, the present invention relates to a method and an apparatus for reducing the number of heap handles in a program, which reduces the effort and resources involved in manipulating the heap handles.
2. Related Art
The Java 2 Platform, Micro Edition (J2ME™), has become a very popular software platform for memory-constrained devices such as wireless devices. Motorola, Nokia, NTT DoCoMo, RIM, Siemens, and many other key players in the wireless device industry have shipped a large number of J2ME™-enabled devices. In fact, over one billion J2ME™-enabled mobile phones have been shipped during the past few years.
A number of techniques for conserving memory have been developed in order to effectively run applications on such memory-constrained computing devices. One such technique uses exact garbage collection on a shared heap to reclaim memory that is no longer in use. Exact garbage collection uses precise knowledge about all pointers to the heap and between heap objects when scanning and relocating objects. Pointers to the heap are often referred to as “heap roots,” and typically include static pointers and local heap pointers in stack frames.
Exact garbage collection is often implemented using a programming language which is not associated with a garbage-collected heap. For instance, a J2ME™ virtual machine might be written using the C or C++ programming languages. The compilers for such languages are typically unaware of garbage collection, and often do not provide support for locating heap pointers in stack frames. Without such dedicated language support, heap pointers in programs are typically implemented using special structures known as “heap handles.” These heap handles are typically allocated on the stack and require proper management. For instance, heap handles need to be: initialized; inserted into the “root set” of heap roots; and removed after use. Note that the root set is often implemented as a linked list of heap handles.
Using heap handles incurs both direct and indirect costs. For instance, additional code needs to be generated and executed to insert and remove heap handles from the root set, and heap handles consume more memory than ordinary pointers in stack frames. Additionally, compilers that are unaware of heap-handle semantics cannot optimize heap handles in the same manner as ordinary pointers, since heap handles are typically at least two-field data structures, and heap handles escape local scope the moment they are added into the root set. Furthermore, if a heap handle is not promptly removed from the root set or set to null after the corresponding object for the heap handle is used, the garbage collector considers the object to be reachable, and does not dispose of the object.
Hence, what is needed is a method and an apparatus that reduces the above-described costs of manipulating heap handles where possible.