简体   繁体   English

jvm如何运行程序

[英]how jvm run program

program 1: when i write one simple class B without creating object in my java program. 程序1:当我编写一个简单的类B而不在Java程序中创建对象时。 how JVM allocates memory to class code which contain member fields and member function. JVM如何将内存分配给包含成员字段和成员函数的类代码。

class B
{
     int a=10;
     public void print()
     {
         System.out.println("inside B class");
     }

}

program 2:And when i create the object of that class then how jvm allocate memory for object...see code below 程序2:当我创建该类的对象时,jvm如何为对象分配内存...请参见下面的代码

class B
{
    int a=10;
    public void print()
    {
        System.out.println("inside B class");
    }

    public static void main(String args[])
    {
        B b=new B();
        b.print();

    }
}

There are three levels of allocation in this example: 在此示例中,分配分为三个级别:

  • when the class is loaded (usually the first time it is referenced somewhere in your code), then memory for its structure, its code and it static fields is allocated. 当类被加载时(通常是第一次在代码中某个地方引用它),然后将为其结构,其代码和静态字段分配内存。

  • when an instance is created ( new B() ) then memory for the non-static fields and some metadata (object header) is allocated. 当创建实例( new B() )时,将为非静态字段和一些元数据(对象标头)分配内存。 This memory is on the heap. 该内存在堆上。

  • when a method is called, then memory from the stack is allocated for local variables inside that method. 当一个方法被调用时,堆栈中的内存将分配给该方法内部的局部变量。

这个站点非常有用,所以看看它。 链接,您将能够了解jvm是如何运行程序的

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

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