何时在实例创建期间分配内存或使用new关键字创建对象? [英] When is memory allocated during instance creating or using new keyword for object creation?

查看:211
本文介绍了何时在实例创建期间分配内存或使用new关键字创建对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在某处读到时,在创建实例并从堆分配空间时分配内存。如果它在实例和对象创建期间正确分配内存的时间和数量?

解决方案

在方法中声明的变量是存储在堆栈中,而实际对象存储在堆上。考虑

 整数a = new整数(10); 

在这个例子中,在堆上创建一个Integer类型的对象和一个引用(3​​2或者返回64bits)并将其作为变量'a'存储在方法堆栈中。如果JVM更喜欢优化,JVM可以自由地将这样的变量保存在CPU寄存器中。



使用new关键字时,会分配对象的内存。它通常在,非常好地描述了内存布局,


As I read somewhere, memory is allocated when instances are created and a space is allocated from the heap. If it is right than when and how much exactly memory is allocated during instances and objects creation?

解决方案

Variables declared within a method are stored within the stack, while the actual objects are stored on the heap. Consider

Integer a = new Integer(10);

In this example, an object of type Integer is created on the heap and a reference (either 32 or 64bits) is returned and stored within the methods stack as variable 'a'. The JVM is free to keep variables like this within the CPU registers if it prefers as an optimization.

Memory of an object is allocated when the new keyword is used. It is usually assigned within the TLAB (thread local allocation buffer) which is part of the eden heap reserved to the running thread. Thus reducing the overheads of object allocation to a simple 'bump of a pointer'. The two times when the TLAB is not used, is 1) when the object is too large for the space remaining, inwhich case it will be promoted straight to the old gen and 2) when a supporting JVM decides via escape analysis that it can avoid the object entirely and allocate directly on to the stack (or even break the object apart and only assign the fields required on the stack).

The amount of memory reserved consists of an object header, usually 2 words (3 for an array) and then space for each of the fields declared in the object and its parent classes. The total size of those fields depends on the JVM and the underlying platform (eg 32 bit or 64 bit) and JVM configuration such as compressed references.

------------------+------------------+------------------ +--------------------------
|   mark word     |   klass pointer  |  array size (opt) |    padding and fields   |
------------------+------------------+-------------------+--------------------------

Asking the JVM for sizes is not officially supported, but EHCache sizeOf is a very good 'best guess' that uses knowledge of different JVMs and access to the underlying pointers via the use of Java Unsafe.

The starting place for understanding the size of each field is the size of primitives defined by the Java language, however this is only the minimum sizes as the JVM is designed to work with 32bits and so primitives smaller than this are often padded out to 32 bits. For example booleans.

The exact layout of the fields will vary by JVM, but they will tend to be grouped by the class that defines them starting from the root of the inheritence tree. For example, consider

and

The pictures above were taken from this very good blog post that describes the memory layout very well,

这篇关于何时在实例创建期间分配内存或使用new关键字创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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