简体   繁体   English

java中对象的内存管理

[英]Memory management of objects in java

I'm reading this book on data structures and it covers memory management and orphaned objects in Java.The textbook says the following: 我正在读这本关于数据结构的书,它涵盖了Java中的内存管理和孤立对象。教科书说明如下:

For example, consider the three assignment statements in the figure at left. 例如,考虑左图中的三个赋值语句。 After the third assignment statement, not only do a and b refer to the same Date object (1/1/2011), but also there is no longer a reference to the Date object that was created and used to initialize b . 在第三个赋值语句之后,不仅ab引用相同的Date对象(2011年1月1日),而且不再引用已创建并用于初始化b的Date对象。 The only reference to that object was in the variable b , and this reference was overwritten by the assignment, so there is no way to refer to the object again. 对该对象的唯一引用是在变量b中 ,并且该引用被赋值覆盖,因此无法再次引用该对象。 Such an object is said to be orphaned. 据说这样的物体是孤儿。

Code: 码:

Date a=new Date(12, 31, 1999);
Date b=new Date(1, 1, 2011);
b=a;

Is that statement true? 那句话是真的吗? Shouldn't the reference of a (the memory location of object Date(12, 31, 1999) be what the reference of b be? This seems like one huge error but there is even a picture showing memory block for 12, 31, 1999 being the orphaned object. 不应该引用a(对象的内存位置Date(12, 31, 1999)是b的引用吗?这看起来像一个巨大的错误但是甚至有一张图片显示了12,31,1999的内存块是孤儿。

Picture: http://imageshack.us/f/818/3tkx.jpg/ 图片: http//imageshack.us/f/818/3tkx.jpg/

In java you always assign something on the right to a reference to the left . 在java中,您总是将右侧的内容指定给左侧的引用。

So you statements say something like this: 所以你的陈述说出这样的话:

  1. Assign the new Date object Date(12, 31, 1999) to the variable a 将新的Date对象Date(12, 31, 1999)分配给变量a
  2. Assign the new Date object Date(91, 1, 2011) to the variable b 将新Date对象Date(91, 1, 2011)分配给变量b
  3. Assign the reference of variable a to variable b . 将变量a的引用分配给变量b

So it looks like this if I follow those steps: 如果我遵循这些步骤,它看起来像这样:

  1. a -> Date(12, 31, 1999) a - > Date(12, 31, 1999)

  2. a -> Date(12, 31, 1999) b -> Date(1, 1, 2011) a - > Date(12, 31, 1999) b - > Date(1, 1, 2011)

  3. a -> Date(12, 31, 1999) b -> Date(12, 31, 1999) a - > Date(12, 31, 1999) b - > Date(12, 31, 1999)

Please note that after this assignment the original object of Date(1, 1, 2011) is no longer referenced since you cannot reach it from your application. 请注意,在此作业完成后, Date(1, 1, 2011)的原始对象不再被引用,因为您无法从应用程序中获取它。 Its original referencing variable b is overwritten and now the object Date(12, 31, 1999) is referenced from both a and b . 它的原始引用变量b被覆盖,现在从ab引用对象Date(12, 31, 1999) Date(91, 1, 2011) is orphaned and ready to be garbage collected. Date(91, 1, 2011)孤儿 ,准备垃圾收集。

Imagine this as if you were holding a sword and an axe. 想象一下,好像你拿着一把剑和一把斧头。 First you pick up a sword. 首先你拿起一把剑。 Then you pick up the axe. 然后你拿起斧头。 After that you drop the sword and drag the axe you still have in your hand with both hands. 之后,你放下剑,用双手拖动你手中的斧头。 After that you do not hold the sword you dropped (it is lost). 在那之后你没有握住你丢下的剑(它丢失了)。

Edit: This is an error if you tell the author of the book he will be grateful. 编辑:如果你告诉作者这本书他会感激,这是一个错误。

b=a声明放弃的当前值b ,并且因为有到被称为B Date对象的任何其它引用,是的,B日期是孤立的。

You are right, the orphaned object is the one which was pointed to by b , which is Date(1, 1, 2011) . 你是对的,孤立的对象是b指向的对象,即Date(1, 1, 2011) If the picture shows otherwise, then it is definitely wrong. 如果图片显示不然,那肯定是错的。

a --> 31.12.1999     b --> 1.1.2011

                 |
                \ /

a --> 31.12.1999 <-- b     1.1.2011 (orphaned!)

Yep, the 是的,

(1/1/2011) (2011/1/1)

in the text is wrong. 在文中是错误的。 It's most probable a editorial error. 这很可能是编辑错误。 Apart from that everything else is correct. 除此之外,其他一切都是正确的。 b is overwritten with the content of a, which is Date(12, 31, 1999). b被a的内容覆盖,即Date(12月31日,1999年)。 Therefore both variables point to Date(12, 31, 1999) and Date(1, 1, 2011) is orphaned and can be garbage collected. 因此,两个变量都指向Date(12月31日,1999年),而Date(1,1,2011)是孤立的,可以进行垃圾回收。

Maybe you should check an errata of your book. 也许你应该查看你的书的勘误。 :-) :-)

Lets say a 's referring to Date object stored at memory location 123 . 让我们说a是指存储在内存位置123 Date对象。 Going by same logic, lets say b 's referring to Date object stored at memory location 546 .(New object new location). 按照相同的逻辑,假设b指的是存储在存储器位置546 Date对象。(新对象新位置)。

Now when you do b = a . 现在当你做b = a b 's referring to Object @ 123 . b指的是Object @ 123 546 is no longer accessible and can be GC'ed anytime. 546不再可访问,可以随时进行GC。

The sentence is false (the explanation is fine, but he confuses the objects) 句子是假的 (解释很好,但他混淆了对象)

  • The object created with new Date(12, 31, 1999) is referenced two times, by a and b 使用new Date(12, 31, 1999)创建的对象被引用两次, ab
  • The object created with new Date(91, 1, 2011) is not referenced by anyone else after the b=a assigment, so it will be deleted from memory b=a分配之后,其他任何人都没有引用使用new Date(91, 1, 2011)创建的对象,因此它将从内存中删除

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

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