Java Reference
In-Depth Information
You notice that your application seems to be slowing down and suspect that there are
garbage collections happening.
Solution 1
Add
-Xloggc:gc.log-XX:+PrintGCDetails-XX:+PrintGCTimeStamps
as parameters when starting your Java program. These parameters allow you to log
garbage collection information to the
gc.log
file, including the time garbage collec-
tions occur, along with the details (if it was a minor or major garbage collection and
how long it took).
Ant target that executes Recipe 11_5 with garbage logging
on.
<target name="Recipe11_5" depends="build">
<java classname="org.java8recipes.chapter11.Recipe11_5"
fork="true">
<classpath refid="build.path" />
<jvmarg value="-Xloggc:gc.log" />
<jvmarg value="-XX:+PrintGCDetails" />
<jvmarg value="-XX:+PrintGCTimeStamps" />
</java>
</target>
In this
build.xml
file, the Java task is being used to add the arguments for
garbage collection logging to the compiler before launching the application. To run this
example throughout Ant, type
ant Recipe11_5
.
Solution 2
Analyze your program's memory consumption and more by using the NetBeans “Pro-
filer” tool. To run the profiler, select the file or project that you want to perform the
profiling against and choose the Profile Project or Profile File command from within
the NetBeans Profile menu. You can also right-click the project or file to access the
contextual menu Profile option.