Java has automatic memory management, which is also known as garbage collection (GC). GC’s principal tasks are allocating memory, maintaining referenced objects in memory, and recovering memory from objects that no longer have references to them.
Since the J2SE 5.0 release, the Java HotSpot Virtual Machine performs self-tuning. This process includes the attempted best-fit GC and related settings at startup, based on platform information, as well as ongoing GC tuning.
Although the initial settings and runtime tuning for GC are generally successful, there are times when you may wish to change or tune your GC based on the following goals:
The following sections provide an overview of various garbage collectors, their main focus, and situations in which they should be used. Command-Line Options explains how to acquire information for manually selecting the GC.
The serial collector is performed via a single thread on a single CPU. When this GC thread is run, the execution of the application will pause until the collection is complete.
This collection is best used when your application has a small data set up to approximately 100 MB and does not have a requirement for low pause times.
The parallel collector, also known as the throughput collector, can be performed with multiple threads across several CPUs. Using these multiple threads significantly speeds up GC.
This collector is best used when there are no pause time constraints and application performance is the most important aspect of your program.
The parallel compacting collector is similar to the parallel collector except for refined algorithms that reduce collection pause times.
This collector is best used for applications that do have pause time constraints.
The CMS, also known as the low-latency collector, implements algorithms to handle large collections that may warrant long pauses.
This collector is best used when response times take precedence over throughput times and GC pauses.
The Garbage-First collector, also known as the G1 collector, is used for multiprocessor machines with large memories. This server-style GC meets pause time goals with high probability, while achieving high throughput. Whole-heap operations (e.g., global marking) are performed concurrently with the application threads preventing interruptions proportional to the heap or live-data size.
Although tuning your GC may prove to be successful, it is important to note that the GCs do not provide guarantees, only goals; any improvement gained on one platform may be undone on another. It is best to find the source of the problem with memory management tools, including profilers.
Table 11-1 lists such tools. All are command-line applications except HPROF (Heap/CPU Profiling Tool). HPROF is dynamically loaded from a command-line option. The following example returns a complete list of options that can be passed to HPROF:
java
-
agentlib:
hprof
=
help
Resource | Description |
jvisualvm | |
jconsole | Java Management Extensions (JMX)-compliant monitoring tool |
jinfo | Configuration information tool |
jmap | Memory map tool |
jstack | Stack trace tool |
jstat | JVM statistics monitoring tool |
jhat | Heap analysis tool |
HPROF Profiler | CPU usage, heap statistics, and monitor contentions profiler |
jdb | Java debugger tool |
The following GC-related command-line options can be passed into the Java interpreter to interface with the functionality of the Java HotSpot Virtual Machine (for a more complete list of options, visit Java HotSpot VM Options):
-XX:+PrintGC
or -verbose:gc
-XX:+PrintCommandLineFlags -version
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+UseSerialGC
-XX:+UseParallelGC
-XX:+UseParallelOldGC
Old
refers to the fact that new algorithms are used for “old” generation GC.
-XX:+UseParNewGC
-XX:+UseConcMarkSweepGC
-XX:+UseG1GC
-XX:+DisableExplicitGC
System.gc()
) methods.
-XX:ParallelGCThreads=[threads
]
-XX:MaxGCPauseMillis=[milliseconds
]
-XX:+GCTimeRatio=[__value__]
value
])) for the desired throughput goal. The default value is 99. This means that the application will run 99% of the time and therefore, the GC will run one percent of the time. This option applies to the parallel collectors.
-XX:+CMSIncrementalMode
-XX:+CMSIncrementalPacing
-XX:MinHeapFreeRatio=[percent
]
-XX:MaxHeapFreeRatio=[percent
]
-Xms[bytes
]
-Xmx[bytes
]
-Xmn[bytes
]
-XX:OnError=[command_line_tool [__options__]]
-XX+AggressiveOpts
Note that –XX
options are not guaranteed to be stable. They are not part of the Java Language Specification (JLS) and are unlikely to be available in exact form and function from other third-party JVMs, if at all.
The heap is an area in memory that stores all objects created by an executing Java program. You should resize the heap only if it needs to be sized larger than the default heap size. If you are having performance problems or seeing the Permanent Generation (PermGen) error message java.lang.OutOfMemoryError
, you may be running out of heap space.
Native memory is used for the representation of class metadata, creating a memory space called Metaspace. Metaspace is the sucessor to the PermGen model. Because of this, the JDK 8 HotSpot JVM will no longer see any PermGen OutOfMemoryError
occurring. JVisualVM provides analysis support to the Metaspace if any memory leaks should occur.
Interfacing with the garbage collector can be done through explicit invocation or via overriding the finalize
method.
The garbage collector can be explicitly requested to be invoked with System.gc()
or Runtime.getRuntime().gc()
. However, explicit invocation of the GC should generally be avoided because it could force full collections (when a minor collection may suffice), thereby unnecessarily increasing the pause times. The request for System.gc()
is not always fulfilled as the JVM can and does ignore it at times.
Every object has a finalize()
method inherited from class Object
. The garbage collector, prior to destroying the object, can invoke this method, but this invocation is not guaranteed. The default implementation of the finalize()
method does nothing and although it is not recommended, the method can be overridden:
public
class
TempClass
extends
SuperClass
{
...
// Performed when Garbage Collection occurs
protected
void
finalize
()
throws
Throwable
{
try
{
/* Desired functionality goes here */
}
finally
{
// Optionally, you can call the
// finalize method of the superclass
super
.
finalize
();
// From SuperClass
}
}
}
The following example destroys an object:
public
class
MainClass
{
public
static
void
main
(
String
[]
args
)
{
TempClass
t
=
new
TempClass
();
// Object has references removed
t
=
null
;
// GC made available
System
.
gc
();
}
}
18.222.232.128