堆中Java最大的对象大小 [英] Java largest object size in Heap

查看:302
本文介绍了堆中Java最大的对象大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我设置最大Java堆大小 -Xmx512m ,那么单个对象可能的最大大小是多少?

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);
    }
}

注意

正如我提到的JVM堆大小,我要求回答一些定量的问题。

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

推荐答案

您将能够创建的理论上最大的Java对象(如果您有足够大的堆)将是 long [] ,其中2 31 - 1个元素。这是16Gb。

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.

但是,对于给定的堆大小,您将能够创建的最大对象取决于平台。 JVM的堆被分段为两个或更多空格,并且任何给定对象必须适合单个空间。一个接近整个堆大小的对象是不可能的。

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.

如果你真的想要一个定量估计,那么你最好的选择是通过实验来衡量它。编写一个简单的程序,分配连续更大的数组,直到你得到一个OOME。但请注意,您得到的答案取决于您的VM版本以及您使用的JVM命令行选项。

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.

你的 BigSingleObj 类实际上根本没有分配大对象。 HashMap将从小开始,并在条目添加到它时自动增长。

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.

此外, HashMap 对象实际上由许多对象组成。其中最大的是哈希数组,但 HashMap 的总大小将是该数组大小的5倍或更多倍。然后你需要添加键和值占用的空间...在你的情况下将取决于字符串大小。

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.

所有这些意味着大小最大可分配对象的大小并不是您可以在大 HashMap 中存储多少条目的有用预测器。

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.

这篇关于堆中Java最大的对象大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆