开发者

Is there a maximum size to char[]?

开发者 https://www.devze.com 2023-01-27 10:11 出处:网络
IN JAVA Is there a 开发者_StackOverflow中文版maximum size of char[]? Can I have a char[5,000,000]?

IN JAVA

Is there a 开发者_StackOverflow中文版maximum size of char[]? Can I have a char[5,000,000]?

Is array in java composed of contiguous memory blocks?


Since the index is int based the maximum size of an array should be Integer.MAX_VALUE

Obviously the other limit is the amount of memory available to your application :)


Maximum size depends on the architecture.

In C, an array consists in contiguous memory blocks. You can have an array as big as you want, as long as it fits in RAM (including disk-based virtual memory). There is an exception: arrays declared as local variables are allocated on the stack, which is quite small. The typical size for the stack in a multi-threaded application on a PC will be 1 megabyte. If you want a big array, you'd better create it as a global variable, or allocate it dynamically (with malloc()).

In Java, arrays are heap-allocated (with new). Whether they are contiguous or not is "none of your business": the very essence of Java is about shielding the programmer from the concrete implementation details (but really, on most Java virtual machines, arrays are contiguous). Java arrays are created and indexed with values expressed as an int type; thus, a Java array cannot have more than about 2 billions elements, even if the machine has more RAM than that: an int cannot hold bigger values. 5 millions is a piece of cake, and I routinely allocate arrays bigger than that.


heap size of JVM is the limit.

as @wildcodeforjava mentioned int is the paremeter while initilizing array

so which ever is less.


Yes, and yes as far as I know, I think the max in java is around 2,000,000,000 2*10e9


The truth is a little bit more complex. The max char array allocation size is Integer.MAX_VALUE indeed but it does not mean that you can actually allocate a char[] of that size. The VM additionally limits the allocation size dynamically depending on the initially configured heap -Xmx and the currently free heap. So even if there would be sufficiant heap free to allocate the array and the requested size is < Integer.MAX_VALUE you may encounter a OutOfMemoryError.

I have written a small program testing the max allocation size out.

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

Here's the start script

#!/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

It will deliver the following results

$ ./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

Which means you get the maximum allocation size with an application started with min 5g heap + the heap you need to run your application itself.


As Java char is 2 bytes, 2147483647 array indexes can consume up to 3 GB (based on 1024 bytes = 1 KB).

0

精彩评论

暂无评论...
验证码 换一张
取 消