D
D
DDwrt1002021-02-01 10:40:48
Java
DDwrt100, 2021-02-01 10:40:48

How to put a billion objects in a sheet?

Good day to all.
I sit torturing Java, in academic interests.
Faced with the fact that when trying to put a billion numbers in a sheet, I encounter:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

The code itself is simple.
public class billion {
    public static void main(String[] args){
        ArrayList<Long> s = new ArrayList<Long>(1000000000);
        for(long  i = 0; i< 1000000000;i++){
            s.add(i);
        }

        s.toString();
    }
}


Please tell me what needs to be corrected in the JVM in order to execute this code? IntelIdea Runtime.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Cheremisin, 2021-02-01
@DDwrt100

You just need at least 64 gigabytes of free RAM (if I counted everything correctly), but in fact four times more, since we store not primitives, but objects. Therefore, if you own such memory, then the keys for java -Xms128G -Xmx128G

V
Vladimir Korotenko, 2021-02-01
@firedragon

Above are the correct answers. But why do you need such a list? Usually it is beaten into subsets and work already with them. Try changing the algorithm like this

O
Oleg, 2021-02-01
@402d

https://habr.com/en/post/312078/
read this article. Do not put the computer in an uncomfortable position. It is better to write the algorithm in such a way that it comes from the available resources.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question