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

Sponsored Recommendations

What are the Important Considerations when Assessing Cobot Safety?

April 16, 2024
A review of the requirements of ISO/TS 15066 and how they fit in with ISO 10218-1 and 10218-2 a consideration the complexities of collaboration.

Wire & Cable Cutting Digi-Spool® Service

April 16, 2024
Explore DigiKey’s Digi-Spool® professional cutting service for efficient and precise wire and cable management. Custom-cut to your exact specifications for a variety of cable ...

DigiKey Factory Tomorrow Season 3: Sustainable Manufacturing

April 16, 2024
Industry 4.0 is helping manufacturers develop and integrate technologies such as AI, edge computing and connectivity for the factories of tomorrow. Learn more at DigiKey today...

Connectivity – The Backbone of Sustainable Automation

April 16, 2024
Advanced interfaces for signals, data, and electrical power are essential. They help save resources and costs when networking production equipment.

Comments

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