Results 1 to 15 of 15
- 04-23-2009, 05:09 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
[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
- 04-23-2009, 05:23 PM #2
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
java.exe /? for more helpJava Code:java.exe -Xmx256m
and PS: If you'd just googled java.lang.OutOfMemoryError you would have found this information in a few minutes.
- 04-23-2009, 05:25 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
Thanks for the reply
I will check that out
loki
- 04-23-2009, 06:35 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
- 04-23-2009, 06:46 PM #5
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
- 04-23-2009, 09:07 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
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
- 04-23-2009, 09:10 PM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 04-23-2009, 09:13 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
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.
- 04-23-2009, 09:34 PM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
not familiar with file i/o myself, but you don't close the filewriter. could that present a problem?
- 04-23-2009, 09:42 PM #10
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
- 04-23-2009, 09:59 PM #11
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
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
- 04-23-2009, 11:42 PM #12
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.
- 04-24-2009, 12:09 AM #13
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
- 04-24-2009, 03:24 AM #14
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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).
Neil Coffey
Javamex - Java tutorials and performance info
- 04-25-2009, 04:11 PM #15
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
java.lang.OutOfMemoryError: Java heap space
By paul in forum Advanced JavaReplies: 11Last Post: 06-12-2010, 05:30 PM -
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
By Ms.Ranjan in forum New To JavaReplies: 3Last Post: 04-22-2009, 08:25 PM -
java.lang.OutOfMemoryError: Java heap space
By vidjogamer in forum New To JavaReplies: 3Last Post: 02-06-2009, 06:52 AM -
OutOfMemoryError: heap space (SJSAS PE 8.2 u4)
By mikamj in forum Advanced JavaReplies: 0Last Post: 11-21-2008, 07:37 AM -
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
By Eku in forum NetBeansReplies: 14Last Post: 06-12-2008, 08:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks