Print memory used in java

Java Runtime: Get Free, Used and Total Memory in Java

memoryJVM

Although Java provides automatic garbage collection, sometimes you will want to know how large the object heap is and how much of it is left. You can use this information, for example, to check your code for efficiency or to approximate how many more objects of a certain type can be instantiated. To obtain these values, use the totalMemory( ) and freeMemory( ) methods.

The java.lang.Runtime.totalMemory() method returns the total amount of memory in the Java virtual machine. The value returned by this method may vary over time, depending on the host environment. Note that the amount of memory required to hold an object of any given type may be implementation-dependent.

package com.crunchify.tutorials; /** * @author Crunchify.com */ public class CrunchifyJVMParameters < public static void main(String[] args) < int mb = 1024 * 1024; // get Runtime instance Runtime instance = Runtime.getRuntime(); System.out.println("***** Heap utilization statistics [MB] *****\n"); // available memory System.out.println("Total Memory: " + instance.totalMemory() / mb); // free memory System.out.println("Free Memory: " + instance.freeMemory() / mb); // used memory System.out.println("Used Memory: " + (instance.totalMemory() - instance.freeMemory()) / mb); // Maximum available memory System.out.println("Max Memory: " + instance.maxMemory() / mb); >>
##### Heap utilization statistics [MB] ##### Total Memory: 71 Free Memory: 70 Used Memory: 0 Max Memory: 1061

If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion.

Читайте также:  Php компилятор для windows

Suggested Articles.

Источник

Getting JVM heap size, used memory, total memory using Java Runtime

java-text

Reading runtime information about Java is useful sometime when your application is struggling in getting resources. System Memory is one of the main resource that an application developer has to consider while managing the application. Hence sometimes it is beneficial to see what amount of memory is your application using and what is the free memory in system. Java’s Runtime class provide lot of information about the resource details of Java Virtual Machine or JVM. The memory consumed by the JVM can be read by different methods in Runtime class. Following is the small example of getting/reading JVM Heap Size, Total Memory and used memory using Java Runtime api.

/** * Class: TestMemory * @author: Viral Patel * @description: Prints JVM memory utilization statistics */ public class TestMemory < public static void main(String [] args) < int mb = 1024*1024; //Getting the runtime reference from system Runtime runtime = Runtime.getRuntime(); System.out.println("##### Heap utilization statistics [MB] #####"); //Print used memory System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb); //Print free memory System.out.println("Free Memory:" + runtime.freeMemory() / mb); //Print total available memory System.out.println("Total Memory:" + runtime.totalMemory() / mb); //Print Maximum available memory System.out.println("Max Memory:" + runtime.maxMemory() / mb); > >
Code language: Java (java)

You may also want to print the memory utilization in Kilobytes KBs. For that just assign 1024 value to the int variable mb.

You may also like.

How to write a Web Crawler in Java. Part-1

10 Most Useful Java Best Practice Quotes for Java Developers

10 Most Useful Java Best Practice Quotes for Java Developers

Generate Pie Chart/Bar Graph in PDF using iText & JFreeChart

Generate Pie Chart/Bar Graph in PDF using iText & JFreeChart

Google Android ADT, SDK and Eclipse IDE integration on Linux

Google Android ADT, SDK and Eclipse IDE integration on Linux

11 Comments

Hi Viral,
I want to get JVM heap size of WebS portal server pre/post its restarts. Is there any way I can run the above script on RHEL linu box and get it working. Please advise. Vishwanath

I believe you can do this with Sun/Oracle’s jmap utility
find the pid(process Id or Task Id of the JVM process/task and e.g
e.g jmap 1477
It should work try it?

Can you explain to me why the settings do not match the reported amounts?
e.g
java -Xms65m -Xmx256m TestMemory
comes out: ##### Heap utilization statistics [MB] #####
Used Memory:0
Free Memory:61
Total Memory:62
Max Memory:227 Since no memory is used shouldn’t I have 65MB yet I see only 61MB what happened to 4MB?
Also the Maximum was set to 256 and yet the max shows 227MB. What is happening here?
Overhead? From what .

Hi,
in jvm initially i calculated the free memory.. then i created many variables and used it.. after garbage collection.. i again calculated free memory.. it is not equal to the initial free memory.. why this mismatch. plz ans me…

public class Add extends java.lang.object <
public Add();
code:
0: aload-0
1: invoke special #1;//Method java/lang/object.””()u
4: return
public static void main(java.lang.String[]);
code:
0: bipush 10
2: istore-1
3: bipush 20
5: istore-2
6: iload-1
7: iload-2
8: iadd
9: istore-1
10: return
> i want to run command to see the jvm flags pls help me out

Источник

Getting JVM heap size, used memory, total memory using Java Runtime

java-text

Reading runtime information about Java is useful sometime when your application is struggling in getting resources. System Memory is one of the main resource that an application developer has to consider while managing the application. Hence sometimes it is beneficial to see what amount of memory is your application using and what is the free memory in system. Java’s Runtime class provide lot of information about the resource details of Java Virtual Machine or JVM. The memory consumed by the JVM can be read by different methods in Runtime class. Following is the small example of getting/reading JVM Heap Size, Total Memory and used memory using Java Runtime api.

/** * Class: TestMemory * @author: Viral Patel * @description: Prints JVM memory utilization statistics */ public class TestMemory < public static void main(String [] args) < int mb = 1024*1024; //Getting the runtime reference from system Runtime runtime = Runtime.getRuntime(); System.out.println("##### Heap utilization statistics [MB] #####"); //Print used memory System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb); //Print free memory System.out.println("Free Memory:" + runtime.freeMemory() / mb); //Print total available memory System.out.println("Total Memory:" + runtime.totalMemory() / mb); //Print Maximum available memory System.out.println("Max Memory:" + runtime.maxMemory() / mb); > >
Code language: Java (java)

You may also want to print the memory utilization in Kilobytes KBs. For that just assign 1024 value to the int variable mb.

You may also like.

How to write a Web Crawler in Java. Part-1

10 Most Useful Java Best Practice Quotes for Java Developers

10 Most Useful Java Best Practice Quotes for Java Developers

Generate Pie Chart/Bar Graph in PDF using iText & JFreeChart

Generate Pie Chart/Bar Graph in PDF using iText & JFreeChart

Google Android ADT, SDK and Eclipse IDE integration on Linux

Google Android ADT, SDK and Eclipse IDE integration on Linux

11 Comments

Hi Viral,
I want to get JVM heap size of WebS portal server pre/post its restarts. Is there any way I can run the above script on RHEL linu box and get it working. Please advise. Vishwanath

I believe you can do this with Sun/Oracle’s jmap utility
find the pid(process Id or Task Id of the JVM process/task and e.g
e.g jmap 1477
It should work try it?

Can you explain to me why the settings do not match the reported amounts?
e.g
java -Xms65m -Xmx256m TestMemory
comes out: ##### Heap utilization statistics [MB] #####
Used Memory:0
Free Memory:61
Total Memory:62
Max Memory:227 Since no memory is used shouldn’t I have 65MB yet I see only 61MB what happened to 4MB?
Also the Maximum was set to 256 and yet the max shows 227MB. What is happening here?
Overhead? From what .

Hi,
in jvm initially i calculated the free memory.. then i created many variables and used it.. after garbage collection.. i again calculated free memory.. it is not equal to the initial free memory.. why this mismatch. plz ans me…

public class Add extends java.lang.object <
public Add();
code:
0: aload-0
1: invoke special #1;//Method java/lang/object.””()u
4: return
public static void main(java.lang.String[]);
code:
0: bipush 10
2: istore-1
3: bipush 20
5: istore-2
6: iload-1
7: iload-2
8: iadd
9: istore-1
10: return
> i want to run command to see the jvm flags pls help me out

Источник

Java Runtime: Get Free, Used and Total Memory in Java

memoryJVM

Although Java provides automatic garbage collection, sometimes you will want to know how large the object heap is and how much of it is left. You can use this information, for example, to check your code for efficiency or to approximate how many more objects of a certain type can be instantiated. To obtain these values, use the totalMemory( ) and freeMemory( ) methods.

The java.lang.Runtime.totalMemory() method returns the total amount of memory in the Java virtual machine. The value returned by this method may vary over time, depending on the host environment. Note that the amount of memory required to hold an object of any given type may be implementation-dependent.

package com.crunchify.tutorials; /** * @author Crunchify.com */ public class CrunchifyJVMParameters < public static void main(String[] args) < int mb = 1024 * 1024; // get Runtime instance Runtime instance = Runtime.getRuntime(); System.out.println("***** Heap utilization statistics [MB] *****\n"); // available memory System.out.println("Total Memory: " + instance.totalMemory() / mb); // free memory System.out.println("Free Memory: " + instance.freeMemory() / mb); // used memory System.out.println("Used Memory: " + (instance.totalMemory() - instance.freeMemory()) / mb); // Maximum available memory System.out.println("Max Memory: " + instance.maxMemory() / mb); >>
##### Heap utilization statistics [MB] ##### Total Memory: 71 Free Memory: 70 Used Memory: 0 Max Memory: 1061

If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion.

Suggested Articles.

Источник

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