Java collections and garbage collectors

A small question about performance in Java Web applications

Let's suppose I have a list < rubrique > listrubriques and ten rubrique objects

Rubrique contains a list of products (list < Product > listProducts) and a list of clients (list < client > listclients)

If I did, what happened in my memory:

listRubriques.clear(); listRubriques = null;

My point is that since listrubriques is empty, the objects I previously referenced by this list (including listProducts and listclients) will be garbage collected soon But because the collection in Java is a bit tricky, because my application has many performance problems, so I ask this question:)

Editor: now let's assume that my client object contains a list < client > Therefore, I have a circular reference between objects What happens if my listrubrique is set to null? This time, my view is that my client object will become "unreachable" and may cause memory leakage?

Solution

The Java implementation of the actual Java implementation copies all reference / active objects from time to time The copied space can be used again for memory allocation

This means that your example undermines actual performance listRubriques. Clear () doesn't need to (except you reference it elsewhere), because everything listrubrique references is garbage, because listrubriques is no longer referenced If the variable listrubriques is out of range (probably because it is a local variable and the method ends here), listrubriques = null may not be required

Not only do you need to clear the call, because the memory to clear the access object is no longer used, the object is accessed, and modern processors put it into its cache So a dead object is explicitly cached by the processor - some data that may be more useful will be overwritten for the operation

This article is a good reference for getting more information about the Java garbage collector

Editing: react to problem editing: the garbage collector (at least the implementation used by sun) starts with some root references, copies all objects that can reach from this reference, and copies object references So your circularly referenced objects are garbage, because no "external" references point to them, and memory will be recycled in garbage collection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>