Java中的运行时与编译时内存分配 [英] Runtime vs compile time memory allocation in java
问题描述
我对java中的内存分配是在运行时还是在编译时感到困惑。
I am confused regarding whether memory allocation in java occurs at run time or compile time.
例如:
class Test{
int a;
public Test(){
a=10;
}
};
// somewhere else
Test t = new Test();
是在运行时还是在运行时分配了 a
编译时间?如果在编译时,java如何在直接获取已编译.class文件的VM上运行?
Is a
allocated at run time or at compile time? If at compile time, how is it possible as java runs on a VM which directly takes compiled .class files?
也:
-
何时为
a
赋值10
?
它如何用于参考变量 t
?
谢谢。
推荐答案
编译时不进行内存分配。仅在加载和运行时。
Compile time no memory allocation happens. Only at load and runtime.
编译时即会生成.class文件。
Compile time generates .class files that's it.
记住您需要一个运行程序的主类。当您使用Java将类路径与.class文件一起运行程序时,将出现诸如加载&链接等。
Remember you need to have a main class to run the program. When you run your program using Java with classpath to .class file, there will be steps like loading & linking etc.,
类加载器将文件加载到permgen。
Classloaders loads the files to permgen.
调用main方法时,将出现堆栈
When main method invoked, there will be stack created and local variables will be placed there
当运行时遇到 new 时,它将在堆上创建对象并在其中分配所需的内存,例如Test所需的内存。
When runtime encounters new it creates object on heap and allocates required memory there like memory required for Test.
这篇关于Java中的运行时与编译时内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!