Killing an instance?

J

Joe Warrington

is it possible to kill an instance of a class, and if so, how?

many thanks

Joe
 
J

John

Tony said:
No.
Read up on garbage collection.

You can assist the garbage collector by assigning any references to the
class to null. It shouldn't make much difference, but does in practice.

John
 
V

Virgil Green

John said:
You can assist the garbage collector by assigning any references to the
class to null. It shouldn't make much difference, but does in practice.

And then you could "suggest" to the garbage collector that it should run by
calling System.gc(). But, it's only a suggestion. There is no guarantee any
collection will be performed.

- Virgil
 
L

Liz

Virgil Green said:
And then you could "suggest" to the garbage collector that it should run by
calling System.gc(). But, it's only a suggestion. There is no guarantee any
collection will be performed.

- Virgil
Does it work if the class is a running thread?
 
T

Tony Morris

You can assist the garbage collector by assigning any references to the
class to null. It shouldn't make much difference, but does in practice.

A very flaky statement.
I certainly do not recommend "assigning any references to the class [sic] to
null" in an ad hoc fashion - this will cause only problems.
In practice, it causes problems - one should understand the garbage
collector before attempting any type of optimisation.

One of the biggest myths on garbage collection is that removing all strong
references to an object (not a class) will make it eligible for garbage
collection.
This fallacy is the basis of your incorrect and somewhat over-generalised
statement.
 
R

Roedy Green

One of the biggest myths on garbage collection is that removing all strong
references to an object (not a class) will make it eligible for garbage
collection.

What else is required?
 
C

Carl Howells

Roedy said:
What else is required?

First, if the object has a finalizer, that finalizer has to be run, and
the object needs to remain strongly unreachable after that.

Then the JVM must decide to clear all soft references to the object,
which it might delay if it isn't running tight on memory. It also has
to clear weak references, but those are usually cleared immediately
after the object is no longer softly reachable. (Phantom references
can't be used to retreive the object in question, so they don't need to
be cleared. They only need to be queued in their reference queue
sometime after the object they were pointing to was GC'd.)
 
R

Roedy Green

Then the JVM must decide to clear all soft references to the object,
which it might delay if it isn't running tight on memory. It also has
to clear weak references, but those are usually cleared immediately
after the object is no longer softly reachable. (Phantom references
can't be used to retreive the object in question, so they don't need to
be cleared. They only need to be queued in their reference queue
sometime after the object they were pointing to was GC'd.)

I think people who set up their own weak/soft references/finalizers
are aware of this. What I'm curious about are any hidden weak/soft
references in ordinary programs.

The other problem is hidden strong references, such as the JVM's
reference to unstarted Threads and hidden Frames.
 
C

Carl Howells

Roedy said:
I think people who set up their own weak/soft references/finalizers
are aware of this.

Perhaps with soft/weak references, they know. But I've seen far too
many people try to treat a finalizer as the same thing as a C++
destructor, not realizing what a mess that makes the GC process. I
wonder why Sun doesn't just mark finalize() as deprecated, and clearly
document how to use phantom references to do the same job with much
better performance.
 
L

Liz

Carl Howells said:
Perhaps with soft/weak references, they know. But I've seen far too
many people try to treat a finalizer as the same thing as a C++
destructor, not realizing what a mess that makes the GC process. I
wonder why Sun doesn't just mark finalize() as deprecated, and clearly
document how to use phantom references to do the same job with much
better performance.

Isn't finalize() supposed to be used to clean up resources that your
program did not get to clean up due to an exception? This is more than
references.
 
R

Roedy Green

Isn't finalize() supposed to be used to clean up resources that your
program did not get to clean up due to an exception? This is more than
references.

are you confusing finally with finalize?
 
X

Xavier Tarrago

Tony Morris said:
One of the biggest myths on garbage collection is that removing all strong
references to an object (not a class) will make it eligible for garbage
collection.
This fallacy is the basis of your incorrect and somewhat over-generalised
statement.

Why a myth? I used to consider that as true. Could you explain...
I build my current coding on this assumption and the use of weak references,
and i did not notice that it was wrong...
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top