简体   繁体   English

Java中的静态变量和静态块是如何分配内存的?

[英]How Memory Has Been Allocated Static variable and Static Block in Java?

How Memory has been allocated Static variable and Static Block in Java stack or heap?如何在 Java 堆栈或堆中分配静态变量和静态块的内存?

  class A{
    static int a; 
    static{}
    public static void main(String args[]){
        A h=new A();
     }
   }

When create the object how memory allocated for static stack or heap.创建对象时如何为静态堆栈或堆分配内存。

The static keyword is used in java mainly for memory management. static关键字在java中主要用于内存管理。 We may apply static keyword with variables, methods, blocks and nested class.我们可以将static关键字应用于变量、方法、块和嵌套类。 The static keyword belongs to the class than instance of the class. static关键字属于类而不是类的实例。

Memory allocation for stactic variables only happens once when the class is loaded in the memory. stactic变量的内存分配仅在类加载到内存中时发生一次。

So, here once the class is loaded by the classloader the memory will be allocated for integer a and stacic block.因此,一旦类加载classloader加载了classloader ,内存将被分配给整数 a 和静态块。

Static methods (in fact all methods) as well as static variables are stored in the PermGen section of the heap.静态方法(实际上是所有方法)以及静态变量都存储在堆的 PermGen 部分中。

Data that may outlive the call to the procedure that created it is usually allocated on a heap.可能比对创建它的过程的调用寿命更长的数据通常分配在堆上。 Eg new to create objects that may be passed from procedure to procedure.例如 new 创建可以从过程传递到过程的对象。 The size of heap can not be determined at compile time.编译时无法确定堆的大小。 Referenced only through pointers or references, eg, dynamic objects in C++, all objects in Java仅通过指针或引用引用,例如 C++ 中的动态对象,Java 中的所有对象

Names local to a procedure are allocated space on a stack.过程的本地名称在堆栈上分配空间。 The size of stack can not be determined at compile time.编译时无法确定堆栈的大小。

Please refer the following tutorial for more about memory management : http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf有关内存管理的更多信息,请参阅以下教程: http : //www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf

Here you go for step-step tech in detail..在这里,您可以详细了解分步技术。

class A{
    static int a; // goes to method area or Permanent-Generation (which is special mem area within Heap)

    static{} // goes to method area or Permanent-Generation (which is special mem area within Heap)

    public static void main(String args[]){ // goes to method area or Permanent-Generation (which is special mem area within Heap)

        A h=new A(); // 1.using the "new" keyword, an object is created in 
                          Heap
                     // 2. using the constructor A(), the memory has been allocated to the newly created object. This is called object instantiation, based on the variables and methods inside this A class.

                     //3. object ref var "h" will be created in stack

                     //4. using = operator, the memory address of newly created object will be assigned to the object ref h which sits inside the stack.

     }
   }

In short Static Blocks, Class, Vars, Methods - sits inside Permanent-Generation area within heap.简而言之,静态块、类、变量、方法 - 位于堆内的永久生成区域内。

Hope this clarifies everyone!!!希望这能澄清大家!!! Thanks!谢谢!

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

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