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

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

问题描述

一个关于 Java 网络应用程序性能的小问题.

A little question regarding performance in a Java web app.

假设我有一个 List listRubriques 和十个 Rubrique 对象.

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

一个 Rubrique 包含一个产品列表(List listProducts)和一个客户列表(List; 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 是空的,我之前被这个列表引用的所有对象(包括 listProductslistClients) 将很快被垃圾收集.但是由于 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 :)

edit : 现在让我们假设我的 Client 对象包含一个 List.因此,我的对象之间有一种循环引用.如果我的 listRubrique 设置为 null 会发生什么?这一次,我的观点是我的 Client 对象将变得无法访问"并且可能造成内存泄漏?

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?

推荐答案

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

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 = null 如果变量 listRubriques 之后超出范围(可能是因为它是一个局部变量并且方法在这里结束)也可能不需要.

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).

不仅不需要 clear 调用,因为 clear 访问之后不再使用的对象的内存,对象被访问,现代处理器会将其放入缓存中.因此,一个死对象会显式地进入处理器缓存 - 一些可能更有用的数据将被该操作覆盖.

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.

编辑:对问题中的编辑做出反应:垃圾收集器(至少是 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天全站免登陆