Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Jul 14, 1999

Determining Memory Usage

Question:
Is there a way to determine the percentage of CPU usage or RAM usage within a Java application?

Answer:
Java does not provide an API for determining CPU usage, but it does make memory usage information available. The java.lang.Runtime class has two methods that provide information about memory resources.

The totalMemory() method will return the total amount of memory available to the JVM. This value includes both memory in use and memory yet to be allocated. The freeMemory() method will return the amount of unallocated memory in the JVM. This is memory that can be used to create new objects. Neither of these values can be relied upon to stay fixed. The total memory available to a JVM can increase or decrease as the JVM dynamically adjusts its resource consumption. Remember, the JVM is a process running on the operating system that allocates some system memory for its use. As a Java program starts using up this memory, the JVM can allocate more memory from the operating system. By the same token, when a Java program's memory usage decreases, the JVM can reduce the amount of system memory used.

The following example program demonstrates how to get memory usage information, and also how the reported values can change throughout the execution of a program. The example starts out by printing out the total memory, free, and used memory. Then it allocates 1 Megabyte of memory in an array and prints out the memory usage again. Finally, it sets the array to null, making its memory eligible for garbage collection, requests that the garbage collector be executed, and prints out the final memory usage values. You should see the total and used memory values increase after the initial memory allocation and only the free memory increase after the garbage collection.


public class MemoryUsage {
  public static void printUsage(Runtime runtime) {
    long total, free, used;

    total = runtime.totalMemory();
    free  = runtime.freeMemory();
    used = total - free;

    System.out.println("\nTotal Memory: " + total);
    System.out.println("        Used: " + used);
    System.out.println("        Free: " + free);
    System.out.println("Percent Used: " + ((double)used/(double)total)*100);
    System.out.println("Percent Free: " + ((double)free/(double)total)*100);
  }

  public static final void main(String[] args) {
    Runtime runtime;
    byte[] bytes;

    // Print initial memory usage.
    runtime = Runtime.getRuntime();
    printUsage(runtime);

    // Allocate a 1 Megabyte and print memory usage
    bytes = new byte[1024*1024];
    printUsage(runtime);

    bytes = null;
    // Invoke garbage collector to reclaim the allocated memory.
    runtime.gc();

    // Wait 5 seconds to give garbage collector a chance to run
    try {
      Thread.sleep(5000);
    } catch(InterruptedException e) {
      e.printStackTrace();
      return;
    }

    // Total memory will probably be the same as the second printUsage call,
    // but the free memory should be about 1 Megabyte larger if garbage
    // collection kicked in.
    printUsage(runtime);
  }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap