Unable to create native threads in MongoDB

Hello, I am working with a MongoDB dataBase and a Jhipster Java system based which enters smartCity information in the MongoDB . When the system is running for many hours I have the following error:

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 com.mongodb.connection.DefaultServerMonitor.start(DefaultServerMonitor.java:80)
	at com.mongodb.connection.DefaultServer.<init>(DefaultServer.java:73)
	at com.mongodb.connection.DefaultClusterableServerFactory.create(DefaultClusterableServerFactory.java:72)
at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:364)
at com.mongodb.connection.SingleServerCluster.<init>(SingleServerCluster.java:52)
at com.mongodb.connection.DefaultClusterFactory.createCluster(DefaultClusterFactory.java:181)
at com.mongodb.Mongo.createCluster(Mongo.java:738)
at com.mongodb.Mongo.createCluster(Mongo.java:732)
at com.mongodb.Mongo.<init>(Mongo.java:298)

The system is running in a ubuntu 18.04 server with the following parameters:

sistemasfiware@smartcity:~$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 38966
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 999999
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4999999
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

The mongodb runs in a docker
The system requirements are the following:
HardDisk : 19Gb
Ram 9GB

It is supposed that there can not be more threads in the system .

Any idea on how to solve the problem ?


Looks like the JVM is OOM not the host or mongodb.

You need to give the app some more heap to play with.

1 Like

Each instance of MongoClient does manage a set of threads, but usually that is not a problem. Please check whether you are creating more MongoClient instances than you need, and in particular whether you are calling close() on each one before it goes out of scope. Generally, if your application is connecting to a single MongoDB cluster as a single user, you only need to create a single MongoClient instance for the entire lifetime of your application.