简体   繁体   中英

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

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 (eg, -Xms , -Xmx or the notorious -XX: parameters), but can't strictly be controlled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM