Sunday, October 29, 2017

How to find max memory, free memory and total memory in Java

As per Javadoc freeMemory is currently available memory which can be allocated to future objects and totalMemory is the total amount of memory in the Java virtual. Since we know that JVM expands heap whenever it needs so if we start our JVM with -Xms10m and -Xmx120m you should expect that initialfreeMemory and totalMemory should be same with starting heap size of JVM as Virtual machine has not been expanded yet and that's the case exactly. even value returned by Runtime.maxMemory() will be close to value of -Xmx though little less. In this article we will see how to get approximate value of inital and maximum heap size, free memory available in JVM and used memory or memory currently occupied by objects in heap.


How to get free Memory in Java

In order to get currently free Memory available for creating object use Runtime.getRuntime().freeMemory() method, this will return size in bytes, which you convert in Mega Bytes for better readability. we will see an example of getting initial heap and free memory in code example section. Calling Garbage collector by either System.gc() or Runtime.gc() may results in slightly higher free memory reclaimed by dead objects.


How to get total Memory in Java

You can use Runtime.getRuntime.totalMemory() to get total memory from JVM which represents current heap size of JVM which is a combination of used memory currently occupied by objects and free memory available for new objects. As per Javadoc value returned by totalMemory() may vary over time depending upon the environment. see the code example for getting total memory in Java in next code example section.


How to get initial Heap Size from Java Program

We specify initial heap space by using -Xms and JVM creates initial heap with this much size. in order to get this size from Java program call Runtime.getRuntime.totalMemory() before creating any object. See code example of getting initial heap size from java program in next section. Apart from –Xms and –Xmx there are lot of other useful JVM Options I have shared on my post 10 useful JVM parameters Java Programmer should know.

How to get maximum Heap Size from Java Program

This is relatively easy as maximum heap space is not going to change over JVM life cycle and call to Runtime.getRuntime.maxMemory() will return value close to -Xmx but keep in mind exact value will be little less than what have you set.


How to get Used Memory in JVM

by using Runtime.getRuntime.totalMemory() and Runtime.getRuntime.freeMemory() we can calculate how much space has been currently occupied by Java object or you say used memory in JVM as show in below code example of getting memory sizes in Java:

Example:


public class MemoryUtil{

       private static final int MegaBytes = 10241024;

       public static void main(String args[]) {

              long freeMemory = Runtime.getRuntime().freeMemory()/MegaBytes;
              long totalMemory = Runtime.getRuntime().totalMemory()/MegaBytes;
              long maxMemory = Runtime.getRuntime().maxMemory()/MegaBytes;

              System.out.println("JVM freeMemory: " + freeMemory);
              System.out.println("JVM totalMemory also equals to initial heap size of JVM : "
                                         + totalMemory);
              System.out.println("JVM maxMemory also equals to maximum heap size of JVM: "
                                         + maxMemory);

              ArrayList objects = new ArrayList();

              for (int i = 0; i < 10000000; i++) {
                     objects.add(("" + 10 * 2710));
              }

              freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;
              totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
              maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;

              System.out.println("Used Memory in JVM: " + (maxMemory - freeMemory));
              System.out.println("freeMemory in JVM: " + freeMemory);
              System.out.println("totalMemory in JVM shows current size of java heap : "
                                         + totalMemory);
              System.out.println("maxMemory in JVM: " + maxMemory);

       }
}

Output:
JVM freeMemory: 9
JVM totalMemory also equals to initial heap size of JVM : 9
JVM maxMemory also equals to maximum heap size of JVM: 116
Used Memory in JVM: 81
freeMemory in JVM: 35
totalMemory in JVM shows current size of java heap : 108
maxMemory in JVM: 116

No comments:

Post a Comment

Java garbage collection

In this post , we ’ ll take a look at how garbage collection works , why it ’ s important in Java , and how it works in...