jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest2.java
changeset 20784 c0864e216d36
parent 5506 202f599c92aa
child 23010 6dadb192ad81
equal deleted inserted replaced
20783:c0806cb8d4ae 20784:c0864e216d36
    24 /*
    24 /*
    25  * Test low memory detection of non-heap memory pool.
    25  * Test low memory detection of non-heap memory pool.
    26  *
    26  *
    27  * The test set a listener to be notified when any of the non-heap pools
    27  * The test set a listener to be notified when any of the non-heap pools
    28  * exceed 80%. It then starts a thread that continuously loads classes.
    28  * exceed 80%. It then starts a thread that continuously loads classes.
    29  * In the HotSpot implementation this causes perm space to be consumed.
    29  * In the HotSpot implementation this causes metaspace to be consumed.
    30  * Test completes when we the notification is received or an OutOfMemory
    30  * Test completes when we the notification is received or an OutOfMemory
    31  * is generated.
    31  * is generated.
    32  */
    32  */
    33 
    33 
    34 import java.lang.management.*;
    34 import java.lang.management.*;
    98                 0x00, 0x00, 0x00, 0x00, 0x00 };
    98                 0x00, 0x00, 0x00, 0x00, 0x00 };
    99 
    99 
   100 
   100 
   101             // TestNNNNNN
   101             // TestNNNNNN
   102 
   102 
   103             String name = "Test" + Integer.toString(count++);
   103             int load_count = count++;
       
   104             if (load_count > 999999) {
       
   105                 // The test will create a corrupt class file if the count
       
   106                 // exceeds 999999. Fix the test if this exception is thrown.
       
   107                 throw new RuntimeException("Load count exceeded");
       
   108             }
       
   109 
       
   110             String name = "Test" + Integer.toString(load_count);
   104 
   111 
   105             byte value[];
   112             byte value[];
   106             try {
   113             try {
   107                 value = name.substring(4).getBytes("UTF-8");
   114                 value = name.substring(4).getBytes("UTF-8");
   108             } catch (java.io.UnsupportedEncodingException x) {
   115             } catch (java.io.UnsupportedEncodingException x) {
   131          * Run method for thread that continuously loads classes.
   138          * Run method for thread that continuously loads classes.
   132          *
   139          *
   133          * Note: Once the usage threshold has been exceeded the low memory
   140          * Note: Once the usage threshold has been exceeded the low memory
   134          * detector thread will attempt to deliver its notification - this can
   141          * detector thread will attempt to deliver its notification - this can
   135          * potentially create a race condition with this thread contining to
   142          * potentially create a race condition with this thread contining to
   136          * fill up perm space. To avoid the low memory detector getting an OutOfMemory
   143          * fill up metaspace. To avoid the low memory detector getting an
   137          * we throttle this thread once the threshold has been exceeded.
   144          * OutOfMemory we throttle this thread once the threshold has been
       
   145          * exceeded.
   138          */
   146          */
   139         public void run() {
   147         public void run() {
   140             List pools = ManagementFactory.getMemoryPoolMXBeans();
   148             List pools = ManagementFactory.getMemoryPoolMXBeans();
   141             boolean thresholdExceeded = false;
   149             boolean thresholdExceeded = false;
   142 
   150 
   178     public static void main(String args[]) {
   186     public static void main(String args[]) {
   179         ListIterator iter = ManagementFactory.getMemoryPoolMXBeans().listIterator();
   187         ListIterator iter = ManagementFactory.getMemoryPoolMXBeans().listIterator();
   180 
   188 
   181         // Set threshold of 80% of all NON_HEAP memory pools
   189         // Set threshold of 80% of all NON_HEAP memory pools
   182         // In the Hotspot implementation this means we should get a notification
   190         // In the Hotspot implementation this means we should get a notification
   183         // if the CodeCache or perm generation fills up.
   191         // if the CodeCache or metaspace fills up.
   184 
   192 
   185         while (iter.hasNext()) {
   193         while (iter.hasNext()) {
   186             MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
   194             MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
   187             if (p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) {
   195             if (p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) {
   188 
   196 
   189                 // set threshold
   197                 // set threshold
   190                 MemoryUsage mu = p.getUsage();
   198                 MemoryUsage mu = p.getUsage();
   191                 long threshold = (mu.getMax() * 80) / 100;
   199                 long max = mu.getMax();
       
   200                 if (max < 0) {
       
   201                     throw new RuntimeException("There is no maximum set for "
       
   202                             + p.getName() + " memory pool so the test is invalid");
       
   203                 }
       
   204                 long threshold = (max * 80) / 100;
   192 
   205 
   193                 p.setUsageThreshold(threshold);
   206                 p.setUsageThreshold(threshold);
   194 
   207 
   195                 System.out.println("Selected memory pool for low memory " +
   208                 System.out.println("Selected memory pool for low memory " +
   196                         "detection.");
   209                         "detection.");