Click here to Skip to main content
14,550,413 members
Rate this:
Please Sign up or sign in to vote.
See more:
What can be the Maximum size of array?
1000, 20000, or million or billion element what?

if i declare three Dimensional array

long[, ,] contourArray = new long[1000, 1000, 1000];

I get following error:

Exception of type 'System.OutOfMemoryException' was thrown.

What should I do to declare 3dimention array and it can contain 1000 element in each dimension ?

Thanks ..
Posted
Updated 4-Sep-10 19:54pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Think about the size of what you are trying to create.

You are asking for 1000 * 1000 * 1000 elements = 1000000000 elements.
Each element is a long so takes 8 bytes = 8000000000 bytes.

That is >7.45GB of RAM to hold one array! That will be too much for almost any user's machine on the planet :doh:
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Regardless of the size issues pointed out by Davey, would it not be better to create 'point' objects and store them within a suitable collection, rather than a multidimensional array.

e.g.
import java.util.*;

public class Main {
    private static ArrayList<SomePoint> _points = new ArrayList<SomePoint>();

    private static class SomePoint {
        private long x;
        private long y;
        private long z;

        public SomePoint() {
            x = 0;
            y = 0;
            z = 0;
        }
        public long getX()
            {return this.x;}
        public long getY()
            {return this.y;}
        public long getZ()
            {return this.z;}
        public void setPoint(long x, long y, long z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }


    public Main() {
     }

    public static void main(String[] args) {
             //Add 1000 default points
        for (int x=0; x<1000; x++)
        {
            _points.add(new SomePoint());
        }
        
        //Dump the points collection to the output
        for (SomePoint item : _points)
        {
            System.out.println("x:" + item.getX() + " y:" + item.getY() + " z:" + item.getZ());
        }
    }
   
Comments
Toli Cuturicu 4-Sep-10 14:47pm
   
Reason for my vote of 3
java not in tags, sorry
DaveAuld 4-Sep-10 15:07pm
   
I know Java is not in the Tags, but you can implement the same idea in C# just as easily!
Rate this:
Please Sign up or sign in to vote.

Solution 3

There is no limitation if your machine permits.

After a while it depends on your hardware the size of array.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

What do you need the array for?
It's hard for me to understand why you need an array that big.

I am guessing that you are looking for a 2 dimensional array with 3 fields.

like this
long[,]contourArray = new long[1000,3]; 


Meaning you get 1000 "rows" with 3 "columns", that way you can store 3 numbers on each "row".
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100