OutOfMemoryError: unable to create new native thread] Error

I have an endpoint which calls every 5 seconds from front-end to query database and get data like below:

 public CompletionStage<Result> getData(Long frequency, Long minDate, Long maxDate) {
        Executor dbEc = HttpExecution.fromThread((Executor) dbExecutionContext);
        CompletableFuture<String> cf = new CompletableFuture<>();
        return supplyAsync(() -> {
            return new dataDao().getNSampledReadings(
                    "test1",
                    frequency,
                    minDate,
                    maxDate);
        }, dbEc)
                .thenApply(rows -> {
                    return ok(rows);
                });

    }

dbExecutionContext is a custom execution context class created by me like below.

package controllers;

import akka.actor.ActorSystem;
import com.google.inject.Inject;
import play.api.libs.concurrent.CustomExecutionContext;

public class DBExecutionContext extends CustomExecutionContext {

    @Inject
    public DBExecutionContext(ActorSystem actorSystem) {
        // uses a custom thread pool defined in application.conf
        super(actorSystem, "blocking-io-dispatcher");
    }
}

Also in application.conf

blocking-io-dispatcher {
  type = Dispatcher
  executor = "thread-pool-executor"
  thread-pool-executor {
    fixed-pool-size = 32
  }
  throughput = 1
}

Server was running for few hours without any issues and front-end was calling above end-point continuously every 5 seconds then it gave following error.

play.api.UnexpectedException: Unexpected exception[CompletionException: java.lang.OutOfMemoryError: unable to create new native thread]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:328)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:251)
at play.filters.cors.AbstractCORSPolicy$$anonfun$1.applyOrElse(AbstractCORSPolicy.scala:127)
at play.filters.cors.AbstractCORSPolicy$$anonfun$1.applyOrElse(AbstractCORSPolicy.scala:125)
at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:453)
at play.api.libs.streams.Execution$trampoline$.executeScheduled(Execution.scala:109)
at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:71)
at scala.concurrent.impl.Promise$Transformation.submitWithValue(Promise.scala:392)
at scala.concurrent.impl.Promise$DefaultPromise.submitWithValue(Promise.scala:302)
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete0(Promise.scala:249)
Caused by: java.util.concurrent.CompletionException: java.lang.OutOfMemoryError: unable to create new native thread
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1606)
… 5 common frames omitted
Caused by: java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:717)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:957)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1367)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at java.util.concurrent.Executors$DelegatedExecutorService.submit(Executors.java:678)
at com.rethinkdb.net.Connection.connect(Connection.java:105)
at com.rethinkdb.net.Connection.reconnect(Connection.java:94)
at com.rethinkdb.net.Connection.reconnect(Connection.java:83)
at com.rethinkdb.net.Connection$Builder.connect(Connection.java:422)

What is the reason i’m getting this error?

Hard to tell. I think it’s not related to Play, but to your code or a 3rd party library.

The interesting part is

Caused by: java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:717)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:957)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1367)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at java.util.concurrent.Executors$DelegatedExecutorService.submit(Executors.java:678)
at com.rethinkdb.net.Connection.connect(Connection.java:105)
at com.rethinkdb.net.Connection.reconnect(Connection.java:94)
at com.rethinkdb.net.Connection.reconnect(Connection.java:83)
at com.rethinkdb.net.Connection$Builder.connect(Connection.java:422)

I never really used RethinkDB, however lets look at the source of you exception:


So here a new thread is created each time. I guess such a thread is never shut down and you keep allocating more and more threads until you run out of memory. Maybe you have to close some connections or something similiar within your supplyAsync lambda?
Again, I don’t think it’s related to Play, but to your code or to RethinkDB, so asking there would probably make more sense.

1 Like

I think you are on point. This could be the exact reason why i’m getting this error and it explains the error message “unable to create new native thread”.

Thanks