可以使用Java的JConsole自动配置内存吗? [英] Can Java's JConsole be used to automatically configure memory?

查看:68
本文介绍了可以使用Java的JConsole自动配置内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Java JMX

I am studying about Java JMX and JConsole. I am curious about the memory management abilities of JConsole. For example, there is a "Perform GC" button in the Memory tab :

假设我有一个简单的Java应用程序占用了内存,就像这样:

Suppose I have simple Java app that eats up memory, something like this :

public class MemoryEater
{
  public static void main(String[] args)
  {
    Vector v = new Vector();
    while (true)
    {
      byte b[] = new byte[1048576];
      v.add(b);
      Runtime rt = Runtime.getRuntime();
      System.out.println( "free memory: " + rt.freeMemory() );
    }
  }
}

是否有一种方法可以配置JConsole来防止此应用程序消耗X的内存量?还是我需要通过JMX制作一个新的MBean?谢谢

Would there be a way to configure JConsole to prevent this app from consuming X amount of memory? Or do I need to make a new MBean via JMX ? thanks

推荐答案

有没有一种方法可以配置JConsole来防止该应用程序从 消耗X的内存量?

Would there be a way to configure JConsole to prevent this app from consuming X amount of memory?

在JConsole中,我们无法调整/控制内存限制.我看到的唯一选项是在java进程启动期间使用-Xmx.查看下面的屏幕快照,JConsole将内存参数公开为可读且不可写.

From JConsole we cannot tune/control the memory limits. Only option I see is using -Xmx during startup of java process. Looking at the below screenshot, JConsole exposes memory parameters as only as readable but not writable.

还是我需要通过JMX制作一个新的MBean?

Or do I need to make a new MBean via JMX ?

即使我们编写自己的MBean,我也不认为有可能在运行时更改Java进程的内存限制.唯一的方法是在启动期间使用-Xms-Xmx配置内存限制.

Even if we write our own MBean I don't think it is possible to change the memory limits of java process at runtime. The only way is to configure the memory limits during the start up using -Xms and -Xmx.

除非问题是特定于您的示例代码的,否则您要限制可以通过JMX Bean添加到Vector的元素的数量,并且可以通过限制内存消耗来做到这一点,但我怀疑那是否是您想要的.

Unless the question is specific to your example code, where in you want to limit the number of elements that can be added to the Vector through JMX Bean and there by limiting the memory consumption, which is do-able, but I doubt if that is what you are looking for.

这篇关于可以使用Java的JConsole自动配置内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆