Real-Time Garbage Collection Speeds Embedded Java

May 13, 2002
Real-time garbage collection isn't an oxymoron. Just try spreading the cost of garbage collection over time so programming languages like Java can suit real-time applications without resorting to a constrained programming environment. Unfortunately,...

Real-time garbage collection isn't an oxymoron. Just try spreading the cost of garbage collection over time so programming languages like Java can suit real-time applications without resorting to a constrained programming environment. Unfortunately, many designers are familiar with the more traditional Java virtual machines (JVMs) that aren't suitable for real-time applications. Luckily, JVM designers aren't taking this lying down. New embedded JVMs are handling garbage collection more efficiently.

Typical JVMs use a mark-and-sweep garbage collector (GC) that could take as much as a few seconds to recover unused memory for a large application. The process can be interrupted if memory can be locked down, but not for memory allocation. Such a delay is unacceptable in a real-time environment. The trick to using garbage collection in a real-time environment is to minimize the time necessary to perform garbage collection.

NewMonics (www.newmonics.com) uses a hybrid approach in its new PERC JVM. The company combines a copying generational GC with a conventional mark-and-sweep GC. Generational GCs have been around for a while, but not in embedded systems. They're more efficient because most dynamically allocated memory becomes garbage in a short time. A GC that can quickly recover garbage reduces the amount of memory needed to support a system, as there's less outstanding garbage and collection can be done less often. Typically, different generations are kept in separate regions. Some implementations on PCs with megabytes of memory use a large number of regions.

The company employs regions with the youngest data, which is kept in an area called the nursery. The GC copies live data to a second region in the nursery. After adjusting pointers to active data, the destination becomes the active region. Older data that repeatedly survives this process is copied to a region outside the nursery, where it's expected to live longer. Data outside the nursery that becomes garbage is recovered by a subsequent mark-and-sweep GC. It operates on one region at a time and merges regions when possible.

PERC uses heuristics and configuration parameters to pace the time that each GC runs. Therefore, applications have free memory when required, instead of having to start a GC cycle. Pacing allows the garbage collector to run even though there is free memory available.

The generational GC runs more frequently but takes very little time compared to the mark-and-sweep GC that may run every few seconds or minutes. The combination of pacing and generational GC minimizes the time needed to do garbage collection since only small regions are handled at a time. Consequently, less time is spent doing garbage collection. Differences can be dramatic. Applications that spent 70% of their time in the GC can spend less than 10%. No longer must garbage collection be a drag on real-time performance.

See associated Figure 1
See associated Figure 2

HYBRID GARBAGE COLLECTION
Approach
Divide free memory into multiple regions
Frequent generational GC in nursery
Mark-and-sweep over all regions
Heuristics used to anticipate GC needs
GC can be tailored for application performancerequirements

Advantages
Most garbage occurs in nursery
Nursery GC is fast
High-priority tasks can always allocate
Paced GC guarantees free memory
200-ms interrupt latency

Disadvantages
GC interrupt latency is less but is measurable
More complex GC
Maximum allocation size is limited by region size
About the Author

William G. Wong | Senior Content Director - Electronic Design and Microwaves & RF

I am Editor of Electronic Design focusing on embedded, software, and systems. As Senior Content Director, I also manage Microwaves & RF and I work with a great team of editors to provide engineers, programmers, developers and technical managers with interesting and useful articles and videos on a regular basis. Check out our free newsletters to see the latest content.

You can send press releases for new products for possible coverage on the website. I am also interested in receiving contributed articles for publishing on our website. Use our template and send to me along with a signed release form. 

Check out my blog, AltEmbedded on Electronic Design, as well as his latest articles on this site that are listed below. 

You can visit my social media via these links:

I earned a Bachelor of Electrical Engineering at the Georgia Institute of Technology and a Masters in Computer Science from Rutgers University. I still do a bit of programming using everything from C and C++ to Rust and Ada/SPARK. I do a bit of PHP programming for Drupal websites. I have posted a few Drupal modules.  

I still get a hand on software and electronic hardware. Some of this can be found on our Kit Close-Up video series. You can also see me on many of our TechXchange Talk videos. I am interested in a range of projects from robotics to artificial intelligence. 

Sponsored Recommendations

Comments

To join the conversation, and become an exclusive member of Electronic Design, create an account today!