简体   繁体   中英

Monitoring own memory usage by Java application

I want to run several REST web applications inside one Java process to save memory and scale easily with help of Akka. I would like to estimate how much memory each request handler consume and detect these dangerous for the entire system.

  1. Is it possible to monitor memory usage in almost real time inside that process and find out how much memory is used be each request handler? What I need to achieve that? Are there any tools?

  2. Is it possible to catch out of memory exception and based on memory usage do something, for example crash only request handlers exceeding assumed memory limit? If so, what could be bad with that?

The total used / free memory of an program can be obtained in the program via java.lang.Runtime.getRuntime();

The runtime has several method which relates to the memory. The following coding example demonstrate its usage.

package test;

import java.util.ArrayList;
import java.util.List;

public class PerformanceTest {
  private static final long MEGABYTE = 1024L * 1024L;

  public static long bytesToMegabytes(long bytes) {
    return bytes / MEGABYTE;
  }

  public static void main(String[] args) {
    // I assume you will know how to create a object Person yourself...
    List<Person> list = new ArrayList<Person>();
    for (int i = 0; i <= 100000; i++) {
      list.add(new Person("Jim", "Knopf"));
    }
    // Get the Java runtime
    Runtime runtime = Runtime.getRuntime();
    // Run the garbage collector
    runtime.gc();
    // Calculate the used memory
    long memory = runtime.totalMemory() - runtime.freeMemory();
    System.out.println("Used memory is bytes: " + memory);
    System.out.println("Used memory is megabytes: "
        + bytesToMegabytes(memory));
  }
} 

To answer your first question, there are many tools which you can use to monitor memory usage, however i don't know of any application which maps memory usage to threads in "real" time. Within the application you can use the MemoryMXBean and MemoryPoolMXBeans to monitor memory usage,

To answer your second question: no, not really. Besides the fact that it is generally a bad idea to catch OOME, the primary problem is that the thread which receives the exception may not be the actual culprit. the OOME is thrown on the thread which makes the final allocation request. however, some other thread could be the one which filled up most of memory. The other problem is that since OOME can be thrown at any time, it could be thrown inside some critical code within your application, leaving it in a crippled state. long story short, you pretty much always want to restart your application when you receive an OOME.

A good and easy tool to monitor your application is VisualVM. You may try it: http://visualvm.java.net/

You may also add in your code statements to monitor free memory like:

Runtime runtime = Runtime.getRuntime();
System.out.println("Free memory: " + runtime.freeMemory() + " bytes.");

It's not possible to handle this exception. The problem is that if you are running all your web services in a common app server (eg tomcat), then if there is an "out-of-memory" exception, JVM will go down. Try to see if there is any memory leak or bad memory handling in your program. You might use profiling inside Netbeans or Eclipse. Also, code analysis with a tool like "findbugs" or "sonar" might show you possible bad practices inside your code.

In any case, using java options regarding Garbage Collection or JVM Memory (like -Xmx) can reduce such exceptions and improve performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM