simple Garbage Collection question.

C

Chumpmeister

Can someone confirm this for me:

I have a collection, e.g. a Vector or Array and have stuffed a few
objects into it. After using the collection, such as iterating it's
contents, I then nullify the collection. At this point are the
collection's object's eligible for garbage collection, as well as the
Vector itself?.. i.e. I haven't caused a memory leak. I'm assuming
here that there are no other references to the objects that I placed
in collection.

I ask this because I've read numerous articles warning that 'leaving'
objects in an array is the easiest way to create memory leaks... but
surely a Vector for example, is simply an object that references the
objects it 'contains'.

thanks
 
C

Chris Smith

Chumpmeister said:
I have a collection, e.g. a Vector or Array and have stuffed a few
objects into it. After using the collection, such as iterating it's
contents, I then nullify the collection. At this point are the
collection's object's eligible for garbage collection, as well as the
Vector itself?.. i.e. I haven't caused a memory leak. I'm assuming
here that there are no other references to the objects that I placed
in collection.

The phrase "nullify the collection" is not one that has a generally
agreed upon meaning, so you'll have to be clear about what you mean
before you can get a definite answer to that. If you're asking whether
you have to remove objects from a collection before the GC can get at
them, then no you most certainly do not, subject to normal conditions of
reachability.

The actual question of whether a specific object may be garbage
collected, of course, would also have to take into account the
possibility of other references to the object outside of the collection,
as well, as well as fully consider the reachability of the collection
that does reference the objects (again I am unsure what you mean by
"nullify the collection", which might account for this).
I ask this because I've read numerous articles warning that 'leaving'
objects in an array is the easiest way to create memory leaks... but
surely a Vector for example, is simply an object that references the
objects it 'contains'.

Right. The articles you are referring to are warning about the general
problem of being sloppy by keeping references around that you "know"
that you're not going to use any more. So if, for example, you were
implementing a task queue that keeps a reference to an array or List of
tasks, you'd have to remove those tasks when you're done, and leaving
them in when there's no possibility that the code will ever release the
queue or remove the tasks would constitute a memory leak. That general
level of discipline in data management only makes sense, of course, even
in a non-garbage-collected language -- it's just good practice not to
ever be haphazard about your management of pointers and references.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chumpmeister

Thanks Chris ... you clarified what I was asking by concluding with a
real world example. I see that in that case the array or List keeps a
reference to the object so the GC can't reach it, but there's no
longer a use for it so it just takes up memory, consituting a leak,
unless I set the array to null releasing the remaining reference to
the object(s). In the example you gave, I'd assume that task queue
array is around for the life of the application and therefore,
gradually eating up memory with it's unreleased refferences.

To clarify what I meant by nullify, I meant setting my array to null
e.g. myArray = null;

Sorry for the amiguity and thanks for the clarification... I needed a
clear example like this for discussion.
 
C

Chris Smith

Chumpmeister said:
To clarify what I meant by nullify, I meant setting my array to null
e.g. myArray = null;

Ah, okay. When we're talking about garbage collection, it's a good idea
to be precise -- i.e., to say that you've set one *particular* reference
to your array to null. As long as there are no other references, then
all of my comments apply. If there are potentially other references,
then setting just one of them to null doesn't necessarily cause the
array to be eligible for garbage collection, and you may still have a
problem.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Michael Borgwardt

Chumpmeister said:
I ask this because I've read numerous articles warning that 'leaving'
objects in an array is the easiest way to create memory leaks... but
surely a Vector for example, is simply an object that references the
objects it 'contains'.

As is an array, and in fact Vector uses an array internally to store its
references.

What the articles meant is that you often forget about references kept in
some collection, especially if they're put in there as a side effect.
But this is only a problem if the collection itself is not egligible for
garbage collection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top