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

后端 未结 4 1851
甜味超标
甜味超标 2021-02-07 01:33

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

相关标签:
4条回答
  • 2021-02-07 02:13

    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.

    0 讨论(0)
  • 2021-02-07 02:15

    In addition to Roland Kuhn's answer, rather than create a new actor for every request, you could create a predefined set of actors that share the same dispatcher, or you can use a router that distributes requests to a pool of actors.

    The Balancing Pool Router, for example, allows you to have a fixed set of actors of a particular type share the same mailbox:

    akka.actor.deployment {
      /parent/router9 {
        router = balancing-pool
        nr-of-instances = 5
      }
    }
    

    Read the documentation on dispatchers and on routing for further detail.

    0 讨论(0)
  • 2021-02-07 02:25

    I was profiling(visualvm) one of the sample cluster application from AKKA documentation and I see garbage collection cleaning up the per request actors during every GC. Unable to completely understand the recommendation of explicitly killing the actor after use. My actorsystem and actors are managed by SPRING IOC container and I use spring extension in-direct actor-producer to create actors. The "aggregator" actor is getting garbage collected on every GC, i did monitor the # of instances in visual VM.

    @Component
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class StatsService extends AbstractActor {
    
        private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
    
        @Autowired
        private ActorSystem actorSystem;
    
        private ActorRef workerRouter;
    
        @Override
        public void preStart() throws Exception {
            System.out.println("Creating Router" + this.getClass().getCanonicalName());
           workerRouter = getContext().actorOf(SPRING_PRO.get(actorSystem)
                .props("statsWorker").withRouter(new FromConfig()), "workerRouter"); 
            super.preStart();
        }
    
        @Override
        public Receive createReceive() {
            return receiveBuilder()
                .match(StatsJob.class, job -> !job.getText().isEmpty(), job -> {
                    final String[] words = job.getText().split(" ");
                    final ActorRef replyTo = sender();
                    final ActorRef aggregator = getContext().actorOf(SPRING_PRO.get(actorSystem)
                        .props("statsAggregator", words.length, replyTo));
    
                    for (final String word : words) {
                        workerRouter.tell(new ConsistentHashableEnvelope(word, word),
                            aggregator);
                    }
                })
                .build();
        }
    }
    
    0 讨论(0)
  • 2021-02-07 02:26

    Actors by default do not consume much memory. If the application intends to use actor b later on, you can keep them alive. If not, you can shut them down via poisonpill. As long your actors are not holding resources, leaving an actor should be fine.

    0 讨论(0)
提交回复
热议问题