Reputation: 91
I need to monitor JVM Metaspace usage. Can you help me please with the following things?
How can I find the metaspace size used ?
Using the following commands I found the maxmetaspace and the min metaspace :
jmap -heap `ps -ef | grep java | grep -v grep | awk '{print $2}'` | grep -i Metaspace
MetaspaceSize = 21807104 (20.796875MB)
MaxMetaspaceSize = 1073741824 (1024.0MB)
but how can I find what is the value of memory used right now ?
Upvotes: 9
Views: 12478
Reputation: 7069
If you are using JDK 16, you can write a simple program to monitor your application using the new Remote streaming API. First start JMX on the host you want to monitor
$ jcmd <pid> ManagementAgent.start jmxremote.port=7091
jmxremote.authenticate=false jmxremote.ssl=false
Then use the following program, which will print all metaspace events to standard out.
$ java MetaspaceMonitor.java localhost 7091
Source code:
import java.io.IOException;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import jdk.management.jfr.RemoteRecordingStream;
public class MetaspaceMonitor {
public static void main(String... args) throws IOException {
String host = args[0];
Strin port = args[1];
String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
JMXServiceURL u = new JMXServiceURL(url);
JMXConnector c = JMXConnectorFactory.connect(u);
MBeanServerConnection connection = c.getMBeanServerConnection();
try (var s = new RemoteRecordingStream(connection)) {
s.enable("jdk.MetaspaceSummary");
s.enable("jdk.MetaspaceGCThreshold");
s.enable("jdk.MetaspaceAllocationFailure").withStackTrace();
s.enable("jdk.MetaspaceOOM").withStackTrace();
s.onEvent("jdk.MetaspaceSummary", System.out::println);
s.onEvent("jdk.MetaspaceGCThreshold", System.out::println);
s.onEvent("jdk.MetaspaceAllocationFailure", System.out::println);
s.onEvent("jdk.MetaspaceOOM", System.out::println);
s.start();
}
}
}
Upvotes: 0
Reputation: 18990
As of Java 11, the jcmd utility comes with a new command for displaying detailed metaspace information.
jcmd <PID> VM.metaspace
You can find an in-depth explanation in this post by Thomas Stuefe.
Alternatively, you can also use the JDK Flight Recorder (JFR) for metaspace monitoring, see the "Garbage Collections" view in JMC, and/or "Metaspace Summary" events in the event browser.
Upvotes: 1
Reputation: 61
I use
jstat -gcutil <pid> | awk '{print($5)}'
that prints the Metaspace utilization as a percentage of the space's current capacity.
there are further options for jstat, explained here:
http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html
Upvotes: 4
Reputation: 1400
You can use the MemoryPoolMXBean
.
List<MemoryPoolMXBean> memPool = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean p : memPool)
{
if ("Metaspace".equals(p.getName())
{
... here you have the memory pool mx bean with information about he metaspace
}
}
Upvotes: 4