Post

Checking Real-time Swap Memory with JShell

Introducing how to monitor real-time swap memory status of servers using JShell.

Problem Situation

What methods are there to check the swap memory set on the currently serving server and how much free space is available in real-time?
You can tag these values and poll them to check on a monitoring server, or check directly on the server.
In this article, I’ll organize how to check the server’s swap memory in real-time using JShell.

Environment and Stack

Checking Swap Memory Using JShell

1
2
// Run JShell
$ jshell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Import commands to use in JShell
import java.lang.management.ManagementFactory;
import javax.management.*;
import java.util.logging.Logger;

var DOMAIN = "java.lang";
var OBJECT_KEY = "type";
var OBJECT_VALUE = "OperatingSystem";

var mBeanServer = ManagementFactory.getPlatformMBeanServer();
var objectName = new ObjectName(DOMAIN + ":" + OBJECT_KEY + "=" + OBJECT_VALUE);

var totalSwapSpaceSize = Long.parseLong(mBeanServer.getAttribute(objectName, "TotalSwapSpaceSize").toString());
var freeSwapSpaceSize = Long.parseLong(mBeanServer.getAttribute(objectName, "FreeSwapSpaceSize").toString());

Logger log = Logger.getLogger("MyLogger");
log.info("Total Swap Space Size: " + totalSwapSpaceSize);
log.info("Free Swap Space Size: " + freeSwapSpaceSize);
This post is licensed under CC BY 4.0 by the author.