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 16 of 16

Thread: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

    Hi i'm creating a program that opens a file and then sends the output to the GUI. If the user wishes they can then save a copy of the output to another file.

    My program seems to open files without any bother however when i try and save the output of some larger files to a text file i get this error below:

    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

    I tried a file of size 1.3mb which opened and then saved fine, however when i tried a file of 10mb the file opened but i could not save the output as i kept getting the above error.

    I had a look in the java controller in the java control panel and the disk space allowed is at 1GB which is its maximum.

    I'm just curious is there anything else i can do in order for this to work?

    Thanks

    john


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Thumbs up Re: OutOfMemoryError: Java heap space

    Good evening John,

    You need to increase your Java heap space.

    If Java runs out of memory, the following error occurs:

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

    This can have two reasons:

    * Your Java application has a memory leak.
    * Your Java application really needs a lot of memory (more than 128 MB by default!). In this case the Java heap size can be increased using the following runtime parameters:

    java -Xms<initial heap size> -Xmx<maximum heap size>

    Defaults are:

    java -Xms32m -Xmx128m

    You can set this either in the Java Control Panel or on the command line, depending on the environment you run your application.

    Take a look at this link for more information:

    Java How To ...: 6 Common Errors in Setting Java Heap Size
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: OutOfMemoryError: Java heap space

    Quote Originally Posted by JavaPF View Post
    Good evening John,

    java -Xms<initial heap size> -Xmx<maximum heap size>

    Defaults are:

    java -Xms32m -Xmx128m

    You can set this either in the Java Control Panel or on the command line, depending on the environment you run your application.

    Take a look at this link for more information:

    Java How To ...: 6 Common Errors in Setting Java Heap Size
    Hey cheers for the reply

    I've tried to change it in the control panel but it's at its maximum and it doesn't seem to work either when i try to do it via the command prompt.

    The program still open and saves smaller files so i guess i'll crack on with it and see if i can address this problem later.

    PS Someone else told me that it's possible to increase the heap space by hard coding it into the program but they didn't know the code. I had a google but couldn't find anything so i'm not sure whether it's possible or not?

    John

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: OutOfMemoryError: Java heap space

    I've never heard of you being able to hard code the heap space but I may be wrong.

    I suggest you look at how the program is coded.. It could be poor coding that is causing this. A 10mb file shouldn't really cause you to increase your heap space
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: OutOfMemoryError: Java heap space

    Quote Originally Posted by JavaPF View Post
    I've never heard of you being able to hard code the heap space but I may be wrong.

    I suggest you look at how the program is coded.. It could be poor coding that is causing this. A 10mb file shouldn't really cause you to increase your heap space
    Hey i've posted my code below

    Do you think there could be a problem in my coding that may be causing this?

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.regex.*; 
    import javax.swing.*;
     
    public class Editor
        extends JFrame
        implements ActionListener {
      public static void main(String[] s) { new Editor(  ); }
     
      private JEditorPane textPane = new JEditorPane(  );
     
      public Editor(  ) {
        super("Editor v1.0");
        addWindowListener(new WindowAdapter(  ) {
          public void windowClosing(WindowEvent e) { System.exit(0); }
        });
        Container content = getContentPane(  );
        content.add(new JScrollPane(textPane), BorderLayout.CENTER);
        JMenu menu = new JMenu("File");
        menu.add(makeMenuItem("Open"));
        menu.add(makeMenuItem("Clean"));
        menu.add(makeMenuItem("Extract"));
        menu.add(makeMenuItem("Save"));
        menu.add(makeMenuItem("Quit"));
        JMenuBar menuBar = new JMenuBar(  );
        menuBar.add(menu);
        setJMenuBar(menuBar);
        setSize(300, 300);
        setLocation(200, 200);
        setVisible(true);
      }
     
      public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand(  );
        if (command.equals("Quit")) System.exit(0);
        else if (command.equals("Open")) loadFile(  );
        else if (command.equals("Save")) saveFile(  );
      }
     
      private void loadFile (  ) {
        JFileChooser chooser = new JFileChooser(  );
        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.CANCEL_OPTION) return;
        try {
          File file = chooser.getSelectedFile(  );
          java.net.URL url = file.toURL(  );
          textPane.setPage(url);
          	}
        catch (Exception e) {
          textPane.setText("Could not load file: " + e);
        }
      }
     
      private void saveFile(  ) {
        JFileChooser chooser = new JFileChooser(  );
        int result = chooser.showSaveDialog(this);
        // Save file data...
     
        if (result == JFileChooser.CANCEL_OPTION) return; // if user click on Save button
    	File file = chooser.getSelectedFile();
    	try {
             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
             bw.write(textPane.getText());
             bw.close();
     
          	}
        catch (Exception e) {
             JOptionPane.showMessageDialog(
                this,
                e.getMessage(),
                "File Error",
                JOptionPane.ERROR_MESSAGE
             );
        } 
      } 
     
      private JMenuItem makeMenuItem( String name ) {
        JMenuItem m = new JMenuItem( name );
        m.addActionListener( this );
        return m;
      }
    }


    thanks

    john

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: OutOfMemoryError: Java heap space

    Hmm.. That does actually all look OK to me.

    I think we need to try to get your head space increased. Do you know how much memory your computer has?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: OutOfMemoryError: Java heap space

    Quote Originally Posted by JavaPF View Post
    Hmm.. That does actually all look OK to me.

    I think we need to try to get your head space increased. Do you know how much memory your computer has?
    I've got 1GB of RAM and the CPU usage seems to vary between 30 and 60% when the program is running

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: OutOfMemoryError: Java heap space

    1GB of RAM is more than enough.

    What happens when you type java -Xms32m -Xmx128m into a command prompt?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: OutOfMemoryError: Java heap space

    Do you use the Eclipse IDE John?

    To set java heap size in Eclipse you have 2 options:

    1. Edit eclipse-home/eclipse.ini to be something like the following and restart Eclipse.

    -vmargs
    -Xms64m
    -Xmx256m

    2. Or, you can just run eclipse command with additional options at the very end. Anything after -vmargs will be treated as JVM options and passed directly to the JVM. JVM options specified in the command line this way will always override those in eclipse.ini. For example,

    eclipse -vmargs -Xms64m -Xmx256m
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #10
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: OutOfMemoryError: Java heap space

    When i try the command: java -Xms32m -Xmx128m it comes upwith the following:
    Usage: java [-options] class [args...]
               (to execute a class)
       or  java [-options] -jar jarfile [args...]
               (to execute a jar file)
     
    where options include:
        -client       to select the "client" VM
        -server       to select the "server" VM
        -hotspot      is a synonym for the "client" VM  [deprecated]
                      The default VM is client.
     
        -cp <class search path of directories and zip/jar files>
        -classpath <class search path of directories and zip/jar files>
                      A ; separated list of directories, JAR archives,
                      and ZIP archives to search for class files.
        -D<name>=<value>
                      set a system property
        -verbose[:class|gc|jni]
                      enable verbose output
        -version      print product version and exit
        -version:<value>
                      require the specified version to run
        -showversion  print product version and continue
        -jre-restrict-search | -jre-no-restrict-search
                      include/exclude user private JREs in the version search
        -? -help      print this help message
        -X            print help on non-standard options
        -ea[:<packagename>...|:<classname>]
        -enableassertions[:<packagename>...|:<classname>]
                      enable assertions
        -da[:<packagename>...|:<classname>]
        -disableassertions[:<packagename>...|:<classname>]
                      disable assertions
        -esa | -enablesystemassertions
                      enable system assertions
        -dsa | -disablesystemassertions
                      disable system assertions
        -agentlib:<libname>[=<options>]
                      load native agent library <libname>, e.g. -agentlib:hprof
                        see also, -agentlib:jdwp=help and -agentlib:hprof=help
        -agentpath:<pathname>[=<options>]
                      load native agent library by full pathname
        -javaagent:<jarpath>[=<options>]
                      load Java programming language agent, see java.lang.instrument
     
        -splash:<imagepath>
                      show splash screen with specified image

    The same also applies when i type in java -Xmx128m

    When i try java -Xmx128m Editor i just seem to get an exception saying that no class has been found

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: OutOfMemoryError: Java heap space

    When i try java -Xmx128m Editor i just seem to get an exception saying that no class has been found
    OK, in the command prompt, navigate to the location of your Editor class, then try the command:

    java -Xmx128m Editor
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  12. The Following User Says Thank You to JavaPF For This Useful Post:

    John (April 24th, 2009)

  13. #12
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: OutOfMemoryError: Java heap space

    Emm no i use JCreater.

    Think this is another problem that i may have to deal with later amongst others lol.

    I mean it still saves files of a smaller size, but the sizes of .wmdb can vary.

    I guess i could continue on with the rest of the program

    However i'm sure i've got some other problems that i may have to pickle your brain with lol

    I think java is starting to melt my head!

  14. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: OutOfMemoryError: Java heap space

    Did you try:

    in the command prompt, navigate to the location of your Editor class, then try the command:

    java -Xmx128m Editor
    I'm interested to know if that works?!

    I think maybe there is too much junk in the .wmdb file. Have you tried saving a .txt file in your application of a similar size?

    Yes sometimes Java can be complicated but its fun though right?!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  15. #14
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: OutOfMemoryError: Java heap space

    Quote Originally Posted by JavaPF View Post
    Did you try:

    I'm interested to know if that works?!

    I think maybe there is too much junk in the .wmdb file. Have you tried saving a .txt file in your application of a similar size?

    Yes sometimes Java can be complicated but its fun though right?!
    Wahoo it worked!!!!

    I think i must have been navigating to the wrong directory when i was trying to execute it! I'm a complete idiot!

    Thanks for that!

    Right basically i've got my program to open a .wmdb file and then save it - that works!

    The thing that i've noticed with the .wmdb file is that the xml tags seem to contain whitespace, e.g. < t a g > instead of <tag> which is one one of the reasons why the DOMParse wouldn't work. However i've got a program that removes the white space. I tried it on the .wmdb file but it was taking forever and had to cancel it because it was going through the whole file line by line. I tried it on a smaller file however and it works.

    My problem now is once i open a file using the code that i posted earlier i want the other program to remove the whitespace from the output if that makes sense lol.

    Then after that i can try the DOMParse to see if i can extract the xml

    I can post the code for removing the white space or do you want me to post it in the other thread?

    Sorry for the long post

    John

  16. #15
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: OutOfMemoryError: Java heap space

    RESULT!!! Knew we would get there in the end...

    With the program that removes white space, you should make it skip all the lines that are irrelevant and only start to remove the white space once it hits the first XML tag.

    When reading the file line by line, you could do something like:

    if (mystring.contains("< t a g >")){
    //Start remove white space code
    }

    You might as well start a new thread seeing as this one has been solved.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  17. #16
    Junior Member xdigg's Avatar
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Any suggestions for High Bandwidth web hosting

    First, I am searching for a hosting company that bills monthly. I don't want to pay in advance and get stuck with a company.

    I came across chime host - Unlimited Bandwidth Web Hosting, High Bandwidth Hosting, Inexpensive Web Hosting by Chime Host but I can't seem to find many websites reviewing them. I see justhost on hosting review sites but they require advance payment for the low price.

    suggestions please?

Tags for this Thread