summaryrefslogtreecommitdiff
path: root/vm
diff options
context:
space:
mode:
authorAndrew John Hughes <gnu_andrew@member.fsf.org>2007-08-15 23:47:55 +0000
committerAndrew John Hughes <gnu_andrew@member.fsf.org>2007-08-15 23:47:55 +0000
commit5d93822aa5eb0e1b79b07779df3c1284f0ac36ed (patch)
treeedfd61a077678f37d1cae332b194bd987f62a70b /vm
parentaa8a248e3724782139c5231dde92ed8ef1d13865 (diff)
downloadclasspath-5d93822aa5eb0e1b79b07779df3c1284f0ac36ed.tar.gz
2007-08-16 Andrew John Hughes <gnu_andrew@member.fsf.org>
* NEWS: Update with info on VM changes. * vm/reference/gnu/java/lang/management/VMMemoryMXBeanImpl.java: (getHeapMemoryUsage()): New default implementation. (getNonHeapMemoryUsage()): Likewise.
Diffstat (limited to 'vm')
-rw-r--r--vm/reference/gnu/java/lang/management/VMMemoryMXBeanImpl.java54
1 files changed, 49 insertions, 5 deletions
diff --git a/vm/reference/gnu/java/lang/management/VMMemoryMXBeanImpl.java b/vm/reference/gnu/java/lang/management/VMMemoryMXBeanImpl.java
index 43b9ae1a1..b9073319c 100644
--- a/vm/reference/gnu/java/lang/management/VMMemoryMXBeanImpl.java
+++ b/vm/reference/gnu/java/lang/management/VMMemoryMXBeanImpl.java
@@ -37,8 +37,13 @@ exception statement from your version. */
package gnu.java.lang.management;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryPoolMXBean;
+import java.lang.management.MemoryType;
import java.lang.management.MemoryUsage;
+import java.util.List;
+
/**
* Provides access to information about the memory
* management of the current invocation of the virtual
@@ -62,10 +67,7 @@ final class VMMemoryMXBeanImpl
*/
static MemoryUsage getHeapMemoryUsage()
{
- Runtime runtime = Runtime.getRuntime();
- long totalMem = runtime.totalMemory();
- return new MemoryUsage(-1, totalMem - runtime.freeMemory(),
- totalMem, runtime.maxMemory());
+ return getUsage(MemoryType.HEAP);
}
/**
@@ -76,7 +78,10 @@ final class VMMemoryMXBeanImpl
* @return an {@link java.lang.management.MemoryUsage} instance
* for non-heap memory.
*/
- static native MemoryUsage getNonHeapMemoryUsage();
+ static MemoryUsage getNonHeapMemoryUsage()
+ {
+ return getUsage(MemoryType.NON_HEAP);
+ }
/**
* Returns the number of objects ready to be garbage collected.
@@ -106,4 +111,43 @@ final class VMMemoryMXBeanImpl
*/
static native void setVerbose(boolean verbose);
+ /**
+ * Totals the memory usage from all the pools that match
+ * the given type.
+ *
+ * @param type the type of memory pools to accumulate
+ * (heap or non-heap).
+ * @return the memory usage overall.
+ */
+ private static MemoryUsage getUsage(MemoryType type) {
+ long init = 0, committed = 0, used = 0, max = 0;
+ List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
+ for (MemoryPoolMXBean pool: pools)
+ {
+ if (pool.getType() == type)
+ {
+ MemoryUsage usage = pool.getUsage();
+ if (init != -1)
+ {
+ long poolInit = usage.getInit();
+ if (poolInit == -1)
+ init = -1;
+ else
+ init += poolInit;
+ }
+ committed += usage.getCommitted();
+ used += usage.getUsed();
+ if (max != -1)
+ {
+ long poolMax = usage.getMax();
+ if (poolMax == -1)
+ max = -1;
+ else
+ max += poolMax;
+ }
+ }
+ }
+ return new MemoryUsage(init, used, committed, max);
+ }
+
}