Path: blob/master/test/functional/cmdLineTests/cmdLineTest_J9tests/src/TestStringBufferAndBuilderGrowth.java
6004 views
/*******************************************************************************1* Copyright (c) 2020, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122import com.sun.management.OperatingSystemMXBean;23import java.lang.management.ManagementFactory;2425/**26* @file TestStringBufferAndBuilderGrowth.java27* @brief Grows a StringBuffer and StringBuilder to Integer.MAX_VALUE28*/29public class TestStringBufferAndBuilderGrowth {3031public static void main(String[] args) {32OperatingSystemMXBean opBean = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();33long physicalMemory = opBean.getTotalPhysicalMemorySize();34System.out.println("Machine has physical memory " + physicalMemory + " bytes or " + (physicalMemory >> 20) + " MB or " + (physicalMemory >> 30) + " GB");35// An AIX machine with 7616 MB doesn't work36long limit = 8193L << 20;37if (physicalMemory < limit) {38// Machines with less memory may swap and timeout trying to run the test39System.out.println("Not enough resource to run test.");40return;41}4243char[] cbuf = new char[Integer.MAX_VALUE / 32];4445// Create a StringBuffer big enough that the default algorithm to46// grow it will overflow Integer.MAX_VALUE. Some smaller sizes could47// be used without affecting the test, such as the default size, but48// starting it big avoids unnecessary copying so the test runs faster.49StringBuffer sbuf = new StringBuffer((Integer.MAX_VALUE / 2) + 1);50// 17 iterations adds approximately 1G + 64M (minus a few bytes)51// to the Buffer, causing it to grow.52for (int i = 0; i < 17; i++) {53try {54sbuf.append(cbuf);55} catch (OutOfMemoryError e) {56System.out.println("OOM StringBuffer occurred iteration " + i);57return;58}59}60int sbufCapacity = sbuf.capacity();61// sbuf is no longer used after this point and will be GCed. Nulling it62// isn't technically required for OpenJ9, but make it explicit for clarity.63sbuf = null;6465// Duplicate the above test for StringBuilder.66StringBuilder sbld = new StringBuilder((Integer.MAX_VALUE / 2) + 1);67for (int i = 0; i < 17; i++) {68try {69sbld.append(cbuf);70} catch (OutOfMemoryError e) {71System.out.println("OOM StringBuilder occurred iteration " + i);72return;73}74}75int sbldCapacity = sbld.capacity();7677System.out.println("StringBuffer capacity=" + sbufCapacity + " StringBuilder capacity=" + sbldCapacity);78}79}808182