⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 softreferencecache.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.//// $Id: SoftReferenceCache.java,v 1.2 2004/03/21 21:05:51 leomekenkamp Exp $package org.ozoneDB.core.storage;import java.lang.ref.ReferenceQueue;import java.lang.ref.SoftReference;import java.util.Properties;/** * <p>Caches objects which may be garbage collected at any time by the Java virtual * machine garbage collector. Uses soft references to hold objects in memory.</p> * <p>Note: due to bug 4640743  * <a href="http://developer.java.sun.com/developer/bugParade/bugs/4640743.html">found here</a> * it is currently not possible to 'override' the garbage collector by saving * a reference to an instance from its finalize statement to prevent that * object from being garbage collected. If you do try to create extra references * to an object that was only softly reachable (as with all objects that are * only referenced from this cache) after the garbage collector has decided to * throw the object away and run its finalizer, you will experience a major * memory leak.<br/> * Unfortunately, it seems that a simple self-reference through 'this' already * causes this bug to appear. Use of a finalize method on objects that are to * be put in this cache is _very_ _bad_ _news_.</p> * <p>bug 4190589 is a rfe that has been 'in progress' for a couple of years (!) * now, and could also solve the finalization problem. Same goes for 4227324.</p> * <p>Javadocs state that a garbage collector is encouraged to first collect * the softly reachable objects that are oldest. As we have no way of finding * out if this is really implemented within the current JVM, and we also have no * way to tell the gc which objects we prefer to keep in memory, and (due to bug  * 4640743) we cannot override the gc-s decisions, we just have to cross our * thumbs and hope that the JVM gc implementation is a smart one. The Sun * implementation uses timestamps on soft references, directly linked with the * number of times the gc has run, and it seems smart enough.</p> * * @author <a href="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a> * @version $Id: SoftReferenceCache.java,v 1.2 2004/03/21 21:05:51 leomekenkamp Exp $ */public class SoftReferenceCache extends AbstractReferenceCache {        private static class ObjectReference extends SoftReference implements KeyedReference {                /**         * same key as the key passed in <code>put(key, value)</code>         */        private Object key;                ObjectReference(Object key, Object value, ReferenceQueue q) {            super(value, q);            this.key = key;        }                public Object getKey() {            return key;        }                public String toString() {            return key.toString();        }                public int hashCode() {            return key.hashCode();        }                public boolean equals(Object o) {            if (!(o instanceof ObjectReference)) {                return false;            } else {                ObjectReference otherRef = (ObjectReference) o;                return otherRef.getKey().equals(getKey());            }        }            } // ObjectReference        public SoftReferenceCache(Properties properties, String prefix) {        super(properties, prefix);    }        protected KeyedReference createKeyedReference(Object key, Object value, ReferenceQueue referenceQueue) {        return new ObjectReference(key, value, referenceQueue);    }    // DEBUG AND BUGTEST CODE ONLY FOLLOWING; not needed for normal operation        public SoftReferenceCache() {    }        private static class Item {                public Object key;        public byte[] filling = new byte[1000];                Item(Object key) {            this.key = key;        }                public String toString() {            return key.toString();        }    }        public static void main(String args[]) {        SoftReferenceCache cache = new SoftReferenceCache();        int last = 0;        for (int i = 0; i < 10000000; i++) {            Object key = new Integer(i);            Item item = new Item(key);            cache.put(key, item);//            for (int j = 0; j < 20; j++) {//                cache.get(new Integer(j));//            }            if (cache.size() != last + 1) {                System.out.println(cache.size() + ", last: " + last);            }            last = cache.size();                        }        System.out.println("completed successfully");    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -