简体   繁体   English

WeakReference和Scala REPL

[英]WeakReference and the Scala REPL

I would like to play with scala.ref.WeakReference . 我想玩scala.ref.WeakReference However, before trying to implement the big thing, I would like to try to check the behavior in scala console. 但是,在尝试实现大事之前,我想尝试检查scala控制台中的行为。 I tried a few thing but I was unable to obtained my object to be dereferenced. 我尝试了一些但我无法获得被取消引用的对象。 Here is one of my tries: 这是我的尝试之一:

> 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)

Another try is given by oxbow_lakes below. 另一个尝试是由下面的oxbow_lakes给出的。

I've also tried to explicitly run the garbage collector (calling java.lang.System.gc ) in vain. 我也试图显式运行垃圾收集器(调用java.lang.System.gc )是徒劳的。

Is there any way to dereference the content of res1 ? 有没有办法取消引用res1的内容?

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

By the way, if I run it as a script without an explicit System gc , it won't delete the reference either. 顺便说一句,如果我在没有显式System gc情况下将其作为脚本运行,它也不会删除引用。 So to me it's not an issue of the REPL but just the way that weak references and the garbage collector work. 所以对我而言,这不是REPL的问题,而只是弱引用和垃圾收集器的工作方式。

Run your code with scala -Xprint:parser and you'll see what's keeping old value of a var referenced even after you reassign it. 与运行代码scala -Xprint:parser ,你会看到什么养的旧值var重新分配它甚至后参考。

I'm going to simplify things here, and just run two lines of code: 我将在这里简化一些事情,并运行两行代码:

var b=1
b=2

And this is what Scala prints: 这就是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

EDIT: To add on to Debilski's answer , I think the following solution will let you reassign the variable as many times as you want without the REPL keeping a reference to the old value: 编辑:要添加到Debilski的答案 ,我认为以下解决方案将允许您根据需要多次重新分配变量,而REPL不会引用旧值:

class A
class B{
   var _value:A = new A
   def value = _value
   def pleaseUpdate( closure: B => Unit ) = closure(this)
}

Define your container object as: 将容器对象定义为:

val b=new B

And whenever you want to update the variable that's inside it: 每当你想更新里面的变量时:

b.pleaseUpdate( _._value = new A )

My idea was to use a var explicitly and set to null : 我的想法是显式使用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)

It still does not work, though: the REPL is presumably doing stuff under the covers which keeps hold of the reference. 但它仍然不起作用:REPL可能是在封面下做的东西,它保留了引用。 I would therefore not advise using it to test the use of Reference s. 因此,我不建议使用它来测试Reference的使用。

From Java API docs: 来自Java API文档:

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. 调用gc方法表明Java虚拟机花费了大量精力来回收未使用的对象,以使其当前占用的内存可用于快速重用。 When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects. 当控制从方法调用返回时,Java虚拟机已尽最大努力从所有丢弃的对象中回收空间。

In my anecdotal experience this means it rarely triggers a truly complete collection, especially if there is no pressure in terms of available memory. 在我的轶事经验中,这意味着它很少会触发真正完整的集合,特别是在可用内存方面没有压力的情况下。 If you put pressure on it, it will be collected. 如果你对它施加压力,它将被收集。 I think you're expecting for more deterministic behavior out of the GC than it provides. 我认为你期望从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, 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, ...
scala> Array.ofDim[Byte](1024*1024)
res2: Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
scala> Array.ofDim[Byte](1024*1024)
res6: 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, 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, ...
scala> System.gc

scala> var g = w.get
g: Option[java.lang.String] = None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM