Each release of the Java platform includes changes designed to speed startup time and reduce memory requirements. It's an old song, but a necessity nonetheless, as each new release results in a larger, broader platform. The 5.0 release is no exception. This month, I'll explore these additions.
The JVM includes several new command-line
options. One such nonstandard option is a fatal error handler.
Starting the JVM with the -XX:OnError option lets you
specify a command to execute once the error happens and the
JVM stalls. Listing 1 shows several such options:
Listing 1. Some OnError handling options
-XX:OnError="gcore %p; dbx - %p" -XX:OnError="gdb %p" -XX:OnError="pmap %p" |
The command within quotation marks executes when a fatal
error happens within the JVM. The %p option is replaced with the
process ID. Deliberately causing an error isn't easy,
but if you run across a repeatable situation that does happen, the
information made available through the handler can prove to be
extremely helpful.
The OnError option uses the Java Debug Interface (JDI)
serviceability agent connector bridge. With it, you can attach to
core files or stalled VMs, among other tasks. Several
diagnostic tools, shown below, come with the JDK and take advantage of this bridge, though the last three are not available on Microsoft Windows platforms:
jps-- gets process IDjstat-- gets statistics for process ID (jstat -gc pid)jinfo-- gets JVM configuration informationjmap-- prints memory map of librariesjstack pid | core-- generates a stack trace
To help improve startup time, the JVM now works with memory-mapped files. Created at installation time, these files store an internal representation of the system classes. Then, when it is time to start the JVM, instead of loading the system classes from scratch, the memory-mapped files are loaded. This helps in two ways. First, roughly half of the memory-mapped files are read-only, which means they can be shared across multiple simultaneously running processes, reducing startup time considerably and total memory needed. Second, as the files are already in a format usable by the Java HotSpot VM, the memory used to process the original class files never happens, which improves startup time.
The location of the shared archive file is platform dependent. The file is named classes.jsa, where the JSA extension stands for Java Shared Archive. On a Microsoft Windows platform, you'll find the shared archive file in jre\bin\client. On a Linux platform, the location is jre/lib/[arch]/client. Both of these locations are based off the JAVA_HOME root.
If you don't like the file, delete it. In fact, you must delete it if you want to regenerate the file. Use the -Xshare:dump
command-line switch with the java command for regeneration.
When executed, you'll get output similar to Listing 2:
Listing 2. Regenerating shared data file command output
Loading classes to share ... done. Rewriting and unlinking classes ... done. Calculating hash values for String objects .. done. Calculating fingerprints ... done. Removing unshareable information ... done. Moving most read-only objects to shared space at 0x2aad0000 ... done. Moving common symbols to shared space at 0x2ae2e848 ... done. Moving remaining symbols to shared space at 0x2af51148 ... done. Moving string char arrays to shared space at 0x2af51bd8 ... done. Moving additional symbols to shared space at 0x2afd2ef0 ... done. Read-only space ends at 0x2b027960, 5601632 bytes. Moving read-write objects to shared space at 0x2b2d0000 ... done. Moving String objects to shared space at 0x2b825be8 ... done. Read-write space ends at 0x2b8643a8, 5850024 bytes. Updating references to shared objects ... done. |
If you don't like the concept of having a shared data file, don't
use it. You can disable it with the -Xshare:off option. Then,
all the original class information will be loaded from the rt.jar
file.
To ensure shared data file usage is enabled, use
the -Xshare:on option. Usage is not on by default. Instead the
-Xshare:auto option is the default. The auto option means that the
file should be used if possible. If it isn't possible (for instance, if you deleted it), then the option is ignored.
If you manually control the usage of the shared class data file, be aware that the command-line options are subject to change in a future release -- at least that's what the documentation says.
Sun claims startup times improve by roughly 30 percent when enabled.
The Java Thread class includes a setPriority() method to help assign a priority to the created thread. The key thing to know about the
setPriority() method is that the setting can be ignored. Also, different platforms have different thread schedule models, resulting in different results.
With the 5.0 release, the thread priority implementation for the
Solaris platform changed somewhat, reverting to an older behavior.
As a result, the mapping from the 10 logical Java priorities (Thread.MIN_PRIORITY
to Thread.MAX_PRIORITY) to native priorities is different:
- Threads with a Java priority in the range of 5 to 10 are mapped high
- Threads with a Java priority in the range of 1 to 4 are mapped low
It is that simple -- if you want to think of fewer priorities as simpler. The reasoning behind the change is to ensure Java threads are scheduled to run at the same priority as native threads and processes. But this means that Java priorities between 5 and 10 are not treated differently. (For more information on this change, see "Thread Priority on the Solaris™ Platform" in the Resources section.)
If you are on a Linux or Microsoft Windows platform, you can ignore this change. Still, be aware of the difference in case your users are on a Solaris platform.
By default, the JVM starts up with the client HotSpot virtual machine,
unless a 64-bit chip is used, which defaults to the server VM. You can use the -server command-line option to force the
enabling of the server HotSpot VM. If you are on a 32-bit Solaris SPARC or Linux/Solaris
i586 system and don't specify the
-client or -server options, the VM will automatically detect which
version to use. Microsoft Windows defaults to client-side.
When automatic detection is in use, a machine is considered server-class if it has at least two CPUs and two gigabytes of physical memory. If you don't mind the slower startup time, without the newly available shared memory, the server VM will run more quickly over time.
If you do happen to run the server VM, or at least are on a
server-class machine, the default garbage collector has changed
from the old serial version (-XX:+UseSerialGC) to the new parallel
collector (-XX:+UseParallelGC). Other default settings related to
heap size and time limits have also changed. (See "Garbage Collector
Ergonomics" in the Resources section for additional information
related to these settings.)
We finish this tip where we started. With each Java release, things change. The VM configuration has new and different options promising a faster startup and a smaller memory footprint. The 5.0 release of the J2SE platform follows this same pattern.
- The article "Java technology, IBM style: Class sharing" (Corrie, developerWorks, September 2006) shows a completely transparent and dynamic means of sharing all loaded classes that places no restrictions on the JVMs that are sharing the class data.
- Download J2SE 5.0 from the Sun Developer Network.
- The article "Java shared classes" (developerWorks, June 2004) by Lakshmi Shankar, Simon Burns, and Roshan Nichani explores how to start your Java applications faster and with a smaller memory footprint.
-
Java theory and practice
columnist Brian Goetz wrote several articles relevant to this discussion:
- Garbage collection in the HotSpot JVM (November 2003) details generational and concurrent garbage collection.
- Garbage collection and performance (January 2004) gives you hints, tips, and myths about writing garbage collection-friendly classes.
- Dynamic compilation and performance measurement (December 2004) discusses the perils of benchmarking under dynamic compilation.
- "Thread Priority on the
Solaris™ Platform" discusses JVM-to-native-thread priority mapping.
- Read the Garbage Collector Ergonomics documentation to learn about customizing the server-class machine settings.
- "The Java HotSpot Virtual Machine, v1.4.1" white paper includes more in-depth information about an earlier release of HotSpot.
- To learn more about Java technology, visit the
developerWorks Java zone. You'll find technical documentation, how-to articles,
education, downloads, product information, and more.
- Visit the New to Java technology site for the latest resources to help you get started with Java programming.
- Get involved with the community by participating in
developerWorks blogs.
- Browse for books on these and other technical topics.

John Zukowski conducts strategic Java consulting with JZ Ventures, Inc. and is working with SavaJe Technologies to develop a next-generation mobile phone platform. His latest books are Mastering Java 2, J2SE 1.4 (Sybex, April 2002) and Learn Java with JBuilder 6 (Apress, March 2002). Reach him at jaz@zukowski.net.





