簡體   English   中英

WeakReference和Scala REPL

[英]WeakReference and the Scala REPL

我想玩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的內容?

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的問題,而只是弱引用和垃圾收集器的工作方式。

與運行代碼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的答案 ,我認為以下解決方案將允許您根據需要多次重新分配變量,而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 )

我的想法是顯式使用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的使用。

來自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,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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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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