Path: blob/master/test/jdk/java/util/Locale/SoftKeys.java
41149 views
/*1* Copyright (c) 2018, 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*/22/*23* @test24* @bug 819686925* @summary Make sure we deal with internal Key data being cleared properly26* @ignore This test aims to provoke NPEs, but due to the need to constrain27* memory usage it fails intermittently with OOME on various systems28* with no way to ignore such failures.29* @run main/othervm -Xms16m -Xmx16m -esa SoftKeys30*/31import java.util.*;3233public class SoftKeys {3435public static void main(String[] args) {36try {37// With 4 characters in "language", we'll fill up a 16M heap quickly,38// causing full GCs and SoftReference reclamation. Repeat at least two39// times to verify no NPEs appear when looking up Locale's whose40// softly referenced data in sun.util.locale.BaseLocale$Key might have41// been cleared.42for (int i = 0; i < 2; i++) {43for (int j = 0; j < 512*1024; j++) {44new Locale(HexFormat.of().toHexDigits((short)j), "", "");45}46}47} catch (OutOfMemoryError e) {48// Can happen on some system configurations, and while increasing heap49// size would allow GC to keep up, it also makes it impractically hard50// to reproduce NPE issues that could arise when references are being51// cleared.5253// Do a System.gc() to not throw an OOME again in the jtreg wrapper.54System.gc();55}56}57}58596061