Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Where do they go?

9 views
Skip to first unread message

Andy Hill

unread,
Dec 9, 2004, 4:07:46 PM12/9/04
to
"2471" <AdamP...@gmail.com> wrote:
>I have just started programing and have started with java. It seems to
>be going pretty good, but i have a question that i can not find an
>answer to. When creating a data structure such as a linked list, i am
>not sure how to remove a node. I mean i know how to make nothing point
>to it so it is no longer in the linked list but how do i go about
>removing it. The problem seems rather unimportant when using a small
>sample size, but if say my linked list had 1,000,000 nodes and i remove
>500,000, it seems like a waste of computer memory to just have those
>500,000 nodes just hanging around with nothing pointing to them.
>
Um, that's what the garbage collector is for.

Steve W. Jackson

unread,
Dec 9, 2004, 4:16:59 PM12/9/04
to
In article <5kfhr057t9j8nl2jp...@4ax.com>,
Andy Hill <andy...@hp.com> wrote:

Yep. And it might be worth looking into java.util.LinkedList for grins.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama

2471

unread,
Dec 9, 2004, 3:55:34 PM12/9/04
to

Steve Horsley

unread,
Dec 9, 2004, 5:10:10 PM12/9/04
to
Java has a thingy called a garbage collector. It goes round retrieving
the memory from objects that have been forgotten by the program,
meaning objects that can no longer be reached by any active thread.
You can forget an object just by overwriting all references to it,
or letting them go out of scope (like when a method returns).

You cannot instruct the GC to reclaim an object (e.g.
garbageCollect(myObject)) because if you have a reference to the
object (to point the GC at it) then by definition the object is
still reachable and therefore not eligible for collection.

Beginners worry about groups of objects with references to each
other (e.g. in loops) being ineligible for GC, but the GC is
cleverer than that - it doesn't use reference counting. You can
have a circular linked list full of objects, and if you lose your
last reference to the list, a thousand objects may become eligible
simultaneously.

A garbage collection sweep tends to happen when allocating new
objects, but it really is up to the VM to GC whenever it feels
is appropriate. The VM will try to reclaim all it can before it
ever throws an OutOfMemoryException.

Steve

Kieron Briggs

unread,
Dec 9, 2004, 6:48:08 PM12/9/04
to
Steve Horsley wrote:
> Java has a thingy called a garbage collector. It goes round retrieving
> the memory from objects that have been forgotten by the program, meaning
> objects that can no longer be reached by any active thread.
> You can forget an object just by overwriting all references to it, or
> letting them go out of scope (like when a method returns).

For the visually minded, here's a parable I use when explaining garbage
collection to people:

Picture a big empty room with a big furnace/incinerator type thing at
one end. Hanging form the roof are a number of ropes, called Threads.
Attached to the various threads are little sparkly Objects, and those
Objects can have other Object attached to them in turn by little rods
called References. This creates a (hopefully) beautiful structure of
Objects all attaches (either directly or indirectly) to a Thread. You
can even have Objects which link to more than one Thread.

When an Object isn't needed any more, the Reference to it disappears. If
that leaves the Object (or a whole collection of Objects) unattached to
any Thread, it will fall down onto the floor and shatter. This gradually
builds up a layer of broken objects lying around the floor. In a
language like C, eventually the floor would become so full that there
was no room for more Objects, and Bad Things would happen.

But in Java, we have a Garbage Collector. This is a little dude in
overalls that climbs down a special Staff Only Thread, sweeps up all the
broken Objects on the floor, and shovels them into the incinerator. You
never know exactly when he's going to come along, but he's always there
keeping an eye on the mess on the floor to make sure that it doesn't
fill up too much...

Kieron

2471

unread,
Dec 9, 2004, 8:43:42 PM12/9/04
to
Thanks to everyone for all the help

Paul Cager

unread,
Dec 10, 2004, 8:33:26 AM12/10/04
to
Kieron Briggs wrote:

> For the visually minded, here's a parable I use when explaining
garbage
> collection to people:

> << snip >>
> Kieron

I think that was the best description of GC I've heard...

So a java.lang.WeakReference must be exactly what it sounds like - a
reference made out of something so weak that it cannot support the
weight of an object. When the last "strong" reference is gone the weak
reference breaks and the object falls to the floor.

And a SoftReference must be like a cobweb. It is strong enough to
support the object, but when the janitor thinks things are getting a
bit too cluttered he gets a broom and sweeps all of the cobwebs away.
And of course mark/sweep GC makes perfect sense now...

Chris Uppal

unread,
Dec 10, 2004, 8:50:52 AM12/10/04
to
Paul Cager wrote:

> And of course mark/sweep GC makes perfect sense now...

The janitor's name is Mark.

-- chris


Thomas G. Marshall

unread,
Dec 10, 2004, 1:36:01 PM12/10/04
to
Chris Uppal coughed up:


LOL. Oh too good. That was a perfect comment.

--
Everythinginlifeisrealative.Apingpongballseemssmalluntilsomeoneramsitupyourn
ose.


Michiel Konstapel

unread,
Dec 10, 2004, 10:07:52 PM12/10/04
to
>> And of course mark/sweep GC makes perfect sense now...
>
> The janitor's name is Mark.

*grins*

0 new messages