From Training Material
Jump to: navigation, search

JVM Memory
Sam Bashton

JVM Memory          Sam Bashton

Java Memory Management ⌘

  • Java handles memory management
  • Memory within the JVM referred to as the 'heap'
  • When the heap starts to run out of available memory, garbage collection is performed

Garbage Collection ⌘

  • Garbage collection is a process by which the JVM finds objects stored in memory which are no longer needed
  • During garbage collection, the application pauses
  • Minimising this pause time is vital to good performance

The heap ⌘

  • Split into multiple sections
  • Aims to avoid running collection checks on objects used long-term as much as possible
  • Garbage collection runs on each section independently

Heap layout ⌘


Heap usage: Young Generation: Eden Space ⌘

  • Objects initially stored in eden space
  • Once an object survives its first garbage collection, it is moved to the survivor space

Heap usage: Young Generation: Survivor Space ⌘

  • There are two survivor spaces
  • Only one is used at a time
  • Objects copied between the two at collection
  • Once objects have survived two collections, they move to tenured space
  • If survivor space is too small to fit all objects in, it will overflow into tenured space
* This should be avoided

Heap usage: Tenured space ⌘

  • The 'Old generation'
  • Once here, objects stay in place until they expire

Heap usage: Permanent generation ⌘

  • Not actually permanent - will be garbage collected on a Full GC
  • Class objects and constants placed here
  • *May* be removed in future Oracle JVM

Showing heap usage ⌘

  • You can see the heap usage of an application with `jmap`
 jmap <java process pid>

Heap tuning ⌘

  • Heap size can be adjusted at JVM start up
  • Other options can be used to tune performance
  • How to tune depends on the nature of the application
  • Free RAM is useful when the application does much disk I/O, as it leaves space for the kernel to use as cache

Important Java heap tuning options ⌘

  • **Xms** - Initial heap size
  • **Xmx** - Maximum heap size
  • **XX:MinHeapFreeRatio** - heap will be expanded to try to keep this much space free at all times. Defaults to 40%
  • **XX:MaxHeapFreeRatio** - if more heap than this is available, heap will shrink until this ratio is met. Defaults to 70%

Important Java heap tuning options ⌘

  • **Xmn** - Young generation size
  • **XX:NewRatio** - set a ratio for the young generation size.
-  **-XX:NewRatio=3** means that the ratio between the old and young generation is 1:3
  • **XX:SurvivorRatio** - Ratio of survivor to eden space within young generation
- **-XX:SurvivorRatio=6** means each survivor space will be one sixth the size of eden, and thus one eighth the size of the young. generation (not one seventh, because there are two survivor spaces)

Important Java heap tuning options (2) ⌘

  • **XX:PermSize** - initial size of permanent space
  • **XX:MaxPermSize** - maximum size of permanent space

Sizing the young generation ⌘

  • Rule of thumb: 30% of total heap size
  • This will vary by application

Garbage collection types ⌘

  • Three collectors are available
- Serial (Not suitable for server environment)
- Parallel
- Concurrent Mark Sweep

Choosing the best Garbage Collector ⌘

  • If a collector is not specified, the JVM will automatically choose
 the one it believes is best
  • When choosing manually, ask yourself what is more important for this
- Application performance, with occasional pauses of a second or more
  - Use parallel GC
- Response time and minimising application pauses
  - Use concurrent mark/sweep

Parallel collector ⌘

  • Enabled with `-XX:+UseParallelOldGC`
  • Pauses the application, then in parallel looks for objects that can be reaped

Concurrent Mark Sweep ⌘

Mark sweep.gif

Concurrent Mark Sweep ⌘

  • Enabled with `-XX:+UseConcMarkSweepGC`
  • Avoids pausing as much as possible
  • Has a performance impact whilst running, and will take overall much longer than the parallel collector

Exercise ⌘

  • Run tomcat
  • Examine heap usage with jmap
  • What garbage collection is being used?