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 :
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() );
}
}
}
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
You can use JConsole to connect to a running Java virtual machine, and then monitor the memory usage and thread activity. You can obtain class-loading information, plus information on the JVM and the operating system.
4. Committed Size. The committed size is the amount of memory guaranteed to be available for use by the Java virtual machine. The committed memory size is always greater than or equal to the used size.
The Local Process option lists any Java VMs running on the local system that were started with the same user ID as JConsole, along with their process ID and their class and/or argument information. To connect JConsole to your application, select the application you want to monitor, then click the Connect button.
Would there be a way to configure JConsole to prevent this app from consuming X amount of memory?
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.
Or do I need to make a new MBean via JMX ?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With