Reputation: 301
I'm trying to recursively traverse through my Drive to search some files. The code is working fine when there are limited folders/files but when I target my search to C drive where in I have lots of files it throws Out of Heap memory.
Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space
and I don't want to increase the maximum allowable heap space as it is just like postponing the issue for time being.
Code:
void iterateDirectory(String somedir) {
File dir = new File(somedir);
File[] files = dir.listFiles();
if (files != null) {
for (int id = 0; id < files.length; id++) {
if (files[id].isDirectory() == false)
{
fsTree.add(files[id].toString()); // taking list of files
}
else
{
iterateFilesInDirectory(files[id].getAbsolutePath());
}
}
}
}
Upvotes: 0
Views: 710
Reputation: 23680
The culprit, as I see it is this line:
fsTree.add(files[id].toString()); // taking list of files
It appears that you add every single file to a global data structure (fsTree
), and then search there.
My bet is:
A. It won't go away if you 'convert' your recursive function into an iterative one.
B. It will go away if, instead of appending to a global data structure and searching in the end, you do the search/matching locally, and only globally cache the matching hits:
void iterateDirectory (String somedir, String search_term) {
File dir = new File(somedir);
File[] files = dir.listFiles();
if (files != null) {
for (int id = 0; id < files.length; id++) {
if (files[id].isDirectory() == false)
{
if (/* files[id].isDirectory() MATCHES search_term */)
// add to list of matching files:
matching_hits.add(files[id].toString());
}
else
{
iterateFilesInDirectory(files[id].getAbsolutePath());
}
}
}
}
Upvotes: 2
Reputation: 106508
There are limitations as to what recursion can do, namely with respect to stack/heap usage. Remember that whatever you can do recursively, you can do iteratively; rewrite your code to use an iterative solution instead.
Alternate solution: There is an interface in java.nio
that can be used to recursively walk down a filesystem's structure. Take a look at this trail, and SimpleFileVisitor
.
Upvotes: -1
Reputation: 500923
There are two possibilities:
.
and/or ..
correctly). If that's the case, you have to fix the code.-Xmx
JVM option.Upvotes: 1