KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > SoftReferenceCache


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: SoftReferenceCache.java,v 1.2 2004/03/21 21:05:51 leomekenkamp Exp $
7

8 package org.ozoneDB.core.storage;
9
10 import java.lang.ref.ReferenceQueue JavaDoc;
11 import java.lang.ref.SoftReference JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 /**
15  * <p>Caches objects which may be garbage collected at any time by the Java virtual
16  * machine garbage collector. Uses soft references to hold objects in memory.</p>
17  * <p>Note: due to bug 4640743
18  * <a HREF="http://developer.java.sun.com/developer/bugParade/bugs/4640743.html">found here</a>
19  * it is currently not possible to 'override' the garbage collector by saving
20  * a reference to an instance from its finalize statement to prevent that
21  * object from being garbage collected. If you do try to create extra references
22  * to an object that was only softly reachable (as with all objects that are
23  * only referenced from this cache) after the garbage collector has decided to
24  * throw the object away and run its finalizer, you will experience a major
25  * memory leak.<br/>
26  * Unfortunately, it seems that a simple self-reference through 'this' already
27  * causes this bug to appear. Use of a finalize method on objects that are to
28  * be put in this cache is _very_ _bad_ _news_.</p>
29  * <p>bug 4190589 is a rfe that has been 'in progress' for a couple of years (!)
30  * now, and could also solve the finalization problem. Same goes for 4227324.</p>
31  * <p>Javadocs state that a garbage collector is encouraged to first collect
32  * the softly reachable objects that are oldest. As we have no way of finding
33  * out if this is really implemented within the current JVM, and we also have no
34  * way to tell the gc which objects we prefer to keep in memory, and (due to bug
35  * 4640743) we cannot override the gc-s decisions, we just have to cross our
36  * thumbs and hope that the JVM gc implementation is a smart one. The Sun
37  * implementation uses timestamps on soft references, directly linked with the
38  * number of times the gc has run, and it seems smart enough.</p>
39  *
40  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
41  * @version $Id: SoftReferenceCache.java,v 1.2 2004/03/21 21:05:51 leomekenkamp Exp $
42  */

43 public class SoftReferenceCache extends AbstractReferenceCache {
44     
45     private static class ObjectReference extends SoftReference JavaDoc implements KeyedReference {
46         
47         /**
48          * same key as the key passed in <code>put(key, value)</code>
49          */

50         private Object JavaDoc key;
51         
52         ObjectReference(Object JavaDoc key, Object JavaDoc value, ReferenceQueue JavaDoc q) {
53             super(value, q);
54             this.key = key;
55         }
56         
57         public Object JavaDoc getKey() {
58             return key;
59         }
60         
61         public String JavaDoc toString() {
62             return key.toString();
63         }
64         
65         public int hashCode() {
66             return key.hashCode();
67         }
68         
69         public boolean equals(Object JavaDoc o) {
70             if (!(o instanceof ObjectReference)) {
71                 return false;
72             } else {
73                 ObjectReference otherRef = (ObjectReference) o;
74                 return otherRef.getKey().equals(getKey());
75             }
76         }
77         
78     } // ObjectReference
79

80     public SoftReferenceCache(Properties JavaDoc properties, String JavaDoc prefix) {
81         super(properties, prefix);
82     }
83     
84     protected KeyedReference createKeyedReference(Object JavaDoc key, Object JavaDoc value, ReferenceQueue JavaDoc referenceQueue) {
85         return new ObjectReference(key, value, referenceQueue);
86     }
87     
88 // DEBUG AND BUGTEST CODE ONLY FOLLOWING; not needed for normal operation
89

90     public SoftReferenceCache() {
91     }
92     
93     private static class Item {
94         
95         public Object JavaDoc key;
96         public byte[] filling = new byte[1000];
97         
98         Item(Object JavaDoc key) {
99             this.key = key;
100         }
101         
102         public String JavaDoc toString() {
103             return key.toString();
104         }
105     }
106     
107     public static void main(String JavaDoc args[]) {
108         SoftReferenceCache cache = new SoftReferenceCache();
109         int last = 0;
110         for (int i = 0; i < 10000000; i++) {
111             Object JavaDoc key = new Integer JavaDoc(i);
112             Item item = new Item(key);
113             cache.put(key, item);
114 // for (int j = 0; j < 20; j++) {
115
// cache.get(new Integer(j));
116
// }
117
if (cache.size() != last + 1) {
118                 System.out.println(cache.size() + ", last: " + last);
119             }
120             last = cache.size();
121                 
122         }
123         System.out.println("completed successfully");
124     }
125     
126 }
127
Popular Tags