Re: Garbage collection, threads and GUIs.

From: FISH (joeking_at_merseymail.com)
Date: 07/05/04


Date: 5 Jul 2004 10:22:37 -0700

larsgj@hotmail.com (Lars) wrote in message news:<7468e830.0407042347.7d3aaf8c@posting.google.com>...
> Hi,
>
> I'm using a separate thread to create and display a JFrame filled with
> components. When I click a button in the thread I, 1) set the JFrame
> visible to false, 2) dispose the JFrame and 3) fire an event which is
> detected by the "main program" which then proceeds to interrupt the
> thread. I also set the thread to null.
>
> Question 1: Am I correct in believing that this should make the
> resources used by thread (including all GUI resources) elligible for
> garbage collection?

It depends what the thread does after it is interrupted. Interrupting
a thread does not stop it. Quite the opposite in fact, it will unblock
it (if blocked) allowing it to continue running. If, after firing the
event, the run() method exits then you will have successfully ended the
thread and the JVM's own internal reference to it should be freed, which
makes your own references to it the only remaining links stopping the
thread from being garbage collected.

> I'm wondering, because when I do this repeatedly, my program grows in
> size.

Wrap your thread's run() method in a try/finally block which prints
something out to stdout/err when it terminates. You will be able to
see immediately whether threads are being terminated. Do the same
with finalize() and you'll get a hint as to what is being GC'd.

If you have access to a profiler, try studying the statisics it
produces on object creation.

> Question 2: Am I doing something here that I should not be doing, or
> am I not doing something that I should be doing?

There should be no need to interrupt your thread. Just fire the event
and then exit the run() method. Btw: you only need to keep a reference
to a thread yourself if you plan to manipulate that object after it has
been created. The JVM keeps its own internal reference to all threads
until they terminate - meaning that a thread object which goes out of
scope will not be garbage collected until it stops. (Even if you have
no references to it, the JVM still holds one!)

> Questions 3 and 4: Will the Java VM ever return resources to the
> system? For example if a program temporarily needs 20 Mb of memory,
> will the Java process shrink back down to it's original size?

As and when the JVM acquires and frees memory is, IIRC, specific to
each JVM implementation. I suspect there are a few guidelines in the
JVM spec, but I think the low-level mechanics are left up to each JVM
maker.

Strictly speaking, it is perfectly valid to have a JVM which does not
gargage collect. The spec lays down how a GC should work if present,
but you're not to make any assumptions as to how or when it will do its
job (which also therefore means _if_ it will ever do its job!)

-FISH- ><>



Relevant Pages

  • Re: Garbage collection
    ... Tony is right and this *is* implementation dependent. ... But there's more to it than just the spec. ... JVM implementation is usable for a specific purpose. ... Another JVM implementation might use reference counting if it ...
    (comp.lang.java.programmer)
  • Re: A Java Brainteaser - a Static Factory method Narrative
    ... When a Java programmer invokes a System.gccall it ... >>> memory to the JVM at the earliest algorithmically determined time. ... Look at it this way - when a reference loses scope the JVM throws it ... >>> created by a factory class from a static method ever be clean ...
    (comp.object)
  • Re: A Java Brainteaser - a Static Factory method Narrative
    ... When a Java programmer invokes a System.gccall it ... >>> memory to the JVM at the earliest algorithmically determined time. ... Look at it this way - when a reference loses scope the JVM throws it ... >>> created by a factory class from a static method ever be clean ...
    (comp.lang.java.programmer)
  • Re: Garbage Collection
    ... >>is up to the JVM to decide. ... > Be careful. ... that indicates that a JVM implementation is required ... to hold an inactive Thread reference in the ThreadGroup. ...
    (comp.lang.java.programmer)
  • Re: Why is the main() of Java void ?
    ... its documentation should specify its exit codes. ... The situation in C and Java are not the same. ... a JVM may run multiple Java programs/applets. ... The JVM always terminates successfully, ...
    (comp.lang.java.programmer)