我正在运行String vs StringBuilder测试来探索Java GC以及JIT。
我注意到了一些奇怪的东西。此图,从0分钟到2:31 AM,除了我的Java程序外,我的计算机基本上什么也不做:
然后我开始四处游荡,打开游戏,浏览网页,然后突然垃圾回收变得更具攻击性:(大约2:35 AM)
让我们再次使用Thread.sleep(1000)运行测试:
这对我来说毫无意义。看一下他在前2个图中分配了Heap ...没有变化。 JVM是否检测到它的堆空间可能受到攻击?当我查看CPU时,其使用率保持在25%或更少,因此没有CPU瓶颈。 然后在第三张图中,它与我在玩游戏而不是在玩游戏时完全相同地分配堆(使用CPU是我要在此处说明的要点)。
为什么会这样?
代码可能有所作为:
public static void main(String[] args) throws IOException, InterruptedException {
GCTests test = new GCTests();
// Test setup
int iterations = 1000; // with 800MB
// Keep running so I can see the results in jvisualm
while(true) {
System.out.println("\n\n\n\n\nSTARTING STRING TEST");
test.withString(iterations);
System.out.println("\n\n\n\n\nALL DONE STRING TEST");
Thread.sleep(20000);
System.out.println("\n\n\n\n\nSTARTING STRINGBUILDER TEST");
test.withStringBuilder(iterations);
System.out.println("\n\n\n\n\nALL DONE STRINGBUILDER TEST");
Thread.sleep(20000);
}
}
public void withString(int iterations) throws IOException, InterruptedException{
// Connect scanner to file
File file = new File("C:\\Users\\userx\\Desktop\\Intellij\\Practice\\DragNDrop\\src\\TextDragNDrop\\wordlist.10000.txt");
Scanner in;
// Append processed data to StringBuilder
StringBuilder collectForOuput = new StringBuilder();
String inputStr = ""; // Allow every line to consume one string
//////// START iterationsIndex times loop ////////////
// Trying to get millions of Strings being made
int iterationsIndex = 0;
while(iterationsIndex < iterations) {
///////// START FILE ///////////////////
in = new Scanner(file);
// Number of lines to expect and an index for each line
int wordsCount = 10000;
int wordCountIndex = 0;
//in.nextLine(); // Consume newline
while (wordCountIndex < wordsCount) {
inputStr = in.nextLine(); // Assign to string, newline is consumed so need to do another
// nextLine()
for (int i = 0; i < inputStr.length(); i += 2) {
collectForOuput.append(inputStr.charAt(i));
}
collectForOuput.append(" ");
for (int i = 1; i < inputStr.length(); i += 2) {
collectForOuput.append(inputStr.charAt(i));
}
collectForOuput.append("\n");
wordCountIndex++;
}
//////// END FILE ///////////////////////////
////////////////////////
// TEST PAUSE FOR GC ///
////////////////////////
//Thread.sleep(1000);
// File processed. Report and start again.
iterationsIndex++;
//System.out.println(iterationsIndex + " iterations complete.");
}//////// END iterationsIndex times loop ////////////
// At 1_000 iterations
//System.out.println(collectForOuput.toString());
}
public void withStringBuilder(int iterations) throws IOException, InterruptedException{
// Connect scanner to file
File file = new File("C:\\Users\\userx\\Desktop\\Intellij\\Practice\\DragNDrop\\src\\TextDragNDrop\\wordlist.10000.txt");
Scanner in;
// Append processed data to StringBuilder
StringBuilder collectForOuput = new StringBuilder();
StringBuilder inputStr = new StringBuilder(); // Every line is appended to
// SB and SB is made length 0
// after every line is processes...
//////// START iterationsIndex times loop ////////////
// Trying to get millions of Strings being made
int iterationsIndex = 0;
while(iterationsIndex<iterations) {
///////// START FILE ///////////////////
in = new Scanner(file);
// Number of lines to expect and an index for each line
int wordsCount = 10000;
int wordCountIndex = 0;
//in.nextLine(); // Consume newline
while (wordCountIndex < wordsCount) {
// Append to StringBuilder, newline is consumed so need to do another
// nextLine()
inputStr.append(in.nextLine());
for (int i = 0; i < inputStr.length(); i += 2) {
collectForOuput.append(inputStr.charAt(i));
}
collectForOuput.append(" ");
for (int i = 1; i < inputStr.length(); i += 2) {
collectForOuput.append(inputStr.charAt(i));
}
collectForOuput.append("\n");
inputStr.setLength(0);
wordCountIndex++;
}
//////// END FILE ///////////////////////////
////////////////////////
// TEST PAUSE FOR GC ///
////////////////////////
//Thread.sleep(1000);
// File processed. Report and start again.
iterationsIndex++;
//System.out.println(iterationsIndex + " iterations complete.");
}//////// END iterationsIndex times loop ////////////
// At 1_000 iterations
//System.out.println(collectForOuput.toString());
}