resource-registry-handlers/src/test/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManagerTest.java

48 lines
2.0 KiB
Java

package org.gcube.smartgears.handler.resourceregistry;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HostingNodeManagerTest {
private static final Logger logger = LoggerFactory.getLogger(HostingNodeManagerTest.class);
@Test
public void compare() throws Exception {
logger.debug("Runtime.getRuntime().maxMemory() : {}", Runtime.getRuntime().maxMemory());
logger.debug("Runtime.getRuntime().totalMemory() : {}", Runtime.getRuntime().totalMemory());
logger.debug("Runtime.getRuntime().freeMemory() : {}", Runtime.getRuntime().freeMemory());
long allocatedMemory = (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
long presumableFreeMemory = Runtime.getRuntime().maxMemory() - allocatedMemory;
logger.debug("allocatedMemory : {}", allocatedMemory);
logger.debug("presumableFreeMemory : {}", presumableFreeMemory);
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Object total = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize");
logger.debug("Total Memory : {}", total);
Object free = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "FreePhysicalMemorySize");
logger.debug("Free Memory : {}", free);
OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
@SuppressWarnings("restriction")
com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean;
@SuppressWarnings("restriction")
long freeMemory = sunmxbean.getFreePhysicalMemorySize();
@SuppressWarnings("restriction")
long totalMemory = sunmxbean.getTotalPhysicalMemorySize();
logger.debug("freeMemory : {}", freeMemory);
logger.debug("totalMemory : {}", totalMemory);
}
}