KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > JBossMemoryTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.lang.ref.WeakReference JavaDoc;
28 import java.lang.reflect.Field JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import org.jboss.profiler.jvmti.JVMTIInterface;
35
36 import junit.framework.TestCase;
37
38 /**
39  *
40  * A Regular MemoryTestCase not using any container side.
41  *
42  * @author <a HREF="clebert.suconic@jboss.com">Clebert Suconic</a>
43  */

44 public class JBossMemoryTestCase extends TestCase
45 {
46    public JBossMemoryTestCase()
47    {
48       super();
49    }
50
51    public JBossMemoryTestCase(String JavaDoc name)
52    {
53       super(name);
54    }
55
56    protected static ClassLoader JavaDoc newClassLoader(Class JavaDoc anyUserClass) throws Exception JavaDoc
57    {
58        URL JavaDoc classLocation = anyUserClass.getProtectionDomain().getCodeSource().getLocation();
59        StringTokenizer JavaDoc tokenString = new StringTokenizer JavaDoc(System.getProperty("java.class.path"),File.pathSeparator);
60        String JavaDoc pathIgnore = System.getProperty("java.home");
61        if (pathIgnore==null)
62        {
63           pathIgnore = classLocation.toString();
64        }
65        
66        ArrayList JavaDoc<URL JavaDoc> urls = new ArrayList JavaDoc<URL JavaDoc>();
67        while (tokenString.hasMoreElements())
68        {
69           String JavaDoc value = tokenString.nextToken();
70           URL JavaDoc itemLocation = new File JavaDoc(value).toURL();
71           if (!itemLocation.equals(classLocation) && itemLocation.toString().indexOf(pathIgnore)>=0)
72           {
73              //System.out.println("Location:" + itemLocation);
74
urls.add(itemLocation);
75           }
76        }
77        
78        URL JavaDoc[] urlArray= urls.toArray(new URL JavaDoc[urls.size()]);
79        
80        ClassLoader JavaDoc masterClassLoader = URLClassLoader.newInstance(urlArray,null);
81        
82        
83        ClassLoader JavaDoc appClassLoader = URLClassLoader.newInstance(new URL JavaDoc[] {classLocation},masterClassLoader);
84        
85        return appClassLoader;
86     }
87    
88    private static void resetObject(Object JavaDoc object, Field JavaDoc[] 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 JavaDoc e)
99          {
100    
101             System.out.println("...error " + e.getMessage());
102             //System.out.println("Exception " + e.getMessage() + " happened during setField");
103
}
104       }
105    }
106
107    /**
108     * If you started your class with -agentlib:jbossAgent in case of leakage (if className still loaded) a file (reportFile) will be created, and a heapSnapshot(./snapshot,mem)
109     *
110     * @param weakReferenceOnLoader A weakReference to the created ClassLoader. If there is no references to this classLoader this reference will be cleared
111     * @param className The class name supposed to be unloade.
112     * @param reportHTMLFile the report file
113     * @throws Exception
114     */

115    protected void checkUnload(WeakReference JavaDoc weakReferenceOnLoader, String JavaDoc className, String JavaDoc reportHTMLFile) throws Exception JavaDoc
116    {
117       JVMTIInterface jvmti = new JVMTIInterface();
118       if (jvmti.isActive())
119       {
120          jvmti.forceGC();
121          Class JavaDoc clazz = jvmti.getClassByName(className);
122          if (clazz!=null)
123          {
124             jvmti.heapSnapshot("snapshot", "mem");
125             clazz=null;
126             
127             String JavaDoc report =jvmti.exploreClassReferences(className, 10, true, false, false, false, false);
128             
129             System.out.println(report);
130             File JavaDoc outputfile = new File JavaDoc(reportHTMLFile);
131             FileOutputStream JavaDoc outfile = new FileOutputStream JavaDoc(outputfile);
132             PrintStream JavaDoc realoutput = new PrintStream JavaDoc(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 JavaDoc[] getDeclaredFields(Class JavaDoc clazz)
157    {
158       ArrayList JavaDoc<Field JavaDoc> list = new ArrayList JavaDoc<Field JavaDoc>();
159       for (Class JavaDoc classIteration = clazz;classIteration!=null;classIteration=classIteration.getSuperclass())
160       {
161           Field JavaDoc[] 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 JavaDoc[list.size()]);
171    }
172
173    /**
174     * This method could be used for debug purposes
175     *
176     * @param className the class name
177     */

178    protected void clearEverySingleFieldOnInstances(String JavaDoc className)
179    {
180       System.out.println("Clearing " + className);
181       JVMTIInterface jvmti = new JVMTIInterface();
182       Class JavaDoc classes[] = jvmti.getLoadedClasses();
183       Object JavaDoc objects[] = null;
184       
185       for (int i=0;i<classes.length;i++)
186       {
187          if (classes[i].getName().equals(className))
188          {
189             Field JavaDoc 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