• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exectuion throwing Out of Memory exception

 
Ranch Hand
Posts: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic