Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java program runs out of memory (-xms1024 -xmx1024)

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program runs out of memory (-xms1024 -xmx1024)

    I do not know why my java program runs out of memeory about half way through the list?

    public static void getMovieFile() {
     
            int myBuffer, counter = 0;
            String aFile = "movies.list";
            Pattern myIgnore = Pattern.compile("\".*");
            Pattern myYear = Pattern.compile("\\(\\d\\d\\d\\d.*");
            String title, year;
            Scanner myScanner;
     
     
            try {
                //New Scanner stream for the file input (aFile)
                myScanner = new Scanner(new FileInputStream(aFile), "ISO-8859-1");
     
                //Skip first 20 lines (Movies are at line 1,312,533)
                while(counter<20){
                    myScanner.nextLine();
                    counter++;
                }
                //Reset counter
                counter = 0;
                //While Scanner starts with quotation marks (") skip line
                while (myScanner.hasNext(myIgnore)){
                    myScanner.nextLine();
                }
     
                //While Scanner has next token read token
                while (myScanner.hasNext()) {
                    //Declaring and instantiating title (Blank)
                    title = "";
                    //While Scanner is not movie year concat title with next token
                    while(!myScanner.hasNext(myYear)){
                        title = title.concat(myScanner.next()).concat(" ");
     
                    }
                    //Else return and substring movie year
                    year = myScanner.next().toString().substring(1,5);
                    //Inputting title and year into the object database
                    //While year is not empty
                    while(!(year.isEmpty()&&title.isEmpty())){
                        try{
                            //Check if year is a valid integer
                            myBuffer = Integer.parseInt(year);
                            //Input records into array
                            MovieMenu.movies.inputMovie(title, myBuffer, "NA", "NA", 0.0, "NA", "NA", counter);
                            //Reset title and year for new input
                            System.out.println("Inputting Movie: " +title +"Year:" +year +"Position: " +counter);
                            title = year = "";
                        } catch(NumberFormatException e) {
                            //Parse Integer Number format exception
                            Logger.getLogger(MovieListImport.class.getName()).log(Level.SEVERE, null, e);
                            System.out.println("Error: Invalid release");
                        }
                        //Increase position in movies array list
                        counter++;
                    }
                    //Move Scanner to next line
                    myScanner.nextLine();
                }
                //Close myScanner
                myScanner.close();
     
            } catch (FileNotFoundException ex) {
                //FileInputStream File not found exception
                Logger.getLogger(MovieListImport.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error: File not Found");
            } catch (SecurityException e){
                //FileInputStream Security exception
                Logger.getLogger(MovieListImport.class.getName()).log(Level.SEVERE, null, e);
                System.out.println("Error: Cannot read file");
            } catch (NullPointerException ioe){
                //Scanner Null pointer exception
                Logger.getLogger(MovieListImport.class.getName()).log(Level.SEVERE, null, ioe);
                System.out.println("Error: Corrupt file");
            } catch (NoSuchElementException e){
                //Scanner No such element exception
                Logger.getLogger(MovieListImport.class.getName()).log(Level.SEVERE, null, e);
                System.out.println("Error: End of file");
            }
     
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program runs out of memory (-xms1024 -xmx1024)

    Is one of your loops out of control? Add some printlns in the loops to see if any of them are not stopping.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java program runs out of memory (-xms1024 -xmx1024)

    It does appear that all my while loops do finish when they are meant to on a short text file: Although the files it is trying to read in are 100mb+ lists from IMDB. When running through the IMDB files it gets to about a quater of the way through before crashing. When I remove all System.out.println in my method the concat does not work and it just reaches end of file error after the first line. Should I be using concat or is that not the problem? If I created a new scanner or stringbuffer when I wanted to concat / append the title would that stop the crashing?

    Skipping Line: 1
    While loop (Skip) finished
    Counter reset to (0): 0
    Skipping Line if starting with ("): 
    While loop (Ignore) finished:
    myScanner.next(): #1
    concat of myScanner.next(): #1 Movie
    concat of myScanner.next(): #1 Movie Test
    concat of myScanner.next(): #1 Movie Test File
    While loop (Title) finished: #1 Movie Test File
     
    Inputting Movie:
    	Title: #1 Movie Test File
    	Year: 2005
    	Position: 0
    While loop (Input) finished:
     
    While loop (hasNext()) finished:

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java program runs out of memory (-xms1024 -xmx1024)

    What do you wish to do with the information afterwards? In other words, do you need to store all the information in memory? My guess is probably not, in which case you may wish to parse the files into some type of relational database that can be more readily accessed at runtime (100mb isn't that large, but loading it all (or many of them) into memory adds quite a bit of overhead). FWIW, I would recommend a StringBuilder for your task (as written this should not impact your out of memory exception however)

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Java program runs out of memory (-xms1024 -xmx1024)

    100MB is pretty large for text, but try appending to a StringBuilder, not buffer as concatenating is a costly business.

    Edit: Seems I got beat to it :p
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. no error when compiled but when the program runs .. help this !!!
    By izzahmed in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 8th, 2011, 08:55 AM
  2. Java Program that Runs in Background
    By 1bun100 in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2011, 02:10 PM
  3. Program Runs in Eclipse but Not When Exported
    By Pantheon8 in forum Java IDEs
    Replies: 2
    Last Post: August 17th, 2011, 05:57 PM
  4. HELP ME PLEASE MEMORY MANAGEMENT PROGRAM USING JAVA
    By lockwater in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2010, 08:30 AM
  5. Memory Handling -de allocate memory in Java
    By 19world in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2010, 04:05 AM