Can Java JConsole be used to configure memory automatically?

I am learning Java JMX and JConsole . I'm interested in learning about the memory management capabilities of JConsole. For example, on the "Memory" tab there is a "Run GC" button:

enter image description here

Suppose I have a simple Java application that eats 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() ); } } } 

Will there be a way to configure JConsole so that this application does not consume X memory? Or do I need to make a new MBean through JMX? thanks

+8
java jmx jconsole
source share
3 answers

Will there be a way to configure JConsole to prevent this application from consuming X memory?

From JConsole, we cannot configure / control memory limits. Only option that I see uses -Xmx when starting a java process. Looking at the screenshot below, JConsole provides memory options only as readable but not writeable.

JConsole readonly memory options


Or do I need to create a new MBean through JMX?

Even if we write our own MBean, I don’t think it is possible to change the memory limits of a java process at runtime. The only way is to adjust the memory limits at startup using -Xms and -Xmx .

If the question is not specific to your sample code, where do you want to limit the number of elements that can be added to Vector through the JMX Bean , and there, limiting memory consumption, which makes it impossible, but I doubt that this is what you are looking for.

+3
source share

No, you can’t. Java memory management is a garbage collector and actual software developers. You can configure memory allocation using different types of links (for example, Soft links), configure GC parameters and the algorithm, or try to force a GC call using JMX or jcmd GC.run. But you cannot change the memory boundaries to run the JVM or set the high water sign to load the heap.

If you need a different Java memory management strategy, take a look at products like Terracotta BigMemory .

0
source share

you can add only MBean functions and call them from JConsole - under operations.

 public void add(){ byte b[] = new byte[1048576]; v.add(b); Runtime rt = Runtime.getRuntime(); System.out.println( "free memory: " + rt.freeMemory() ); } 

see here https://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html

0
source share

All Articles