本文整理汇总了Java中java.lang.management.MemoryMXBean类的典型用法代码示例。如果您正苦于以下问题:Java MemoryMXBean类的具体用法?Java MemoryMXBean怎么用?Java MemoryMXBean使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MemoryMXBean类属于java.lang.management包,在下文中一共展示了MemoryMXBean类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: call
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
@Override
public String call() {
Map<String, String> memoryMap = new HashMap<>();
OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
double cpu = osMxBean.getSystemLoadAverage();
memoryMap.put("cpuLoad", String.format(doubleStringFormatter, round(cpu, doubleRoundPlaces)));
ThreadMXBean threadmxBean = ManagementFactory.getThreadMXBean();
int threadCount = threadmxBean.getThreadCount();
memoryMap.put("cpuRunningThreads", String.valueOf(threadCount));
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memHeapUsage = memBean.getHeapMemoryUsage();
MemoryUsage nonHeapUsage = memBean.getNonHeapMemoryUsage();
memoryMap.put("heapInit", String.valueOf(memHeapUsage.getInit()));
memoryMap.put("heapMax", String.valueOf(memHeapUsage.getMax()));
memoryMap.put("heapCommit", String.valueOf(memHeapUsage.getCommitted()));
memoryMap.put("heapUsed", String.valueOf(memHeapUsage.getUsed()));
memoryMap.put("nonHeapInit", String.valueOf(nonHeapUsage.getInit()));
memoryMap.put("nonHeapMax", String.valueOf(nonHeapUsage.getMax()));
memoryMap.put("nonHeapCommit", String.valueOf(nonHeapUsage.getCommitted()));
memoryMap.put("nonHeapUsed", String.valueOf(nonHeapUsage.getUsed()));
return memoryMap.toString();
}
示例2: logSystemInfo
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
public static void logSystemInfo() {
try {
OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();
Log.info("Operating system"
+ " name: " + operatingSystemBean.getName()
+ " version: " + operatingSystemBean.getVersion()
+ " architecture: " + operatingSystemBean.getArch());
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
Log.info("Java runtime"
+ " name: " + runtimeBean.getVmName()
+ " vendor: " + runtimeBean.getVmVendor()
+ " version: " + runtimeBean.getVmVersion());
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
Log.info("Memory limit"
+ " heap: " + memoryBean.getHeapMemoryUsage().getMax() / (1024 * 1024) + "mb"
+ " non-heap: " + memoryBean.getNonHeapMemoryUsage().getMax() / (1024 * 1024) + "mb");
Log.info("Character encoding: "
+ System.getProperty("file.encoding") + " charset: " + Charset.defaultCharset());
} catch (Exception error) {
Log.warning("Failed to get system info");
}
}
示例3: startJVMThresholdListener
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
/**
* Register with the JVM to get threshold events.
*
* Package private for testing.
*/
void startJVMThresholdListener() {
final MemoryPoolMXBean memoryPoolMXBean = getTenuredMemoryPoolMXBean();
// Set collection threshold to a low value, so that we can get
// notifications after every GC run. After each such collection
// threshold notification we set the usage thresholds to an
// appropriate value.
if (!testDisableMemoryUpdates) {
memoryPoolMXBean.setCollectionUsageThreshold(1);
}
final long usageThreshold = memoryPoolMXBean.getUsageThreshold();
this.cache.getLoggerI18n().info(
LocalizedStrings.HeapMemoryMonitor_OVERRIDDING_MEMORYPOOLMXBEAN_HEAP_0_NAME_1,
new Object[] {Long.valueOf(usageThreshold), memoryPoolMXBean.getName()});
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationEmitter emitter = (NotificationEmitter) mbean;
emitter.addNotificationListener(this, null, null);
}
示例4: envi
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
@Override
public String envi() {
Map<String, String> result = new HashMap<String, String>();
// 系统信息
result.put(RegistryMonitorConstants.OSNAME, System.getProperty(RegistryMonitorConstants.OSNAME, "not specified"));
result.put(RegistryMonitorConstants.JAVA_VERSION, System.getProperty(RegistryMonitorConstants.JAVA_VERSION, "not specified"));
result.put(RegistryMonitorConstants.JDK_PATH, System.getProperty(RegistryMonitorConstants.JDK_PATH, "not specified"));
result.put(RegistryMonitorConstants.APP_PATH, System.getProperty(RegistryMonitorConstants.APP_PATH, "not specified"));
// jvm信息
MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
MemoryUsage usage = memorymbean.getHeapMemoryUsage();
result.put(RegistryMonitorConstants.JVM_INIT, String.valueOf(usage.getInit()));
result.put(RegistryMonitorConstants.JVM_MAX, String.valueOf(usage.getMax()));
result.put(RegistryMonitorConstants.JVM_USED, String.valueOf(usage.getUsed()));
//线程信息
ThreadMXBean tm = (ThreadMXBean) ManagementFactory.getThreadMXBean();
result.put(RegistryMonitorConstants.JVM_THREAD_COUNT, String.valueOf(tm.getThreadCount()));
result.put(RegistryMonitorConstants.JVM_PEAKTHREAD_COUNT, String.valueOf(tm.getPeakThreadCount()));
result.put(RegistryMonitorConstants.JVM_CURRENTTHREAD_CPUTIME, String.valueOf(tm.getCurrentThreadCpuTime()));
result.put(RegistryMonitorConstants.JVM_CURRENTTHREAD_USERTIME, String.valueOf(tm.getCurrentThreadUserTime()));
return JSON.toJSONString(result);
}
示例5: getMaxMemory
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
private static long getMaxMemory()
{
try {
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heap = null;
if (memoryBean != null) {
heap = memoryBean.getHeapMemoryUsage();
}
if (heap != null) {
return Math.max(heap.getMax(), heap.getCommitted());
}
else {
Runtime runtime = Runtime.getRuntime();
return Math.max(runtime.maxMemory(), runtime.totalMemory());
}
} catch (Exception e) {
e.printStackTrace();
}
return Runtime.getRuntime().maxMemory();
}
示例6: monitorMemory
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
public String monitorMemory() {
StringBuilder sb = new StringBuilder("Memory:");
MemoryMXBean mmbean = ManagementFactory.getMemoryMXBean();
MemoryUsage hmu = mmbean.getHeapMemoryUsage();
sb.append("[HeapMemoryUsage:");
sb.append(" Used=" + formatBytes(hmu.getUsed() * 8));
sb.append(" Committed=" + formatBytes(hmu.getCommitted() * 8));
sb.append("]");
MemoryUsage nhmu = mmbean.getNonHeapMemoryUsage();
sb.append("[NonHeapMemoryUsage:");
sb.append(" Used=" + formatBytes(nhmu.getUsed() * 8));
sb.append(" Committed=" + formatBytes(nhmu.getCommitted() * 8));
sb.append("]");
return sb.toString();
}
示例7: doGet
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
/**
* Renders tools page.
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Bundle[] bundles = BundleContextHolder.INSTANCE.getBundleContext().getBundles();
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
TemplateEngine.getInstance().render(TemplateContext.builder()
.request(req)
.response(resp)
.template(TOOLS_TEMPLATE)
.locale(req.getLocale())
.contextObject(ContextObject.newContextObject()
.put("username", req.getRemoteUser())
.put("sysProps", System.getProperties().entrySet())
.put("totalBundles", bundles.length)
.put("bundles", bundles)
.put("runtime", JAVA_RUNTIME_NAME + "(build " + JAVA_RUNTIME_VERSION + ")")
.put("jvm", JAVA_VM_NAME + "(build " + JAVA_VM_VERSION + ", " + JAVA_VM_INFO + ")")
.put("startTime", new Date(runtimeMXBean.getStartTime()))
.put("upTime", TimeUnit.MILLISECONDS.toMinutes(runtimeMXBean.getUptime()))
.put("maxMemory", FileUtils.byteCountToDisplaySize(memoryMXBean.getHeapMemoryUsage().getMax()))
.put("usedMemory", FileUtils.byteCountToDisplaySize(memoryMXBean.getHeapMemoryUsage().getUsed()))
.put("processors", Runtime.getRuntime().availableProcessors()))
.build());
}
示例8: main
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
public static void main(String[] args)
{
MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
System.out.println("Bean: " + bean);
System.out.println("Heap memory usage: "
+ bean.getHeapMemoryUsage());
System.out.println("Non-heap memory usage: "
+ bean.getNonHeapMemoryUsage());
System.out.println("Objects pending finalization: "
+ bean.getObjectPendingFinalizationCount());
System.out.println("Running garbage collector via bean...");
bean.gc();
System.out.println("Heap memory usage: "
+ bean.getHeapMemoryUsage());
System.out.println("Non-heap memory usage: "
+ bean.getNonHeapMemoryUsage());
System.out.println("Objects pending finalization: "
+ bean.getObjectPendingFinalizationCount());
boolean verbosity = bean.isVerbose();
System.out.println("Verbose memory output: " + (verbosity ? "yes" : "no"));
System.out.println("Changing verbose setting...");
bean.setVerbose(!verbosity);
System.out.println("Verbose memory output: " + (bean.isVerbose() ? "yes" : "no"));
}
示例9: getMemoryUsageStatsAsString
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
/**
* Gets the memory footprint of the JVM in a string representation.
*
* @return A string describing how much heap memory and direct memory are allocated and used.
*/
public static String getMemoryUsageStatsAsString(MemoryMXBean memoryMXBean) {
MemoryUsage heap = memoryMXBean.getHeapMemoryUsage();
MemoryUsage nonHeap = memoryMXBean.getNonHeapMemoryUsage();
long heapUsed = heap.getUsed() >> 20;
long heapCommitted = heap.getCommitted() >> 20;
long heapMax = heap.getMax() >> 20;
long nonHeapUsed = nonHeap.getUsed() >> 20;
long nonHeapCommitted = nonHeap.getCommitted() >> 20;
long nonHeapMax = nonHeap.getMax() >> 20;
return String.format("Memory usage stats: [HEAP: %d/%d/%d MB, " +
"NON HEAP: %d/%d/%d MB (used/committed/max)]",
heapUsed, heapCommitted, heapMax, nonHeapUsed, nonHeapCommitted, nonHeapMax);
}
示例10: ClusterStatus
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
/**
* @param numDecommissionedNodes number of decommission trackers
*/
ClusterStatus(int trackers, int blacklists, long ttExpiryInterval,
int maps, int reduces, int maxMaps, int maxReduces,
JobTracker.State state, int numDecommissionedNodes) {
numActiveTrackers = trackers;
numBlacklistedTrackers = blacklists;
this.numExcludedNodes = numDecommissionedNodes;
this.ttExpiryInterval = ttExpiryInterval;
map_tasks = maps;
reduce_tasks = reduces;
max_map_tasks = maxMaps;
max_reduce_tasks = maxReduces;
this.state = state;
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage status = memoryMXBean.getHeapMemoryUsage();
used_memory = status.getUsed();
max_memory = status.getMax();
}
示例11: MemoryMonitor
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
public MemoryMonitor() {
LOG.info("initializing");
this.springContextId = "Unknown";
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationEmitter emitter = (NotificationEmitter) mbean;
emitter.addNotificationListener(new NotificationListener() {
public void handleNotification(Notification n, Object hb) {
if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
long maxMemory = tenuredGenPool.getUsage().getMax();
long usedMemory = tenuredGenPool.getUsage().getUsed();
for (Listener listener : listeners) {
listener.memoryUsageLow(springContextId, usedMemory, maxMemory);
}
}
}
}, null, null);
}
示例12: main
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
public static void main(String[] args)
{
MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
System.out.println("Bean: " + bean);
System.out.println("Heap memory usage: "
+ bean.getHeapMemoryUsage());
System.out.println("Non-heap memory usage: "
+ bean.getNonHeapMemoryUsage());
System.out.println("Objects pending finalization: "
+ bean.getObjectPendingFinalizationCount());
System.out.println("Running garbage collector via bean...");
bean.gc();
System.out.println("Heap memory usage: "
+ bean.getHeapMemoryUsage());
System.out.println("Non-heap memory usage: "
+ bean.getNonHeapMemoryUsage());
System.out.println("Objects pending finalization: "
+ bean.getObjectPendingFinalizationCount());
boolean verbosity = bean.isVerbose();
System.out.println("Verbose memory output: " + (verbosity ? "yes" : "no"));
System.out.println("Changing verbose setting...");
bean.setVerbose(!verbosity);
System.out.println("Verbose memory output: " + (bean.isVerbose() ? "yes" : "no"));
}
示例13: memory
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
private void memory(Renderer renderer) {
System.gc();
MemoryMXBean mxbean = ManagementFactory.getMemoryMXBean();
{
long total = 0;
for (int i = 0; i < 1000000; i++) {
long start = mxbean.getHeapMemoryUsage().getUsed();
renderer.render(text, entitiesList.get(i % 1000));
long end = mxbean.getHeapMemoryUsage().getUsed();
long diff = end - start;
if (diff > 0) {
total += diff;
}
}
System.out.println("Memory: " + renderer.getClass().getSimpleName() + ": " + total / 1000000 + " bytes/op");
}
}
示例14: memoryUsage
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
private MemoryUsage memoryUsage() {
MemoryUsage usage = new MemoryUsage();
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
java.lang.management.MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
usage.heapInit = heapMemoryUsage.getInit();
usage.heapUsed = heapMemoryUsage.getUsed();
usage.heapCommitted = heapMemoryUsage.getCommitted();
usage.heapMax = heapMemoryUsage.getMax();
java.lang.management.MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
usage.nonHeapInit = nonHeapMemoryUsage.getInit();
usage.nonHeapUsed = nonHeapMemoryUsage.getUsed();
usage.nonHeapCommitted = nonHeapMemoryUsage.getCommitted();
usage.nonHeapMax = nonHeapMemoryUsage.getMax();
return usage;
}
示例15: MemoryTileCache
import java.lang.management.MemoryMXBean; //导入依赖的package包/类
public MemoryTileCache() {
log = Logger.getLogger(this.getClass());
hashtable = new Hashtable<String, CacheEntry>(cacheSize);
lruTiles = new CacheLinkedListElement();
cacheSize = 500;
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationBroadcaster emitter = (NotificationBroadcaster) mbean;
emitter.addNotificationListener(this, null, null);
// Set-up each memory pool to notify if the free memory falls below 10%
for (MemoryPoolMXBean memPool : ManagementFactory.getMemoryPoolMXBeans()) {
if (memPool.isUsageThresholdSupported()) {
MemoryUsage memUsage = memPool.getUsage();
memPool.setUsageThreshold((long) (memUsage.getMax() * 0.95));
}
}
}