char []的最大大小是多少?

时间:2010-11-30 16:26:57

标签: java

IN JAVA

char []的最大大小是多少?我有炭[5,000,000]吗?

java中的数组是否由连续的内存块组成?

6 个答案:

答案 0 :(得分:5)

由于索引是基于int的,因此数组的最大大小应为Integer.MAX_VALUE

显然,另一个限制是应用程序可用的内存量:)

答案 1 :(得分:2)

最大尺寸取决于架构。

在C中,数组包含连续的内存块。你可以拥有一个你想要的数组,只要它适合RAM(包括基于磁盘的虚拟内存)。有一个例外:声明为局部变量的数组在堆栈上分配,这是非常小的。 PC上多线程应用程序中堆栈的典型大小为1兆字节。如果你想要一个大数组,你最好将它创建为全局变量,或者动态分配它(使用malloc())。

在Java中,数组是堆分配的(使用new)。它们是否是连续的是“没有你的业务”:Java的本质是关于屏蔽程序员的具体实现细节(但实际上,在大多数Java虚拟机上,数组是连续的)。创建Java数组并使用表示为int类型的值进行索引;因此,Java数组不能超过大约2亿个元素,即使机器有更多的RAM:int也不能容纳更大的值。 5百万是小菜一碟,我经常分配比这更大的数组。

答案 2 :(得分:1)

JVM的堆大小是限制。

as @wildcodeforjava提到int是启动数组时的paremeter

所以哪个更少。

答案 3 :(得分:0)

是的,是的,据我所知,我认为java中的最大值约为2,000,000,000 2 * 10e9

答案 4 :(得分:0)

事实有点复杂。最大字符数组分配大小确实是 Integer.MAX_VALUE ,但这并不意味着您实际上可以分配该大小的 char[] 。 VM 还根据初始配置的堆 -Xmx 和当前空闲堆动态限制分配大小。因此,即使有足够的可用堆来分配数组并且请求的大小为 < Integer.MAX_VALUE,您也可能会遇到 OutOfMemoryError。

我编写了一个小程序来测试最大分配大小。

public class TestRamAllocation
{
   public static void main(String[] args)
   {
      char[] dummy = new char[47483647];

      determineArrayAllocationSizeVMLimit();
   }

   private static int determineArrayAllocationSizeVMLimit()

   {

      //System.out.println("Integer.MAX_VALUE=" + Integer.MAX_VALUE);

      int lastSize = Integer.MAX_VALUE - 10;
      int stepSize = Integer.MAX_VALUE / 100 * 1; // 1 % steps

      System.gc();

      while (lastSize > 0) {
         try {
            @SuppressWarnings("unused")
            char[] array = new char[lastSize];
            // allocation succeeded without OOM
            array = null;
            long xmx = Runtime.getRuntime().maxMemory();
            int approxFileSizeinMB_oneByte = lastSize / 1024 / 1024;      
            int approxFileSizeinMB_fourBytes = lastSize / 1024 / 1024 * 4;
            
            System.out.println(
                  String.format(
                        "XMX=%d allocation succeeded with %d elements. File size when stored to harddrive in MB (1-4 bytes per char)=%dMB-%dMB", xmx, lastSize, approxFileSizeinMB_oneByte, approxFileSizeinMB_fourBytes));
            return lastSize;
         }
         catch (OutOfMemoryError oom) {
            lastSize = lastSize - stepSize;
         }
      }

      return 0;
   }
}

这是启动脚本

#!/bin/bash


echo "Finding the max allocation size per configured heap size"
echo "The maximum size for an string is Integer.MAX_VALUE=2147483647"


javac TestRamAllocation.java

## declare an array variable
declare -a arr=("512m" "1g" "2g" "3g" "4g" "5g" "6g" "7g" "8g" "9g" "10g" "11g" "12g" "13g" "14g")

## now loop through the above array
for i in "${arr[@]}"
do
   printf "Xmx=$i: "
   java -Xmx$i TestRamAllocation
done

它将提供以下结果

$ ./run_char_array_allocation_test.sh
Finding the max allocation size per configured heap size
The maximum size for an string is Integer.MAX_VALUE=2147483647
Xmx=512m: XMX=536870912 allocation succeeded with 214748397 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=204MB-816MB
Xmx=1g: XMX=1073741824 allocation succeeded with 472446429 elements. File size when stored to harddrive in MB (1-4
bytes per char)=450MB-1800MB
Xmx=2g: XMX=2147483648 allocation succeeded with 1009317329 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=962MB-3848MB
Xmx=3g: XMX=3221225472 allocation succeeded with 1546188229 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=1474MB-5896MB
Xmx=4g: XMX=4294967296 allocation succeeded with 2083059129 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=1986MB-7944MB
Xmx=5g: XMX=5368709120 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=2047MB-8188MB
Xmx=6g: XMX=6442450944 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=2047MB-8188MB
Xmx=7g: XMX=7516192768 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=2047MB-8188MB
Xmx=8g: XMX=8589934592 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=2047MB-8188MB
Xmx=9g: XMX=9663676416 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1-4
 bytes per char)=2047MB-8188MB
Xmx=10g: XMX=10737418240 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1
-4 bytes per char)=2047MB-8188MB
Xmx=11g: XMX=11811160064 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1
-4 bytes per char)=2047MB-8188MB
Xmx=12g: XMX=12884901888 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1
-4 bytes per char)=2047MB-8188MB
Xmx=13g: XMX=13958643712 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1
-4 bytes per char)=2047MB-8188MB
Xmx=14g: XMX=15032385536 allocation succeeded with 2147483637 elements. File size when stored to harddrive in MB (1
-4 bytes per char)=2047MB-8188MB

这意味着您可以通过最小 5g 堆 + 运行应用程序本身所需的堆开始的应用程序获得最大分配大小。

答案 5 :(得分:-1)

由于Java char是2个字节,2147483647个数组索引最多可以消耗3 GB(基于1024个字节= 1 KB)。