简体   繁体   中英

declaration of an array inside the for loop in Java

public class Test1 {
    public static void main () {
        int N = 10;
        int M = 100000;
        for(int i =0; i< N; i++) {
             int[] box = new int[M];
        }
    }
}

Will this code cause cause the memory to be allocated for the array with size equal to M elements for each iteration of the for loop?

Yes. But since no reference on the new array exists outside the loop, each instance is eligible for garbage-collection and will most probably be collected with the next gc-run.

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