Avoid going out of memory when deleting files
@@ -110,17 +110,19 @@ public class FileFinder {
* @return true iff anything was matched and deleted
*/
public boolean deleteRecursively(TaskContext context) {
+ final int limit = 20;
+ int[] numDeleted = { 0 };
List<Path> deletedPaths = new ArrayList<>();
try {
forEach(attributes -> {
if (attributes.unixPath().deleteRecursively()) {
- deletedPaths.add(attributes.path());
+ if (numDeleted[0]++ <= limit) deletedPaths.add(attributes.path());
}
});
} finally {
- if (deletedPaths.size() > 20) {
- context.log(logger, "Deleted " + deletedPaths.size() + " paths under " + basePath);
+ if (numDeleted[0] > limit) {
+ context.log(logger, "Deleted " + numDeleted[0] + " paths under " + basePath);
} else if (deletedPaths.size() > 0) {
List<Path> paths = deletedPaths.stream()
.map(basePath::relativize)
