Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting OutOfMemory Error while reading EXE file content and placing in JTextArea

 
manasa manogna
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to read the content of EXE file and convert that into HEX Representation. After getting the hex representation as string, I am trying to place that text into JTextArea. But I am getting OutOfMemory Error while attempting to do this. below is the code I am trying



In the above code, I am using readBytesFromFile method to read the content of the exe file and getHex to convert this byte[] into hex string representation. I am trying to place this string in JTextArea using JTextArea.setText() method. But I am getting the OutOfMemory Error. Below is the error I am receiving

File length: 4518720 Maximum Length: 2147483647

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at javax.swing.text.WrappedPlainView$WrappedLine.breakLines(WrappedPlainView.java:782)
at javax.swing.text.WrappedPlainView$WrappedLine.update(WrappedPlainView.java:717)
at javax.swing.text.WrappedPlainView$WrappedLine.insertUpdate(WrappedPlainView.java:708)
at javax.swing.text.WrappedPlainView.insertUpdate(WrappedPlainView.java:455)
at javax.swing.plaf.basic.BasicTextUI$RootView.insertUpdate(BasicTextUI.java:1590)
at javax.swing.plaf.basic.BasicTextUI$UpdateHandler.insertUpdate(BasicTextUI.java:1849)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:185)
at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:734)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:693)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:114)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:655)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1693)
at SupportFiles.EXEFile.createAndDisplayGUI(EXEFile.java:58)
at SupportFiles.EXEFile$1.run(EXEFile.java:33)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Can you please help me to solve this issue.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at SupportFiles.EXEFile.createAndDisplayGUI(EXEFile.java:58)

So what do you have on line 58 of EXEFile.java?
 
manasa manogna
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi maneesh

At line 58 of EXEFile, I am calling the statement textArea.setText(getHex(getBytesFromFile(fileName))). Here textArea is an object of JTextArea and I have set the following properties for textArea.



 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try doing the processing in chunks instead of reading the entire file all at once. That is read 4K from the file, convert the data into the StringBuilder, then append the builder to the textArea. Repeat until the entire file has been processed. This way you will need smaller amounts of work spaces. That is the buffer for the file will only be 4K and the StringBuilder will only be 8K. The Document of the textare will grow as more data is appended to it.
 
manasa manogna
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob

Thank you for the suggestion. Can you please help me how to achieve this with a piece of code please. I am trying to google the same but not getting satisfactory solution to achieve.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example that simply reads a file in 4K blocks and writes the data to another file. You would need to change the write() logic to convert the data to your StringBuilder and then update the text area.

 
manasa manogna
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob

Thank you very much for the example. It solved my problem. Thanks once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic