Java virtual machine

Virtual machine

  • Memory allocation facilities
  • Simplified programming interface
  • Compile once, run everywhere

Memory allocation

Garbage collection

The HEAP

GC Roots

  • Local variables
  • Static variables
  • Active threads
  • JNI references

 

 

  • Local scope
  • Class scope
  • Runtime/Manual
  • Manual

 

Good practices

  • Always set -Xms and -Xmx flags
  • Avoid global state
  • Avoid long-running processes
  • Keep it as local as possible
  • Mark elements for garbage collection
  • Create-new is simpler than re-use

Garbage collection

Cleanup while working

  1. Create objects
  2. Use them
  3. Pause the world and check them
  4. Remove the orphans
  5. Resume the world
  6. Start again

Define your collector

Collector Scenario
SerialGC Single processor
Serial GC 100 MB of RAM or less
Parallel GC Performance, response time
Parallel GC + MarkSweep GC Performance, short garbage collection period

Define your garbage collection period

  • New ratio
  • Survivor ratio
  • Pause between collections
  • Disable explicit GC

Dump on error

  • Dump heap on memory error
  • On critical scenarios turn on GC-log
  • Use a specific GC-log file

Must have production flags

Flag Flags
-server -XX:+CMSParallelRemarkEnabled
-Xms<heap size>[g|m|k] -XX:ParallelGCThreads=<amount of threads>
-Xmx<heap size>[g|m|k] -XX:MaxGCPauseMillis
-XX:PermSize=<perm gen size>[g|m|k] -XX:MaxPermSize
-XX:MaxPermSize=<perm gen size>[g|m|k] -XX:+CMSClassUnloadingEnabled
-Xmn<young size>[g|m|k] -XX:+CMSPermGenSweepingEnabled
-XX:SurvivorRatio=<ratio> -XX:+DisableExplicitGC
-XX:+UseConcMarkSweepGC -XX:+HeapDumpOnOutOfMemoryError
-XX:+ScavengeBeforeFullGC -XX:HeapDumpPath=<path to dump>`date`.hprof

Good practices

  • Never use defaults for production
  • Consider memory and process capacities
  • Always dump on error
  • Always enable GC logging on error
  • Do not manually call GC

Tooling

Memory usage/allocation

  • JMAP
  • Eclipse MAT
  • YourKit Profiler

Monitoring

  • JMX + JConsole
  • JavaMelody
  • Perf4j

Right now the JVM is perhaps the best virtual machine out there, hands down

Languages summit 2013

Made with Slides.com