简体   繁体   English

监视Java应用程序自己的内存使用情况

[英]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. 我想在一个Java进程中运行几个REST Web应用程序,以便在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? 是否有可能捕获out of memory exception并基于内存使用情况做一些事情,例如仅崩溃请求处理程序超出假定的内存限制? 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(); 程序的总使用/可用内存可以通过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, 在应用程序中,您可以使用MemoryMXBeanMemoryPoolMXBeans来监视内存使用情况,

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. 除了捕获OOME通常是一个坏主意之外,主要问题是接收异常的线程可能不是真正的罪魁祸首。 the OOME is thrown on the thread which makes the final allocation request. OOME被抛出到进行最终分配请求的线程上。 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. 另一个问题是,由于OOME可能随时被抛出,因此可能会将其抛入应用程序中的某些关键代码中,使其处于残缺状态。 long story short, you pretty much always want to restart your application when you receive an OOME. 长话短说,当你收到OOME时,你几乎总是希望重新启动你的应用程序。

A good and easy tool to monitor your application is VisualVM. VisualVM是一个监控应用程序的好工具。 You may try it: http://visualvm.java.net/ 你可以尝试一下: 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. 问题是,如果您在一个公共应用服务器(例如tomcat)中运行所有Web服务,那么如果存在“内存不足”异常,JVM将会关闭。 Try to see if there is any memory leak or bad memory handling in your program. 尝试查看程序中是否存在任何内存泄漏或内存处理错误。 You might use profiling inside Netbeans or Eclipse. 您可以在Netbeans或Eclipse中使用分析。 Also, code analysis with a tool like "findbugs" or "sonar" might show you possible bad practices inside your code. 此外,使用“findbugs”或“sonar”等工具进行代码分析可能会向您显示代码中可能存在的不良做法。

In any case, using java options regarding Garbage Collection or JVM Memory (like -Xmx) can reduce such exceptions and improve performance. 在任何情况下,使用有关垃圾收集或JVM内存的Java选项(如-Xmx)可以减少此类异常并提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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