★ wanayoo — archive 1999 http://java.sun.com/products/personaljava/MemoryUsage.htmlNouvelle recherche | Portail wanayoo

PersonalJavaTM 1.1 Application Environment Memory Usage Technical Note

July 1998

Abstract

One of the primary design goals for the PersonalJava application environment is the minimization of the static memory footprint and the runtime memory usage. The static memory footprint is comprised of mostly ROM, but also some RAM. It is the memory which is used when the PersonalJava application environment classes are preloaded with JavaCodeCompact. The runtime memory usage, as the term suggests, is the memory which is consumed at runtime by the PersonalJava virtual machine for dynamically loaded classes, native and JavaTM stacks, and heap storage to support dynamic allocation and garbage collection. Through SunTM's work on the PersonalJava 1.1 virtual machine, the following areas of memory usage have been decreased: the preloaded and dynamically loaded class memory footprint, the native stack usage and the Java stack usage. The result is a system with drastically reduced ROM and RAM requirements which has no perceivable degradation in execution speed.

This paper discusses in detail the optimizations which were made to achieve an approximately 28% decrease in class memory footprint from the PersonalJava 1.0 application environment to the PersonalJava 1.1 application environment. It is assumed that the reader is familiar with Java technology and the workings of the Java virtual machine* as specified in "The Java Virtual Machine Specification" by Lindholm and Yellin. Familiarity with one of the Java virtual machines implemented by Sun is also helpful.

Note that the optimizations discussed in this paper affect the implementation of the Java virtual machine and tools such as JavaCodeCompact only. No change has been made to either the Java Virtual Machine specification or the PersonalJava API.

Introduction

By conducting an extensive analysis of memory usage in the Java application environment, a few categories from among the top memory consumers were identified as having the most potential for memory usage reduction:
  1. The memory footprint of classes and their various components
  2. Native stack usage
  3. Java stacks
  4. Java heap usage
  5. C code size
#1, #2 and #3 were selected as having the most potential for application-independent memory use reductions. The focus of Sun's work is on these three categories. #4 is application dependent and therefore hard to reduce generically. #5 does not lend itself to too much reduction without significant rewrites of the virtual machine and native methods.

Class Footprint Reductions

It is customary for the classes of a Java program to be loaded as late during the program's execution as possible: they are loaded on demand from the network (stored on a server), or from a local file system when first referenced during the program's execution. The virtual machine locates and loads each class, parses the class file, allocates internal data structures for its various components, and links it in with other loaded classes. This process makes the method code in the class readily executable by the virtual machine. A large part of the runtime memory consumption in Java is due to the dynamic allocation of the internal data structures for classes and their components (class metadata), and of the memory allocated for the method bytecodes.

For small and embedded systems which lack fast dynamic class loading facilities such as a network connection, a local file system, or other permanent storage, it makes sense to have a "class preloader" to load the class off-line. Source licensees of the PersonalJava application environment receive a class preloader called JavaCodeCompact which performs this task. JavaCodeCompact creates the internal virtual machine data structures representing a class off-line, and lays them out in mostly read-only memory.

Changes were made that reduce the memory allocations required per class. Some of these changes occur only in JavaCodeCompact, thus affecting only the ROM footprint. Some of the other techniques apply both to preloaded and dynamically loaded classes, affecting both the ROM and RAM footprints.

 

Memory reductions that apply to preloaded classes only

Using JavaCodeCompact has the advantage of preloading all of the classes of the PersonalJava application environment at once. This allows for the sharing of information between classes, thereby reducing the ROM requirement considerably. Space is saved by exploiting the fact that all symbolic references in these classes are resolved, and that bytecode instructions referring to these symbolic entries have been quickened (i.e. re-written with versions that do not do symbolic lookups).

Memory reductions that apply to both preloaded and dynamically loaded classes

Numbers

Here are some measurements that demonstrate the effectiveness of the class memory footprint reductions in the PersonalJava 1.1 application environment. We measure the memory footprint of the pre-loaded classes in ROM and RAM. This measurement is also highly characteristic of memory footprint for dynamically loaded classes.

The first comparison is of the PersonalJava 1.1 application environment with all optional packages included (rmi, sql, math, zip, and code signing), with a similarly configured Java 1.1.6 application environment, adjusted to exclude components unsupported by the PersonalJava application environment (e.g. java.security.acl).

 
PersonalJava 1.1 
Application Environment
Java 1.1.6 
Application Environment
No. of classes1
913
911
Total size of class files
1.997M
2.022M
Total class memory footprint
1.593M
2.222M
% of RAM in total footprint
0.2%
6.5%
1For the PersonalJava 1.1 application environment, this number includes core as well as all optional packages. For the Java application environment, this number includes the packages which would be necessary to implement as close to the same functionality as for a fully configured PersonalJava application environment.

The Java 1.1.6 application environment comes out to be around 39.5% larger. It also has about 32.5 times the RAM requirement of the PersonalJava 1.1 application environment.

The second comparison is of the PersonalJava 1.1 application environment with no optional packages included, with the PersonalJava 1.0 application environment.

 
PersonalJava 1.0 
Application Environment
PersonalJava 1.1 
Application Environment
No. of classes2
641
689
Total size of class files
1.284M
1.506M
Total class memory footprint
1.402M
1.227M
% of RAM in total footprint
7.6%
0.3%
2For both the PersonalJava 1.0 and 1.1 application environments, this number includes core packages only. Since the PersonalJava 1.1 application environment has a richer set of features than the PersonalJava 1.0 application environment, there are more classes in the PersonalJava 1.1 application environment.

The PersonalJava 1.0 application environment comes out to be 14% bigger, even though it has only 83% of the .class data of the PersonalJava 1.1 application environment. Normalized, this translates to the class footprint of the PersonalJava 1.0 application environment being 38% bigger than that of the PersonalJava 1.1 application environment. Similarly, the RAM requirement for the classes has gone down as much as 25-fold when measured relative to total class memory footprint. The normalized value is around 30-fold.

Note that the results shown above are for the core classes of the PersonalJava application environment which are stored primarily in ROM. More important, perhaps, is the memory usage savings which can be achieved in RAM. By recognizing that the core classes contain a representative cross section of the classes which will appear in PersonalJava applications, the above analysis can be extended to dynamically loaded applications which are the primary consumers of RAM. The result is that an equivalent savings is expected for RAM usage.

Native Stack Size Reductions

Extensive static analysis of the native code in the virtual machine and supporting libraries was performed with the aim of: These make it possible to determine a tight upper bound on the stack usage of any thread. To realize this, the following investigations and modifications were performed: Based on these efforts, a default thread stack size of 20 kbytes is expected to be a good candidate if libraries optimized to reduce stack consumption are used. Moreover, the potential for a memory corruption caused by stack overflow is eliminated since it is guaranteed that a StackOverflowError will be thrown in advance at a safe point. This result is a significant improvement from the PersonalJava 1.0 virtual machine level of 128 kbytes, a very conservative, yet not even safe stack size choice.

Note that the SPARC architecture consumes more stack because of its register windows architecture, and that a lower default stack size can be expected for embedded purpose CPUs.

Java Stack Size Reductions

Java stacks in the PersonalJava virtual machine are allocated in a chunky fashion during execution of a program; whenever the virtual machine determines that more Java stack space is required, it allocates new stack chunks. A few typical applications were analyzed to figure out an optimal Java stack chunk size for the PersonalJava application environment with the goal of minimizing stack space waste and making the average case run in as few chunks of stack space as possible. It was determined that a stack chunk size of 2 kbytes is a good number, improving upon the 8 kbytes size in the PersonalJava 1.0 virtual machine.

Conclusion

The consumer device market is one which is driven by thin margins and high volume. In order to achieve profitability in this market, device manufacturers are always seeking ways to reduce component costs. A large part of this cost is the cost of memory. Sun understands this need and has undertaken efforts to optimize the PersonalJava application environment in order to minimize the memory requirements. This paper describes the techniques which were employed to achieve an approximately 28% reduction in memory requirements for the core classes of the PersonalJava application environment from version 1.0 to version 1.1. Because these classes are a representative cross-section of the classes used by dynamically-loaded applications, a similar reduction is expected in the RAM usage requirements. Going into the future, Sun will continue to make improvements to reduce memory requirements in order to support customer needs as the industry evolves.

Copyright © 1998 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, CA 94303 USA. All rights reserved.

*As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.