Prefetch Technologies // Keeping your cache lines cozy

Java Notes

Enable hotspot Dtrace probes

$ jinfo -flag -XX:+DTraceAllocProbes JVM_PID
$ jinfo -flag -XX:+DTraceMethodProbes JVM_PID
$ jinfo -flag -XX:+DTraceMonitorProbes JVM_PID
$ jinfo -flag -XX:+ExtendedDTraceProbes JVM_PID

Get garbage collection metrics

$ java -verbose:gc -XX:-PrintGCDetails and -XX:-PrintGCTimeStamps -Xloggc:/home/matty/gc.log

Start JVM with remote management capabilities

$ java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8180
       -Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false

Get a heap dump and view it in a browser

$ jmap -dump:file=heap.bin
$ jhat -J-mx512m heap.bin
$ curl http://localhost:7000

Set the minimum and mximum amount of heap space

$ java -server Xms1g -Xmx1g

Enable parallel garbage collection

This will use all CPUs to perform garbage collection, so short pauses may occur.

$ java -server -XX:UseParallelGC -XX:UseParallelOldGC

Use the concurrent mark/sweep method to minimize pauses

$ java -server -XX:+UseConcMarkSweepGC

Set the thread stack size to 256k (default 1MB)

$ java -server -XX:ThreadStackSize=256k

Tell java to ignore calls to System.gc()

$ java -server  -XX:+DisableExplicitGC

Set the new generation size for newly created objects

$ java -server -Xmn256m

Log when garbage colelctions events occur

$ java -server -verbose:gc

Enable the JMX management agents so jconsole works

$ java -server -Dcom.sun.management.jmxremote

Dump the heap if we run out of memory

$ java -XX:+HeapDumpOnOutOfMemoryError

Touch each memory page to avoid ZFODs

$ java XX:+AlwaysPreTouch ...

Set the old and new ratio to 2:1 (384mb -> 256mb old, 128mb new)

$ java -XX:NewRatio=2

Adjust the perm gen size

$ java -server -Xss512k -XX:PermSize=256 -XX:MaxPermSize=256m -Xms1024m \
       -Xmx1024m -Xloggc:/var/llogs/gclog -verbose:gc

DTrace hotspot probes

  • Threads: thread-start, thread-stop
  • Garbage collection: gc-begin, gc-end, mem-pool-gc-begin, mem-pool-gc-end
  • Class loading: class-loaded, class-unloaded
  • Monitors: monitor-contended-enter, monitor-contended-entered, monitor-contended-exit, monitor-wait, monitor-waited, monitor-notify, monitor-notifyAll
  • Methods: method-entry, method-return
  • Object allocations: object-alloc

Probe arguments

  • arg0 - thread id
  • arg1 - Class
  • arg3 - Size of allocation

References

  • Garbage collection tuning:
  <http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html>
  • Brian Goetz presentation on concurrency
  <http://developers.sun.com/events/techdays/presentations/2007/TD_BOS_Concurrency_Goetz.pdf>
  • Generational collection:
  <http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html>
  • IBM's Java developer site:
  <http://www.ibm.com/developerworks/java>
  • Using jconsole:
  <http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html>
  • Troubleshooting:
  http://java.sun.com/j2se/1.5/pdf/jdk50_ts_guide.pdf>
  • Quick troubleshooting guide:
  <http://java.sun.com/javase/6/webnotes/trouble/other/matrix6-Unix.html>
  • Garbage collection FAQ:
  <http://java.sun.com/docs/hotspot/gc1.4.2/faq.html>
  • Java threads tutorial:
  <http://scv.bu.edu/Doc/Java/tutorial/java/threads/>
  • JVM specification:
  <http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html>
  • Java DTrace probes:
  <http://java.sun.com/javase/6/docs/technotes/guides/vm/dtrace.html>
  • Jstat utility:
  <http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jstat.html>
  • Reference objects and garbage collection
  <http://java.sun.com/developer/technicalArticles/ALT/RefObj/>
  • JVM option list:

  • Improving Java Application Performance and Scalability by Reducing Garbage Collection Times and Sizing Memory Using JDK 1.4.1:

  <http://developers.sun.com/mobility/midp/articles/garbagecollection2/>
  • Turbo-charging Java HotSpot Virtual Machine, v1.4.x to Improve the Performance and Scalability of Application Servers:
  <http://java.sun.com/developer/technicalArticles/Programming/turbo/>
  • Object query language:

  • JVM monitoring with SNMP:

   <http://gentoo-wiki.com/JVM_Monitoring_with_SNMP>