Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java's JConsole be used to automatically configure memory?

Tags:

java

jmx

jconsole

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 :

enter image description here

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

like image 708
Caffeinated Avatar asked Mar 01 '16 01:03

Caffeinated


People also ask

What is the purpose of JConsole?

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.

What is committed memory in JConsole?

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.

How does JConsole connect to local process?

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.


1 Answers

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.

JConsole memory parameters readonly


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.

like image 196
Madhusudana Reddy Sunnapu Avatar answered Sep 27 '22 18:09

Madhusudana Reddy Sunnapu