Results 1 to 15 of 15
  1. #1
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default [SOLVED] Java heap space OutOfMemoryError

    Hi all

    I'm pretty new to java and have tried to fix this problem for the past day or so but not sure how. I seem to be getting the following error whenever i try to save the output from my program to a file:

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

    The file itself is about 1.5mb.

    I'm just curious is there any way of increasing the memory so i don't get this error?

    Thanks

    loki

  2. #2
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    Java Code:
    java.exe -Xmx256m
    java.exe /? for more help

    and PS: If you'd just googled java.lang.OutOfMemoryError you would have found this information in a few minutes.

  3. #3
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks for the reply

    I will check that out

    loki

  4. #4
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by corlettk View Post
    Java Code:
    java.exe -Xmx256m
    java.exe /? for more help

    and PS: If you'd just googled java.lang.OutOfMemoryError you would have found this information in a few minutes.
    Hey sorry i don't mean to ask a stupid question, but do i just run that code in the command prompt?

  5. #5
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    if your in windows you can add the Xmx256m to the Java Controller in the Control Panel. Otherwise yes, thats at the command line and you have to do it every time you run the application

  6. #6
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by logicbug View Post
    if your in windows you can add the Xmx256m to the Java Controller in the Control Panel. Otherwise yes, thats at the command line and you have to do it every time you run the application
    Hey i tried to increase the memory via the java controller however it was already set at its maximum of 1GB. The file i was trying to save was actually 10MB and not 1.5 as i originally thought. Do you think 10MB is too large or is there anything else i can do?

    Thanks

    loki

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    i'm surprised it hasn't been addressed, but what exactly are you doing to exceed the heap space? it's not too often that this occurs unless you have a memory intensive program, and it might mean that you have objects that arent being garbage collected for whatever reason.

  8. #8
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    i'm surprised it hasn't been addressed, but what exactly are you doing to exceed the heap space? it's not too often that this occurs unless you have a memory intensive program, and it might mean that you have objects that arent being garbage collected for whatever reason.
    Hi i've basically got my program to read in a file then i'm trying to export the output to a text file. I've pasted my code below to give you an idea of what i'm trying to do.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    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;
      }
    }
    Last edited by loki; 04-23-2009 at 09:19 PM.

  9. #9
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    not familiar with file i/o myself, but you don't close the filewriter. could that present a problem?

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    i couldnt see any reason for your program to exceed memory bounds so i decided to run it myself and got no problems... sorry but cant see anything wrong or any cause for your issues

  11. #11
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    i couldnt see any reason for your program to exceed memory bounds so i decided to run it myself and got no problems... sorry but cant see anything wrong or any cause for your issues
    Hey no worries.

    The program itself opens the 10mb file fine but its just when i try and save it to a text file that it gives me the error.

    However i tried using a smaller file of 1.3mb and the program opened and saved it with no problems so it must just have issues with saving larger files however i need it to open larger files.

    Thanks for the help anyway

    Cheers

    loki

  12. #12
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    You can increase the size of your heapspace with 1 line of code, I however do not know that line of code. Perhaps someone can lend insight and hope it fixes the problem?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  13. #13
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    You can increase the size of your heapspace with 1 line of code, I however do not know that line of code. Perhaps someone can lend insight and hope it fixes the problem?
    Yeah i had a google to see if i could hard code it into my program to see if that would work, but can't find anything. :(

  14. #14
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    You might want to CHECK the amount of free heap space from within your program to make sure it's really being set to what you think it is (see the methods on Runtime for querying memory).

  15. #15
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by corlettk View Post
    Java Code:
    java.exe -Xmx256m
    java.exe /? for more help

    and PS: If you'd just googled java.lang.OutOfMemoryError you would have found this information in a few minutes.
    Ahh got it working finally using the command below with the class name at the end of the command.

    Java Code:
     java -Xmx256m
    Thanks for your help

    loki

Similar Threads

  1. java.lang.OutOfMemoryError: Java heap space
    By paul in forum Advanced Java
    Replies: 11
    Last Post: 06-12-2010, 05:30 PM
  2. Replies: 3
    Last Post: 04-22-2009, 08:25 PM
  3. java.lang.OutOfMemoryError: Java heap space
    By vidjogamer in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 06:52 AM
  4. OutOfMemoryError: heap space (SJSAS PE 8.2 u4)
    By mikamj in forum Advanced Java
    Replies: 0
    Last Post: 11-21-2008, 07:37 AM
  5. Replies: 14
    Last Post: 06-12-2008, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •