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)
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
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.
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
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.