1 22 package org.jboss.test; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.PrintStream ; 27 import java.lang.ref.WeakReference ; 28 import java.lang.reflect.Field ; 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 import java.util.ArrayList ; 32 import java.util.StringTokenizer ; 33 34 import org.jboss.profiler.jvmti.JVMTIInterface; 35 36 import junit.framework.TestCase; 37 38 44 public class JBossMemoryTestCase extends TestCase 45 { 46 public JBossMemoryTestCase() 47 { 48 super(); 49 } 50 51 public JBossMemoryTestCase(String name) 52 { 53 super(name); 54 } 55 56 protected static ClassLoader newClassLoader(Class anyUserClass) throws Exception 57 { 58 URL classLocation = anyUserClass.getProtectionDomain().getCodeSource().getLocation(); 59 StringTokenizer tokenString = new StringTokenizer (System.getProperty("java.class.path"),File.pathSeparator); 60 String pathIgnore = System.getProperty("java.home"); 61 if (pathIgnore==null) 62 { 63 pathIgnore = classLocation.toString(); 64 } 65 66 ArrayList <URL > urls = new ArrayList <URL >(); 67 while (tokenString.hasMoreElements()) 68 { 69 String value = tokenString.nextToken(); 70 URL itemLocation = new File (value).toURL(); 71 if (!itemLocation.equals(classLocation) && itemLocation.toString().indexOf(pathIgnore)>=0) 72 { 73 urls.add(itemLocation); 75 } 76 } 77 78 URL [] urlArray= urls.toArray(new URL [urls.size()]); 79 80 ClassLoader masterClassLoader = URLClassLoader.newInstance(urlArray,null); 81 82 83 ClassLoader appClassLoader = URLClassLoader.newInstance(new URL [] {classLocation},masterClassLoader); 84 85 return appClassLoader; 86 } 87 88 private static void resetObject(Object object, Field [] fields) 89 { 90 for (int fieldN=0;fieldN<fields.length;fieldN++) 91 { 92 try 93 { 94 System.out.print(" Field["+fieldN+"] "+fields[fieldN].getName()); 95 fields[fieldN].set(object,null); 96 System.out.println("...done"); 97 } 98 catch (Exception e) 99 { 100 101 System.out.println("...error " + e.getMessage()); 102 } 104 } 105 } 106 107 115 protected void checkUnload(WeakReference weakReferenceOnLoader, String className, String reportHTMLFile) throws Exception 116 { 117 JVMTIInterface jvmti = new JVMTIInterface(); 118 if (jvmti.isActive()) 119 { 120 jvmti.forceGC(); 121 Class clazz = jvmti.getClassByName(className); 122 if (clazz!=null) 123 { 124 jvmti.heapSnapshot("snapshot", "mem"); 125 clazz=null; 126 127 String report =jvmti.exploreClassReferences(className, 10, true, false, false, false, false); 128 129 System.out.println(report); 130 File outputfile = new File (reportHTMLFile); 131 FileOutputStream outfile = new FileOutputStream (outputfile); 132 PrintStream realoutput = new PrintStream (outfile); 133 realoutput.println(report); 134 realoutput.close(); 135 136 137 jvmti.forceGC(); 138 139 clazz = jvmti.getClassByName(className); 140 141 if (clazz==null) 142 { 143 System.out.println("Attention: After clearing every field on AspectManager, GC could release the classLoader"); 144 } 145 146 fail ("Class " + className + " still referenced. Look at report for more details"); 147 } 148 } 149 else 150 { 151 System.gc(); 152 Thread.sleep(1000); 153 } 154 assertNull("The classLoader is supposed to be released. Something is holding a reference. If you activate -agentlib:jbossAgent this testcase will generate a report with referenceHolders.",weakReferenceOnLoader.get()); 155 } 156 private Field [] getDeclaredFields(Class clazz) 157 { 158 ArrayList <Field > list = new ArrayList <Field >(); 159 for (Class classIteration = clazz;classIteration!=null;classIteration=classIteration.getSuperclass()) 160 { 161 Field [] fields = classIteration.getDeclaredFields(); 162 for (int i = 0; i < fields.length; i++) 163 { 164 fields[i].setAccessible(true); 165 list.add(fields[i]); 166 } 167 168 } 169 170 return list.toArray(new Field [list.size()]); 171 } 172 173 178 protected void clearEverySingleFieldOnInstances(String className) 179 { 180 System.out.println("Clearing " + className); 181 JVMTIInterface jvmti = new JVMTIInterface(); 182 Class classes[] = jvmti.getLoadedClasses(); 183 Object objects[] = null; 184 185 for (int i=0;i<classes.length;i++) 186 { 187 if (classes[i].getName().equals(className)) 188 { 189 Field fields[] = getDeclaredFields(classes[i]); 190 objects = jvmti.getAllObjects(classes[i]); 191 for (int j=0;j<objects.length;j++) 192 { 193 resetObject(objects[j], fields); 194 } 195 if (objects.length==0) 196 { 197 resetObject(null, fields); 198 } 199 } 200 } 201 classes= null; 202 objects = null; 203 } 204 205 206 } 207 | Popular Tags |