Abaixo esta um trecho explicando que ainda posso ter vazamento de memória no java, mesmo com o garbage collector, mas eu nao entendi o exemplo dele, de como abandonando o valor retornado ele nao pode ser eleito para ser limpado pelo garbage collector.
The nature of automatic garbage collection has an important consequence: you can still get memory leaks. If you allow live, accessible references to unneeded objects to persist in your programs, then those objects cannot be garbage collected. Therefore, it may be a good idea to explicitly assign null into a variable when you have finished with it. This issue is particularly noticeable if you are implementing a collection of some kind.
In this example, assume the array storage is being used to maintain the storage of a stack. This pop() method is inappropriate:
- public Object pop() {
- return storage[index–];
- }
If the caller of this pop() method abandons the popped value, it will not be eligible for garbage collection until the array element containing a reference to it is overwritten. This might take a long time. You can speed up the process like this: - public Object pop() {
- Object returnValue = storage[index];
- storage[index–] = null;
- return returnValue;
- }
pequeno trecho retirado do livro SCJP & SCJD - Complete Java 2 Certification Study Guide