Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 13 ■ GARBAGE COLLECTION

As a case in point, consider an XML parser. It must have a reference to each node in the document
tree from the top to its current location. In addition, processing each node creates a reference to each
child node. Consequently, by the time an XML parser has gotten very far into a document tree, it
probably has references to a lot of nodes, each represented by an object, and each object having a
reference from the object that represents its parent node. Parsers are one common example of the types
of programs that don't benefit much from garbage collection. Of course, a program that parses a
document and then does something else could free the memory used by the parser when the parsing is
done. That memory could then be for another purpose, such that the program's footprint would be just
the larger of the parsing or whatever else it does, rather than the total of both parsing and other work.
Conversely, a program that runs other processes in the background must have enough memory to
enable all of its concurrent processes.
So how do Java programmers give their programs more memory? They do it through switches on the
JVM when they start the program. Here are the basic memory settings:


Table 13-1. Basic Java Memory Settings


Switch Effect


-Xms Sets the starting heap size. For example: -Xms64M sets the starting heap size to 64
megabytes (64MB is the default memory allocation, by the way)


-Xmx (^) Sets the maximum heap size. For example: -Xmx1024M sets the maximum heap size to
1024 megabytes (that is, 1 gigabyte)
You can increase a program's performance by setting the starting heap size properly. If you set the
number too low, the JVM starts at that amount and then has to adjust upward (perhaps several times)
until it has enough memory for your program's initial set of objects. Then, as soon as the program does
something (perhaps because a user did something with the program), the JVM has to allocate yet more
memory. Rather than force your users to put up with the slow performance that a low starting memory
value creates, you should instead allocate enough initial memory to handle all the objects your program
needs at start-up time and to handle the first several operations it might need to perform.
The maximum memory setting is both more obvious and more critical. If you don't allocate enough
memory, your program crashes. Usually, finding the right amount of maximum memory is a matter of
experimentation. You have to start the program, let it run for a while, and see how much memory it uses.
On Windows, the Task Manager shows how much memory each process, including a Java program, uses.
Other operating systems reveal that information in other ways, but it should be there somewhere.
Here's an example of using the memory switches when you start a Java program:
Listing 13-2. Example of Java Memory Switches
java -Xms1024M -Xmx8192M ExampleProgram


Understanding Garbage Collection


The first thing to understand about the Java garbage collector is that you can't directly control it. You can
give it hints that certain objects are now ready for collection. You can also set a number of garbage
collection options when you start your program, and those settings will control the garbage collector's
behavior. However, you cannot explicitly tell the garbage collector to remove an object from memory

Free download pdf