通过 Java 应用程序监控自己的内存使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10754748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Monitoring own memory usage by Java application
提问by Rafa? Sobota
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.
我想在一个 Java 进程中运行多个 REST Web 应用程序以节省内存并在 Akka 的帮助下轻松扩展。我想估计每个请求处理程序消耗多少内存并检测这些对整个系统的危险。
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?
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?
是否可以在该进程内几乎实时监控内存使用情况并找出每个请求处理程序使用了多少内存?我需要什么来实现这一目标?有工具吗?
是否可以捕获
out of memory exception
并根据内存使用情况做一些事情,例如仅崩溃超过假定内存限制的请求处理程序?如果是这样,那有什么不好呢?
采纳答案by jtahlborn
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 MemoryMXBeanand MemoryPoolMXBeansto monitor memory usage,
要回答您的第一个问题,您可以使用许多工具来监视内存使用情况,但是我不知道有任何应用程序可以“实时”将内存使用情况映射到线程。在应用程序中,您可以使用MemoryMXBean和MemoryPoolMXBeans来监控内存使用情况,
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 otherthread 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 alwayswant to restart your application when you receive an OOME.
回答你的第二个问题:不,不是真的。除了捕获 OOME 通常是一个坏主意之外,主要问题是接收异常的线程可能不是真正的罪魁祸首。在发出最终分配请求的线程上抛出 OOME。但是,其他一些线程可能是占满大部分内存的线程。另一个问题是,由于 OOME 可以随时抛出,它可能会被抛出到应用程序中的某些关键代码中,使其处于瘫痪状态。长话短说,当您收到 OOME 时,您几乎总是希望重新启动您的应用程序。
回答by Rafa? Sobota
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));
}
}
回答by stzoannos
A good and easy tool to monitor your application is VisualVM. You may try it: http://visualvm.java.net/
VisualVM 是一个很好且简单的应用程序监控工具。你可以试试:http: //visualvm.java.net/
回答by stzoannos
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 (e.g. 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.
无法处理此异常。问题是,如果您在一个通用应用服务器(例如 tomcat)中运行所有 Web 服务,那么如果出现“内存不足”异常,JVM 将关闭。尝试查看您的程序中是否存在任何内存泄漏或错误的内存处理。您可以在 Netbeans 或 Eclipse 中使用分析。此外,使用“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)可以减少此类异常并提高性能。