*
The moose likes Beginning Java and the fly likes Exectuion throwing Out of Memory exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exectuion throwing Out of Memory exception" Watch "Exectuion throwing Out of Memory exception" New topic
Author

Exectuion throwing Out of Memory exception

Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

Hi

I was trying to execute a program in my computer. Im getting the below error when trying to execute this



Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at Fileread.readFile(Fileread.java:57)
at Fileread.main(Fileread.java:17)


Can you please let me know what can be done to avoid this error. I have tried to increase the heap size. But does not seem to work. Please let me know how to increase the heap size in windows. I tried to execute the below command in the command prompt to increase the heap size



But does not seem to work. Please assist. Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

by "it does not seem to work" do you mean you still get a OOM error, or that the command won't even start up java?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
A couple of things. You say:
Java -Xms512m -Xmx1024m

1. I assume that the upper case J is a typo. If not, do you have a script named Java that is ignoring the parameters?
2. Your exception looks like you are reading a file. What is the size of the file?
3. Maybe there is a bug in the code. If you have access to the source, please share.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Tom Reilly wrote:Your exception looks like you are reading a file.


Yes, it does. And it looks like the code might be building a StringBuffer which contains the entire contents of the file.

It probably isn't necessary to have the whole file in memory. And if the file is so large that it can't fit in memory, then the code should be redesigned so that it doesn't do that.
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

Hi All,

Please find the below code that Im using. While executing the below one Im getting the Outof memory exveption.





The file that Im trying to read is a huge file. I have specifically written the program to reads from large files. Please assist.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
1) The code isn't pretty but it works. If I comment the if with canIReadLine it simply creates a copy of a text file when I call

2) Don't use deprecated classes/methods. Use BufferedReader to read the file.

3) How big is your 'huge' file? -> TellTheDetails! If you're trying to read a file that's a few Gigabytes in size, you cannot assume to hold it all in memory at once...

4) Debug information. Maybe it would be a good idea to log some debug information in your methods (e.g. to determine exactly what was the latest line you could read from the file before you got OutOfMemoryError)


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Your program looks like it first reads the whole file, interpreting the lines according to some rules, and then it writes all the data back to another file. If the file is large, you might get an OutOfMemoryError, because the program reads the whole file into memory. Oh wait, it does not only that, it's even worse - it reads a whole bunch of files, which it all tries to keep in memory at once.

You could structure your application in a different way, so that it doesn't need a lot of memory at all. Do it so that it reads and writes at the same time, line by line: read a line, process it according to the rules, write the processed line to the output file, continue with the next line. That way, you only need to have one line in memory at a time, and it would work for files of any size without memory problems.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3502
    
  13
Tom Reilly wrote:A couple of things. You say:
Java -Xms512m -Xmx1024m

1. I assume that the upper case J is a typo. If not, do you have a script named Java that is ignoring the parameters?


Or the OP is using a case insensitive operating system such as Windows


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exectuion throwing Out of Memory exception