简体   繁体   English

Java(Stack&Heap)-在这个简单的示例中明智地执行内存操作

[英]Java (Stack&Heap) - what happens memory wise in this simple example

Anyone could explain what happens memory wise (Stack & Heap) in this example? 任何人都可以解释在此示例中明智的内存(堆栈和堆)发生了什么? If I understand it correctly, java stores objects on the heap, so i1 will be on the heap... same with string? 如果我正确理解,java将对象存储在堆上,因此i1将在堆上...与字符串相同吗? But what about i2, considering it is a class field declaration. 但是关于i2呢,考虑到它是一个类字段声明。

public ExampleClass {
   Integer i1=new Integer(1);
   int i2 = 2;
   String str = "abc";
}

All of them are stored in the heap. 所有这些都存储在堆中。

As the SO tag says: 正如SO标签所言:

The heap is process memory set aside for dynamic allocation. 堆是为动态分配预留的进程内存。

So any variable will be placed on the heap. 因此,任何变量都将放置在堆上。

However, any primitive types ( int , float , etc) will be stored on the stack **Only if they are allocated locally inside a method). 但是,任何原始类型( intfloat等)都将存储在堆栈上**仅当它们在方法内部本地分配时才可以。

Look here for more info. 在这里查看更多信息。

Very concise: Stack: [i1-addr, 2, str-addr] Heap : [i1-addr : 1, str-addr : 'a','b','c'] . 非常简洁: Stack: [i1-addr, 2, str-addr] Heap : [i1-addr : 1, str-addr : 'a','b','c'] For heap I use notation [addres: value] . 对于堆,我使用符号[addres: value] Of course, heap apart of value heap contains some object information (eg link to .class object) also. 当然,除值堆之外的堆还包含一些对象信息(例如,指向.class对象的链接)。

Nothing happens until you have some code like new ExampleClass() . 在您拥有诸如new ExampleClass()类的代码之前,什么都不会发生。 Once you do that, a new object is allocated on the heap. 完成此操作后,将在堆上分配一个新对象。 Included in that will be references to i1 , i2 , and str . 其中包括对i1i2str引用。 I'm guessing that since you're not in a method, that i2 will be automatically converted behind the scenes to the equivalent of Integer i2 = new Integer(0) . 我猜想因为您不在使用方法中,所以i2将在幕后自动转换为Integer i2 = new Integer(0)的等效项。 All 3 of these references will be pointing to objects also allocated on the heap. 这些引用中的所有3个都将指向也在堆上分配的对象。 Note that strings are immutable so if there is already a String with the value "abc" , then the reference may point to that. 请注意,字符串是不可变的,因此,如果已经有一个值为"abc"String ,则引用可能指向该String

public ExampleClass {
   Integer i1=new Integer(1); //heap
   int i2 = 2; //heap
   String str = "abc"; //permGen
}

In stack stores only local instances/primitives available only to one thread, heap is a shared place (available to any number of threads), permGen is an another part of java memory used to store interned string and loaded classes. 在堆栈中存储仅对一个线程可用的本地实例/基元,堆是共享的位置(可用于任意数量的线程),permGen是Java内存的另一部分,用于存储内部字符串和已加载的类。

All the initialised values are set in the constructor. 所有初始化值都在构造函数中设置。 While in the constructor, the references are placed on the stack and then the fields on the heap. 在构造函数中时,将引用放在堆栈上,然后将字段放在堆上。 Once the constructor returns, the reference to the object itself is still on the stack, but all the fields are only on the heap. 构造函数返回后,对对象本身的引用仍在堆栈上,但是所有字段仅在堆上。

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

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