是否可以强制JVM在堆栈以外的堆栈中创建对象? [英] Is it possible to force JVM to create an object in stack other than heap?

查看:61
本文介绍了是否可以强制JVM在堆栈以外的堆栈中创建对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到一些内容,有时JVM将识别一些对象并尝试在堆中而不是堆中创建对象,因为栈上的内存分配比堆中的内存分配便宜,栈上的释放是免费的,栈是由运行时有效地管理. 那么,该对象在堆栈中的分配方式是如何工作的,是否有任何方法可以迫使JVM做到这一点?

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 ?

推荐答案

无法强制JVM在任何地方分配对象.您不必担心Java实际在何处分配对象,因为规范中未定义对象.话虽这么说,JVM已经变得更加智能,并使用了称为

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.

这篇关于是否可以强制JVM在堆栈以外的堆栈中创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆