< prev index next >

test/hotspot/jtreg/gc/g1/TestHumongousShrinkHeap.java

8217332: JTREG: Clean up, use generics instead of raw types

34  * @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10                                              
35  * -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc                                                
36  * gc.g1.TestHumongousShrinkHeap                                                                                           
37  */                                                                                                                        
38 
39 import com.sun.management.HotSpotDiagnosticMXBean;                                                                         
40 import java.lang.management.ManagementFactory;                                                                             
41 import java.lang.management.MemoryUsage;                                                                                   
42 import java.util.ArrayList;                                                                                                
43 import java.util.List;                                                                                                     
44 import java.text.NumberFormat;                                                                                             
45 import gc.testlibrary.Helpers;                                                                                             
46 import static jdk.test.lib.Asserts.*;                                                                                      
47 import jtreg.SkippedException;                                                                                             
48 
49 public class TestHumongousShrinkHeap {                                                                                     
50 
51     public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";                                              
52     public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";                                              
53 
54     private static final List<List<byte[]>> garbage = new ArrayList();                                                     
55     private static final int REGION_SIZE = 1024 * 1024; // 1M                                                              
56     private static final int LISTS_COUNT = 10;                                                                             
57     private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE);                                                   
58 
59     private static final long TOTAL_MEMORY = Runtime.getRuntime().totalMemory();                                           
60     private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();                                               
61 
62     private static final int HUMON_COUNT = (int) ((TOTAL_MEMORY / HUMON_SIZE) / LISTS_COUNT);                              
63 
64     public static void main(String[] args) {                                                                               
65         if (HUMON_COUNT == 0) {                                                                                            
66             throw new SkippedException("Heap is too small");                                                               
67         }                                                                                                                  
68 
69         if (TOTAL_MEMORY + REGION_SIZE * HUMON_COUNT > MAX_MEMORY) {                                                       
70             throw new SkippedException("Initial heap size is to close to max heap size.");                                 
71         }                                                                                                                  
72 
73         System.out.format("Running with %s initial heap size of %s maximum heap size. "                                    

34  * @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10
35  * -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc
36  * gc.g1.TestHumongousShrinkHeap
37  */
38 
39 import com.sun.management.HotSpotDiagnosticMXBean;
40 import java.lang.management.ManagementFactory;
41 import java.lang.management.MemoryUsage;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.text.NumberFormat;
45 import gc.testlibrary.Helpers;
46 import static jdk.test.lib.Asserts.*;
47 import jtreg.SkippedException;
48 
49 public class TestHumongousShrinkHeap {
50 
51     public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
52     public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
53 
54     private static final List<List<byte[]>> garbage = new ArrayList<>();
55     private static final int REGION_SIZE = 1024 * 1024; // 1M
56     private static final int LISTS_COUNT = 10;
57     private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE);
58 
59     private static final long TOTAL_MEMORY = Runtime.getRuntime().totalMemory();
60     private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();
61 
62     private static final int HUMON_COUNT = (int) ((TOTAL_MEMORY / HUMON_SIZE) / LISTS_COUNT);
63 
64     public static void main(String[] args) {
65         if (HUMON_COUNT == 0) {
66             throw new SkippedException("Heap is too small");
67         }
68 
69         if (TOTAL_MEMORY + REGION_SIZE * HUMON_COUNT > MAX_MEMORY) {
70             throw new SkippedException("Initial heap size is to close to max heap size.");
71         }
72 
73         System.out.format("Running with %s initial heap size of %s maximum heap size. "

90 
91         free();                                                                                                            
92         MemoryUsagePrinter.printMemoryUsage("free");                                                                       
93         MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();                                     
94 
95         assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(                                        
96                 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"           
97                 + "%s = %s%n%s = %s",                                                                                      
98                 MIN_FREE_RATIO_FLAG_NAME,                                                                                  
99                 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)                                         
100                     .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),                                                     
101                 MAX_FREE_RATIO_FLAG_NAME,                                                                                  
102                 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)                                         
103                     .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()                                                      
104         ));                                                                                                                
105     }                                                                                                                      
106 
107     private void allocate() {                                                                                              
108 
109         for (int i = 0; i < LISTS_COUNT; i++) {                                                                            
110             List<byte[]> stuff = new ArrayList();                                                                          
111             allocateList(stuff, HUMON_COUNT, HUMON_SIZE);                                                                  
112             MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1));                                                     
113             garbage.add(stuff);                                                                                            
114         }                                                                                                                  
115     }                                                                                                                      
116 
117     private void free() {                                                                                                  
118         // do not free last one list                                                                                       
119         garbage.subList(0, garbage.size() - 1).clear();                                                                    
120 
121         // do not free last one element from last list                                                                     
122         List stuff = garbage.get(garbage.size() - 1);                                                                      
123         stuff.subList(0, stuff.size() - 1).clear();                                                                        
124         System.gc();                                                                                                       
125     }                                                                                                                      
126 
127     private static void allocateList(List garbage, int count, int size) {                                                  
128         for (int i = 0; i < count; i++) {                                                                                  
129             garbage.add(new byte[size]);                                                                                   
130         }                                                                                                                  
131     }                                                                                                                      
132 }                                                                                                                          
133 
134 /**                                                                                                                        
135  * Prints memory usage to standard output                                                                                  
136  */                                                                                                                        
137 class MemoryUsagePrinter {                                                                                                 
138 
139     public static final NumberFormat NF = Helpers.numberFormatter();                                                       
140 
141     public static void printMemoryUsage(String label) {                                                                    
142         MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();                                   
143         float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();                                       
144         System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",                             
145                 label,                                                                                                     
146                 NF.format(memusage.getInit()),                                                                             

90 
91         free();
92         MemoryUsagePrinter.printMemoryUsage("free");
93         MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
94 
95         assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(
96                 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
97                 + "%s = %s%n%s = %s",
98                 MIN_FREE_RATIO_FLAG_NAME,
99                 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
100                     .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
101                 MAX_FREE_RATIO_FLAG_NAME,
102                 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
103                     .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
104         ));
105     }
106 
107     private void allocate() {
108 
109         for (int i = 0; i < LISTS_COUNT; i++) {
110             List<byte[]> stuff = new ArrayList<>();
111             allocateList(stuff, HUMON_COUNT, HUMON_SIZE);
112             MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1));
113             garbage.add(stuff);
114         }
115     }
116 
117     private void free() {
118         // do not free last one list
119         garbage.subList(0, garbage.size() - 1).clear();
120 
121         // do not free last one element from last list
122         List<byte[]> stuff = garbage.get(garbage.size() - 1);
123         stuff.subList(0, stuff.size() - 1).clear();
124         System.gc();
125     }
126 
127     private static void allocateList(List<byte[]> garbage, int count, int size) {
128         for (int i = 0; i < count; i++) {
129             garbage.add(new byte[size]);
130         }
131     }
132 }
133 
134 /**
135  * Prints memory usage to standard output
136  */
137 class MemoryUsagePrinter {
138 
139     public static final NumberFormat NF = Helpers.numberFormatter();
140 
141     public static void printMemoryUsage(String label) {
142         MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
143         float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
144         System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
145                 label,
146                 NF.format(memusage.getInit()),
< prev index next >