Last active
March 29, 2021 20:52
-
-
Save aVolpe/8846126cf7aa085e0abd7717258314d1 to your computer and use it in GitHub Desktop.
Check the ram of a jvm process using nashron (given only a jre)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addUrlToClasspath(pathName) { | |
var/*java.net.URLClassLoader*/ | |
sysloader = /*(java.net.URLClassLoader) */ java.lang.ClassLoader.getSystemClassLoader(); | |
var/*java.lang.Class*/ sysclass = java.net.URLClassLoader.class; | |
var ClassArray = Java.type("java.lang.Class[]"); | |
var parameters = new ClassArray(1); | |
parameters[0] = java.net.URL.class; | |
var/*java.lang.reflect.Method*/ method = sysclass.getDeclaredMethod("addURL", parameters); | |
method.setAccessible(true); | |
var ObjectArray = Java.type("java.lang.Object[]"); | |
var array = new ObjectArray(1); | |
var/*java.io.File*/ f = new java.io.File(pathName); | |
if (f.isFile()) { | |
var/*java.net.URL*/ u = f.toURL(); | |
array[0] = u; | |
//if(u.toString().endsWith(".jar")) | |
method.invoke(sysloader, array); | |
} else { | |
var/*File[]*/ listOfFiles = f.listFiles(); | |
if (listOfFiles != null) | |
for (var i = 0; i < listOfFiles.length; i++) { | |
if (listOfFiles[i].isFile()) { | |
var/*java.net.URL*/ u = listOfFiles[i].toURL(); | |
array[0] = u; | |
method.invoke(sysloader, array); | |
} | |
} | |
} | |
} | |
function addToLibPath(path) { | |
var/*java.io.File*/ f = new java.io.File(path); | |
path = f.getAbsolutePath(); | |
if (!f.exists()) { | |
throw new Error("THe lib " + path + " doesn't exists") | |
} | |
if (java.lang.System.getProperty("java.library.path") != null) { | |
// If java.library.path is not empty, we will prepend our path | |
// Note that path.separator is ; on Windows and : on Unix-like, | |
// so we can't hard code it. | |
var newLibraryPath = path | |
+ java.lang.System.getProperty("path.separator") | |
+ java.lang.System.getProperty("java.library.path") | |
print("To set", newLibraryPath) | |
java.lang.System.setProperty("java.library.path", newLibraryPath); | |
} else { | |
java.lang.System.setProperty("java.library.path", path); | |
} | |
// Important: java.library.path is cached | |
// We will be using reflection to clear the cache | |
var ClassLoader = Java.type("java.lang.ClassLoader") | |
var fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); | |
fieldSysPath.setAccessible(true); | |
fieldSysPath.set(null, null); | |
} | |
addUrlToClasspath("./tools.jar"); | |
addToLibPath(".") | |
print("JAVA_LIBRARY_PATH", java.lang.System.getProperty("java.library.path")); | |
var AttachProvider = Java.type("com.sun.tools.attach.spi.AttachProvider") | |
var list = Java.type("com.sun.tools.attach.VirtualMachine").list() | |
var managementAgentJar = "/usr/local/openjdk-8/lib/management-agent.jar" | |
var attachProvider = AttachProvider.providers().get(0); | |
var descriptor = null; | |
var processString = "my-jar.jar" | |
Array.prototype.forEach.call(list, function(desc) { | |
if(desc.displayName().indexOf(processString) >= 0) descriptor = desc | |
}) | |
print(list) | |
print("Using", descriptor) | |
if (descriptor == null) | |
throw new Error("not found"); | |
var virtualMachine = attachProvider.attachVirtualMachine(descriptor); | |
// virtualMachine.loadAgent(managementAgentJar, "com.sun.management.jmxremote"); | |
var portObject = virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress"); | |
var target = new javax.management.remote.JMXServiceURL(portObject + ""); | |
var connector = javax.management.remote.JMXConnectorFactory.connect(target); | |
var remote = connector.getMBeanServerConnection(); | |
var c = remote.getMBeanCount(); | |
print("MBeanCount: " + c); | |
var composite = remote.getAttribute(new javax.management.ObjectName("java.lang:type=Memory"), "HeapMemoryUsage"); | |
print("Commited", composite.get("committed")); | |
print("Init", composite.get("init")); | |
print("MAX", composite.get("max")); | |
print("USED", composite.get("used")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment