Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/cmdLineTest_J9tests/src/TestStringBufferAndBuilderGrowth.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2020, 2020 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
import com.sun.management.OperatingSystemMXBean;
24
import java.lang.management.ManagementFactory;
25
26
/**
27
* @file TestStringBufferAndBuilderGrowth.java
28
* @brief Grows a StringBuffer and StringBuilder to Integer.MAX_VALUE
29
*/
30
public class TestStringBufferAndBuilderGrowth {
31
32
public static void main(String[] args) {
33
OperatingSystemMXBean opBean = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
34
long physicalMemory = opBean.getTotalPhysicalMemorySize();
35
System.out.println("Machine has physical memory " + physicalMemory + " bytes or " + (physicalMemory >> 20) + " MB or " + (physicalMemory >> 30) + " GB");
36
// An AIX machine with 7616 MB doesn't work
37
long limit = 8193L << 20;
38
if (physicalMemory < limit) {
39
// Machines with less memory may swap and timeout trying to run the test
40
System.out.println("Not enough resource to run test.");
41
return;
42
}
43
44
char[] cbuf = new char[Integer.MAX_VALUE / 32];
45
46
// Create a StringBuffer big enough that the default algorithm to
47
// grow it will overflow Integer.MAX_VALUE. Some smaller sizes could
48
// be used without affecting the test, such as the default size, but
49
// starting it big avoids unnecessary copying so the test runs faster.
50
StringBuffer sbuf = new StringBuffer((Integer.MAX_VALUE / 2) + 1);
51
// 17 iterations adds approximately 1G + 64M (minus a few bytes)
52
// to the Buffer, causing it to grow.
53
for (int i = 0; i < 17; i++) {
54
try {
55
sbuf.append(cbuf);
56
} catch (OutOfMemoryError e) {
57
System.out.println("OOM StringBuffer occurred iteration " + i);
58
return;
59
}
60
}
61
int sbufCapacity = sbuf.capacity();
62
// sbuf is no longer used after this point and will be GCed. Nulling it
63
// isn't technically required for OpenJ9, but make it explicit for clarity.
64
sbuf = null;
65
66
// Duplicate the above test for StringBuilder.
67
StringBuilder sbld = new StringBuilder((Integer.MAX_VALUE / 2) + 1);
68
for (int i = 0; i < 17; i++) {
69
try {
70
sbld.append(cbuf);
71
} catch (OutOfMemoryError e) {
72
System.out.println("OOM StringBuilder occurred iteration " + i);
73
return;
74
}
75
}
76
int sbldCapacity = sbld.capacity();
77
78
System.out.println("StringBuffer capacity=" + sbufCapacity + " StringBuilder capacity=" + sbldCapacity);
79
}
80
}
81
82