Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store 100 million integers in a Java array?

I am working on a small task where I am required to store around 1 billion integers in an Array. However, I am running into a heap space problem. Could you please help me with this?

Machine Details : Core 2 Duo Processor with 4 GB RAM. I have even tried -Xmx 3072m . Is there any work around for this? The same thing works in C++ , so there should definitely be a way to store this many numbers in memory.

Below is the code and the exception I am getting :

public class test {
    private static int C[] = new int[10000*10000];

    public static void main(String[] args) {
        System.out.println(java.lang.Runtime.getRuntime().maxMemory()); 

    }

}

Exception : Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at test.(test.java:3)

like image 217
user2035996 Avatar asked Feb 02 '13 21:02

user2035996


1 Answers

Use an associative array. The key is an integer, and the value is the count (the number of times the integer has been added to the list).

This should get you some decent space savings if the distribution is relatively random, much more so if it's not.

like image 150
Dave Avatar answered Oct 05 '22 16:10

Dave