How to create actors efficiently?

I need to process 100 messages.

For that I am creating 100 actors and I am giving each message to each actor.

Or grouping messages and creating few actors to the messages

which approach is better?

It depends how important parallelism is for the processing of the messages. Each actor can only process one message at a time. Generally speaking, if you need the messages of the same type to be processed in parallel, you need to create more than one actor. If a single actor does not cause a bottleneck, meaning that it does not slow down other parts of the application, then one actor is all you need.

If you want to use multiple actors, you could use a Router to distribute the work evenly among a number of worker actors: https://doc.akka.io/docs/akka/current/routing.html

Thanks @ignatius

I am creating 100 actors one by one and I am killing every actors after completing its execution.

So If I kill the actor after its execution will it in turn clears the object space allocated to that actor in JVM?

So If I kill the actor after its execution will it in turn clears the object space allocated to that actor in JVM?

So, when an actor stops, yes it releases the JVM object as well. So the general answer is that, yes, if the actor goes away, the JVM object representing that actor goes away too.

However, you use the term “kill” which throws a minor wrinkle into things. Sending a kill message actually just forces an exception and doesn’t directly stop/release the actor. The default case for handling actor exceptions does stop the actor. But, depending on your supervisor strategy, might actually be handled in a different way.

You might find this StackExchange post helpful, which goes into the various ways of stopping an actor (stop/PoisonPill/kill) in more detail. https://stackoverflow.com/questions/13847963/akka-kill-vs-stop-vs-poison-pill

Thanks @davidogren

Could you please help me out with this one

https://discuss.lightbend.com/t/how-to-re-create-actors-when-the-node-gets-terminated/4690?u=anilkumble