7.12.1 Implementing to place objects in the Explicit heap
This subsection describes the overview of classes of the Explicit Memory Management functionality API and the basic way using the API.
- Organization of this subsection
(1) Relationship between the ExplicitMemory instance and the Explicit memory block
The Explicit memory blocks in the Explicit heap map 1:1 to the instances of the ExplicitMemory class handled by the Explicit Memory Management functionality API.
The following figure shows the relationship between the ExplicitMemory instance and the Explicit memory block.
|
The Explicit memory block is initialized if you execute a constructor of the ExplicitMemory class. Thereafter, the initialized instance becomes an interface for operating Explicit memory blocks. Instance em1 in the figure maps to the Explicit memory block 1 while instance em2 maps to the Explicit memory block 2.
(2) Class configuration of the Explicit Memory Management functionality API
The following table describes classes in the Explicit Memory Management functionality API.
Class |
Explanation |
---|---|
MemoryArea class |
This is an abstract class that represents the Java heap or the Explicit memory block. |
ExplicitMemory class |
This is an abstract class that represents Explicit memory block. Use functionalities of this class through the BasicExplicitMemory class, which is a derived class. |
BasicExplicitMemory class |
This is a class that represents Explicit memory block handled in an application. In application, implement the Explicit Memory Management functionality by using the API of this class. |
The following figure shows the class hierarchy.
|
(3) How to use the Explicit Memory Management functionality API
The mapping of basic operations and methods is as follows:
-
Directly generating the objects in Explicit memory block
Use the newArray method or newInstance method.
-
Releasing Explicit memory blocks
Use the reclaim method.
An example of using the BasicExplicitMemory class is described below. This example creates two Explicit memory blocks.
Example of using the BasicExplicitMemory class
Line |
Java program |
---|---|
01 02 03 04 05 06 07 08 |
BasicExplicitMemory em1 = new BasicExplicitMemory(); BasicExplicitMemory em2 = new BasicExplicitMemory(); Object o1 = em1.newInstance(Object.class); Object o2 = em2.newInstance(Object.class); ExplicitMemory.reclaim(em1); |
The BasicExplicitMemory instances are generated on lines 01 and 02. In this example, em1 maps to the Explicit memory block 1 while em2 maps to the Explicit memory block 2.
Directly generate the objects in the Explicit memory block by using the newInstance method on lines 04 and 06. On line 04, place an object of the Object class in the Explicit memory block 1 by invoking the newInstance method from em1 instance. On line 06, place an object of the Object class in the Explicit memory block 2 by invoking the newInstance method from em2 instance.
Destroy the Explicit memory block when it becomes unnecessary. An execution of the ExplicitMemory.reclaim(em1) method on line 08 is a processing to release the Explicit memory block 1. The processing releases the Explicit memory block 1 and at the same time, destroys object o1 that was generated on line 04. Note that when releasing Explicit memory blocks, entire area corresponding to the appropriate Explicit memory block is targeted for release and not the objects.
In this example, survival period of the Explicit memory block 1 is from line 01 to line 08.