Java program to get the details about memory

Hi everyone, in this post, I am writing about how to get the details of maximum memory, free memory, total memory and used memory in java.
  • maxMemory() - Specifies the maximum amount of memory java virtual machine will attempt to use. maxMemory is returned in bytes
  • totalMemory() - Specifies the total amount of memory in java virtual machine. The totalMemory will vary according to the host environment. totalMemory is returned in bytes.
  • freeMemory() - Specifies the amount of free memory in jvm. freeMemory is returned in bytes.
The information about all these memory types are provided by Runtime class of java. The memory used by jvm can be obtained from different methods of the Runtime class.

Following java program specifies a clear idea about getting details about  3 types of memories.

package com.techjourney;

/**
 * Created with IntelliJ IDEA.
 * User: Sujith
 * Date: 6/19/13
 * Time: 11:17 PM
 * To change this template use File | Settings | File Templates.
 */

public class Runtime {
    public static void main(String args[])
    {
        int size = 1024*1024;
       System.out.println(" Total Memory = "+ java.lang.Runtime.getRuntime().totalMemory()/size);
       System.out.println(" Maximum Memory = "+ java.lang.Runtime.getRuntime().maxMemory()/size);
       System.out.println(" Free Memory = "+ java.lang.Runtime.getRuntime().freeMemory()/size);
       System.out.println(" Used Memory = "+ ((java.lang.Runtime.getRuntime().totalMemory()/size)- (java.lang.Runtime.getRuntime().freeMemory()/size)));
    }
}


In this program, the returned memory is divided by 1 mb, so we will get the returned value in megabytes. Used Memory is the memory, which is used by jvm. It is the difference between total memory and free memory.

The output of the program is as follows :

 "C:\Program Files\Java\jdk1.7.0_05\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_05\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_05\jre\lib\ext\zipfs.jar;C:\Users\Sujith\IdeaProjects\Runtime\out\production\Runtime;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.techjourney.Runtime  
  Total Memory = 53  
  Maximum Memory = 792  
  Free Memory = 52  
  Used Memory = 1  
 Process finished with exit code 0  

I have written this program using IntelliJ IDE, rather than Eclipse IDE (which is used for my usual posts). The change in format of output is because of the reason of using a different IDE.

I hope everyoen understood the program very well. Please comment below if you have any doubts. Thank you very much for reading the post.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++