Viral Patel is a keen technology blogger and enthusiast. Working with a software company, he uses his leisure time to learn and explore emerging areas in tech world. His passion for technology has motivated him from creating an operating system to writing tutorials/articles on Java, JavaEE, Spring, Hibernate, Struts, jQuery, PHP. Visit the website http://viralpatel.net for a wide range of articles and tutorials on these topics. Viral has posted 13 posts at DZone. View Full User Profile

Using the Java Runtime API for JVM Memory Details

05.20.2009
| 24859 views |
  • submit to reddit
Reading runtime information about Java is useful when your application is struggling to get resources. System Memory is one of the main resources that an application developer has to consider while managing the application. Hence sometimes it is beneficial to see what amount of memory your application using and what amount of free memory is in system.

Java’s Runtime class provides 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.

The following is a 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);
}
}

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

From http://viralpatel.net/blogs

2
Your rating: None Average: 2 (3 votes)
Published at DZone with permission of its author, Viral Patel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Martin Wildam replied on Wed, 2009/05/20 - 11:27am

Thanks for the article!

I found out that the VM just gives me 5 MB of total memory. It would be now interesting in which vm options one has to modify to change the results returned by the functions above (see also Java Hotspot VM options).

Russ Tennant replied on Thu, 2009/05/21 - 12:20am

Good tip and you can get more detailed information from java.lang.management.

final MemoryMXBean mb = ManagementFactory.getMemoryMXBean(); ...

and you can get some OS level memory information from com.sun.management.OperatingSystemMXBean if java.lang.management.OperationSystemMXBean implements that interface.

green boal replied on Fri, 2009/07/17 - 12:10am

The JVM verifies all bytecode before it is executed. This verification consists primarily of three types of checks: Branches are always to valid locations Data is always initialized and references are always type-safe Access to "private" or "package private" data and methods is rigidly controlled. "tax lawyer" 20%

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.