当JVM执行新关键字创建对象时会发生什么? [英] What happens when JVM executes new key word to create an object?

查看:115
本文介绍了当JVM执行新关键字创建对象时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JVM使用堆栈和堆为对象引用分配内存,为方法分配对象值和内存.但是我对以下术语感到困惑:METAL AREA,HEAP和JAVA STACK,我的问题很少.

I know JVM uses stack and heap for allocation of memory for object reference, object value and memory for methods. But I am confused about the terminologies: METHOD AREA, HEAP and JAVA STACK and I have few question's.

  1. 当我们说"ClassName obj = new ClassName()"时,new在HEAP上创建了一个对象(实例变量和静态变量也是如此),并且返回给reference(obj)的内容是什么?有人说它是CLASS TYPE,是不是意味着哈希码?

  1. When we say "ClassName obj = new ClassName()", new creates an object on the HEAP(the instance variables and static variables too) and what is returned to the reference(obj)? Some people use to say it is CLASS TYPE, does it mean the hash code?

当new在堆上创建对象时,同时:i)对应于该对象的方法ii)局部变量和iii)对该对象的引用存储为STACK的一部分(是否是) JAVA STACK?).如果是这样,那么METHOD AREA会做什么?还是我错了?

When new creates the object on the heap, at the same time: i)the methods,corresponding to that object ii)local variables and iii)the reference to that object are stored as part of STACK( is it JAVA STACK?). If so, then what does METHOD AREA do? Or am I wrong?

为该对象分配的内存量是多少?
一世.供对象参考
ii.用于对象值(取决于局部变量)
iii.

What is the amount of memory allocated for that object?
i. for object reference
ii. for object values(it depends on the local variables)
iii. will there be a memory allocated to point the object's methods?( because the non-static members are not shared among the objects and a separate copy is maintained for each objects including the methods).

顺便说一下,静态方法存储在哪里?

By the way, where does static methods are stored?

推荐答案

我知道JVM使用堆栈和堆来分配用于对象引用的内存

I know JVM uses stack and heap for allocation of memory for object reference

正确.

对象值

我假设您是指对象的标题和字段.

I assume you means the object's header and fields.

和方法的内存.

and memory for methods.

方法未存储在堆或堆栈中.当您探查堆使用情况或设置最大堆大小时,方法的使用没有什么区别,因为它们不在Oracle或OpenJDK JVM中的堆上.

Methods are not stored in the heap, or stack. When you profiler the heap usage or set the maximum heap size, the use of methods makes no difference as they are not on the heap in the Oracle or OpenJDK JVM.

根据您使用的JVM,它们存储在PermGen或MetaSpace或其他一些空间中.

They are stored in the PermGen or MetaSpace or some other space depending on which JVM you are using.

但是我对以下术语感到困惑:METHOD AREA,

But I am confused about the terminologies: METHOD AREA,

来自Java中方法区域

Java虚拟机具有一个方法区域,该区域在所有Java虚拟机线程之间共享.该方法区域类似于常规语言的编译代码的存储区域,或者类似于操作系统过程中的文本"段.它存储每个类的结构,例如运行时常量池,字段和方法数据,以及方法和构造函数的代码,包括用于类和实例初始化以及接口初始化的特殊方法(§2.9).

The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in an operating system process. It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods (§2.9) used in class and instance initialization and interface initialization.

HEAP

用于存储对象的共享空间.这通常是由JVM管理的本地内存的一个连续区域.

Shared space for storing objects. This is usually one continuous region of native memory managed by the JVM.

和JAVA STACK

and JAVA STACK

该线程的堆栈实际上是大多数JVM上的本机线程堆栈.

The thread's stack which is actually a native thread stack on most JVM.

当我们说"ClassName obj = new ClassName()"时,new在HEAP上创建一个对象(实例变量和静态变量也是如此)

When we say "ClassName obj = new ClassName()", new creates an object on the HEAP(the instance variables and static variables too)

虽然可以,但也可以通过转义分析消除对象,将字段放在堆栈上,并可能消除这些对象.

It might, but it can also eliminate the object with escape analysis, placing the fields on the stack, and possibly eliminating those.

,返回给引用(obj)的内容是什么?

and what is returned to the reference(obj)?

正确的,Java仅具有引用和原语(如果忽略了两者均不包含的void类型)

Correct, Java only has references and primitives (if you ignore the void type which is neither)

有些人经常说它是CLASS TYPE,

Some people use to say it is CLASS TYPE,

通过提供引用的类型(即类或接口)来定义引用.

The reference is defined by giving the type of the reference which is a class or interface.

这是否意味着哈希码?

does it mean the hash code?

哈希码是对象的哈希值.它与您提到的其他内容无关.

A hash code is a hash value for the object. It is not releated to anything else you have mentioned.

当new在堆上创建对象时,

When new creates the object on the heap,

在堆上创建new对象时,只需为对象的标题(指向类及其方法)和其字段的空间创建空间. (那些JVM并没有进行优化)

When you create a new object on the heap, you just create space for the header of the object (which points to the class and it's methods) and space for it's fields. (Those the JVM doesn't optimise away)

同时:i)方法

at the same time: i)the methods

方法在各个阶段被加载/编译.这些方法是在第一次需要时加载的,如果需要的话会在以后编译.

The methods are loaded/compiled in various stages. The methods are loaded as they are needed for the first time and later if they are compiled.

对应于该对象ii)局部变量

corresponding to that object ii)local variables

局部变量在堆栈上,不在堆上,不在对象中.

Local variables are on the stack, not on the heap, not in the object.

iii)对该对象的引用存储为STACK的一部分(是否为JAVA STACK?).

iii)the reference to that object are stored as part of STACK( is it JAVA STACK?).

Java堆栈是堆栈,是本机堆栈.

The Java Stack, is the Stack, is the native stack.

如果是,那么METHOD AREA会做什么?

If so, then what does METHOD AREA do?

存储方法的代码.

为该对象分配的内存量是多少?

What is the amount of memory allocated for that object?

每个标头大约8-12个字节,每个基本字段都有空间,引用和对齐填充为8或16个字节(32 GB-64 GB堆).

About 8-12 bytes per header, space for each primitive field and reference and alignment padding of 8 or 16 bytes (32 GB - 64 GB heaps).

i.供对象参考

i. for object reference

通常,这是64位JVM(使用压缩的oop)上的32位.如果您有超过64 GB的堆,它将是64位.

Typically this is 32-bit on 64-bit JVMs (With compressed oops). If you have more than 64 GB heap it will be 64 -bit.

ii.对象值(取决于局部变量)

ii. for object values(it depends on the local variables)

局部变量在堆上而不是对象上.

Local variables are on the heap not the object.

iii.会分配一个内存来指向对象的方法吗?

iii. will there be a memory allocated to point the object's methods?

您看不到方法的内存使用情况.它既不在堆上,也不在每种方法的基础上进行度量.我不知道一个探查器甚至可以向您显示.

Method memory usage is not visible to you. It is not on the heap, nor something you can measure on a per method basis. I don't know of a profiler which will even show you this.

(因为非静态成员不在对象之间共享,并且为每个对象(包括方法)维护一个单独的副本).

( because the non-static members are not shared among the objects and a separate copy is maintained for each objects including the methods).

这听起来像是对空间的疯狂浪费,这就是JVM这样做的原因.一个方法只有一个副本,与实例数无关.

That sounds like an insane waste of space, and it is, which is why the JVM does do that. There is only one copy for a method, regardless of the number of instances.

顺便说一下,静态方法存储在哪里?

By the way, where does static methods are stored?

使用所有其他方法.静态方法和非静态方法之间没有区别,只是非静态方法必须将实例作为其在JVM级别的第一个参数. (并且修饰符中有一点可以说它是否是静态的)

With all the other methods. There is no difference between a static method and a non-static method except a non-static method must take an instance as it's first argument at the JVM level. (And there is a bit in the modifiers to say whether it is static or not)

这篇关于当JVM执行新关键字创建对象时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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