/** 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)); }
/** * Prints the verbose GC log to System.out to list the memory usage of all memory pools as well as * the GC statistics. */ @Deprecated public String[] getVerboseGc() { String gcs = ""; StringBuilder sb = new StringBuilder(); sb.append("Uptime: " + StringFormatter.formatMillis(rmbean.getUptime())); for (GarbageCollectorMXBean gc : gcmbeans) { sb.append(" [" + gc.getName() + ": "); sb.append("Count=" + gc.getCollectionCount()); sb.append(" GCTime=" + StringFormatter.formatMillis(gc.getCollectionTime())); sb.append("]"); gcs += StringFormatter.formatMillisShort(gc.getCollectionTime()) + ":(" + gc.getCollectionCount() + ");"; } sb.append("\n"); for (MemoryPoolMXBean p : pools) { sb.append(" [" + p.getName() + ":"); MemoryUsage u = p.getUsage(); sb.append(" Used=" + StringFormatter.formatBytes(u.getUsed())); sb.append(" Committed=" + StringFormatter.formatBytes(u.getCommitted())); sb.append("]\n"); } return new String[] {sb.toString(), gcs}; }
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); }
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 String getInodeLimitText() { long inodes = fsn.dir.totalInodes(); long blocks = fsn.getBlocksTotal(); long maxobjects = fsn.getMaxObjects(); MemoryMXBean mem = ManagementFactory.getMemoryMXBean(); MemoryUsage heap = mem.getHeapMemoryUsage(); long totalMemory = heap.getUsed(); long maxMemory = heap.getMax(); long used = (totalMemory * 100) / maxMemory; String str = inodes + " files and directories, " + blocks + " blocks = " + (inodes + blocks) + " total"; if (maxobjects != 0) { long pct = ((inodes + blocks) * 100) / maxobjects; str += " / " + maxobjects + " (" + pct + "%)"; } str += ". Heap Size is " + StringUtils.byteDesc(totalMemory) + " / " + StringUtils.byteDesc(maxMemory) + " (" + used + "%) <br>"; return str; }
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 static int getFreePermGenSpacePromille() { MemoryUsage mu = getPermGenSpaceSize(null); if (mu == null) return -1; long max = mu.getMax(); long used = mu.getUsed(); if (max < 0 || used < 0) return -1; return (int) (1000L - (1000L * used / max)); }
public static long getFreePermGenSpaceSize() { MemoryUsage mu = getPermGenSpaceSize(null); if (mu == null) return -1; long max = mu.getMax(); long used = mu.getUsed(); if (max < 0 || used < 0) return -1; return max - used; }
/** * Grid-enables method only if heap utilized over 80%. Otherwise, method method will proceed with * local execution without calling grid at all. * * @param gridify {@inheritDoc} * @param arg {@inheritDoc} * @return {@inheritDoc} * @throws GridException {@inheritDoc} */ @Override public boolean isGridify(Annotation gridify, GridifyArgument arg) throws GridException { MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); // If heap memory for the local node is above 80% of maximum heap // memory available, return true which means that method will // be grid-enabled. Otherwise, return false, which means that // method will execute locally without grid. return heap.getUsed() > 0.8 * heap.getMax(); }
/** * 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 int getPermGenFreeSpaceAsAPercentageOfAvailable() { MemoryUsage mu = getPermGenSpaceSize(null); if (mu == null) return -1; long max = mu.getMax(); long used = mu.getUsed(); if (max < 0 || used < 0) return -1; // return a value that equates to a percentage of available free memory return 100 - ((int) (100 * (((double) used) / ((double) max)))); }
protected void updateMemory() { // Determining current memory usage state MemoryUsage mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); usedMemory = mu.getUsed(); allocatedMemory = mu.getCommitted(); // Updating bar text setText(getMemoryBarText()); // Updating view repaint(); }
public void dumpMemory(String title) { System.err.println("----------" + title + "----------"); double total = 0; for (MemoryPoolMXBean m : memoryBeans) { MemoryUsage u = m.getUsage(); double used = u.getUsed(); total += used; System.err.println(m.getName() + " " + toMemoryString(used)); } System.err.println("Total " + toMemoryString(total)); System.err.println("---------------------------------"); }
public static synchronized State release() { long now = System.currentTimeMillis(); if (marks.empty()) throw new RuntimeException( "TimeStiatistics: Called release() without a matching call to mark()"); State m = marks.pop(); m.garbageCounterDelta = garbageCounter.get() - m.garbageCounterMark; m.end = now; MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); m.memoryUsedEnd = memoryUsage.getUsed(); System.out.println(m.toString()); return m; }
/** * Creates a new instance. This can't be called outside of container b/c agents have no refs to * the singleton container. So we can be sure this method is going to create services just once. * * @param c Reference to the container. * @return The sole instance. * @throws NullPointerException If the reference to the {@link Container} is <code>null</code>. */ public static PixelsServicesFactory getInstance(Container c) { if (c == null) throw new NullPointerException(); // An agent called this method? if (singleton == null) { registry = c.getRegistry(); singleton = new PixelsServicesFactory(); // Retrieve the maximum heap size. MemoryUsage usage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); String message = "Heap memory usage: max " + usage.getMax(); registry.getLogger().info(singleton, message); // percentage of memory used for caching. maxSize = (int) (RATIO * usage.getMax()) / FACTOR; } return singleton; }
/* * (non-Javadoc) * * @see com.orientechnologies.common.test.SpeedTest#startTimer(java.lang.String) */ public void startTimer(final String iName) { Runtime.getRuntime().runFinalization(); Runtime.getRuntime().gc(); try { Thread.sleep(TIME_WAIT); } catch (InterruptedException e) { } final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); final MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); final MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage(); currentTestName = iName; currentTestHeapCommittedMemory = heapMemoryUsage.getCommitted(); currentTestHeapUsedMemory = heapMemoryUsage.getUsed(); currentTestHeapMaxMemory = heapMemoryUsage.getMax(); currentTestNonHeapCommittedMemory = nonHeapMemoryUsage.getCommitted(); currentTestNonHeapUsedMemory = nonHeapMemoryUsage.getUsed(); currentTestNonHeapMaxMemory = nonHeapMemoryUsage.getMax(); System.out.println("-> Started the test of '" + currentTestName + "' (" + cycles + " cycles)"); currentTestTimer = System.currentTimeMillis(); }
/** * Puts a marker in the marker stack. Every call to this method should have a corresponding call * to {@link #release()}. Calls to this method may be nested. * * @param markName the name of the marker */ public static synchronized void mark(String markName) { State marker = new State(); marker.id = markName; marker.start = System.currentTimeMillis(); MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); marker.memoryUsedStart = memoryUsage.getUsed(); marker.garbageCounterMark = garbageCounter.get(); marks.push(marker); System.out.println( "Set block marker \"" + markName + "\", memory used " + Util.getFormatBytes(marker.memoryUsedStart)); }
@Override public Iterable<Measurement> measure() { final long timestamp = clock.wallTime(); final MemoryPoolMXBean mbean = ref.get(); final List<Measurement> ms = new ArrayList<>(); if (mbean != null) { final String typeKey = "memtype"; final String type = mbean.getName(); final MemoryUsage usage = mbean.getUsage(); ms.add(new Measurement(usedId.withTag(typeKey, type), timestamp, usage.getUsed())); ms.add(new Measurement(committedId.withTag(typeKey, type), timestamp, usage.getCommitted())); ms.add(new Measurement(maxId.withTag(typeKey, type), timestamp, usage.getMax())); } return ms; }
public static String formatHeapMemoryUsage(MemoryUsage heap) { return heap != null ? CapString.concat( "Heap Usage: Committed=", bytesToMegabytes(heap.getCommitted()), "->", bytesToMegabytes(heap.getCommitted()), " MB, Init=", bytesToMegabytes(heap.getInit()), " MB, Max=", bytesToMegabytes(heap.getMax()), " MB, Used=", bytesToMegabytes(heap.getUsed()), " MB ") : ""; }
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; }
@SuppressWarnings("unchecked") public JSONObject info() throws JSONException { JSONObject object = new JSONObject(); object.put("gossip_active", isInitialized()); object.put("thrift_active", isThriftServerRunning()); object.put("token", getTokens().toString()); object.put("load", getLoadString()); object.put("generation_no", getCurrentGenerationNumber()); object.put("uptime", getUptime() / 1000); MemoryUsage heapUsage = getHeapMemoryUsage(); double memUsed = (double) heapUsage.getUsed() / (1024 * 1024); double memMax = (double) heapUsage.getMax() / (1024 * 1024); object.put("heap_memory_mb", memUsed + "/" + memMax); object.put("data_center", getDataCenter()); object.put("rack", getRack()); return object; }
private void setThreshold(float fltThresholdPercent, MemoryPoolMXBean memPool) { MemoryUsage memUsage = memPool.getUsage(); long max = memUsage.getMax(); long threshold = (long) (max * fltThresholdPercent); String strThresholdPercent = Integer.toString(Math.round(fltThresholdPercent * 100)) + "%"; log.info( "setting threshold on pool " + memPool.getName() + " to " + strThresholdPercent + " of " + max + "(" + threshold + ")"); memPool.setUsageThreshold(threshold); }
@Override public long[] getMaxSystemParameters() { long[] systemData = new long[3]; MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); systemData[0] = memoryMXBean.getHeapMemoryUsage().getMax(); systemData[1] = memoryMXBean.getNonHeapMemoryUsage().getMax(); List<MemoryPoolMXBean> mbeans = ManagementFactory.getMemoryPoolMXBeans(); if (mbeans != null) { for (MemoryPoolMXBean mbean : mbeans) { MemoryUsage memUsage = mbean.getUsage(); if (mbean.getName().equals(PERMGEN_MEMORY_POOL_NAME)) { systemData[2] = memUsage.getMax(); } } } return systemData; }
public String getMemoryState() { try { StringBuilder sb = new StringBuilder(); sb.append("Uptime: " + StringFormatter.formatMillis(rmbean.getUptime())); sb.append("\n"); for (MemoryPoolMXBean p : pools) { sb.append(" [" + p.getName() + ":"); MemoryUsage u = p.getUsage(); sb.append(" Used=" + StringFormatter.formatBytes(u.getUsed())); sb.append(" Committed=" + StringFormatter.formatBytes(u.getCommitted())); sb.append("]\n"); } return sb.toString(); } catch (Throwable e) { return e.getMessage(); } }
/** * Prints the verbose GC log to System.out to list the memory usage of all memory pools as well as * the GC statistics. */ public void printVerboseGc() { System.out.println("Uptime: " + formatMillis(rmbean.getUptime())); System.out.println("Heap usage: " + mmbean.getHeapMemoryUsage()); System.out.println("Non-Heap memory usage: " + mmbean.getNonHeapMemoryUsage()); for (GarbageCollectorMXBean gc : gcmbeans) { System.out.print(" [" + gc.getName() + ": "); System.out.print("Count=" + gc.getCollectionCount()); System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime())); System.out.print("]"); } System.out.println(); for (MemoryPoolMXBean p : pools) { System.out.print(" [" + p.getName() + ":"); MemoryUsage u = p.getUsage(); System.out.print(" Used=" + formatBytes(u.getUsed())); System.out.print(" Committed=" + formatBytes(u.getCommitted())); System.out.println("]"); } }
@Override public long[] getSystemStatus() { long[] systemData = new long[4]; MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); systemData[0] = memoryMXBean.getHeapMemoryUsage().getUsed(); systemData[1] = memoryMXBean.getNonHeapMemoryUsage().getUsed(); List<MemoryPoolMXBean> mbeans = ManagementFactory.getMemoryPoolMXBeans(); if (mbeans != null) { for (MemoryPoolMXBean mbean : mbeans) { MemoryUsage memUsage = mbean.getUsage(); if (mbean.getName().equals(PERMGEN_MEMORY_POOL_NAME)) { systemData[2] = memUsage.getUsed(); } } } ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); systemData[3] = threadMXBean.getThreadCount(); return systemData; }
public static Struct getMemoryUsageCompact(int type) { java.util.List<MemoryPoolMXBean> manager = ManagementFactory.getMemoryPoolMXBeans(); Iterator<MemoryPoolMXBean> it = manager.iterator(); MemoryPoolMXBean bean; MemoryUsage usage; MemoryType _type; Struct sct = new StructImpl(); 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; double d = ((int) (100D / usage.getMax() * usage.getUsed())) / 100D; sct.setEL(KeyImpl.init(bean.getName()), Caster.toDouble(d)); } return sct; }
public static void memUsage(PrintWriter out) { // Overall MemoryMXBean memBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heap = memBean.getHeapMemoryUsage(); MemoryUsage nonHeap = memBean.getNonHeapMemoryUsage(); out.println("Heap: " + heap); out.println("Non-heap: " + nonHeap); // Generations details List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean mbean : memoryPoolMXBeans) { println(out, "Memory Group", mbean.getName()); MemoryUsage memUsage = mbean.getUsage(); println(out, "Used", memUsage.getUsed()); println(out, "Committed", memUsage.getCommitted()); println(out, "High water", memUsage.getMax()); out.println(); } }
public void handleNotification(Notification notification, Object handback) { log.warn("================================================================================"); String message = notification.getMessage(); log.warn("Message: " + message); String type = notification.getType(); log.warn("Type: " + type); if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) || type.equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) { CompositeData cd = (CompositeData) notification.getUserData(); MemoryNotificationInfo info = MemoryNotificationInfo.from(cd); String poolName = info.getPoolName(); log.warn("Pool Name: " + poolName); long count = info.getCount(); log.warn("Count: " + count); MemoryUsage usage = info.getUsage(); long maxMemory = usage.getMax(); long maxMB = maxMemory / (1024 * 1024); long usedMemory = usage.getUsed(); long usedMB = usedMemory / (1024 * 1024); double percentUsed = (double) usedMemory / maxMemory; log.debug( "Used Memory : " + usedMB + "/" + maxMB + " MB (" + percentFormat.format(percentUsed) + ")"); } }
private void createRuntimeProps(MemberStateImpl memberState) { Runtime runtime = Runtime.getRuntime(); ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); ClassLoadingMXBean clMxBean = ManagementFactory.getClassLoadingMXBean(); MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heapMemory = memoryMxBean.getHeapMemoryUsage(); MemoryUsage nonHeapMemory = memoryMxBean.getNonHeapMemoryUsage(); Map<String, Long> map = new HashMap<String, Long>(); map.put( "runtime.availableProcessors", Integer.valueOf(runtime.availableProcessors()).longValue()); map.put("date.startTime", runtimeMxBean.getStartTime()); map.put("seconds.upTime", runtimeMxBean.getUptime()); map.put("memory.maxMemory", runtime.maxMemory()); map.put("memory.freeMemory", runtime.freeMemory()); map.put("memory.totalMemory", runtime.totalMemory()); map.put("memory.heapMemoryMax", heapMemory.getMax()); map.put("memory.heapMemoryUsed", heapMemory.getUsed()); map.put("memory.nonHeapMemoryMax", nonHeapMemory.getMax()); map.put("memory.nonHeapMemoryUsed", nonHeapMemory.getUsed()); map.put("runtime.totalLoadedClassCount", clMxBean.getTotalLoadedClassCount()); map.put( "runtime.loadedClassCount", Integer.valueOf(clMxBean.getLoadedClassCount()).longValue()); map.put("runtime.unloadedClassCount", clMxBean.getUnloadedClassCount()); map.put("runtime.totalStartedThreadCount", threadMxBean.getTotalStartedThreadCount()); map.put("runtime.threadCount", Integer.valueOf(threadMxBean.getThreadCount()).longValue()); map.put( "runtime.peakThreadCount", Integer.valueOf(threadMxBean.getPeakThreadCount()).longValue()); map.put( "runtime.daemonThreadCount", Integer.valueOf(threadMxBean.getDaemonThreadCount()).longValue()); memberState.setRuntimeProps(map); }