Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to force JVM to create an object in stack other than heap?

I read some where that sometimes JVM will identify some objects and try to create it in stack than heap as the memory allocation on the stack is cheaper than the memory allocation in the heap, deallocation on the stack is free and the stack is efficiently managed by the run-time. So is how this object allocation in stack works and is there any way to force the JVM to do this ?

like image 948
vrnithinkumar Avatar asked Aug 29 '14 04:08

vrnithinkumar


1 Answers

There is no way to force the JVM to allocate objects anywhere. You should not care where Java actually allocates your Object because it is not defined in the specifications. That being said the JVM has gotten a lot smarter and using a technique called escape analysis it can, if it wants to, allocate an object on the stack. There is however no way to force the JVM to allocate an Object at a specific place and it is not required by the specification that this behavior happen.

like image 61
Smith_61 Avatar answered Nov 02 '22 05:11

Smith_61