KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > ref > SoftReference

java.lang.ref
Class SoftReference<T>

java.lang.Object
  extended by java.lang.ref.Reference<T>
      extended by java.lang.ref.SoftReference<T>
See Also:
Top Examples, Source Code

public T get()
See Also:
Reference
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1702]Prevent object loitering
By just a geek on 2006/01/24 10:11:06  Rate
Soft references, like weak references, can help applications prevent object loitering by enlisting the aid of the garbage collector in making cache eviction decisions. Soft references are suitable for use only if the application can tolerate the loss of the softly referenced object.  
  
  
  
 


public SoftReference(Object referent)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SoftReference(Object referent,
                     ReferenceQueue q)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SoftReference(T referent)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SoftReference(T referent,
                     ReferenceQueue<? super T> q)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1701]Fixing memory leak with a soft reference
By Anonymous on 2006/01/24 10:06:02  Rate
// The following Class that exhibits "object loitering",  
 // A form of memory leak. 
 // 
 // It try to be clever and cache the buffer in an instance field to reduce memory churn.   
 // The cause of the problem in LeakyChecksum is that the buffer is logically local to  
 // the getFileChecksum (  )  operation, but its lifecycle has been artificially prolonged  
 // by promoting it to an instance field. As a result, the class has to manage the  
 // lifecycle of the buffer itself rather than letting the JVM do it. 
  
  
 // BAD CODE - DO NOT EMULATE 
 public class LeakyChecksum  {  
     private byte [  ]  byteArray; 
      
     public synchronized int getFileChecksum ( String fileName )   {  
         int len = getFileSize ( fileName ) ; 
         if  ( byteArray == null || byteArray.length  <  len )  
             byteArray = new byte [ len ] ; 
         readFileContents ( fileName, byteArray ) ; 
         // calculate checksum and return it 
      }  
  }  
  
  
  
 //Fixing LeakyChecksum with a soft reference 
 public class CachingChecksum  {  
     private SoftReference < byte [  ]  >  bufferRef; 
      
     public synchronized int getFileChecksum ( String fileName )   {  
         int len = getFileSize ( fileName ) ; 
         byte [  ]  byteArray = bufferRef.get (  ) ; 
         if  ( byteArray == null || byteArray.length  <  len )   {  
             byteArray = new byte [ len ] ; 
             bufferRef.set ( byteArray ) ; 
          }  
         readFileContents ( fileName, byteArray ) ; 
         // calculate checksum and return it 
      }  
  }  
  

Popular Tags