Java heap and stack memory allocation?

On the heap. Any time you use new to create an object, it is allocated on the heap.

Up vote 3 down vote favorite 1 share g+ share fb share tw.

Class Person{ private String name; public Person(){ } public Person(String name){ this. Name=name; } public static void main(String arg) { Person per= new Person("Andy"); } } per is a local variable, where will it be stored, on the heap or the stack? Java memory memory-management memory-allocation link|improve this question edited Sep 29 '11 at 13:08Jonas12.2k741103 asked Jun 18 '11 at 13:44yyy I 77769610 76% accept rate.

Objects are always stored in the heap. However, the reference to per will be stored in the local variable array, which is stored in the frame created for main(String), which is stored in the stack. For more information, see: The Structure of the Java Virtual Machine.

Edit: I've learnt that JVMs are actually able to allocate objects on the stack by performing escape analysis. Better yet, a technique called scalar replacement can be applied, where the object allocation is omitted, and the object's fields are treated as local variables. It is possible for the variables to be allocated on machine registers.

Escape analysis by stack allocation has been implemented by HotSpot VMs since Java 6u14. It has been enabled by default since Java 6u23. For an object to be allocated on the stack, it must not escape the executing thread, the method body or be passed as an argument to another method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions