javaakkaactorobject-poolingmemory-pool

Akka actor message needs memory pool


I a new in java. I'm c++ programmer and nowadays study java for 2 months. Sorry for my pool English.

I have a question that if it needs memory pool or object pool for Akka actor model. I think if i send some message from one actor to one of the other actors, i have to allocate some heap memory(just like new Some String, or new Some BigInteger and other more..) And times on, the garbage collector will be got started(I'm not sure if it would be started) and it makes my application calculate slowly.

So I search for the way to make the memory-pool and failed(Java not supported memory pool). And I Could Make the object pool but in others project i did not find anybody use the object-pool with actor(also in Akka Homepage).

Is there any documents bout this topic in the akka hompage? Plz tell me the link or tell me the solution of my question.

Thanks.


Solution

  • Using an ArrayBlockingQueue to hold a pool of your objects should help,

    Here is the example code.

    TO create a pool and insert an instance of pooled object in it.

    BlockingQueue<YOURCLASS> queue = new ArrayBlockingQueue<YOURCLASS>(256);//Adjust 256 to your desired count. ArrayBlockingQueues size cannot be adjusted once it is initialized.
    
    
    queue.put(YOUROBJ); //This should be in your code that instanciates the pool
    

    and later where you need it (in your actor that receives message)

    YOURCLASS instanceName = queue.take();
    

    You might have to write some code around this to create and manage the pool. But this is the gist of it.