在Java中加载类时如何管理内存? [英] How is memory managed while loading classes in Java?

查看:123
本文介绍了在Java中加载类时如何管理内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个包含10个方法的类,我需要从类中实例化10个对象。
问题是: JVM是否会在对象创建时为10个实例分配10个不同的内存空间(我的意思是在我调用构造函数时,即新的MyClass();?或者它将加载类定义一次在内存中,每个实例在调用这10个方法中的每一个时,在运行时,JVM会分配内存吗?

Imagine I've got a class with 10 methods and I need to instantiate 10 objects from the class. The question is: Will JVM allocate 10 different memory spaces for 10 instances at object creation time (I mean at the time I call the constructor i.e. new MyClass(); ? or it will load the class definition once in memory and each instance while calling each of those 10 methods, at run time, JVM will allocate the memory?

为了清除一些误解,我的问题是在创建对象时,我知道所有数据成员都分配在堆内存中,但我不确定尚未调用的方法是否在每个对象的内存中分配不同?

To clear some misunderstanding, my question is when creating an object, I know that all data members are allocated in heap memory, but I'm not sure whether the methods which hasn't been yet called are allocated differently in memory for each object or not?

推荐答案


JVM是否会在对象创建时为10个实例分配10个不同的内存空间(我的意思是在我调用构造函数时)即新的MyClass();

Will JVM allocate 10 different memory spaces for 10 instances at object creation time (I mean at the time I call the constructor i.e. new MyClass();

它可能会做,但是通过转义分析,它可以将它们放在堆栈上或完全消除它们。

It might do, however with escape analysis, it can place them on the stack or eliminate them entirely.

或者它会在内存和每个实例中加载一次类定义,同时调用这10个方法中的每一个,在运行时,JVM会分配内存吗?

or it will load the class definition once in memory and each instance while calling each of those 10 methods, at run time, JVM will allocate the memory?

如果您有一个ClassLoader,您将获得该类的一个实例,但是如果每个实例都有自己的ClassLoader,您将在每个ClassLoader中获得该类的副本。注意:每个ClassLoader可以具有相同名称的不同版本的类。

If you have one ClassLoader you will get one instance of the class, however if each instance has it's own ClassLoader, you will get a copy of the class in each ClassLoader. Note: each ClassLoader could have a different version of the class with the same name.


为了解决一些误解,我的问题是在创建时对象,我知道所有数据成员都分配在堆内存中,

To clear some misunderstanding, my question is when creating an object, I know that all data members are allocated in heap memory,

它存储的类和方法信息(包括字节代码) PermGen中的堆(Java< 7)或MetaSpace(Java 8 +)

The class and method information (including byte code) it stored off the heap in the PermGen (Java <7) or MetaSpace (Java 8+)

实际的实例在名义上被添加到堆中,但不一定是。

The actual instance is notionally added to the heap, but doesn't have to be.


我不确定尚未调用的方法是否在每个对象的内存中分配不同? / p>

I'm not sure whether the methods which hasn't been yet called are allocated differently in memory for each object or not?

JVM经历了许多优化阶段,当你调用一个方法时,它可能会优化它,内联它甚至消除它。您可以通过在命令行上添加 -XX:+ PrintCompilation 来查看正在编译(甚至重新优化)的方法。

The JVM goes through many stages of optimisation and when you call a method it might optimise it, inline it or even eliminate it. You can see methods being compiled (and even re-optimised) by adding -XX:+PrintCompilation on the command line.

这篇关于在Java中加载类时如何管理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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