commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Stansberry <bes_commons_...@yahoo.com>
Subject Re: [logging] Static references to Log objects
Date Tue, 05 Apr 2005 15:50:46 GMT

--- Brian Stansberry <bes_commons_dev@yahoo.com>
wrote:
<snip>
> 
> This line of thought led me to reconsider an idea
> I'd
> rejected a couple weeks back related to the JCL
> memory
> leak problem.  Basically the leak occurs if
> LogFactoryImpl is defined by a parent classloader
> while the class of one of its Log instances is
> defined
> by a child loader.  The chain of references from the
> Log instance to its classloader will prevent gc of
> the
> child loader on undeploy.
> 
> This chain could (potentially) be broken if
> LogFactoryImpl only held WeakReferences to its Log
> instances as follows:
> 
>   public Log getInstance(String name) throws
> LogConfigurationException {
> -
> -    Log instance = (Log) instances.get(name);
> -    if (instance == null) {
> -      instance = newInstance(name);
> -      instances.put(name, instance);
> +        
> +    Log instance = null;
> +    WeakReference ref = (WeakReference)
> instances.get(name);
> +    if (ref != null) {
> +      instance = (Log) instances.get(name);
> +      if (instance == null) {
> +        instance = newInstance(name);
> +        instances.put(name, new
> WeakReference(instance));
> +      }
>    }
>    return (instance);

Oops.  Try:

  public Log getInstance(String name) throws
LogConfigurationException {
-
-    Log instance = (Log) instances.get(name);
-    if (instance == null) {
-      instance = newInstance(name);
-      instances.put(name, instance);
+        
+    Log instance = null;
+    WeakReference ref = (WeakReference)
instances.get(name);
+    if (ref != null) {
+      instance = (Log) instances.get(name);
+    }
+    if (instance == null) {
+      instance = newInstance(name);
+      instances.put(name, new
WeakReference(instance));
+    }
   return (instance);


Brian


		
__________________________________ 
Yahoo! Messenger 
Show us what our next emoticon should look like. Join the fun. 
http://www.advision.webevents.yahoo.com/emoticontest

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message