java 堆对象,堆中Java最大的对象大小

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值