-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Classloader leak - EnumType class
PostPosted: Wed Feb 04, 2009 8:48 am 
Newbie

Joined: Wed Feb 04, 2009 6:48 am
Posts: 1
Location: Warsaw/Poland
I'm running my project on JBoss 4.2. Hibernate is used outside project ear (for some reason I don't know). I've been investigating classloader memory leaks (as described in article http://blogs.sun.com/fkieviet/entry/classloader_leaks_the_dreaded_java) - the problem is that project classes are not garbage collected after undeployment.
I've found that EnumType Hibernate class has a static field:
private static Map<Class, Object[]> enumValues
which holds references to my enum types, which all have reference to the classloader of most of my classes. As Hibernate is outside ear, EnumType class is not garbage collected after project undeployment and because of that static field all my classes are not garbage collected as well, which is a memory leak.
Is that a bug in that class ? Should that Map hold weak references or sth ?
Any ideas how I can deal with that problem ?


Top
 Profile  
 
 Post subject: Re: Classloader leak - EnumType class
PostPosted: Tue Jul 21, 2009 6:08 pm 
Newbie

Joined: Tue Jul 21, 2009 6:05 pm
Posts: 1
It's a bug;
the field must be java.util.WeakHashMap

>>I'm running my project on JBoss 4.2. Hibernate is used outside project ear (for some reason I don't know)
it's in the lib directory of jboss w/ the deployer MBean

To deal w/ the issuel

in some class of yours add
static{
java.lang.reflect.Field f = EnumType.class.getDeclaredField("enumValues");
f.setAccessible(true);
Map m = (Map) f.get(null);
if (m instanceof HashMap){
Map weak = new WeakHashMap(m);
f.set(null, weak);
}
}

or recompile hibernate replace new HashMap w/ new WeakHashMap
----
edit: upon reflection i recalled simple WeakHashMap will not do since the value has reference to the class, thus the it will not be cleared
Instead of WeakHashMap, use something like that (entryset(), values()), containsValue(Object) will not work though)
new WeakHashMap(){
public Object get(Object key){
WeakReference w = (WeakReference) super.get(key):
if (w==null) return null;
return w.get();
}
public Object put(Object key, Object value){
WeakReference w = (WeakReference) super.put(key, new WeakReference(value)):
if (w==null) return null;
return w.get();
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.