Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/StringInternGC/StringInternGC.java
41155 views
/*1* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @key stress randomness27*28* @summary converted from VM Testbase gc/gctests/StringInternGC.29* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, quick]30*31* @library /vmTestbase32* /test/lib33* @run main/othervm -XX:+ExplicitGCInvokesConcurrent gc.gctests.StringInternGC.StringInternGC34*/3536package gc.gctests.StringInternGC;3738import nsk.share.test.*;39import nsk.share.gc.*;4041/**42* Test that strings returned by String.intern() can be collected.43*44* Create strings consisting of random characters, call String.intern().45* String pool should not overflow.46*/47public class StringInternGC extends ThreadedGCTest {48private int maxLength = 1000; // Maximum number of characters to add per operation.49private int maxTotalLength = 128 * 1024; // Total maximum length of the string until a new StringBuffer will be allocated.50private long lastTime = System.currentTimeMillis();5152private class StringGenerator implements Runnable {53private StringBuffer sb = new StringBuffer();5455private String generateString() {56int length = LocalRandom.nextInt(maxLength);57if (sb.length() > maxTotalLength) {58sb = new StringBuffer();59}6061for (int i = 0; i < length; ++i)62sb.append((char) LocalRandom.nextInt(Integer.MAX_VALUE));63return sb.toString();64}656667public void run() {68long currentTime = System.currentTimeMillis();69if (currentTime - lastTime > 5000) { // Cause a full gc every 5s.70lastTime = currentTime;71System.gc();72}7374generateString().intern();75}76}7778protected Runnable createRunnable(int i) {79return new StringGenerator();80}8182public static void main(String[] args) {83GC.runTest(new StringInternGC(), args);84}85}868788