x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Java 8 -Xms -Xmx example, Java 8 Heap sizing

    1) Java 8 -Xms -Xmx options

    If the heap is too small, the application will spend too much time in performing GC (Garbage collection) instead of performing application logic and if JVM sees it is doing too much GC with the initial heap size, it will keep it increasing till it finds a suitable value or it hits maximum size.

    But if the heap is too large, GC (Garbage collection) will be performed less frequently but duration of pauses will increase and will cause performance issues.

    You can control the size of heap by two values: an initial value using -Xms flag and a maximum value using
    -Xms flag.

    Types of JIT compilers

    There are two types of Java compilers client and server which can selected by using java -client or java -server command. The client compiler starts compiling earlier than a server compiler. Therefore startup will be fast for a client machine but for a long running applciation server type is preferred choice. With tiered compilation both client and server compilations are used. This is enabled by default from Java 8 onwards.

    Client JVM machine Initial and Maximum Heap Sizes

    For client JVM machine, the default maximum heap size is half of the physical memory up to a physical memory size of 192 megabytes (MB) else one fourth of the physical memory up to a physical memory size of 1 gigabyte (GB).

    Server JVM machine Initial and Maximum Heap Sizes

    For server JVM machine on 32-bit JVMs, the default maximum heap size can be up to 1 GB if there is 4 GB or more of physical memory, while on 64-bit JVMs, the default maximum heap size can go up to 32 GB if there is 128 GB or more of physical memory.

    Specifying Initial and Maximum Heap Sizes

    You can specify the initial and maximum heap sizes using the flags -Xms (initial heap size) and -Xms (maximum heap size). If you don't specify -Xms value, and application doesn't need a larger heap than the default maximum, it is fine.

    But if the application is spending too much time in GC, then the heap size will need to be increased by setting -Xmx flag. On the other hand if you know exactly what heap your application needs, then you can set -Xms and -Xms to the same value. That makes GC bit more efficient, as it never needs to figure out whether the heap should be increased.

    Few examples of -Xms -Xmx

    java -Xms512m -Xmx1024m
    
    java -Xms512m -Xmx2g    

    2) Sizing the Generations

    You can also decide how much of heap should be allocated to young and old generations using below flags. The default NewRatio for the Server JVM is 2. The old generation occupies 2/3 of the heap while the new generation occupies 1/3.

    • -XX:NewRatio=2 set the ratio of the young generation to the old generation
    • -XX:NewSize=512m set the initial size of the young generation
    • -XX:MaxNewSize=1024m set the maximum size of the young generation
    • -Xmn1024m set the size of both NewSize and MaxNewSize to the same value
    java -Xms512m -Xmx1024m -XX:NewRatio=2 -XX:NewSize=512m
        

    3) Sizing Permgen

    The metadata of the the classes loaded by JVM is stored in seperate heap space called permgen (permanent generation). In Java 8, this is called as the metaspace.

    For example for permgen, the size is specified using -XX:PermSize=512m and -XX:MaxPermSize=1024m. For metaspace, the size is specified using -XX:MetaspaceSize=512m and -XX:MaxMetaspaceSize=1024m

    java -Xms512m -Xmx1024m -XX:PermSize=2 -XX:MaxPermSize=512m
        

    View this page to see various GC mechanisms

    GC Algorithms
    References :

    Garbage Collector Ergonomics

    Default heap size

    Heap Tuning Parameters

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This