简体   繁体   中英

Java - read text file with specific jvm size

I have created a .txt test file containing only character 'a'. The file size is about 20 MB. I tried to read the file in three ways

Using BufferedReader

String fileName = "C:\\testFile.txt";
        FileReader fileReader = new FileReader(fileName);

        try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            String line;

            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        }

Using Scanner

 Scanner sc = null;

    try {

        sc = new Scanner(new File("C:\\testFile2.txt"), "UTF-8");
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            System.out.println(line);
        }

        if (sc.ioException() != null) {
            throw sc.ioException();
        }
    } finally {

        if (sc != null) {
            sc.close();
        }
    }

And, using Java8 stream

try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
        stream.forEach(System.out::println);
}

I set my jvm heap size using eclipse run configuration as shown here http://www.planetofbits.com/eclipse/increase-jvm-heap-size-in-eclipse/

In all three cases my value is : -Xms5M -Xmx100M.

Even though the file size is 20MB, it is always giving java.lang.OutOfMemoryError. When I set the heap size to -Xmx200M, it runs fine for all. I wonder what is eating up the memory and whether there is another more memory efficient way to read the file.

Any help is appreciated.

EDIT Profiling

Thanks Rann and Adam. I did use Jprofiler for the Scanner code. The memory usage is shown in the char[] array takes a lot and OverAll memory usage

It is interesting to see that char[] array taking up this much. No idea why!

Side note: Now I have changed my test file to contain integer and newline, so each line contains a random integer. With that in place, I can read the file ( even size of 100MB) easily with Scanner, as it will only read one line at a time. Previously, there was only character 'a' in the file and no new line.

The best way (imo) to answer your question would be to inspect you application with a memory profiling tool such as Java VisualVM or JProfiler . These applications will allow you to inspect how your program's memory is allocated and perhaps allow you to see where excessive memory is being used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM