代码如下:

public class TestTenuringThreshold {
private static final int _1MB = 1024 * 1024; /**
* vm-args: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
* -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution
*/
public static void testTenuringThreshold() {
byte[] allocation1, allocation2, allocation3;
allocation1 = new byte[_1MB / 4]; allocation2 = new byte[4 * _1MB]; allocation3 = new byte[4 * _1MB];
allocation3 = null;
allocation3 = new byte[4 * _1MB];
}
public static void main(String[] args) {
testTenuringThreshold();
}
}

 

代码分析:

  这个代码会发生两次Minor GC:

    第一次:allocation3第一次创建byte数组时,因为eden只有8M左右,没有内存再分配给新对象,所有进行了第一次GC-----allocation2的索引对象进入了老年代.

    第二次:在allocation3 = null;执行以后,之前创建的对象已经"死亡",触发第二次GC回收死亡对象.与此同时,如果MaxTenuringThreshold的参数为1的话,在survivor中的allocation1的索引对象,因为age已经为1,所以会进入老年代.而当MaxTenuringThreshold大于1时,survivor中的allocation1的索引对象age+=1,不会进入老年代;

运行结果

MaxTenuringThreshold=1

[GC[DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age 1: 700488 bytes, 700488 total
: 5392K->684K(9216K), 0.0048021 secs] 5392K->4780K(19456K), 0.0048690 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC[DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age 1: 224 bytes, 224 total
: 5028K->0K(9216K), 0.0016226 secs] 9124K->4776K(19456K), 0.0016547 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4234K [0x33c00000, 0x34600000, 0x34600000)
eden space 8192K, 51% used [0x33c00000, 0x34022830, 0x34400000)
from space 1024K, 0% used [0x34400000, 0x344000e0, 0x34500000)
to space 1024K, 0% used [0x34500000, 0x34500000, 0x34600000)
tenured generation total 10240K, used 4775K [0x34600000, 0x35000000, 0x35000000)
the space 10240K, 46% used [0x34600000, 0x34aa9f70, 0x34aaa000, 0x35000000)
compacting perm gen total 12288K, used 233K [0x35000000, 0x35c00000, 0x39000000)
the space 12288K, 1% used [0x35000000, 0x3503a4a0, 0x3503a600, 0x35c00000)
ro space 10240K, 44% used [0x39000000, 0x3947d3a8, 0x3947d400, 0x39a00000)
rw space 12288K, 52% used [0x39a00000, 0x3a049a18, 0x3a049c00, 0x3a600000)

MaxTenuringThreshold=15

[GC[DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 15)
- age 1: 700488 bytes, 700488 total
: 5556K->684K(9216K), 0.0053202 secs] 5556K->4780K(19456K), 0.0053826 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC[DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age 1: 224 bytes, 224 total
: 4948K->0K(9216K), 0.0016949 secs] 9044K->4776K(19456K), 0.0017323 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4234K [0x33c00000, 0x34600000, 0x34600000)
eden space 8192K, 51% used [0x33c00000, 0x34022810, 0x34400000)
from space 1024K, 0% used [0x34400000, 0x344000e0, 0x34500000)
to space 1024K, 0% used [0x34500000, 0x34500000, 0x34600000)
tenured generation total 10240K, used 4775K [0x34600000, 0x35000000, 0x35000000)
the space 10240K, 46% used [0x34600000, 0x34aa9f70, 0x34aaa000, 0x35000000)
compacting perm gen total 12288K, used 233K [0x35000000, 0x35c00000, 0x39000000)
the space 12288K, 1% used [0x35000000, 0x3503a4a0, 0x3503a600, 0x35c00000)
ro space 10240K, 44% used [0x39000000, 0x3947d3a8, 0x3947d400, 0x39a00000)
rw space 12288K, 52% used [0x39a00000, 0x3a049a18, 0x3a049c00, 0x3a600000)

  

 问题分析:

问题出现:MaxTenuringThreshold=1时,结果与分析完全对应.但MaxTenuringThreshold=15时的结果相对预期出现了偏差.

-----------第一次编辑-----------17.8.12 11:20

尝试解决:

尝试1:此时jdk使用的是1.7.0_80,更换jdk为1.6.0_24

  MaxTenuringThreshold=1

  

[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age 1: 441368 bytes, 441368 total
: 5023K->431K(9216K), 0.0033469 secs] 5023K->4527K(19456K), 0.0033763 secs] [Times: user=0.00 sys=0.02, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age 1: 88 bytes, 88 total
: 4611K->0K(9216K), 0.0007613 secs] 8707K->4526K(19456K), 0.0007800 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4260K [0x33040000, 0x33a40000, 0x33a40000)
eden space 8192K, 52% used [0x33040000, 0x33468fe0, 0x33840000)
from space 1024K, 0% used [0x33840000, 0x33840058, 0x33940000)
to space 1024K, 0% used [0x33940000, 0x33940000, 0x33a40000)
tenured generation total 10240K, used 4526K [0x33a40000, 0x34440000, 0x34440000)
the space 10240K, 44% used [0x33a40000, 0x33eabb98, 0x33eabc00, 0x34440000)
compacting perm gen total 12288K, used 412K [0x34440000, 0x35040000, 0x38440000)
the space 12288K, 3% used [0x34440000, 0x344a7368, 0x344a7400, 0x35040000)
ro space 10240K, 54% used [0x38440000, 0x389bd9f8, 0x389bda00, 0x38e40000)
rw space 12288K, 55% used [0x38e40000, 0x394e13f8, 0x394e1400, 0x39a40000)

  

  MaxTenuringThreshold=15

  

[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age 1: 441368 bytes, 441368 total
: 5023K->431K(9216K), 0.0034562 secs] 5023K->4527K(19456K), 0.0034839 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age 1: 88 bytes, 88 total
- age 2: 441224 bytes, 441312 total
: 4611K->430K(9216K), 0.0009541 secs] 8707K->4526K(19456K), 0.0009800 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4747K [0x33040000, 0x33a40000, 0x33a40000)
eden space 8192K, 52% used [0x33040000, 0x33477090, 0x33840000)
from space 1024K, 42% used [0x33840000, 0x338abbe0, 0x33940000)
to space 1024K, 0% used [0x33940000, 0x33940000, 0x33a40000)
tenured generation total 10240K, used 4096K [0x33a40000, 0x34440000, 0x34440000)
the space 10240K, 40% used [0x33a40000, 0x33e40010, 0x33e40200, 0x34440000)
compacting perm gen total 12288K, used 413K [0x34440000, 0x35040000, 0x38440000)
the space 12288K, 3% used [0x34440000, 0x344a76f8, 0x344a7800, 0x35040000)
ro space 10240K, 54% used [0x38440000, 0x389bd9f8, 0x389bda00, 0x38e40000)
rw space 12288K, 55% used [0x38e40000, 0x394e13f8, 0x394e1400, 0x39a40000)

  发现在参数为15时第二次GC出现了age 2 字样,而且运行结果的 from space(即一个survivor)中存在数据

结果:

  更换jdk版本以后结果符合了预期,可以看出jdk在升级以后对虚拟机进行了改动.

  至于此改动的具体内容与意义,笔者现在无法分析,留在以后解决再更新

-----------第二次编辑-----------17.8.12 15:18

    

testTenuringThreshold()方法的分析与问题处理的更多相关文章

  1. 对tableView三种计算动态行高方法的分析

    tableView是一个神奇的东西,可以这么说,就算是一个初学者如果能把tableView玩的很6,那编一般的iOS的需求都问题不大了.tableView是日常开发中用烂了的控件,但是关于tableV ...

  2. JQuery 支持 hide 和 show 事件的方法与分析

    问题提出  JQuery不支持hide和show作为事件形式出现, 实际上这两个仅仅是JQuery对象的一个方法(fn): 有一类UI交互需求,根据一个DOM对象的或者显示对附属的DOM对象做相同操作 ...

  3. set方法内存分析

    // //  main.m //  04-set方法的内存管理分析 // //  Created by apple on 14-3-17. // // #import <Foundation/F ...

  4. JVM内存状况查看方法和分析工具

    Java本身提供了多种丰富的方法和工具来帮助开发人员查看和分析GC及JVM内存的状况,同时开源界和商业界也有一些工具可用于查看.分析GC及JVM内存的状况.通过这些分析,可以排查程序中内存泄露的问题及 ...

  5. JavaScript加密解密7种方法总结分析

    原文地址:http://wenku.baidu.com/view/9048edee9e31433239689357.html 本文一共介绍了七种javascript加密方法: 在做网页时(其实是网页木 ...

  6. 常用的CSS清除浮动的方法优缺点分析(个人学习笔记)

    一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class="outer"> <div class="di ...

  7. uboot启动阶段修改启动参数方法及分析

    作者:围补 本来启动方式这节不是什么复杂的事儿,不过想简单的说清楚明白,还真是不知道怎么组织.毕竟文字跟有声语言表达有别.但愿简单的东西别让我讲的太复杂! Arm板系统文件一般有三个——bootloa ...

  8. java执行程序的内存分析系列专栏二之static变量和方法内存分析

    昨天写了简单的聊了下java执行程序时简单的内存划分,今天我们接着往下聊,聊聊static变量和方法的内存分析. 1.static变量和方法的第一个特性内存分析 statiic变量和方法的第一个特性能 ...

  9. Java ArrayList正确循环添加删除元素方法及分析

    在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 一.foreach循环 foreach循环(Foreach loop)是计算机编程 ...

随机推荐

  1. Andrew Ng机器学习课程笔记--week1(机器学习介绍及线性回归)

    title: Andrew Ng机器学习课程笔记--week1(机器学习介绍及线性回归) tags: 机器学习, 学习笔记 grammar_cjkRuby: true --- 之前看过一遍,但是总是模 ...

  2. Oracle 与Mysql区别

    1.组函数用法规则 mysql中组函数在select语句中可以随意使用,但在oracle中如果查询语句中有组函数,那其他列名必须是组函数处理过的,或者是group by子句中的列否则报错 eg: se ...

  3. hibernate 一对多 多对一 关系表 增删改查大礼包ps二级查也有

    今天来到混元气功 这货大概的意思就是你中有我 我中有你 ps 这里就要说到维护关系 ps写这个用了我一下午.......也是刚刚好复习到这里 顺便就写写 注意:一般都在多方维护关系,至于是用单向还是用 ...

  4. malloc/free 的使用要点

    函数malloc的原型如下: void * malloc(size_t size); 用malloc申请一块长度为length的整数类型的内存,程序如下: int   *p = (int *)mall ...

  5. dede织梦如何防止被黑客入侵渗透?

    dede精简设置篇:避免被hack注射挂马 精简设置篇:不需要的功能统统删除.比如不需要会员就将member文件夹删除.删除多余组件是避免被hack注射的最佳办法.将每个目录添加空的index.htm ...

  6. Tomcat闪退的问题

    问题:双击tomcat bin下的startup.bat,tomcat的窗口一闪而过,未成功启动: 原因是:在启动tomcat是,需要读取环境变量和配置信息,缺少了这些信息就会导致了tomcat的闪退 ...

  7. mysql数据库优化之开启慢查询日志

    进入mysql数据库,使用 show variables like 'slow_query_log'; 查看是否开启了慢查询日志 value值为OFF,则慢查询日志没有开启,在开启慢查询日志之前,我们 ...

  8. 拨开字符编码的迷雾--MySQL数据库字符编码

    拨开字符编码迷雾系列文章链接: 拨开字符编码的迷雾--字符编码概述 拨开字符编码的迷雾--编译器如何处理文件编码 拨开字符编码的迷雾--字符编码转换 拨开字符编码的迷雾--MySQL数据库字符编码 1 ...

  9. Redis[三] @Hash 哈希

    Redis的哈希值是字符串字段和字符串值之间的映射,所以他们是表示对象的完美数据类型 在Redis中的哈希值,可存储超过400十亿键值对. redis 提供了2套操纵 一种是批量 一种是非批量 假设需 ...

  10. 浅析HTTP协议的请求报文和响应报文

    1.HTTP协议与报文简介  HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则. 而客户端与服务端通信时 ...