diff --git a/pom.xml b/pom.xml
index 3d06099..f9e0225 100644
--- a/pom.xml
+++ b/pom.xml
@@ -64,6 +64,18 @@
3.0.1
provided
+
+
+ junit
+ junit
+ 4.11
+ test
+
+
+ ch.qos.logback
+ logback-classic
+ test
+
diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java
index df6343e..bab110a 100644
--- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java
+++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java
@@ -34,6 +34,13 @@ import java.util.concurrent.ScheduledFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import javax.management.AttributeNotFoundException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.ReflectionException;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
@@ -605,20 +612,41 @@ public class HostingNodeManager extends ContainerHandler {
return memoryFacet;
}
-
+
+ private static final long BYTE_TO_MB = 1024*1024;
+
private MemoryFacet getRamInfo(MemoryFacet memoryFacet) {
if (memoryFacet == null) {
memoryFacet = new MemoryFacetImpl();
}
- OperatingSystemMXBean mxbean = ManagementFactory
- .getOperatingSystemMXBean();
+ /*
+ OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean;
- long freeMemory = sunmxbean.getFreePhysicalMemorySize() / 1048576; // in
- // MB
- long totalMemory = sunmxbean.getTotalPhysicalMemorySize() / 1048576; // in
- // MB
-
+ long freeMemory = sunmxbean.getFreePhysicalMemorySize() / 1048576; // in MB
+ long totalMemory = sunmxbean.getTotalPhysicalMemorySize() / 1048576; // in MB
+ */
+
+ MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
+ long freeMemory;
+ try {
+ freeMemory = Long.parseLong(mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "FreePhysicalMemorySize").toString()) / BYTE_TO_MB;
+ } catch (NumberFormatException | InstanceNotFoundException | AttributeNotFoundException
+ | MalformedObjectNameException | ReflectionException | MBeanException e) {
+ logger.warn("Unable to get free memory from Operating System. Going to get JVM Memory. Better than nothing");
+ long allocatedMemory = (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
+ freeMemory = Runtime.getRuntime().maxMemory() - allocatedMemory;
+ }
+ long totalMemory;
+ try {
+ totalMemory = Long.parseLong(mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize").toString()) / BYTE_TO_MB;
+ } catch (NumberFormatException | InstanceNotFoundException | AttributeNotFoundException
+ | MalformedObjectNameException | ReflectionException | MBeanException e) {
+ logger.warn("Unable to total memory from Operating System. Going to get JVM Memory. Better than nothing");
+ totalMemory = Runtime.getRuntime().maxMemory();
+ }
+
+
memoryFacet.setUnit(MemoryUnit.MB);
memoryFacet.setSize(totalMemory);
memoryFacet.setUsed(totalMemory - freeMemory);
diff --git a/src/test/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManagerTest.java b/src/test/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManagerTest.java
new file mode 100644
index 0000000..43e268b
--- /dev/null
+++ b/src/test/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManagerTest.java
@@ -0,0 +1,47 @@
+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);
+
+ }
+
+}