Java集合和垃圾收集器 [英] Java Collections and Garbage Collector

查看:216
本文介绍了Java集合和垃圾收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关Java Web应用程序性能的一点问题。

A little question regarding performance in a Java web app.

让我们假设我有一个 List< Rubrique> listRubriques 与十个 Rubrique 对象。

Let's assume I have a List<Rubrique> listRubriques with ten Rubrique objects.

A Rubrique 包含一个产品列表( List< product> listProducts )和一个客户端列表( List< Client> listClients )。

A Rubrique contains one list of products (List<product> listProducts) and one list of clients (List<Client> listClients).

如果我这样做,会发生什么:

What exactly happens in memory if I do this:

listRubriques.clear(); listRubriques = null;

我的观点是,因为 listRubriques 为空,所有我的对象以前引用的列表(包括 listProducts listClients 不久。但由于在Java中的Collection有点棘手,而且我的应用程序有相当的性能问题,我问的问题:)

My point of view would be that, since listRubriques is empty, all my objects previously referenced by this list (including listProducts and listClients) will be garbage collected pretty soon. But since Collection in Java are a little bit tricky and since I have quite performance issues with my app i'm asking the question :)

编辑:现在我们假设我的Client对象包含一个 List< Client> 。因此,我有一个在我的对象之间的循环引用。如果我的 listRubrique 设置为 null ,会发生什么?这一次,我的观点是,我的客户端对象将变成无法访问,并且可能会 创建内存泄漏。

edit : let's assume now that my Client object contains a List<Client>. Therefore, I have kind of a circular reference between my objects. What would happen then if my listRubrique is set to null? This time, my point of view would be that my Client objects will become "unreachable" and might create a memory leak?

推荐答案

实际的Sun实现为Java拷贝不时引用所有被引用/活的对象。从中复制的空间可以再次用于内存分配。

The actual Sun-implementation for Java copies from time to time all referenced/living objects. The space from which was copied can then be used again for memory allocation.

这说明你的例子损害了实际的性能。 listRubriques.clear()是不需要的(除非你拥有其他地方对它的引用),因为listRubrique引用的所有东西都是垃圾的,listRubriques不再被引用。如果变量listRubriques在之后超出范围(可能是因为它是一个局部变量,并且方法在这里结束),还可能不需要 listRubriques = null

That said your examples damages actually performance. listRubriques.clear() is unneeded (except you hold somethere else a reference to it), because everything referenced by listRubrique is garbage the moment listRubriques is no longer referenced. listRubriques = null may also unneeded if the variable listRubriques go out of scope afterwards (possibly because it's a local variable and the method ends here).

不仅不需要调用清除,因为清除访问对象的内存,而后者不再使用,对象被访问,现代处理器将其放入其缓存中。所以一个死对象明确地去处理器缓存 - 一些可能更有用的数据将被该操作覆盖。

Not only the call to clear is unneeded, as clear accesses the memory of the object that is afterwards no longer in use, the object is accessed and modern processors will put it into their cache. So a dead object is explicitly going to the processor-cache - some possibly more useful data will be overwritten for that operation.

本文是获取有关Java垃圾收集器更多信息的好参考。

This article is a good reference to get more informations about the Java Garbage collector.

EDIT :回答问题中的编辑:垃圾收集器(至少Sun使用的实现)从一些根引用开始,并复制所有对象可以从这个引用到达并且被复制的对象引用。所以你的循环引用对象是垃圾,因为没有'外部'引用指向它们,并且内存将在垃圾回收中被回收。

EDIT: To react on the edit in the question: The Garbage collector (the implementation used by Sun at least) is starting from some root references and copies all objects it can reach from this references and that are referenced by the copied objects. So your circular referenced objects are garbage, as no 'outer' reference is pointing to them and the memory will be reclaimed in the garbage collection.

这篇关于Java集合和垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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