2

What is the heap size that we need to allocate for a normal server application? What it should be for an application as big as Facebook, Twitter, etc?

Or is there a way we can increase the heap size dynamically for an application so that we don't have to worry about the heap allocation?

Shrikant Kakani
  • 1,511
  • 2
  • 17
  • 37
  • 4
    There is no "normal" server application. Each application is different from the others, and it also depends on the number of users using the application. An application as big as Facebook or Twitter is distributed on many, many different servers. A single JVM is not sufficient to handle the load. To know what you should use, make load tests, and measure. – JB Nizet Mar 14 '14 at 12:42
  • Possible duplicate: http://stackoverflow.com/questions/1651225/how-to-choose-the-jvm-heap-size – Christophe Roussy Mar 14 '14 at 13:06

2 Answers2

3

The Java heap size is a security feature: It is there so a Java program can't simply take over the machine by allocating endless amounts of memory.

That's why almost no Java VM has a "increase heap as necessary" option.

As such, it's almost impossible to tell in advance how much memory a Java application or server will need. You will have to run it with real production data to find out. There are tools to help you there but usually, you just wait for the OutOfMemoryError, make sure it's not due to a bug and then increase the heap (professionals assume that it's not a but and simply increase the heap, then fix the bug and then leave the heap size as it is :-) ).

PS: on this site there is a question "How can I give Java code more than 144 GB of RAM?"

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
2

That is a too general question and it depends on the number of objects created, their life span and their sizes.

For example:

  • If you're dealing with big data structure, let's say you load images to memory - you'll need a larger heap. But then you need to consider - do these images long-lived, or they're loaded, manipulated and die. If they die quickly you'll probably have to change the ratio between old and young generation (assuming the old generation contains only "regular" objects)

  • You need to make sure the size of the young generation is large enough to not let any object get to the older the generation on the one hand, but not too large which will cause longer computation to count live objects

  • You can adjust the ratio of the survivor space. If almost nothing makes it to the survivor space then it should be relatively small.

There are all sorts of things to think about and there's no one-size-fit all solution.

Here's a lecture I like that can get you into this whole idea of GC tuning: Tuning the JVM

After you see the video and get the general idea, I would recommend a good article that I recently read to get deeper into the GC and JVM tuning: Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning

Avi
  • 21,182
  • 26
  • 82
  • 121