tags:

views:

127

answers:

3

Scenario: I have this code:

class MyActor extends Actor {
   def act() {
     react { 
       case Message() => println("hi")
     }
   }
}

def meth() {
  val a = new MyActor
  a.start
  a ! Message()
}

is the MyActor instance garbage collected? if not, how do i make sure it is? if I create an ad-hoc actor (with the 'actor' method), is that actor GCed?

+1  A: 

Have you tried adding a finalize method to see whether it is? I think the answer here is that the actors subsystem behaves no different to how you would expect it to: it does not cache any reference to your actor except in a thread-local for the duration of processing.

I would therefore expect that your actor is a candidate for collection (assuming the subsystem correctly clears out the ThreadLocal reference after the actor has processed the message which it does indeed appear to do in the Reaction.run method).

oxbow_lakes
+1  A: 

Memory leaks with the standard Actor library has lead to other Actor implementations. This was the reason for David Pollak and Jonas Boner's Actor library for Lift that you can read much more about here: http://blog.lostlake.org/index.php?/archives/96-Migrating-from-Scala-Actors-to-Lift-Actors.html

michaelg
+1  A: 

This thread on the scala-user mailing list is relevant.

There Phillip Haller mentions using a particular scheduler (available in Scala 2.8) to enable termination of an Actor before garbage collection, either on a global or per-actor basis.

seh