Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemory when list files in a directory

When I list files of a directory that has 300,000 files with Java, out of memory occurs.

String[] fileNames = file.list();

What I want is a way that can list all files of a directory incrementally no matter how many files in that specific directory and won't have "out of memory" problem with the default 64M heap limit.

I have Google a while, and cannot find such a way in pure Java.
Please help me!!

Note, JNI is a possible solution, but I hate JNI.

like image 629
James Avatar asked Jan 13 '10 04:01

James


2 Answers

I know you said "with the default 64M heap limit", but let's look at the facts - you want to hold a (potentially) large number of items in memory, using the mechanisms made available to you by Java. So, unless there is some dire reason that you can't, I would say increasing the heap is the way to go.

Here is a link to the same discussion at JavaRanch: http://www.coderanch.com/t/381939/Java-General/java/iterate-over-files-directory

Edit, in response to comment: the reason I said he wants to hold a large number of items in memory is because this is the only mechanism Java provides for listing a directory without using the native interface or platform-specific mechanisms (and the OP said he wanted "pure Java").

like image 185
danben Avatar answered Nov 13 '22 11:11

danben


The only possible solution for you is Java7 and then you sould use an iterator.

final Path p = FileSystems.getDefault().getPath("Yourpath");
Files.walk(p).forEach(filePath -> {
        if (Files.isRegularFile(filePath)) {
            //Do something with filePath
        }
});
like image 5
Mirko Avatar answered Nov 13 '22 11:11

Mirko