清除或设置null到java中的对象 [英] clearing or set null to objects in java

查看:294
本文介绍了清除或设置null到java中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在考虑释放Java对象占用的内存。在这样做时,我对如何在Java中复制(浅/深)对象以及如何避免在对象仍处于使用状态时意外清除/无效而感到困惑。

I was recently looking into freeing up memory occupied by Java objects. While doing that I got confused about how objects are copied (shallow/deep) in Java and how to avoid accidently clearing/nullifying objects while they are still in use.

考虑以下场景:


  1. ArrayList< Object> 作为参数传递给方法。

  2. ArrayList< Object> 传递给可由线程处理的可运行类。

  3. ArrayList< Object> 放入 HashMap

  1. passing a ArrayList<Object> as an argument to a method.
  2. passing a ArrayList<Object> to a runnable class to be processed by a thread.
  3. putting a ArrayList<Object> into a HashMap.

现在在这种情况下,如果我调用 list = null; list.clear(); ,对象会发生什么?在这种情况下,对象会丢失,在这种情况下只将引用设置为null?

Now in these case, if I call list = null; or list.clear();, what happens to the objects? In which case the objects are lost and in which cases only the reference is set to null?

我想这与对象的浅层和深层复制有关,但是在哪些情况下会发生浅层复制,在这种情况下是否会在Java中发生深层复制?

I guess it has to do with shallow and deep copying of objects, but in which cases does shallow copying happens and in which case does deep copy happens in Java?

推荐答案

首先,你从未设置过 object 为null。这个概念毫无意义。您可以将 null 的值分配给变量,但您需要非常仔细地区分变量和对象的概念。一旦你这样做,你的问题就会回答:)

Firstly, you never set an object to null. That concept has no meaning. You can assign a value of null to a variable, but you need to distinguish between the concepts of "variable" and "object" very carefully. Once you do, your question will sort of answer itself :)

现在就浅拷贝和深拷贝而言 - 它可能值得避免使用浅层这个术语复制在这里,通常浅拷贝涉及创建一个新对象,但只是直接复制现有对象的字段。深拷贝将获取这些字段引用的对象的副本(对于引用类型字段)。这样一个简单的赋值:

Now in terms of "shallow copy" vs "deep copy" - it's probably worth avoiding the term "shallow copy" here, as usually a shallow copy involves creating a new object, but just copying the fields of an existing object directly. A deep copy would take a copy of the objects referred to by those fields as well (for reference type fields). A simple assignment like this:

ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = list1;

...不执行 浅拷贝或深拷贝在那个意义上复制。它只是复制参考。在上面的代码之后, list1 list2 是独立变量 - 它们碰巧具有相同的值(引用)at此时此刻。我们可以更改其中一个的值,它不会影响另一个:

... doesn't do either a shallow copy or a deep copy in that sense. It just copies the reference. After the code above, list1 and list2 are independent variables - they just happen to have the same values (references) at the moment. We could change the value of one of them, and it wouldn't affect the other:

list1 = null;
System.out.println(list2.size()); // Just prints 0

现在,如果不是更改变量,我们对变量值引用的对象进行更改,该更改也将通过另一个变量显示:

Now if instead of changing the variables, we make a change to the object that the variables' values refer to, that change will be visible via the other variable too:

list2.add("Foo");
System.out.println(list1.get(0)); // Prints Foo

回到原来的问题 - 你永远不会存储实际对象在地图,列表,数组等中。您只存储引用。当没有实时代码到达该对象的方式时,对象只能被垃圾收集。所以在这种情况下:

So back to your original question - you never store actual objects in a map, list, array etc. You only ever store references. An object can only be garbage collected when there are no ways of "live" code reaching that object any more. So in this case:

List<String> list = new ArrayList<String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Foo", list);
list = null;

... ArrayList 对象仍然可以不会被垃圾收集,因为 Map 有一个引用它的条目。

... the ArrayList object still can't be garbage collected, because the Map has an entry which refers to it.

这篇关于清除或设置null到java中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆