我有一些使用WeakReference
的代码。我不得不实现一个丑陋的解决方法来解决这个问题,但我想知道仅仅添加一个this.synchronized
是否可以解决我的垃圾收集器问题。这是代码,问题出在函数create
中
/**
* The canonical map.
*/
var unicityTable = new WeakHashMap[CanonicalType, LightWeightWrapper[CanonicalType]] with SynchronizedMap[CanonicalType, LightWeightWrapper[CanonicalType]]
/**
* Create a new element from any object.
* @param from the object that will be used to generate a new instance of your canonical object.
*/
def create(from: FromType) = {
val newElt = makeFrom(from)
// I wonder if adding a this.synchronized here (and of course removing the test for garbage collection) might solve the problem more elegantly
val wrapper = unicityTable.get(newElt)
wrapper match {
case Some(w) => w._wrap.get match { // if the element is in the map
case Some(element) => element // the element was not garbage collected while we obtaining it
case None => // some how, the wrapped element was garbage collected before we get it, so we recreate it, and put it back
unicityTable.put(newElt, LightWeightWrapper(newElt))
newElt
}
case None => // the element is not in the map
unicityTable.put(newElt, LightWeightWrapper(newElt))
newElt
}
}
class LightWeightWrapper[T <: AnyRef] private (wrap: T) {
val _wrap = new WeakReference(wrap)
def wrapped = _wrap.get match {
case Some(w) => w
case None => // should never happen
throw new IllegalStateException
}
override lazy val hashCode = wrapped.hashCode
override def equals(o: Any): Boolean = o match {
case LightWeightWrapper(w) => w eq this.wrapped
case _ => false
}
}
所以问题是:垃圾收集是否在同步块的执行过程中停止?
否,垃圾收集仍然发生在synchronized
内部。此外,由于this
在堆栈上,因此它将不会被垃圾收集,因此您也不会得到任何帮助。
我不确定你在做什么,但我想也许你真的想使用SoftReference
而不是WeakReference
。只要所有硬引用都消失了,就可以收集仅由WeakReference
引用的对象。仅由SoftReference
引用的对象可以被保留,直到VM的内存不足为止。