Miguel Barros
Miguel Barros

Reputation: 21

GRPC Java - Prevent Memory Leaks

I'm trying to prevent GRPC from using more than one thread per channel. To do this I setup a single thread executor with the following code:

for (int i = 0; i < 3 * numFaults + 1; i++) {
            //One thread for each channel
            ManagedChannel channel = NettyChannelBuilder
                    .forAddress(host, port + i + 1)
                    .usePlaintext()
                    .executor(Executors.newSingleThreadExecutor())
                    .build();

I then create an asyn stub for each channel.

However, this doesn't seem to be working as I am still spawning way too many threads in my program, eventually running out of memory.

Should i pass the executor to the stub instead? Or is there something fundamentally wrong.

Thanks in advance

Upvotes: 0

Views: 1789

Answers (1)

creamsoup
creamsoup

Reputation: 878

The additional spawned threads are probably EventLoop threads (by default it can create up to # of core threads). There is an option to provide EventLoopGroupin NettyChannelBuilder. To use this API you also need to set ChannelType. EventLoopGroup and Channel Type are from netty see linked javadoc for more details if you are interested.

you can use Epoll if on linux otherwise using NIO is current gRPC behavior. Below example is using NIO. All the network events are happening on the event loop thread, therefore using less threads can affect overall performance dramatically (depends on how channel is used). you can also consider using directExecutor and allocate more threads for the EventLoopGroup.

NioEventLoopGroup sharedEventLoopGroup = new NioEventLoopGroup(numThread);
ManagedChannel channel = NettyChannelBuilder
    .forAddress(host, port + i + 1)
    .usePlaintext()
    .channelType(NioSocketChannel.class)
    .eventLoopGroup(sharedEventLoopGroup)
    .executor(Executors.newSingleThreadExecutor())
    .build();

Upvotes: 1

Related Questions