Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to store 10pow9 number of integers. When i tried storing it in array, it is giving run time error. So what should i used instead of array to avoid run time error.

What I have tried:

I tried declaring array globally, but that is not working.
Posted
Updated 23-Jan-18 3:53am
Comments
Dave Kreskowiak 23-Jan-18 9:38am    
So, you want to hold onto 1,000,000,000 integers? 32-bit or 64-bit integers? 32-bit is going to need over 4GB of RAM, while 64-bit integers will need twice that.

This sounds like it's a bad idea, depending on what you're doing.
Akshit11 23-Jan-18 9:45am    
Yes, then what should be the alternate way to store these number of integers?

I am new to programming, so please help.
Dave Kreskowiak 23-Jan-18 13:14pm    
I am new to programming,

This is what makes me question why you think you need to store a billion data points in memory.
Patrice T 23-Jan-18 10:13am    
Looks like a wrong solution to another problem.
You need to explain the problem to get advices.
Akshit11 23-Jan-18 11:02am    
Thank you for your help.

It's not so much a case of "how do I do this?" as "why are you trying to do this?"
Even as 32 bit integers (as Dave says) that's 4GB of data which is a spectacular amount for a single object. And if this is a mobile app (as is quite possible, given it's Java which is mostly used for Android) I'd say that the best thing you can do is look at your application and why it needs random access to that much data!

If you don't actually need to access it randomly, then you might consider using a disk file instead, and seeking that to the right place instead of trying to keep all this in memory. The chances are that the data is already being sourced from somewhere and it may be possible to access the somewhere directly instead of pulling it all into memory and manipulating it there.

I certainly wouldn't start off by designing a system than needed a 4GB array without exploring other avenues first!
 
Share this answer
 
Comments
Akshit11 23-Jan-18 9:55am    
Thanks a lot for your help.I got what you are saying, there must be some other approach to this.
OriginalGriff 23-Jan-18 10:26am    
You're welcome!
Java supports arrays with 1 billion items. But such an array would consume 4 GB of memory which is probably above the default maximum heap size which depends on the OS and Java version and the available amount of memory.

You can set the maximum heap size with the -Xmx option. But it should not exceed the amount of available memory. So you might try for example -Xmx5g when you have at least 8 GB of RAM installed in your system. See also Tuning Java Virtual Machines (JVMs)[^].
 
Share this answer
 
Comments
Akshit11 23-Jan-18 9:55am    
Thanks a lot for your help.I got what you are saying, there must be some other approach to this.
Jochen Arndt 23-Jan-18 10:22am    
Thank you for accepting my solution.

If you have not enough RAM the only solution is those mentioned in the other answer:
Store the data in a file and use the RandomAccessFile class to read, write, and seek (set the position within the file).

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