Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String Memory Leak

I am not java expert.

My code is reading a file into a String. This code gets executed every 5 minutes. The size of file varies. Sometimes it is 100 sometimes it is 1000 lines.

I am experience Out Of Memory, after some days.

The question I have is, when my codes goes out of scope of the Reading file function, does Java garbage collect the String?

I am pretty confused by reading on the internet. Some people says it does not get deleted and use StringBuffer.

// Demonstrate FileReader.

import java.io.*;
class FileReaderDemo {
    public static void read(BufferedReader br) throws Exception {
        long length = 0;
        String s;
        while (true) {
            s = br.readLine();
            s += "abcd";
            if (s == null) {
                break;
            }
            length += s.length();
            //System.out.println(s);
        }
        System.out.println("Read: " + (length / 1024 / 1024) + " MB");
    }

    public static void main(String args[]) throws Exception {
        //FileReader fr = new FileReader("FileReaderDemo.java");
        FileReader fr = new FileReader("big_file.txt.1");
        BufferedReader br = new BufferedReader(fr);
        String s;
        read(br);
        fr = new FileReader("big_file.txt.1");
        br = new BufferedReader(fr);
        read(br);
        fr = new FileReader("big_file.txt.1");
        br = new BufferedReader(fr);
        read(br);
        fr = new FileReader("big_file.txt.1");
        br = new BufferedReader(fr);
        read(br);
        BufferedReader in = new BufferedReader(new InputStreamReader(System. in )); in .readLine();
        fr.close();
    }
}
like image 792
Avinash Avatar asked May 20 '11 12:05

Avinash


People also ask

Can strings cause memory leaks?

Memory leak usually takes place with strings. If you use buffers skips and regular expressions and then delete them you can avoid most of the memory leak.

How do you fix a memory leak in Java?

Use reference objects to avoid memory leaks Using the java. lang. ref package, you can work with the garbage collector in your program. This allows you to avoid directly referencing objects and use special reference objects that the garbage collector easily clears.

Is Java prone to memory leak?

The whole Java Virtual Machine ecosystem – and thus Java – is susceptible to memory leaks as well. Let's look into what the Java memory leak is, how to detect whether our software is suffering from one and how to deal with them.

What is the memory leak issue in String class?

In the String object, when you call substring , the value property is shared between the two strings. So, if you get a substring from a big string and keep it for a long time, the big string won't be garbage collected. It could result in a memory leak, actually.


2 Answers

Hello, I am not java expert.

Everyone has something they can learn.

My code is reading a file into a String, This code gets executed every 5 minutes. Now Sometime file size of 100 lines sometimes 1000 lines.

Doesn't sound very large or very often. Shouldn't be a problem.

I am experience Out Of Memory, after some days.

You should be able to get a heap dump and see where you have run out of memory and why.

Question I have is, When my codes goes out of scope of the Reading file Function. Does Java Garbage collect the String .

It can be collected when it is no longer reachable via a Strong reference.

I am pretty confuse by reading on internet some says it does not get deleted and use StringBuffer

Sounds like you came to the right place. I have never heard that one.

like image 76
Peter Lawrey Avatar answered Sep 20 '22 11:09

Peter Lawrey


Your read method will never terminate. Once you reach the end of the file, you just continue adding the string "nullabcd" to s, forever.

EDIT: forget that, s is re-assigned each time. Still, I can't see how your read method can terminate.

like image 25
OpenSauce Avatar answered Sep 21 '22 11:09

OpenSauce