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

Monitoring own memory usage by Java application(监控 Java 应用程序自己的内存使用情况)

本文介绍了监控 Java 应用程序自己的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个 Java 进程中运行多个 REST Web 应用程序,以在 Akka 的帮助下节省内存并轻松扩展.我想估计每个请求处理程序消耗多少内存并检测这些对整个系统的危险.

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. 是否可以在该进程内几乎实时监控内存使用情况,并找出每个请求处理程序使用了多少内存?我需要什么来实现这一目标?有什么工具吗?

  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?

是否可以捕获 内存不足异常 并根据内存使用情况做一些事情,例如仅崩溃请求处理程序超出假定的内存限制?如果是这样,那有什么不好的?

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?

推荐答案

回答你的第一个问题,有很多工具可以用来监控内存使用情况,但是我不知道有任何应用程序可以将内存使用情况映射到实时"线程.在应用程序中,您可以使用 MemoryMXBean 和 MemoryPoolMXBeans 来监控内存使用情况,

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,

回答你的第二个问题:不,不是.除了捕获 OOME 通常是个坏主意之外,主要问题是接收异常的线程可能不是真正的罪魁祸首.OOME 被抛出到发出最终分配请求的线程上.然而,一些 other 线程可能会占满大部分内存.另一个问题是,由于 OOME 可以随时抛出,它可能会被抛出到应用程序的一些关键代码中,使其处于瘫痪状态.长话短说,您几乎总是希望在收到 OOME 时重新启动您的应用程序.

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.

这篇关于监控 Java 应用程序自己的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:监控 Java 应用程序自己的内存使用情况

基础教程推荐