This page is no longer maintained — Please continue to the home page at www.scala-lang.org

[OT] a simple garbage collection question

2 replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

hi,

this is a bit OT as it's a generic java VM question i guess.... but maybe someone smart can answer it quickly.

if i have this

class Test {
private val anoObject = new AnyRef
}

val x = new Test

is anoObject threatened to be garbage collected while i still have references to x? i.e. does the VM "know" that anoObject is unreachable? do i need to make it's scope 'protected' to save it from garbage collection?

thanks, -sciss-

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: [OT] a simple garbage collection question


On Mon, Mar 15, 2010 at 7:57 AM, Sciss <contact@sciss.de> wrote:
hi,

this is a bit OT as it's a generic java VM question i guess.... but maybe someone smart can answer it quickly.

if i have this

class Test {
       private val anoObject = new AnyRef
}

val x = new Test

is anoObject threatened to be garbage collected while i still have references to x?

No.  Because x references anoObject, as long as there's a reference to x or anoObject, anoObject will not be GCed.
 
i.e. does the VM "know" that anoObject is unreachable? do i need to make it's scope 'protected' to save it from garbage collection?

thanks, -sciss-




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: [OT] a simple garbage collection question

thanks!

Am 15.03.2010 um 16:00 schrieb David Pollak:

>
>
> On Mon, Mar 15, 2010 at 7:57 AM, Sciss wrote:
> hi,
>
> this is a bit OT as it's a generic java VM question i guess.... but maybe someone smart can answer it quickly.
>
> if i have this
>
> class Test {
> private val anoObject = new AnyRef
> }
>
> val x = new Test
>
> is anoObject threatened to be garbage collected while i still have references to x?
>
> No. Because x references anoObject, as long as there's a reference to x or anoObject, anoObject will not be GCed.
>
> i.e. does the VM "know" that anoObject is unreachable? do i need to make it's scope 'protected' to save it from garbage collection?
>
> thanks, -sciss-
>
>
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland