For the below codes, after the for loop, will memory leak still happen please?
static List list = new ArrayList();
for (int i = 1; i<100; i++){
Object o = new Object();
v.add(o);
o = null;
}
For the below codes, after the for loop, will memory leak still happen please?
static List list = new ArrayList();
for (int i = 1; i<100; i++){
Object o = new Object();
v.add(o);
o = null;
}
Fear not, the garbage collector will collect the garbage. Yes, every time o = null;
creates garbage as the reference lost but GC collects it. Always try to avoid creating objects into any loops.
I think giving Yes/No answer will help you less whereas you will be more beneficial if you know when true memory leaks occur in java. So read Creating a memory leak with Java