public static Struct getMemoryUsageAsStruct(int type) { java.util.List<MemoryPoolMXBean> manager = ManagementFactory.getMemoryPoolMXBeans(); Iterator<MemoryPoolMXBean> it = manager.iterator(); MemoryPoolMXBean bean; MemoryUsage usage; MemoryType _type; long used = 0, max = 0, init = 0; while (it.hasNext()) { bean = it.next(); usage = bean.getUsage(); _type = bean.getType(); if ((type == MEMORY_TYPE_HEAP && _type == MemoryType.HEAP) || (type == MEMORY_TYPE_NON_HEAP && _type == MemoryType.NON_HEAP)) { used += usage.getUsed(); max += usage.getMax(); init += usage.getInit(); } } Struct sct = new StructImpl(); sct.setEL(KeyConstants._used, Caster.toDouble(used)); sct.setEL(KeyConstants._max, Caster.toDouble(max)); sct.setEL(KeyConstants._init, Caster.toDouble(init)); sct.setEL(KeyImpl.init("available"), Caster.toDouble(max - used)); return sct; }
public static void main(String args[]) { MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); long init = heapMemoryUsage.getInit(); long used = heapMemoryUsage.getUsed(); long max = heapMemoryUsage.getMax(); long committed = heapMemoryUsage.getCommitted(); long free = max - used; int usedPercent = (int) (used / max); int freePercent = (int) (usedPercent / max); MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage(); List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans(); List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); long commitVirtual = operatingSystemMXBean.getCommittedVirtualMemorySize(); println(init, used, max, committed); println(commitVirtual); }
/** Add JVM heap metrics. */ protected void addHeapMetrics(Collection<Metric<?>> result) { MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); result.add(new Metric<Long>("heap.committed", memoryUsage.getCommitted() / 1024)); result.add(new Metric<Long>("heap.init", memoryUsage.getInit() / 1024)); result.add(new Metric<Long>("heap.used", memoryUsage.getUsed() / 1024)); result.add(new Metric<Long>("heap", memoryUsage.getMax() / 1024)); }
private static JSONObject toJson(MemoryUsage useage) { JSONObject item = new JSONObject(); item.put("commit", useage.getCommitted()); item.put("used", useage.getUsed()); item.put("init", useage.getInit()); item.put("max", useage.getMax()); return item; }
public Map<String, String> getProperties() { Map<String, String> map = new TreeMap<String, String>(); RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); List<String> aList = runtimeMXBean.getInputArguments(); String parameters = ""; for (int i = 0; i < aList.size(); i++) { parameters = parameters + " " + aList.get(i); } map.put("params", parameters); map.put("name", runtimeMXBean.getVmName()); map.put("vendor", runtimeMXBean.getVmVendor()); map.put("version", runtimeMXBean.getVmVersion()); map.put("specification", runtimeMXBean.getSpecVersion()); map.put("uptime", DurationFormatUtils.formatDurationHMS(runtimeMXBean.getUptime())); Date date = new Date(runtimeMXBean.getStartTime()); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); map.put("start time", sdf.format(date)); MemoryMXBean memory = ManagementFactory.getMemoryMXBean(); MemoryUsage heap = memory.getHeapMemoryUsage(); map.put( "memory. (heap)", readableFileSize(heap.getUsed()) + "/" + readableFileSize(heap.getCommitted()) + " min:" + readableFileSize(heap.getInit()) + " max:" + readableFileSize(heap.getMax())); MemoryUsage nonheap = memory.getNonHeapMemoryUsage(); map.put( "memory (non heap)", readableFileSize(nonheap.getUsed()) + "/" + readableFileSize(nonheap.getCommitted()) + " min:" + readableFileSize(nonheap.getInit()) + " max:" + readableFileSize(nonheap.getMax())); return map; }
/** * Format contents of a MemoryUsage object. * * @param name a text prefix used in formatting * @param usage the MemoryUsage object to format * @return the formatted contents */ private static String formatMemoryUsage(String name, MemoryUsage usage) { if (usage != null) { StringBuilder sb = new StringBuilder(); sb.append(INDENT1 + name + " init: " + usage.getInit() + CRLF); sb.append(INDENT1 + name + " used: " + usage.getUsed() + CRLF); sb.append(INDENT1 + name + " committed: " + usage.getCommitted() + CRLF); sb.append(INDENT1 + name + " max: " + usage.getMax() + CRLF); return sb.toString(); } return ""; }
public static String formatHeapMemoryUsage(MemoryUsage beforHeap, MemoryUsage afterHeap) { return afterHeap != null ? CapString.concat( "Heap Usage: Committed=", bytesToMegabytes(beforHeap.getCommitted()), "->", bytesToMegabytes(afterHeap.getCommitted()), " MB, Init=", bytesToMegabytes(beforHeap.getInit()), "->", bytesToMegabytes(afterHeap.getInit()), " MB, Max=", bytesToMegabytes(beforHeap.getMax()), "->", bytesToMegabytes(afterHeap.getMax()), " MB, Used=", bytesToMegabytes(beforHeap.getUsed()), "->", bytesToMegabytes(afterHeap.getUsed()), "=", (bytesToMegabytes(afterHeap.getUsed() - beforHeap.getUsed())), " MB ") : ""; }
public static Query getMemoryUsageAsQuery(int type) throws DatabaseException { java.util.List<MemoryPoolMXBean> manager = ManagementFactory.getMemoryPoolMXBeans(); Iterator<MemoryPoolMXBean> it = manager.iterator(); Query qry = new QueryImpl( new Collection.Key[] { KeyConstants._name, KeyConstants._type, KeyConstants._used, KeyConstants._max, KeyConstants._init }, 0, "memory"); int row = 0; MemoryPoolMXBean bean; MemoryUsage usage; MemoryType _type; while (it.hasNext()) { bean = it.next(); usage = bean.getUsage(); _type = bean.getType(); if (type == MEMORY_TYPE_HEAP && _type != MemoryType.HEAP) continue; if (type == MEMORY_TYPE_NON_HEAP && _type != MemoryType.NON_HEAP) continue; row++; qry.addRow(); qry.setAtEL(KeyConstants._name, row, bean.getName()); qry.setAtEL(KeyConstants._type, row, _type.name()); qry.setAtEL(KeyConstants._max, row, Caster.toDouble(usage.getMax())); qry.setAtEL(KeyConstants._used, row, Caster.toDouble(usage.getUsed())); qry.setAtEL(KeyConstants._init, row, Caster.toDouble(usage.getInit())); } return qry; }
@Override public void testRunFinished(final Result result) throws Exception { if (!HybrisJUnit4Test.intenseChecksActivated()) { return; } final MemoryUsage heapAtTestEnd = memoryBean.getHeapMemoryUsage(); final MemoryUsage nonHeapAtTestEnd = memoryBean.getNonHeapMemoryUsage(); final long initHeapDiffInK = (heapAtTestEnd.getInit() - heapAtTestStart.getInit()) / 1024; final long usedHeapDiffInK = (heapAtTestEnd.getUsed() - heapAtTestStart.getUsed()) / 1024; final long comittedHeapDiffInK = (heapAtTestEnd.getCommitted() - heapAtTestStart.getCommitted()) / 1024; final long initNonHeapDiffInK = (nonHeapAtTestEnd.getInit() - nonHeapAtTestStart.getInit()) / 1024; final long usedNonHeapDiffInK = (nonHeapAtTestEnd.getUsed() - nonHeapAtTestStart.getUsed()) / 1024; final long comittedNonHeapDiffInK = (nonHeapAtTestEnd.getCommitted() - nonHeapAtTestStart.getCommitted()) / 1024; LOG.info("+---------------------------- Memory usage after test " + currentTestClass); LOG.info( "+ heap: init=" + (heapAtTestEnd.getInit() / 1024) + "K (" + initHeapDiffInK + "K)\t " + // "used=" + (heapAtTestEnd.getUsed() / 1024) + "K (" + usedHeapDiffInK + "K)\t " + // "comitted=" + (heapAtTestEnd.getCommitted() / 1024) + "K (" + comittedHeapDiffInK + "K)"); LOG.info( "+ non-heap: init=" + (nonHeapAtTestEnd.getInit() / 1024) + "K (" + initNonHeapDiffInK + "K)\t " + // "used=" + (nonHeapAtTestEnd.getUsed() / 1024) + "K (" + usedNonHeapDiffInK + "K)\t " + // "comitted=" + (nonHeapAtTestEnd.getCommitted() / 1024) + "K (" + comittedNonHeapDiffInK + "K)"); LOG.info("+ ObjectPendingFinalizationCount: " + memoryBean.getObjectPendingFinalizationCount()); for (final MemoryPoolMXBean mpbean : ManagementFactory.getMemoryPoolMXBeans()) { LOG.info( "+ " + mpbean.getName() + "(" + mpbean.getType() + "): " + mpbean.getCollectionUsage()); } LOG.info( "+--------------------------------------------------------------------------------------------------"); }