提交 8037908f 编写于 作者: K kohsuke

just committing the memory monitoring test code that I wrote elsewhere.

Still needs a lot more work before this can be integrated into Hudson.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@13089 71c3de6d-444a-0410-be80-ed276b4c234a
上级 fa1bb122
package hudson.model;
import hudson.triggers.SafeTimerTask;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
import java.lang.management.MemoryUsage;
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.ArrayList;
/**
* TODO:
* @author Kohsuke Kawaguchi
*/
public class MemoryUsageMonitor extends SafeTimerTask {
class MemoryGroup {
private final List<MemoryPoolMXBean> pools = new ArrayList<MemoryPoolMXBean>();
MemoryGroup(List<MemoryPoolMXBean> pools, MemoryType type) {
for (MemoryPoolMXBean pool : pools) {
if (pool.getType() == type)
this.pools.add(pool);
}
}
public String metrics() {
long used = 0;
long max = 0;
long cur = 0;
for (MemoryPoolMXBean pool : pools) {
MemoryUsage usage = pool.getCollectionUsage();
if(usage==null) continue; // not available
used += usage.getUsed();
max += usage.getMax();
usage = pool.getUsage();
if(usage==null) continue; // not available
cur += usage.getUsed();
}
// B -> KB
used /= 1024;
max /= 1024;
cur /= 1024;
return String.format("%d/%d/%d (%d%%)",used,cur,max,used*100/max);
}
}
private final MemoryGroup heap;
private final MemoryGroup nonHeap;
public MemoryUsageMonitor() {
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
heap = new MemoryGroup(pools, MemoryType.HEAP);
nonHeap = new MemoryGroup(pools, MemoryType.NON_HEAP);
}
protected void doRun() {
System.out.printf("%s\t%s\n", heap.metrics(), nonHeap.metrics());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册