Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka: Cleanup of dynamically created actors necessary when they have finished?

Tags:

I have implemented an Actor system using Akka and its Java API UntypedActor. In it, one actor (type A) starts other actors (type B) dynamically on demand, using getContext().actorOf(...);. Those B actors will do some computation which A doesn't really care about anymore. But I'm wondering: is it necessary to clean up those actors of type B when they have finished? If so, how?

  • By having B actors call getContext().stop(getSelf()) when they're done?
  • By having B actors call getSelf().tell(Actors.poisonPill()); when they're done? [this is what I'm using now].
  • By doing nothing?
  • By ...?

The docs are not clear on this, or I have overlooked it. I have some basic knowledge of Scala, but the Akka sources aren't exactly entry-level stuff...

like image 968
Robert Petermeier Avatar asked Mar 05 '12 15:03

Robert Petermeier


1 Answers

What you are describing are single-purpose actors created per “request” (defined in the context of A), which handle a sequence of events and then are done, right? That is absolutely fine, and you are right to shut those down: if you don’t, they will accumulate over time and you run into a memory leak. The best way to do this is the first of the possibilities you mention (most direct), but the second is also okay.

A bit of background: actors are registered within their parent in order to be identifyable (e.g. needed in remoting but also in other places) and this registration keeps them from being garbage collected. OTOH, each parent has a right to access the children it created, hence no automatic termination (i.e. by Akka) makes sense, instead requiring explicit shutdown in user code.

like image 95
Roland Kuhn Avatar answered Sep 21 '22 20:09

Roland Kuhn