内存泄漏,与使用POJO的Mongo Java连接引起了内存溢出。

huangapple 未分类评论49阅读模式
英文:

Memory leak with Mongo Java Connection with POJO cause OOM

问题

String dbURI = "mongodb://mongoji:pass@XXXX:27017,xxxx:27017,xxxx:27017/admin?replicaSet=rs0";
MongoClient mongoClient = new MongoClient(new MongoClientURI(dbURI));
DB db = mongoClient.getDB("test");
DBCollection collection = db.getCollection("read");
BasicDBObject data = new BasicDBObject();
data.append("DT", now.getTime());
collection.insert(data);
DBCollection collection2 = db.getCollection("sent");
BasicDBObject searchQuery = new BasicDBObject();
List<BasicDBObject> list = new ArrayList<BasicDBObject>();
list.add(new BasicDBObject("email", email));
list.add(new BasicDBObject("email2", email));// multiple

searchQuery.put("$and", list);
BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("Yes", "Y")); 
collection2.updateMulti(searchQuery, newDocument);

Over multiple transactions in the loop, I am seeing some memory eat up with the application which is not getting released and after some time my server getting halt and giving "bash fork Cannot allocate memory" error. I have tried ulimit:

* soft nofile 65535
* hard nofile 65535

but nothing happens, also getting below error:

java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:714)
    at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950)
    at java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1587)
    at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:334)
    at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:573)
    at java.util.concurrent.Executors$DelegatedScheduledExecutorService.scheduleAtFixedRate(Executors.java:735)
    at com.mongodb.connection.DefaultConnectionPool.createTimer(DefaultConnectionPool.java:316)
    at com.mongodb.connection.DefaultConnectionPool.<init>(DefaultConnectionPool.java:75)
    at com.mongodb.connection.DefaultClusterableServerFactory.create(DefaultClusterableServerFactory.java:60)
    at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:351)
    at com.mongodb.connection.MultiServerCluster.addServer(MultiServerCluster.java:297)
    at com.mongodb.connection.MultiServerCluster.addNewHosts(MultiServerCluster.java:360)
    at com.mongodb.connection.MultiServerCluster.ensureServers(MultiServerCluster.java:339)
    at com.mongodb.connection.MultiServerCluster.handleReplicaSetMemberChanged(MultiServerCluster.java:208)
    at com.mongodb.connection.MultiServerCluster.onChange(MultiServerCluster.java:158)
    at com.mongodb.connection.MultiServerCluster.access$100(MultiServerCluster.java:44)
    at com.mongodb.connection.MultiServerCluster$DefaultServerStateListener.stateChanged(MultiServerCluster.java:121)
    at com.mongodb.connection.DefaultServer$DefaultServerStateListener.stateChanged(DefaultServer.java:186)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.sendStateChangedEvent(DefaultServerMonitor.java:197)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:164)
    at java.lang.Thread.run(Thread.java:745)
英文:
String dbURI = &quot;mongodb://mongoji:pass@XXXX:27017,xxxx:27017,xxxx:27017/admin?replicaSet=rs0&quot;;
    MongoClient mongoClient = new MongoClient(new MongoClientURI(dbURI));
    DB db = mongoClient.getDB(&quot;test&quot;);
    DBCollection collection = db.getCollection(&quot;read&quot;);
BasicDBObject data = new BasicDBObject();
    data.append(&quot;DT&quot;, now.getTime());
collection.insert(data);
DBCollection collection2 = db.getCollection(&quot;sent&quot;);
BasicDBObject searchQuery = new BasicDBObject();
List&lt;BasicDBObject&gt; list = new ArrayList&lt;BasicDBObject&gt;();
list.add(new BasicDBObject(&quot;email&quot;, email));
list.add(new BasicDBObject(&quot;email2&quot;, email));// multiple

searchQuery.put(&quot;$and&quot;, list);
BasicDBObject newDocument = new BasicDBObject();
newDocument.append(&quot;$set&quot;, new BasicDBObject().append(&quot;Yes&quot;, &quot;Y&quot;)); 
collection2.updateMulti(searchQuery, newDocument);

Over multiple transactions in the loop, I am seeing some memory eat up with the application which is not getting released and after some time my server getting halt and giving "bash fork Cannot allocate memory" error
I have tried ulimit

* soft nofile 65535
  * hard nofile 65535

but nothing happens , also getting below error

> java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:714)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950)
at java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1587)
at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:334)
at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:573)
at java.util.concurrent.Executors$DelegatedScheduledExecutorService.scheduleAtFixedRate(Executors.java:735)
at com.mongodb.connection.DefaultConnectionPool.createTimer(DefaultConnectionPool.java:316)
at com.mongodb.connection.DefaultConnectionPool.<init>(DefaultConnectionPool.java:75)
at com.mongodb.connection.DefaultClusterableServerFactory.create(DefaultClusterableServerFactory.java:60)
at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:351)
at com.mongodb.connection.MultiServerCluster.addServer(MultiServerCluster.java:297)
at com.mongodb.connection.MultiServerCluster.addNewHosts(MultiServerCluster.java:360)
at com.mongodb.connection.MultiServerCluster.ensureServers(MultiServerCluster.java:339)
at com.mongodb.connection.MultiServerCluster.handleReplicaSetMemberChanged(MultiServerCluster.java:208)
at com.mongodb.connection.MultiServerCluster.onChange(MultiServerCluster.java:158)
at com.mongodb.connection.MultiServerCluster.access$100(MultiServerCluster.java:44)
at com.mongodb.connection.MultiServerCluster$DefaultServerStateListener.stateChanged(MultiServerCluster.java:121)
at com.mongodb.connection.DefaultServer$DefaultServerStateListener.stateChanged(DefaultServer.java:186)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.sendStateChangedEvent(DefaultServerMonitor.java:197)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:164)
at java.lang.Thread.run(Thread.java:745)

huangapple
  • 本文由 发表于 2020年5月5日 01:04:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/61597698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定