Memory manager in java

Java memory management

Azure Spring Apps is the new name for the Azure Spring Cloud service. Although the service has a new name, you’ll see the old name in some places for a while as we work to update assets such as screenshots, videos, and diagrams.

This article applies to: ✔️ Basic/Standard ✔️ Enterprise

This article describes various concepts related to Java memory management to help you understand the behavior of Java applications hosted in Azure Spring Apps.

Java memory model

A Java application’s memory has several parts, and there are different ways to divide the parts. This article discusses Java memory as divided into heap memory, non-heap memory, and direct memory.

Heap memory

Heap memory stores all class instances and arrays. Each Java virtual machine (JVM) has only one heap area, which is shared among threads.

Spring Boot Actuator can observe the value of heap memory. Spring Boot Actuator takes the heap value as part of jvm.memory.used/committed/max . For more information, see the jvm.memory.used/committed/max section in Tools to troubleshoot memory issues.

Heap memory is divided into young generation and old generation. These terms are described in the following list, along with related terms.

  • Young generation: all new objects are allocated and aged in young generation.
    • Eden space: new objects are allocated in Eden space.
    • Survivor space: objects will be moved from Eden to survivor space after surviving one garbage collection cycle. Survivor space can be divided to two parts: s1 and s2.

    Before Java 8, another section called permanent generation was also part of the heap. Starting with Java 8, permanent generation was replaced by metaspace in non-heap memory.

    Non-heap memory

    Non-heap memory is divided into the following parts:

    • The part of non-heap memory that replaced the permanent generation (or permGen) starting with Java 8. Spring Boot Actuator observes this section and takes it as part of jvm.memory.used/committed/max . In other words, jvm.memory.used/committed/max is the sum of heap memory and the former permGen part of non-heap memory. The former permanent generation is composed of the following parts:
      • Metaspace, which stores the class definitions loaded by class loaders.
      • Compressed class space, which is for compressed class pointers.
      • Code cache, which stores native code compiled by JIT.

      Direct memory

      Direct memory is native memory allocated by java.nio.DirectByteBuffer , which is used in third party libraries like nio and gzip.

      Spring Boot Actuator doesn’t observe the value of direct memory.

      The following diagram summarizes the Java memory model described in the previous section.

      Diagram of the Java memory model.

      Java garbage collection

      There are three terms regarding of Java Garbage Collection (GC): «Minor GC», «Major GC», and «Full GC». These terms aren’t clearly defined in the JVM specification. Here, we consider «Major GC» and «Full GC» to be equivalent.

      Minor GC performs when Eden space is full. It removes all dead objects in young generation and moves live objects to from Eden space to s1 of survivor space, or from s1 to s2.

      Full GC or major GC does garbage collection in the entire heap. Full GC can also collect parts like metaspace and direct memory, which can be cleaned only by full GC.

      The maximum heap size influences the frequency of minor GC and full GC. The maximum metaspace and maximum direct memory size influence full GC.

      When you set the maximum heap size to a lower value, garbage collections occur more frequently, which slow the app a little, but better limits the memory usage. When you set the maximum heap size to a higher value, garbage collections occur less frequently, which may create more out-of-memory (OOM) risk. For more information, see the Types of out-of-memory issues section of App restart issues caused by out-of-memory issues.

      Metaspace and direct memory can be collected only by full GC. When metaspace or direct memory is full, full GC will occur.

      Java memory configurations

      The following sections describe important aspects of Java memory configuration.

      Java containerization

      Applications in Azure Spring Apps run in container environments. For more information, see Containerize your Java applications.

      Important JVM options

      You can configure the maximum size of each part of memory by using JVM options. You can set JVM options by using Azure CLI commands or through the Azure portal. For more information, see the Modify configurations to fix problems section of Tools to troubleshoot memory issues.

      The following list describes the JVM options:

Оцените статью