2018-01-11 10:05:17 +00:00
/ *
2024-04-12 12:16:49 +00:00
* Copyright ( c ) 2018 , 2024 , Oracle and / or its affiliates . All rights reserved .
2018-01-11 10:05:17 +00:00
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
*
* This code is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License version 2 only , as
* published by the Free Software Foundation .
*
* This code is distributed in the hope that it will be useful , but WITHOUT
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* version 2 for more details ( a copy is included in the LICENSE file that
* accompanied this code ) .
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work ; if not , write to the Free Software Foundation ,
* Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 USA .
*
* Please contact Oracle , 500 Oracle Parkway , Redwood Shores , CA 94065 USA
* or visit www . oracle . com if you need additional information or have any
* questions .
* /
2019-01-28 14:53:56 +00:00
package gc.stress ;
2018-01-11 10:05:17 +00:00
/ *
* @test TestReclaimStringsLeaksMemory
* @bug 8180048
* @summary Ensure that during a Full GC interned string memory is reclaimed completely .
2020-07-10 18:52:35 +00:00
* @requires vm . gc = = " null "
* @requires ! vm . debug
2018-01-11 10:05:17 +00:00
* @library / test / lib
* @modules java . base / jdk . internal . misc
2020-04-29 14:08:31 +00:00
* @run driver gc . stress . TestReclaimStringsLeaksMemory
* @run driver gc . stress . TestReclaimStringsLeaksMemory - XX : + UseSerialGC
* @run driver gc . stress . TestReclaimStringsLeaksMemory - XX : + UseParallelGC
* @run driver gc . stress . TestReclaimStringsLeaksMemory - XX : + UseG1GC
2018-01-11 10:05:17 +00:00
* /
import java.util.Arrays ;
import java.util.ArrayList ;
import java.util.regex.Pattern ;
import java.util.regex.Matcher ;
import jdk.test.lib.Asserts ;
import jdk.test.lib.process.OutputAnalyzer ;
import jdk.test.lib.process.ProcessTools ;
public class TestReclaimStringsLeaksMemory {
2021-02-12 22:41:08 +00:00
// The amount of memory in B reserved in the "Symbol" category that indicates a memory leak for
2018-01-11 10:05:17 +00:00
// this test.
2021-02-12 22:41:08 +00:00
public static final int ReservedThreshold = 70000000 ;
2018-01-11 10:05:17 +00:00
public static void main ( String [ ] args ) throws Exception {
2019-02-08 11:09:41 +00:00
ArrayList < String > baseargs = new ArrayList < > ( Arrays . asList ( " -Xms256M " ,
" -Xmx256M " ,
2024-04-12 12:16:49 +00:00
" -Xlog:gc*,stringtable*=debug,oopstorage+blocks=debug:gc.log " ,
2019-02-08 11:09:41 +00:00
" -XX:NativeMemoryTracking=summary " ,
" -XX:+UnlockDiagnosticVMOptions " ,
" -XX:+PrintNMTStatistics " ) ) ;
2018-01-11 10:05:17 +00:00
baseargs . addAll ( Arrays . asList ( args ) ) ;
baseargs . add ( GCTest . class . getName ( ) ) ;
2024-01-05 09:12:24 +00:00
OutputAnalyzer output = ProcessTools . executeLimitedTestJava ( baseargs ) ;
verifySymbolMemoryUsageNotTooHigh ( output ) ;
2018-01-11 10:05:17 +00:00
}
private static void verifySymbolMemoryUsageNotTooHigh ( OutputAnalyzer output ) throws Exception {
String stdout = output . getStdout ( ) ;
System . out . println ( stdout ) ;
Pattern p = Pattern . compile ( " Symbol \\ (reserved=( \\ d*) " ) ;
Matcher m = p . matcher ( stdout ) ;
if ( ! m . find ( ) ) {
throw new RuntimeException ( " Could not find Symbol memory usage in NMT output " ) ;
}
int reserved = Integer . parseInt ( m . group ( 1 ) ) ;
2021-02-12 22:41:08 +00:00
Asserts . assertLT ( reserved , ReservedThreshold , " Reserved memory size is " + reserved + " B which is greater than or equal to " + ReservedThreshold + " B indicating a memory leak " ) ;
2018-01-11 10:05:17 +00:00
output . shouldHaveExitValue ( 0 ) ;
}
static class GCTest {
public static final String BaseName = " SomeRandomBaseString " ;
public static volatile String lastString ;
public static void main ( String [ ] args ) {
for ( int iterations = 0 ; iterations < 20 ; ) {
for ( int i = 0 ; i < 1000000 ; i + + ) {
lastString = ( BaseName + i ) . intern ( ) ;
}
if ( + + iterations % 5 = = 0 ) {
2019-06-11 11:31:47 +00:00
System . gc ( ) ;
2018-01-11 10:05:17 +00:00
}
}
2019-06-11 11:31:47 +00:00
// Do one last GC and sleep to give ServiceThread a chance to run.
System . out . println ( " One last gc " ) ;
System . gc ( ) ;
for ( int i = 0 ; i < 100 ; i + + ) {
try {
Thread . sleep ( 10 ) ;
} catch ( InterruptedException ex ) {
}
}
System . out . println ( " End of test " ) ;
2018-01-11 10:05:17 +00:00
}
}
}