Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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:
 
Share this answer
 
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.
Java
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());
        }
    }
 
Share this answer
 
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!
There is no limitation if your machine permits.

After a while it depends on your hardware the size of array.
 
Share this answer
 
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".
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900