user3495562
user3495562

Reputation: 335

Does the allocated memory in java remain there for following code

    for(int i=0; i<100; i++){
    List<Integer> ls =new ArrayList<Integer>();
    ls.add(1);
}

After each iterations, does the allocated memory still remains there. I mean I want to free them. If I want it not be there, what should I do?

I met a outOfmemory problem, I do not know whether it is caused by this kind of thing or not

Upvotes: 2

Views: 72

Answers (1)

Mureinik
Mureinik

Reputation: 311073

ls is a local variable inside the for loop. This means that once an iteration is done, it can be garbage collected, and is indeed marked as such. Whether the JVM decides to collect it or not is an entirely different matter, and is up to the JVM's implementation. It can be effected by parameters you pass to java (e.g., -Xms, -Xmx or the notorious -XX: parameters), but can't strictly be controlled.

Upvotes: 2

Related Questions