How to get the size of heap in Java
KW.java
class KW
{
    public static void main(String args[])
    {
        System.out.println("Max Heap Memory: "+Runtime.getRuntime().maxMemory());
        System.out.println("Total Heap Memory: "+Runtime.getRuntime().totalMemory());
        System.out.println("Free Heap Memory: "+Runtime.getRuntime().freeMemory());
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Max Heap Memory: 2030043136 Total Heap Memory: 130023424 Free Heap Memory: 128347808 kodingwindow@kw:~$
Linux command to get the size of Java Heap
kodingwindow@kw:~$ java -XX:+PrintFlagsFinal -version | grep HeapSize
   size_t ErgoHeapSizeLimit                        = 0                        {product} {default}
   size_t HeapSizePerGCThread                      = 43620760                 {product} {default}
   size_t InitialHeapSize                          = 127926272                {product} {ergonomic}
   size_t LargePageHeapSizeThreshold               = 134217728                {product} {default}
   size_t MaxHeapSize                              = 2030043136               {product} {ergonomic}
   size_t MinHeapSize                              = 8388608                  {product} {ergonomic}
    uintx NonNMethodCodeHeapSize                   = 5832780                  {pd product} {ergonomic}
    uintx NonProfiledCodeHeapSize                  = 122912730                {pd product} {ergonomic}
    uintx ProfiledCodeHeapSize                     = 122912730                {pd product} {ergonomic}
   size_t SoftMaxHeapSize                          = 2030043136               {manageable} {ergonomic}
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
kodingwindow@kw:~$ 
Advertisement