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 conf = new HashMap();
public String getConf(String key) {
return conf.get(key);
}
}
NOTE
As I mentioned my JVM heap size, I'm requesting to answer something quantitative.
解决方案
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.