0

My app is constantly running out of memory. I am using a static instance of most Classes to perform operations that have no state, but I am worried that this is the cause of my memory leaks.

My tech stack is heroku/playframework 1.2.7/mongodb (compose.io)

The basic pattern I have is:

public class Product{

  public productName;
  public productDesc;
  public productPrice;

  private static final Product INSTANCE = new Product();

  public static Product instance() {
      return INSTANCE;
  }

  // an example (not actual) method is
  public List<Product> listAllProducts(String brand, String category){
       //not the actual DB code
       Db.connect().find(Product.class).listAllItems(brand, category);   
  }

}

Which I use like this

 List<Product> products = Product.instance().listAllProducts("hugo-boss", "jeans");

Is there something fundamentally wrong with this code that will cause memory leaks in a web app with a handful - but not a huge number of concurrent users?

Thanks for your help.

UPDATE Doing heroku config:set _JAVA_OPTIONS="-Xms256m -Xmx384m -Xss512k -XX:+UseCompressedOops" didnt' work for now.

I enabled one of the add-ons that gives memory info in the logs, here's what I'm getting:

2015-01-09T16:18:58.614435+00:00 heroku[web.3]: source=web.3 dyno=heroku.18312286.ad920471-c3dd-4d5d-a55b-28ea79905c14 sample#load_avg_1m=3.65
2015-01-09T16:18:58.614631+00:00 heroku[web.3]: source=web.3 dyno=heroku.18312286.ad920471-c3dd-4d5d-a55b-28ea79905c14 sample#memory_total=439.86MB sample#memory_rss=438.86MB sample#memory_cache=1.00MB sample#memory_swap=0.00MB sample#memory_pgpgin=138042pages sample#memory_pgpgout=25438pages
2015-01-09T16:19:10.683077+00:00 heroku[web.2]: source=web.2 dyno=heroku.18312286.910cbbef-7613-4daf-a856-230e08618cc7 sample#load_avg_1m=1.90 sample#load_avg_5m=0.86
2015-01-09T16:19:10.683364+00:00 heroku[web.2]: source=web.2 dyno=heroku.18312286.910cbbef-7613-4daf-a856-230e08618cc7 sample#memory_total=367.73MB sample#memory_rss=365.02MB sample#memory_cache=2.71MB sample#memory_swap=0.00MB sample#memory_pgpgin=141344pages sample#memory_pgpgout=47205pages
Ankur
  • 50,282
  • 110
  • 242
  • 312
  • If the answer is no, this should be fine, then please feel free to tell me that as well. – Ankur Jan 09 '15 at 16:06
  • Perhaps this might help: http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java – user2570465 Jan 09 '15 at 16:11
  • Try starting up the JVM console and then run your app: you should be able to see where the memory is going. – davek Jan 09 '15 at 16:14
  • You can tell the JVM to dump the heap when it runs out of memory and load that in a tool to identify what is leaking – Amir Afghani Jan 09 '15 at 16:14
  • @m-z thanks, I've done that ... if in 5 or so min the memory issues don't start then I think that means it's worked. Will let you know ASAP – Ankur Jan 09 '15 at 16:15
  • @m-z that doesn't seem to work, I'll update the question – Ankur Jan 09 '15 at 16:18

1 Answers1

0

There is no leak here. At least, not in the code you posted. But, depending on how large your list is, and how large heap you are running with, you might be running OOM simply because you are trying to create several copies of the "allProducts" list simultaneously.

Perhaps, it is better to cache the list of "all products", and let all clients use that single instance?

Dima
  • 39,570
  • 6
  • 44
  • 70