简体   繁体   English

memory 如何分配给 Java 中的 class 成员函数

[英]How the memory is allocated for class member functions in Java

I found a similar question to mine:我发现了一个与我类似的问题:

"Is class member function code memory allocated once or at every instantiation of objects? ", which can be found here “是 class 成员 function 代码 memory 分配一次还是在每次对象实例化时分配?”,可以在这里找到

But the answer there only talked about the case for C/C++.但是那里的答案只谈到了 C/C++ 的情况。 Could anyone please tell me the answer to this question if I am using Java?如果我使用的是 Java,谁能告诉我这个问题的答案?

Is class member function code memory allocated once or at every instantiation of objects class 成员 function 代码 memory 是否分配一次或每次对象实例化

It is allocated once per class, not once per object.每个 class 分配一次,而不是每个 object 分配一次。 To be precise, it is allocated once per class/classloader pair.准确地说,它为每个类/类加载器对分配一次。 You can think of it as being allocated by the compiler, as long as you understand the compiler to include whatever the JIT or HotSpot does.您可以将其视为由编译器分配,只要您了解编译器包含 JIT 或 HotSpot 所做的任何事情。

It's not a straightforward answer.这不是一个简单的答案。

The code is loaded once when the class is loaded, but the code can be inlined into methods of other classes (and so loaded each time those other classes are loaded) and classes can be garbage collected and later re-loaded, so the code is loaded again.代码在加载 class 时加载一次,但代码可以内联到其他类的方法中(因此每次加载其他类时加载),并且可以对类进行垃圾收集,然后重新加载,所以代码是再次加载。

Many aspects of the allocation will be dependent on the implementation of the VM, too.分配的许多方面也将取决于 VM 的实现。

The answer is basically the same as in your other question, just that it is more dynamic.答案与您的其他问题基本相同,只是它更具动态性。 For the most popular Sun/Oracle VM: The executable code is compiled into the code cache on demand by the JIT (Just-In-Time) compiler and further optimized (eg inlined) on-the-fly by the Hotspot compiler.对于最流行的 Sun/Oracle VM:可执行代码由 JIT(即时)编译器按需编译到代码缓存中,并由 Hotspot 编译器即时进一步优化(例如内联)。

When the method is called the method pointer, method variable references and values are placed on the stack as a context and then the code in the code cache is executed.当方法被调用时,方法指针、方法变量引用和值作为上下文放在堆栈上,然后执行代码缓存中的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM