i am trying to instanciate the FileReader giving it a String path to a file inside a while Loop the problem is that after 30 files, i am getting the error
java.lang.OutOfMemoryError: Java heap space
the path is dynamically given to the fileReaders inside the loop :
DocumentBuilder doc= DocumentBuilderFactory.newInstance().newDocumentBuilder();
while (Index < Events.size() ) {
String FileName = getFileName(canal,Date,time);
Doc =doc.parse(new File(Deposit+File.separator+FileName));
FileReader fR = new FileReader(Deposit+File.separator+FileName);
PlaylistString =FileCopyUtils.copyToString(fR);
if(Doc!=null && PlaylistString.contains(some_text)){
// Some treatment **********
}
}
here the FileName changes for each iteration
i want to find a way to avoid having the out of memory error i tried fR.close() ans System.gc() but i still have the error does anyone have an idea ?
Someone is holding a reference to the FileReader objects or to another object. (BTW: You have to close the FileReader, look for try-with-resource for easier handling) Its not necessarily the case that the FileReader is the problem it could also be another object which is retained (referenced) inside your program.
Try to use a profiler like VisualVM (or YourKit, is not free) to determine memory leaks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With