3 / 3
Aug 2018

A simple program for LIfe,Universe And Everything5 in java is showing 4468MB memory use. How come?

Code:

import java.io.IOException;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args) throws IOException {
        
        Scanner sc=new Scanner(System.in);

        long no;
        for(;;)
        {

            no=sc.nextLong();
            if(no==42)
            {
               sc.close();
               break; 
            }
            System.out.println(no);
            
        }
        
        
    }
       
    
}
  • created

    Aug '18
  • last reply

    Aug '18
  • 2

    replies

  • 949

    views

  • 3

    users

  • 1

    link

I guess, it comes from -Xmx runtime option. In 2006, any JAVA solution showing at least 190M. I think memory limit was increased to ~4.5G since then.

Scanner is costly operation as well, try BufferReader.