Can using too many static variables cause a memory leak in Java?

前端 未结 5 1765

If my application has too many static variables or methods, then as per definition they will be stored in heap. Please correct me if I am wrong

1) Will these variab

5条回答
  •  悲哀的现实
    2020-12-08 01:08

    It won't cause a memory leak in the classic C sense... For example

    Class A{
    
    static B foo;
    
    ...
    
    static void makeFoo(){
       foo = new B();
       foo = new B();
    }
    

    In this case, a call to makeFoo() won't result in a memory leak, as the first instance can be garbage collected.

提交回复
热议问题