Question

I am now learning JVM memory model.I have a question:

package com; public class TestAllocation { private static final int _1MB = 1204*1024; public static void main(String[] args){ testAllocation(); } public static void testAllocation(){ byte[] allocation1,allocation2,allocation3,allocation4; allocation1 = new byte[2 * _1MB]; allocation2 = new byte[2 * _1MB]; allocation3 = new byte[2 * _1MB]; allocation4 = new byte[2 * _1MB]; } } So ,where does reference allocation1,allocation2 stored? Since testAllocation() is a static method ,so ,variable inside this function is stored in method area?But every time a function is called, it is the stack push/pop operation.So , allocation1 ,allocation is stored in
the stack frame of testAllocation()?

But one thing is clear ,that is ,the array value ,is stored in java heap ,right?

Était-ce utile?

La solution

Making the method static does not change where its variables are stored.

At the end of testAllocation() there will be four references (allocation1, allocation2, allocation3 and allocation4) on the stack.

These four references will refer to four byte arrays that are stored on the heap.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top