0

I am trying to perform a scan of the project containing the word stub for searching files in it. Files in UTF-16 encoding. However, when the parser comes to 1017 files, all with the error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.

How I can fix it?

import java.io.*;
import java.util.Scanner;

public class FolderScanerV2 {

    public static void FolderScaner(File file) throws Exception {
        if (file.isDirectory()) {
            File[] directory = file.listFiles();
            for (File enter : directory) {
                if ((enter.isFile() & enter.getCanonicalPath().contains("txt"))) {
                    FileScaner(enter);
                } else {
                    FolderScaner(enter);
                }
            }
        }
    }

    public static void FileScaner(File file) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsolutePath()), "UTF-16"));
        Scanner text = new Scanner(in);
        while (text.hasNext()) {
            if(text.next().contains("Cap")) {
                System.out.println("Cap" + " " + file.getCanonicalPath());
            }
        }
        text.close();
        in.close();
    }

    public static void main(String[] args) {
        String path = new String();
        Scanner inpunt = new Scanner(System.in);
        System.out.println("Input path");
        path = inpunt.nextLine(); // 

        File dir = new File(path);
        path.close();
        try {
            FolderScaner(dir);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
xlm
  • 4,594
  • 13
  • 42
  • 47
alex fry
  • 13
  • 4

2 Answers2

0

You would need to give more memory to jvm. You can find the process in following post.

Increase heap size in Java

Community
  • 1
  • 1
Aditya T
  • 1,110
  • 10
  • 21
0

Thank you add VM Options: -Xms512m -Xmx1024m -XX:-UseGCOverheadLimit and all works

alex fry
  • 13
  • 4