`
cloudeagle_bupt
  • 浏览: 533832 次
文章分类
社区版块
存档分类
最新评论

关于一个GC回收内存总量的疑问

 
阅读更多
package ringBuffer;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class PerformanceWriteTest {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String outputFile = "F:\\test\\ioTest.txt";
		Long length = 0L ; 
		Long totalTime = 0L;
		
//        try {
//   	   		raf = new RandomAccessFile("F:\\test\\ioTest.txt", "rw");
//   	   		FileChannel fc = raf.getChannel();
//   	   		mbb = fc.map(MapMode.READ_WRITE, 0, 85*1024*1024);
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		for (int j = 0; j < 5; j++) {
				StringBuffer sb = new StringBuffer();
				for (Integer i = 0; i < 1000000; i++) {
					sb.append(j+i.toString() + "V");
				}
				sb.append("S");
				length = (long) sb.toString().length() ;
			    long start =  System.currentTimeMillis() ;
				appendFileTest(outputFile,sb.toString());
				totalTime = totalTime + (System.currentTimeMillis() - start) ;
		}
		System.out.println(" Total Data is : " + length*5/1000 + " Kbytes! ") ;
		System.out.println(" Total Time is : " + totalTime) ;
		System.out.println(" Averge Speed is :" + length*5/(totalTime*1000) + " Kbytes");
	}

	private static void appendFileTest(String outputFile, String msgs) {
//             append1(outputFile, msgs) ;  //FileOutputStream
//             append2(outputFile, msgs) ;  //FileWriter
             append3(outputFile, msgs) ;  //RandomAccessFile 
//			   append4(outputFile, msgs) ;  //RandomAccessFile 
	}

	private static void append1(String outputFile, String msgs) {
		BufferedWriter out = null;   
	    try {   
	         out = new BufferedWriter(new OutputStreamWriter(   
	                  new FileOutputStream(outputFile, true)));   
	         out.append(msgs) ;
	        } catch (Exception e) {   
	            e.printStackTrace();   
	        } finally {   
	            try {   
	                out.close();   
	            } catch (IOException e) {   
	                e.printStackTrace();   
	            }   
	        }   
	}

	private static void append2(String outputFile, String msgs) {
		try {   
            FileWriter writer = new FileWriter(outputFile, true);     // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件   
            writer.write(msgs);   
            writer.close();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
	}

	private static void append3(String outputFile, String msgs) {
		  try {   
	            RandomAccessFile randomFile = new RandomAccessFile(outputFile, "rw");    // 打开一个随机访问文件流,按读写方式   
	            long fileLength = randomFile.length();    // 文件长度,字节数   
	            randomFile.seek(fileLength);    // 将写文件指针移到文件尾
	            randomFile.writeBytes(msgs);   
	            randomFile.close();   
	        } catch (IOException e) {   
	            e.printStackTrace();   
	        }   
	}
	
	private static void append4(String outputFile, String msgs) {
		  try {   
			    mbb.position(pos) ;
			    mbb.put(msgs.getBytes());
			    pos = pos + msgs.getBytes().length ;
			    raf.close();   
	        } catch (IOException e) {   
	            e.printStackTrace();   
	        }   
	}

	static RandomAccessFile raf ;
	static MappedByteBuffer mbb  ;
	static Integer pos = 0 ;
}


加入-XX::+PrintGC, 打印的结果是:

[GC 32704K->2928K(124992K), 0.0024320 secs]
[GC 35632K->5200K(124992K), 0.0020096 secs]
[GC 29268K->5200K(124992K), 0.0014802 secs]
[GC 37904K->9792K(157696K), 0.0035590 secs]
[GC 60504K->9840K(157696K), 0.0008594 secs]
[GC 75248K->28224K(224640K), 0.0079131 secs]
[GC 159040K->30572K(224768K), 0.0014706 secs]
[GC 159705K->37516K(355008K), 0.0029869 secs]
[GC 299148K->46668K(355008K), 0.0031385 secs]
[GC 308300K->48980K(511296K), 0.0010842 secs]
[GC 467604K->55860K(511616K), 0.0036752 secs]


可以看到一共产生了11次GC, 而让我困惑的是,使用jstat -gcutil 18360 20 200 > 2.txt

结果如下:

S0 S1 E O P YGC YGCT FGC FGCT GCT
0.00 0.00 12.01 0.00 11.93 0 0.000 0 0.000 0.000
0.00 0.00 12.01 0.00 11.93 0 0.000 0 0.000 0.000
0.00 54.32 5.86 0.01 11.94 1 0.002 0 0.000 0.002
0.00 54.32 5.86 0.01 11.94 1 0.002 0 0.000 0.002
96.58 0.00 0.00 0.01 11.94 2 0.004 0 0.000 0.004
96.58 0.00 21.54 0.01 11.94 2 0.004 0 0.000 0.004
0.00 96.58 0.00 0.01 11.94 3 0.006 0 0.000 0.006
0.00 96.58 82.32 0.01 11.94 3 0.006 0 0.000 0.006
10.57 0.00 0.00 10.61 11.94 4 0.009 0 0.000 0.009
10.57 0.00 0.00 10.61 11.94 4 0.009 0 0.000 0.009
10.57 0.00 41.35 10.61 11.94 4 0.009 0 0.000 0.009
10.57 0.00 41.35 10.61 11.94 4 0.009 0 0.000 0.009
0.00 11.46 40.92 10.61 11.94 5 0.010 0 0.000 0.010
0.00 11.46 40.92 10.61 11.94 5 0.010 0 0.000 0.010
0.00 11.46 40.92 10.61 11.94 5 0.010 0 0.000 0.010
0.00 11.46 93.70 10.61 11.94 6 0.010 0 0.000 0.010
8.22 0.00 0.00 31.82 11.94 6 0.018 0 0.000 0.018
8.22 0.00 18.62 31.82 11.94 6 0.018 0 0.000 0.018
8.22 0.00 18.62 31.82 11.94 6 0.018 0 0.000 0.018
8.22 0.00 18.62 31.82 11.94 6 0.018 0 0.000 0.018
8.22 0.00 70.07 31.82 12.06 6 0.018 0 0.000 0.018
8.22 0.00 70.07 31.82 12.06 6 0.018 0 0.000 0.018
8.22 0.00 89.85 31.82 12.07 6 0.018 0 0.000 0.018
8.22 0.00 89.85 31.82 12.07 7 0.018 0 0.000 0.018
0.00 34.09 0.00 32.41 12.07 7 0.020 0 0.000 0.020
0.00 34.09 19.26 32.41 12.07 7 0.020 0 0.000 0.020
0.00 34.09 19.26 32.41 12.07 7 0.020 0 0.000 0.020
0.00 34.09 53.07 32.41 12.07 7 0.020 0 0.000 0.020
0.00 34.09 53.07 32.41 12.07 7 0.020 0 0.000 0.020
0.00 34.09 53.07 32.41 12.07 7 0.020 0 0.000 0.020
0.00 34.09 79.83 32.41 12.07 7 0.020 0 0.000 0.020
0.00 34.09 79.83 32.41 12.07 7 0.020 0 0.000 0.020
1.98 0.00 10.34 43.02 12.07 8 0.023 0 0.000 0.023
1.98 0.00 10.34 43.02 12.07 8 0.023 0 0.000 0.023
1.98 0.00 10.34 43.02 12.07 8 0.023 0 0.000 0.023
1.98 0.00 23.53 43.02 12.07 8 0.023 0 0.000 0.023
1.98 0.00 23.53 43.02 12.07 8 0.023 0 0.000 0.023
1.98 0.00 36.71 43.02 12.07 8 0.023 0 0.000 0.023

可以看到只有8次GC.

jstat -gccapacity 18360

NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC PGCMN PGCMX PGC PC YGC FGC
43456.0 695616.0 43456.0 5376.0 5376.0 32704.0 86912.0 1391296.0 86912.0 86912.0 21248.0 83968.0 21248.0 21248.0 0 0


这里有两个问题:

1.为啥两个测出的GC次数不一样?

2. 如何计算该JVM生存期内的yongGc回收的内存大小?

求指点。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics