MemoryMXBean

package cn.zno.outofmomery;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.ArrayList;
import java.util.List; public class Test {
MemoryMXBean memoryMXBean;
{
memoryMXBean = ManagementFactory.getMemoryMXBean();
System.out.println(memoryMXBean.isVerbose());
} void h(){
List<byte[]> list = new ArrayList<byte[]>();
while(true){
// System.out.println(memoryMXBean.getNonHeapMemoryUsage());
System.out.println(memoryMXBean.getHeapMemoryUsage());
list.add(new byte[1024*1024]);
}
} public static void main(String[] args) {
new Test().h();
}
}
  • 垃圾回收是否启用
  • 获取堆内存使用情况
  • 获取非堆内存使用情况

VM args

-verbose:gc  -XX:+PrintGCDetails

-verbose:gc

-Xloggc:D://data.log 

-Xloggc:D://data.log -XX:+PrintGCDetails

运行结果:

true
init = (32768K) used = (594K) committed = (31680K) max = (31680K)
init = (32768K) used = (1618K) committed = (31680K) max = (31680K)
init = (32768K) used = (2642K) committed = (31680K) max = (31680K)
init = (32768K) used = (3666K) committed = (31680K) max = (31680K)
init = (32768K) used = (4690K) committed = (31680K) max = (31680K)
init = (32768K) used = (5714K) committed = (31680K) max = (31680K)
init = (32768K) used = (6738K) committed = (31680K) max = (31680K)
init = (32768K) used = (7762K) committed = (31680K) max = (31680K)
[GC 7762K->7548K(31680K), 0.0032242 secs]
init = (32768K) used = (8572K) committed = (31680K) max = (31680K)
init = (32768K) used = (9689K) committed = (31680K) max = (31680K)
init = (32768K) used = (10887K) committed = (31680K) max = (31680K)
init = (32768K) used = (11911K) committed = (31680K) max = (31680K)
init = (32768K) used = (12935K) committed = (31680K) max = (31680K)
init = (32768K) used = (13959K) committed = (31680K) max = (31680K)
init = (32768K) used = (14983K) committed = (31680K) max = (31680K)
init = (32768K) used = (16007K) committed = (31680K) max = (31680K)
[GC 16007K->15739K(31680K), 0.0032697 secs]
init = (32768K) used = (16824K) committed = (31680K) max = (31680K)
init = (32768K) used = (17849K) committed = (31680K) max = (31680K)
init = (32768K) used = (18873K) committed = (31680K) max = (31680K)
init = (32768K) used = (19897K) committed = (31680K) max = (31680K)
init = (32768K) used = (20921K) committed = (31680K) max = (31680K)
init = (32768K) used = (21945K) committed = (31680K) max = (31680K)
init = (32768K) used = (22969K) committed = (31680K) max = (31680K)
init = (32768K) used = (23993K) committed = (31680K) max = (31680K)
[Full GC 23993K->23932K(31680K), 0.0051448 secs]
init = (32768K) used = (24956K) committed = (31680K) max = (31680K)
init = (32768K) used = (26020K) committed = (31680K) max = (31680K)
init = (32768K) used = (27044K) committed = (31680K) max = (31680K)
init = (32768K) used = (28068K) committed = (31680K) max = (31680K)
init = (32768K) used = (29092K) committed = (31680K) max = (31680K)
init = (32768K) used = (30116K) committed = (31680K) max = (31680K)
init = (32768K) used = (31140K) committed = (31680K) max = (31680K)
[Full GC 31140K->31100K(31680K), 0.0034119 secs]
[Full GC 31100K->31090K(31680K), 0.0033546 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at cn.zno.outofmomery.Test.h(Test.java:)
at cn.zno.outofmomery.Test.main(Test.java:)

垃圾回收格式解读

init = 33554432(32768K) used = 24568936(23993K) committed = 32440320(31680K) max = 32440320(31680K)
[Full GC 23993K->23932K(31680K), 0.0051448 secs]

-----------------------------------------------------------
[Full GC gc前used->gc后used(committed), 耗费时间]

参考 : https://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

Measurement

Throughput and footprint are best measured using metrics particular to the application. For example, throughput of a web server may be tested using a client load generator, while footprint of the server might be measured on the Solaris Operating System using the pmap command. On the other hand, pauses due to garbage collection are easily estimated by inspecting the diagnostic output of the virtual machine itself.

The command line option -verbose:gc causes information about the heap and garbage collection to be printed at each collection. For example, here is output from a large server application:

               
[GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs]

Here we see two minor collections followed by one major collection. The numbers before and after the arrow (e.g., 325407K->83000K from the first line) indicate the combined size of live objects before and after garbage collection, respectively. After minor collections the size includes some objects that are garbage (no longer alive) but that cannot be reclaimed. These objects are either contained in the tenured generation, or referenced from the tenured or permanent generations.

The next number in parentheses (e.g., (776768K) again from the first line) is the committed size of the heap: the amount of space usable for java objects without requesting more memory from the operating system. Note that this number does not include one of the survivor spaces, since only one can be used at any given time, and also does not include the permanent generation, which holds metadata used by the virtual machine.

The last item on the line (e.g., 0.2300771 secs) indicates the time taken to perform the collection; in this case approximately a quarter of a second.

The format for the major collection in the third line is similar.

The format of the output produced by -verbose:gc is subject to change in future releases.

The option -XX:+PrintGCDetails causes additional information about the collections to be printed. An example of the output with -XX:+PrintGCDetails using the serial garbage collector is shown here.

               
[GC [DefNew: 64575K->959K(64576K), 0.0457646 secs] 196016K->133633K(261184K), 0.0459067 secs]

indicates that the minor collection recovered about 98% of the young generation, DefNew: 64575K->959K(64576K) and took 0.0457646 secs (about 45 milliseconds).

The usage of the entire heap was reduced to about 51% 196016K->133633K(261184K) and that there was some slight additional overhead for the collection (over and above the collection of the young generation) as indicated by the final time of 0.0459067 secs.

The option -XX:+PrintGCTimeStamps will add a time stamp at the start of each collection. This is useful to see how frequently garbage collections occur.

111.042: [GC 111.042: [DefNew: 8128K->8128K(8128K), 0.0000505 secs]111.042: [Tenured: 18154K->2311K(24576K), 0.1290354 secs] 26282K->2311K(32704K), 0.1293306 secs]

The collection starts about 111 seconds into the execution of the application. The minor collection starts at about the same time. Additionally the information is shown for a major collection delineated by Tenured. The tenured generation usage was reduced to about 10% 18154K->2311K(24576K) and took 0.1290354 secs (approximately 130 milliseconds).

As was the case with -verbose:gc, the format of the output produced by -XX:+PrintGCDetailsis subject to change in future releases.

ManagementFactory (二) getMemoryMXBean的更多相关文章

  1. java 利用ManagementFactory获取jvm,os的一些信息--转

    原文地址:http://blog.csdn.net/dream_broken/article/details/49759043 想了解下某个Java项目的运行时jvm的情况,可以使用一些监控工具,比如 ...

  2. java监控之ManagementFactory分析

    The ManagementFactory class is a factory class for getting managed beans for the Java platform. This ...

  3. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  4. 深入理解java虚拟机---JDK8-废弃永久代(PermGen)迎来元空间(Metaspace)(十二)

    引用:https://www.cnblogs.com/yulei126/p/6777323.html JDK8-废弃永久代(PermGen)迎来元空间(Metaspace)   1.背景 2.为什么废 ...

  5. 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态

    最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...

  6. 前端开发中SEO的十二条总结

    一. 合理使用title, description, keywords二. 合理使用h1 - h6, h1标签的权重很高, 注意使用频率三. 列表代码使用ul, 重要文字使用strong标签四. 图片 ...

  7. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  8. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  9. 谈谈一些有趣的CSS题目(十二)-- 你该知道的字体 font-family

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

随机推荐

  1. Sublime安装插件的方法

    一:打开:工具--命令面板 二:输入:package,,选择下拉列表里提示的install package 三:输入需要安装的插件的名称,如:angularJS  Less  sass 回车即可安装, ...

  2. 聊聊Oracle 11g的Snapshot Standby Database(上)

    Oracle 11g是Data Guard的重要里程碑版本.在11g中,Active DataGuard.Advanced Compression等特性大大丰富了Data Guard的功能和在实践领域 ...

  3. ssh 或者 scp 无需输入密码的解决办法

    这里假设主机A(192.168.100.3)用来获到主机B(192.168.100.4)的文件.   在主机A上执行如下命令来生成配对密钥: ssh-keygen -t rsa   遇到提示回车默认即 ...

  4. [C#搜片神器] 之P2P中DHT网络爬虫原理

    继续接着上一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 昨天由于开源的时候没有注意运行环境,直接没有考虑下载BT种子文件时生成子文件夹,可能导致有的朋友运行 ...

  5. 讲解HTML服务器推送相关技术知识(转)

    1. 为什么需要服务器推送? 最大的优点:实时 健康知识平台重庆男科医院 重庆妇科医院适用场景:实时股票价格.商品价格.实时新闻.Twitter/weibo timeline.基于浏览器的聊天系统 2 ...

  6. LoadRunner error -27728

    错误现象1:Action.c(16): Error -27728: Step download timeout (120 seconds) has expired whendownloading no ...

  7. 数往知来 asp.net 聊天室问题解决方案<十六>

      1:在服务端创建了一个负责监听的sokcet   //三个:采用TCP协议.              ListenSocket = new Socket(AddressFamily.InterN ...

  8. (转载)OC学习篇之---类的延展

    前一篇文章我们介绍了类的类目概念和使用,那么这篇文章我们继续来介绍一下OC中的一个特性:延展. 其实说白了,延展就是弥补C语言中的前向申明,我们知道,在C语言中,如果你想调用一个函数的话,那么在此之前 ...

  9. keyCode 与charCode

    键盘事件拥有两个属性,keyCode和CharCode,他们之间有一些不一样之处.keyCode表示用户按下键的实际的编码,而charCode是指用户按下字符的编码. IE下 keyCode:对于ke ...

  10. String内存陷阱简介

    String 方法用于文本分析及大量字符串处理时会对内存性能造成一些影响.可能导致内存占用太大甚至OOM. 一.先介绍一下String对象的内存占用 一般而言,Java 对象在虚拟机的结构如下:•对象 ...