Entries Tagged as ''

JVM Memory Management

JVM is still an application running on the OS. It’s interesting to know how JVM manages memory. Better performance in production servers is possible with proper configuration of JVM parameters, particularly those related to memory usage and garbage collection. The allocation of memory for the JVM is specified using -X options.

Heap Size

  • -Xms: initial java heap size.
  • -Xmx: maximum java heap size.

Stack Size

  • -Xss:     the stack size for each thread.

The snippet for Java Option.

JAVA_OPTS=”-Xms256m -Xmx2200m -Xss384k…

Generally, a JVM is using the following rules to manage memory:

  • When a JVM is invoked to run an application, it will ask the operating system for enough memory to run the JVM itself and some free memory for the application to create new objects.
  • When a new object is created, the JVM will allocate memory for that object out of the free memory area.
  • When the free memory area is getting too small, the JVM will ask the operating system for more.
  • When a object is no longer used by the application, it will be destroyed. Its memory will be freed up and merged back to the free memory area.
  • When the free memory area is used up, and there is no more additional memory available from the operating system, the JVM will stop the application and issue the “Out of memory error”.