Java 8 allows you to do this:
- Code: Select all
Files.list(Paths.get("")).forEach(System.out::println); //Do not copy& paste this
It's not obvious that the code above has a memoryleak: The jvm will open a native directory descriptor pointing to the current directory, but won't close it. Even the GC won't save you.
The correct code to use would be
- Code: Select all
try (final Stream<Path> list = Files.list(Paths.get(""))){
list.forEach(System.out::println);
}
I would appreciate it if Yourkit could help to trace down these Mistakes in a similar fashion as does with unclosed FileInputStreams.