我想和scala.ref.WeakReference一起玩。但是,在尝试实现大事之前,我想尝试检查scala控制台中的行为。我尝试了一些但我无法获得被取消引用的对象。这是我的尝试之一:
> class A
defined class A
> class B(var value: A)
defined class B
> new B(new A)
res0: B = B@c8aeb3
> new scala.ref.WeakReference(res0.value)
res1: scala.ref.WeakReference[A] = scala.ref.WeakReferenceWithWrapper@16a5d72
> res0.value = new A
> res1.get // Here I hope to get None
res3: Option[A] = Some(A@135707c)
另一个尝试是由下面的oxbow_lakes。
我也尝试过徒劳地显式运行垃圾收集器(调用java.lang.System.gc
)。
有没有办法取消引用res1
的内容?
答案 0 :(得分:5)
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class A
defined class A
scala> class B(var value: A)
defined class B
scala> new B(new A)
res0: B = B@4223d9b
scala> new scala.ref.WeakReference(res0.value)
res1: scala.ref.WeakReference[A] = scala.ref.WeakReferenceWithWrapper@20eb607d
scala> res0.value = new A
scala> System gc
scala> res1 get
res3: Option[A] = None
顺便说一下,如果我将它作为没有显式System gc
的脚本运行,它也不会删除引用。所以对我而言,这不是REPL的问题,而只是弱引用和垃圾收集器的工作方式。
答案 1 :(得分:1)
使用scala -Xprint:parser
运行您的代码,即使在重新分配后,您也会看到保留var
的旧值。
我将在这里简化一些事情,然后运行两行代码:
var b=1
b=2
这就是Scala打印的内容:
scala> var b=1
[[syntax trees at end of parser]]// Scala source: <console>
package <empty> {
object line2$object extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
var b = 1 ///// YOUR CODE HERE
}
}
}
}
[[syntax trees at end of parser]]// Scala source: <console>
package <empty> { /////THIS IS AN object
/////SO PRESUMABLY IT CAN'T BE GC'ED
object RequestResult$line2$object extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
lazy val scala_repl_value = { /////THIS LAZY VAL
scala_repl_result; /////WILL REFERENCE THE OLD VALUE
line2$object.$iw.$iw.b /////EVEN AFTER YOU REASSIGN THE var
};
val scala_repl_result: String = {
line2$object.$iw.$iw;
"".$plus("b: Int = ").$plus(scala.runtime.ScalaRunTime.stringOf(line2$object.$iw.$iw.b))
}
}
}
b: Int = 1
scala> b=2
[[syntax trees at end of parser]]// Scala source: <console>
package <empty> {
object line3$object extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
import line2$object.$iw.$iw.b; ///// I DON'T THINK THIS (ORDINARILY ILLEGAL)
///// import CONTRIBUTES TO THE PROBLEM
object $iw extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
b = 2; /////YOUR CODE HERE
val synthvar$0 = b
}
}
}
}
[[syntax trees at end of parser]]// Scala source: <console>
package <empty> {
object RequestResult$line3$object extends scala.ScalaObject {
def <init>() = {
super.<init>();
()
};
lazy val scala_repl_value = {
scala_repl_result;
line3$object.$iw.$iw.synthvar$0
};
val scala_repl_result: String = {
line3$object.$iw.$iw;
"".$plus("b: Int = ").$plus(line3$object.$iw.$iw.synthvar$0).$plus("\012")
}
}
}
b: Int = 2
编辑:要添加到Debilski's answer,我认为以下解决方案可让您根据需要多次重新分配变量,而REPL不会引用旧值:
class A
class B{
var _value:A = new A
def value = _value
def pleaseUpdate( closure: B => Unit ) = closure(this)
}
将容器对象定义为:
val b=new B
每当你想更新里面的变量时:
b.pleaseUpdate( _._value = new A )
答案 2 :(得分:0)
我的想法是明确使用var
并设置为null
:
scala> var b = new B(new A)
b: B = B@45033fb5
scala> new scala.ref.WeakReference(b.value)
res0: scala.ref.WeakReference[A] = scala.ref.WeakReferenceWithWrapper@6a7be687
scala> b = null
b: B = null
scala> res0.get
res1: Option[A] = Some(A@79f71773)
但是,它仍然不起作用:REPL可能是在封面下做的事情,它保留了引用。因此,我不建议用它来测试Reference
s的使用。
答案 3 :(得分:0)
来自Java API文档:
调用gc方法表明了这一点 Java虚拟机花费精力 回收未使用的物品 为了使他们记忆 目前可以快速占用 重用。当控制从中返回时 方法调用,Java虚拟机 我已尽最大努力收回 所有丢弃物体的空间。
在我的轶事经验中,这意味着它很少会触发真正完整的收藏,特别是在可用内存方面没有压力的情况下。如果你对它施加压力,它将被收集。我认为你期望从GC中获得更多的确定性行为。
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> var s = new String("Hello, world!")
s: java.lang.String = Hello, world!
scala> import scala.ref.WeakReference
import scala.ref.WeakReference
scala> val w = new WeakReference(s)
w: scala.ref.WeakReference[java.lang.String] = scala.ref.WeakReferenceWithWrapper@663f3fbd
scala> s = null
s: java.lang.String = null
scala> Array.ofDim[Byte](1024*1024)
res1: Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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