Play Framework 1.2.5: OutOfMemoryError: Java heap space in the gaming infrastructure

I get OutOfMemoryError when querying MongoDb with 400k entries. I have a custom collection of about 400 thousand records. When I try to extract all users (to dump them in an elastic search), I get an OutOfMemoryError error.

I went through this link and added jvm.memory = -Xms64m -Xmx1024m to application.config, but still this is an exception.

Here is my stack trace -

 OutOfMemoryError occured : Java heap space play.exceptions.JavaExecutionException: Java heap space at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237) at Invocation.HTTP Request(Play!) Caused by: java.lang.OutOfMemoryError: Java heap space at java.util.HashMap.<init>(HashMap.java:209) at java.util.LinkedHashMap.<init>(LinkedHashMap.java:181) at org.bson.BasicBSONObject.<init>(BasicBSONObject.java:45) at com.mongodb.BasicDBObject.<init>(BasicDBObject.java:42) at com.mongodb.DefaultDBCallback._create(DefaultDBCallback.java:124) at com.mongodb.DefaultDBCallback.create(DefaultDBCallback.java:87) at org.bson.BasicBSONCallback.objectStart(BasicBSONCallback.java:68) at com.mongodb.DefaultDBCallback.objectStart(DefaultDBCallback.java:63) at org.bson.BasicBSONCallback.objectStart(BasicBSONCallback.java:63) at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:206) at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:197) at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:207) at org.bson.BasicBSONDecoder._decode(BasicBSONDecoder.java:80) at org.bson.BasicBSONDecoder.decode(BasicBSONDecoder.java:58) at com.mongodb.DefaultDBDecoder.decode(DefaultDBDecoder.java:56) at com.mongodb.Response.<init>(Response.java:66) at com.mongodb.DBPort.go(DBPort.java:128) at com.mongodb.DBPort.call(DBPort.java:79) at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:218) at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:189) at com.mongodb.DBApiLayer$Result._advance(DBApiLayer.java:452) at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:418) at com.mongodb.DBCursor._hasNext(DBCursor.java:503) at com.mongodb.DBCursor.hasNext(DBCursor.java:523) at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1520) at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1332) at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1318) at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:504) at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:499) at com.salambc.service.ProfileService.getUsers(ProfileService.java:895) at controllers.Admin.index(Admin.java:56) 
+4
source share
2 answers

You are out of memory because you are loading too many objects into memory.

Loading so many objects into Java memory is not a good practice. You cannot scale if many users try to fulfill this use case.

Allowing more memory for your Java process may solve your current error, but is not a solution in the long run.

Try a different design in which you can do some calculations in your persistent storage or load data using small pieces of data (100 or 1000), since loading a piece of data leads to predicted memory consumption.

+1
source

Please check the application, you have run out of memory because you have many large objects in memory. I met the same problem with this ... Because all the variables / functions in Play! static , so some of them cannot be released by GC.

Please check how you use TemplateLoader, RenderArgs, Jobs ... And try to reduce the game pool and the task pool (if you configured), the best is 14 game pools for 4 GB of RAM (in my case).

+4
source

All Articles