test/java/lang/management/MemoryMXBean/MemoryTest.java

Print this page
rev 5700 : 7195557: NPG: Unexpected number of memory pools
Summary: Update management tests to work with a VM without a permanent generation memory pool
Reviewed-by:


  41 import java.util.*;
  42 
  43 public class MemoryTest {
  44     private static boolean testFailed = false;
  45     private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
  46     private static final int HEAP = 0;
  47     private static final int NONHEAP = 1;
  48     private static final int NUM_TYPES = 2;
  49 
  50     // WARNING: if the number of pools changes in the future,
  51     // this test needs to be modified to handle different version of VMs.
  52 
  53     // Hotspot VM 1.5 expected to have
  54     //   heap memory pools     = 3 (Eden, Survivor spaces, Old gen)
  55     //   non-heap memory pools = 2 (Perm gen, Code cache)
  56     //                           or 4 if Class Sharing is enabled.
  57     // Number of memory managers = 3
  58     // They are: Copy/Scavenger + MSC + CodeCache manager
  59     // (or equivalent for other collectors)
  60     // Number of GC memory managers = 2
  61     private static int[] expectedMinNumPools = {3, 2};
  62     private static int[] expectedMaxNumPools = {3, 4};



  63     private static int expectedNumGCMgrs = 2;
  64     private static int expectedNumMgrs = expectedNumGCMgrs + 1;
  65     private static String[] types = { "heap", "non-heap" };
  66 
  67     public static void main(String args[]) throws Exception {
  68         Integer value = new Integer(args[0]);
  69         expectedNumGCMgrs = value.intValue();
  70         expectedNumMgrs = expectedNumGCMgrs + 1;
  71 
  72         checkMemoryPools();
  73         checkMemoryManagers();
  74         if (testFailed)
  75             throw new RuntimeException("TEST FAILED.");
  76 
  77         System.out.println("Test passed.");
  78 
  79     }
  80 
  81     private static void checkMemoryPools() throws Exception {
  82         List pools = ManagementFactory.getMemoryPoolMXBeans();

  83 
  84         int[] numPools = new int[NUM_TYPES];
  85         for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
  86             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
  87             if (pool.getType() == MemoryType.HEAP) {
  88                 numPools[HEAP]++;
  89             }
  90             if (pool.getType() == MemoryType.NON_HEAP) {
  91                 numPools[NONHEAP]++;
  92             }










  93         }
  94 
  95         // Check the number of Memory pools
  96         for (int i = 0; i < NUM_TYPES; i++) {
  97             if (numPools[i] < expectedMinNumPools[i] ||
  98                     numPools[i] > expectedMaxNumPools[i]) {
  99                 throw new RuntimeException("TEST FAILED: " +
 100                     "Number of " + types[i] + " pools = " + numPools[i] +
 101                     " but expected <= " + expectedMaxNumPools[i] +
 102                     " and >= " + expectedMinNumPools[i]);
 103             }
 104         }
 105     }
 106 
 107     private static void checkMemoryManagers() throws Exception {
 108         List mgrs = ManagementFactory.getMemoryManagerMXBeans();
 109 
 110         int numGCMgr = 0;
 111 
 112         // Check the number of Memory Managers




  41 import java.util.*;
  42 
  43 public class MemoryTest {
  44     private static boolean testFailed = false;
  45     private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
  46     private static final int HEAP = 0;
  47     private static final int NONHEAP = 1;
  48     private static final int NUM_TYPES = 2;
  49 
  50     // WARNING: if the number of pools changes in the future,
  51     // this test needs to be modified to handle different version of VMs.
  52 
  53     // Hotspot VM 1.5 expected to have
  54     //   heap memory pools     = 3 (Eden, Survivor spaces, Old gen)
  55     //   non-heap memory pools = 2 (Perm gen, Code cache)
  56     //                           or 4 if Class Sharing is enabled.
  57     // Number of memory managers = 3
  58     // They are: Copy/Scavenger + MSC + CodeCache manager
  59     // (or equivalent for other collectors)
  60     // Number of GC memory managers = 2
  61 
  62     // Hotspot VM 1.8+ after perm gen removal is expected to have only
  63     // one non-heap memory pool
  64     private static int[] expectedMinNumPools = {3, 1};
  65     private static int[] expectedMaxNumPools = {3, 1};
  66     private static int expectedNumGCMgrs = 2;
  67     private static int expectedNumMgrs = expectedNumGCMgrs + 1;
  68     private static String[] types = { "heap", "non-heap" };
  69 
  70     public static void main(String args[]) throws Exception {
  71         Integer value = new Integer(args[0]);
  72         expectedNumGCMgrs = value.intValue();
  73         expectedNumMgrs = expectedNumGCMgrs + 1;
  74 
  75         checkMemoryPools();
  76         checkMemoryManagers();
  77         if (testFailed)
  78             throw new RuntimeException("TEST FAILED.");
  79 
  80         System.out.println("Test passed.");
  81 
  82     }
  83 
  84     private static void checkMemoryPools() throws Exception {
  85         List pools = ManagementFactory.getMemoryPoolMXBeans();
  86         boolean hasPerm = false;
  87 
  88         int[] numPools = new int[NUM_TYPES];
  89         for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
  90             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
  91             if (pool.getType() == MemoryType.HEAP) {
  92                 numPools[HEAP]++;
  93             }
  94             if (pool.getType() == MemoryType.NON_HEAP) {
  95                 numPools[NONHEAP]++;
  96             }
  97             if (pool.getName().toLowerCase().contains("perm")) {
  98                 hasPerm = true;
  99             }
 100         }
 101 
 102         if (hasPerm) {
 103             // If the VM has perm gen there will be between 2 and 4 non heap
 104             // pools (4 if class data sharing is used)
 105             expectedMinNumPools[NONHEAP] = 2;
 106             expectedMaxNumPools[NONHEAP] = 4;
 107         }
 108 
 109         // Check the number of Memory pools
 110         for (int i = 0; i < NUM_TYPES; i++) {
 111             if (numPools[i] < expectedMinNumPools[i] ||
 112                     numPools[i] > expectedMaxNumPools[i]) {
 113                 throw new RuntimeException("TEST FAILED: " +
 114                     "Number of " + types[i] + " pools = " + numPools[i] +
 115                     " but expected <= " + expectedMaxNumPools[i] +
 116                     " and >= " + expectedMinNumPools[i]);
 117             }
 118         }
 119     }
 120 
 121     private static void checkMemoryManagers() throws Exception {
 122         List mgrs = ManagementFactory.getMemoryManagerMXBeans();
 123 
 124         int numGCMgr = 0;
 125 
 126         // Check the number of Memory Managers