1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 import sun.hotspot.WhiteBox;
  26 
  27 public class HelloStringGC {
  28     public static String[] array01 = new String[1000];
  29     public static String[] array02 = new String[1000];
  30 
  31     public static void main(String args[]) throws RuntimeException {
  32         String testString1 = "shared_test_string_unique_14325";
  33         String testString2 = "test123";
  34 
  35         WhiteBox wb = WhiteBox.getWhiteBox();
  36         if (!wb.isShared(testString1) && !wb.areSharedStringsIgnored()) {
  37             throw new RuntimeException("testString1 is not shared");
  38         }
  39 
  40         for (int i=0; i<5; i++) {
  41             allocSomeStrings(testString1, testString2);
  42             array01 = null;
  43             array02 = null;
  44             System.gc();
  45             sleep(300);
  46             array01 = new String[1000];
  47             array02 = new String[1000];
  48         }
  49 
  50         wb.fullGC();
  51 
  52         System.out.println("HelloStringGC: PASS");
  53     }
  54 
  55     private static void allocSomeStrings(String s1, String s2) {
  56         for (int i = 0; i < 1000; i ++) {
  57             array01[i] = new String(s1);
  58             array02[i] = new String(s2);
  59         }
  60     }
  61 
  62     private static void sleep(int ms) {
  63         try {
  64             Thread.sleep(ms);
  65         } catch (InterruptedException e) {
  66         }
  67     }
  68 
  69 }