4

If I set the maximum Java heap size -Xmx512m, when what is the possible maximum size for an single object?

Suppose, my application has only a single class and I'm creating exactly a single object.

Is there any approximate size limit for that object?

My class looks something like the one below:

public class BigSingleObj {
    //conf will contain thousand of String.
    private Map<String, String> conf = new HashMap<String, String>();
    public String getConf(String key) {
        return conf.get(key);
    }
}

NOTE

As I mentioned my JVM heap size, I'm requesting to answer something quantitative.

eddie
  • 1,254
  • 3
  • 15
  • 20
mmuzahid
  • 2,202
  • 23
  • 42
  • 3
    check this link : [enter link description here](http://stackoverflow.com/questions/3618122/does-an-object-in-java-have-any-memory-size-constraints) –  Mar 22 '16 at 12:39

3 Answers3

4

It's depend on JRE which you are using. Whether you will use x64 - more space your object will take.

You can use JOL (Java Object Layout) to calculate how large will be your object.

This can give you idea how much space it takes.

Igor Konoplyanko
  • 8,866
  • 6
  • 55
  • 94
4

The theoretical largest Java object you will be able to create (if you have a large enough heap) will be a long[] with 231 - 1 elements. That is 16Gb.

But the largest object you are going to be able to create for a given heap size is platform dependent. The JVM's heap is segmented into two or more "spaces", and any given object must fit inside a single space. An object that approaches the size of the entire heap is impossible.

The default sizes of the spaces are calculated by the memory manager, and I've never come across a simple formula that predicts what the sizes will be.

If you really want a quantitative estimate, then your best bet is to measure it experimentally. Write a simple program that allocates successively larger arrays until you get an OOME. But note that the answer you get will depend on your VM version and on the JVM command line options you use.


Suppose, my application has only a single class and I'm creating exactly a single object.

Your BigSingleObj class is not actually allocating a large object at all. The HashMap will start out small, and will automatically grow as entries are added to it.

Furthermore, the HashMap object is actually composed of a number of objects. The largest of these will be the hash array, but the total size of the HashMap will be 5 or more times the size of that array. And then you need to add the space occupied by the keys and values ... which in your case will depend on the string sizes.

All of this means that the size of largest allocatable object is not a useful predictor of how many entries you can store in your big HashMap.

Stephen C
  • 669,072
  • 92
  • 771
  • 1,162
  • Any sources to argument your answer? – adranale Mar 22 '16 at 13:56
  • Para 1: the JLS and JVMS. Para 2: various Oracle documents on Java garbage collection. Para 4: the javadoc for HashMap (and the source code). Para 5: the source code + background knowledge of typical JVM object representations. – Stephen C Mar 22 '16 at 14:03
0

Java Heap is limit for size of objects those you can have in system. check this link : Does an object in java have any memory size constraints?

Community
  • 1
  • 1