1

I wanted to print out 1 000 000 000 to 1 999 999 999. I thought it would be an easy task but eclipse tells me something about memory error. How could I do this? I want to have a text data where all numbers from 1000000000 to 1999999999 are written down each in a new line. I thought printing them out in the console and then just copy & paste into a .txt data would work, but it sadly doesn't.

My code:

    public static void main(String[] args) {
    int number = 1000000000;
    do {
        System.out.println(number);
        number = number +1;
    } while (number < 2000000000);

}

How would you fix this problem? Please help me.

CoreNoob
  • 115
  • 1
  • 10
  • This could would work right. Is the problem you facing is with the size of the data? – apatniv Oct 06 '18 at 02:54
  • In case if Eclipse goes out of memory try to increase memory in eclipse. – Rans Oct 06 '18 at 02:56
  • After counting from 1 000 000 000 it stops somewhere at 1 006 000 000. I can't even copy it from the console. I don't understand what the issue is. @Rangas: How to increase Eclipse's memoy? wenzi: How to print out directly to a .txt file? – CoreNoob Oct 06 '18 at 02:59
  • Check this link to update eclipse memory settings. https://stackoverflow.com/questions/2610194/how-can-i-give-eclipse-more-memory-than-512m – Rans Oct 06 '18 at 03:02
  • 1
    Dude! Do you _really_ need to print out **a billion** ten-digit numbers to prove that you've mastered using a `do...while` loop to count out a range of numbers? Why not just start at 100 and go up to, say, 1000? Just sayin'... – Kevin Anderson Oct 06 '18 at 03:16
  • Or assuming this is not about mastering loops, but you really need a list of a billion numbers for something, a text file is probably not the best way to store those. What do you need to do with them next? – Thilo Oct 06 '18 at 03:18
  • Yes, I **really** need to print out a billion ten-digit numbers. Not to prove anything. I couldn't open it with the standard editor. But found: https://www.emeditor.com/ – CoreNoob Oct 06 '18 at 03:30

3 Answers3

5

The problem you are encountering is because you are asking Eclipse to buffer 1,000,000,000 x 11 or 12 characters in memory. That is > 20 GB, and it is clearly too large for the Eclipse processes heap.

That approach simply won't work, unless you are running Eclipse on a really high-end PC with a huge amount of RAM. Even then, I have my doubts. For example, copy-and-paste of 10 billion characters probably wouldn't work.

You need to take another approach; e.g. have your application write the numbers directly to the output file.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

I just wrote this piece of code and by running it I got the following results:

  • Final file: 10GB+
  • Start time: Sat Oct 06 00:00:19 BRT 2018
  • Finishing time: Sat Oct 06 00:03:25 BRT 2018

     try { 
        FileWriter writer = new FileWriter("count.txt");
        System.out.println(new Date());
        int number = 1000000000;
        do {
            writer.write(number + "\n");
        } while (number++ < 2000000000);
        System.out.println(new Date());
        writer.close(); 
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
George Neto
  • 681
  • 4
  • 8
2

It is better to run the class file from command line and redirect the output. Assuming you are running on "unixy" kind of environment.

java Counter > output.txt # This will take lot of time.

apatniv
  • 1,771
  • 10
  • 13
  • According to the other poster, not a terrible lot of time, on the order of a couple of minutes (and tens of GB of disk space). :-) – Thilo Oct 06 '18 at 03:16