2

If I define a static variable in my class as follows:

class MyClass
{
  private static List<String> list = new ArrayList<String>();
}

Can list cause memory leakage? If yes, then how?

Mac
  • 1,711
  • 3
  • 12
  • 26
  • 2
    A question for you. Why is it static? – Andrew Thompson Jul 26 '13 at 13:54
  • @AndrewThompson I just came accross following answer http://stackoverflow.com/a/6548647. The first point is telling that it could lead to memroy leakage.But I could'nt get it. So I asked here. – Mac Jul 26 '13 at 13:58
  • 1
    @Mac If the list keeps growing, you're keeping references to objects that you may not need and they won't be garbage collected. It's a memory leak in that sense. You can still fix it by clearing the list. – Sotirios Delimanolis Jul 26 '13 at 14:00
  • @SotiriosDelimanolis but whatever reason you are specifying could also be achieved using non-static List.. – Mac Jul 26 '13 at 14:07
  • 1
    @Mac It would happen much less often. With non-static variables, once the instance goes out of scope, so does the list, so it can get garbage collected. With static variable, they can never go out of scope (unless you `null` it, which you can't do on `final`) since they're linked to the class. – Sotirios Delimanolis Jul 26 '13 at 14:09
  • @SotiriosDelimanolis Ahh yeah..your last two lines strike like bullet to me .. I got the answer now..Thanks :) – Mac Jul 26 '13 at 14:11
  • @SotiriosDelimanolis If you put your comment as the answer then I would be glad to mark your answer as accepted one. Thnx :) – Mac Jul 27 '13 at 13:46

4 Answers4

2

The snippet of code you posted is a memory leak in the sense that if you never clear elements from that list or set it to null, it will keep growing and not get garbage collected.

With non-static lists (instance or local scoped lists), this would happen much less often. With non-static variables, once the instance goes out of scope, so does the variable (and possibly the object), so it can get garbage collected. With static variable, they can never go out of scope (unless you set the reference to null , which you can't do on final) since they're linked to the class.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

Technically it is a memory leak only if it is out of garbage collector's reach. On the other hand, if it is in memory for extended periods even when you don't have a use for it, well that's a logical flaw and it will keep the related objects from getting gc'ed too. The memory will be reclaimed only when the class is unloaded.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
1

It's not necessarily static Lists (Collection) that may cause a memory leak. If we have a long-living Collection (eg cache) we should limit its size somehow, eg by removing old object form it.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
-1

Static variables are in classes alloaced in a special memory place meaning, in your app exsists only 1 array Myclass.list, and the thing is they are not dynamic. So your list has to have a fixed size.

public final  static String[] list=new String[]
    {"str1", //0
    "str2", //1
    "str3", //2
    "str4", //3
    };

You can't modify those values, by result i doubt it will cause leaks.

Bogdan M.
  • 2,161
  • 6
  • 31
  • 53