Re: help with weak references
Rex Kerr <[email protected]> Mon, 4 Jan 2016 13:16:17 -0800
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAP_xLa1fe6GaLGz7t3THhhMnx86_JYaP1sT0_Jg3p+DQoCyZYw@mail.gmail.com> |
Weak references cleared in GC cycles, but pretty aggressively (as if they were regular garbage). The expectation is that they're gone after a single full GC (but maybe not finalized etc). Soft references tend to hang around unless the GC isn't freeing up enough. The precise behavior is not specified in the javadocs (save that soft references are guaranteed to be cleared before an OutOfMemory error is thrown). --Rex On Mon, Jan 4, 2016 at 12:49 PM, Oliver Ruebenacker <[email protected]> wrote: > > Hello, > > Are weak references expected to be collected eagerly? > > Best, Oliver > > On Mon, Jan 4, 2016 at 6:02 AM, <[email protected]> wrote: > >> Weak references don't seem to be eagerly collected as the following >> script illustrates. Why? >> >> --------------------- >> Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java >> 1.8.0_60). >> Type in expressions to have them evaluated. >> Type :help for more information. >> >> scala> import scala.ref.WeakReference >> import scala.ref.WeakReference >> >> scala> val rq = new scala.ref.ReferenceQueue[List[Int]] >> rq: scala.ref.ReferenceQueue[List[Int]] = >> java.lang.ref.ReferenceQueue@ba2f4ec >> >> scala> case class WR(a: List[Int]) { val wr = new WeakReference(a, rq)} >> defined class WR >> >> scala> val wr = WR((0 until 5).toList) >> wr: WR = WR(List(0, 1, 2, 3, 4)) >> >> scala> val x = wr.wr.get >> x: Option[List[Int]] = Some(List(0, 1, 2, 3, 4)) >> >> scala> val x = 3 // re-define x >> x: Int = 3 >> >> scala> System.gc() >> >> scala> val x = wr.wr.get >> x: Option[List[Int]] = Some(List(0, 1, 2, 3, 4)) // <<------ should >> have been None >> >> scala> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "scala-language" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Oliver Ruebenacker > Senior Software Engineer, Diabetes Portal > <http://www.type2diabetesgenetics.org/>, Broad Institute > <http://www.broadinstitute.org/> > > -- > You received this message because you are subscribed to the Google Groups > "scala-language" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "scala-language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.