Title: Memory Management
1Memory Management
- Real-Time Java Specification
- Mikko Laukkanen
2Presentation outline
- Background
- Javas current memory management.
- Garbage collection techniques.
- RTJSs concepts on memory management.
- RTJS-specified classes for memory management.
- Conclusion.
3Java Memory Management
- Class area
- Code and constants.
- Java stack
- Keeps track of method execution and data
associated with each method invocation. - Heap
- Place where objects live.
- Native method stacks.
- For supporting native methods.
4Class area
Class name
Superclass name
Fields
Method definitions
Code area
5Java stack
6Heap
7Native stacks
- Native methods usually use stack to keep track of
their state. - JVM provides a native stack that native methods
can use. - Most commonly supported standard for native
methods is Java Native Interface (JNI).
8Garbage collection - rules
- If there is a reference to the object on the
stack, then it is alive. - If there is a reference to the object in a local
variable, on the stack, or in a static field,
then it is alive. - If a field of a live object contains a reference
to some object, this object is also alive. - The JVM may internally keep references to a
certain objects, for example, to support native
methods. These objects are alive.
9Garbage collection - techniques
- Basic techniques
- Reference counting
- Mark-and-sweep
- Mark-and-compact
- Copying collector
- Incremental tracing collectors
- Tri-colour marking
- Generational techniques
- Young generations of objects are more probably
garbage.
10Real-time garbage collectors?
- Reference counting
- References need memory.
- Cannot handle circular references.
- Reduces efficiency.
- Incremental collectors
- Fine-grained incremental collector is a good
candidate for RT garbage collector.
11Presentation outline
- Background
- Javas current memory management.
- Garbage collection techniques.
- RTJSs concepts on memory management.
- RTJS-specified classes for memory management.
- Conclusion.
12RTJS principles
- Garbage-collected heaps wont work!
- Work-around lets have memory areas that are
never garbage collected. - Both short-lived and long-lived objects can be
allocated from these memory areas.
13Memory areas
- Scoped memory
- Lifetime defined by syntactic scope.
- Physical memory
- Access to physical memory addresses directly.
- Immortal memory
- Objects alive as long as the application.
- Heap memory
- Standard Java heap.
14Requirements for memory areas
- Linear allocation time in some areas.
- Scoped areas
- Own reference counters for scopes.
- Scopes may be nested.
- Immortal areas
- JVM has exactly one immortal area.
- Immortal objects live for the duration of the
application. - Heap memory area
- JVM has exactly one heap memory area.
15Assignment restrictions
- JVM must ensure that above checks are performed
before the statement is executed!
16RTJS garbage collector requirements
- GC algorithm is independent and can be changed,
- Programmer is able to precisely characterise an
implemented GC algorithm's effect on the
execution time, preemption, and dispatching of
real-time Java threads, and - Allocation and reclamation of objects is possible
outside of any interference by any garbage
collector algorithm.
17Presentation outline
- Background
- Javas current memory management.
- Garbage collection techniques.
- RTJSs concepts on memory management.
- RTJS-specified classes for memory management.
- Conclusion.
18MemoryArea class - overview
- Abstract base class.
- enter () method for entering to the area.
- Methods for object creation
- newInstance ()
- newArray ()
- Query methods for consumed and remaining memory
as well as the size of the memory area.
19MemoryArea class - diagram
20Heap memory - overview
- Allows access to standard Java heap.
- Singleton class.
21Heap memory - diagram
22ScopedMemory - overview
- Abstract base class.
- Represents memory space with a limited lifetime.
- ScopedMemory class itself is instantiated in the
currently used memory area.
- Pointer safety rules
- Java heap and immortal objects cannot reference
scoped object. - A reference to a scoped object cannot be assigned
into the enclosing scope.
23ScopedMemory - diagram
24ScopedMemory - example
final ScopedMemory s new LTMemory
(16,1024) s.enter (new Runnable () public void
run () try s.newInstance
(Class.forName ("Foo")) catch
(Exception e) )
final ScopedMemory s new LTMemory
(16,1024) RealtimeThread t new RealtimeThread
(null, null,
new MemoryParameter
(s), null,
new Runnable ()
public void run ()
// ...
25ImmortalMemory - overview
- Shared among all threads.
- Objects in immortal memory live for the lifetime
of the application. - Is not garbage collected, but may be scanned.
- Singleton class.
26ImmortalMemory - diagram
27RawMemoryAccess - overview
- A storage for a fixed-size sequence of bytes.
- Allows different type of memories.
- Allows implementation of device drivers, memory
mapped I/O and similar kind of low-level
software. - Is created by the PhysicalMemoryFactory.
28RawMemoryAccess - diagram
29Physical memory areas - overview
- Allows creation of object mapped to a physical
memory addresses. - Rationale is that for instance faster RAM could
be used, if needed. - Two kinds of classes
- ImmortalPhysicalMemory
- ScopedPhysicalMemory
- Both are created by PhysicalMemoryFactory.
30Physical memory areas - diagram
31Creating the physical memory areas
32Garbage collectors - overview
- System shall provide dynamic and static
information about GC overhead. - GC structure is really simple, only guidelines
are specified and examples given. - Only one mandatory method getPreemptionLatency
(). - Example garbage collectors are just examples and
are not required for RTJS-compliant
implementation.
33Garbage collector - diagram
34Presentation outline
- Background
- Javas current memory management.
- Garbage collection techniques.
- RTJSs concepts on memory management.
- RTJS-specified classes for memory management.
- Conclusion.
35Conclusion
- Automatic memory management and especially
garbage collection is an obstacle for real-time
applications. - RTJS works around this by defining memory areas
that are not garbage collected. - However, (real-time) garbage collecting
algorithms can be used.