Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and Garbage Collection

 
Gisselle Szaritsa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for being such a pain , but I have yet another question. Does the garbage collector run in it's own thread? If so, is it considered a user thread or a daemon thread? I heard somewhere that the gc cannot outlive the last user thread - which would make sense because it would be rather silly to run the gc even after the application has ended (which occurs once the last user thread has died and the system kills off all remaining daemon threads). So, does this imply somehow that the gc is a daemon thread??? Thank you!!!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to do this is to ask Java during the runtime and this is the best way you can find out things you are unsure about. Let's try out the following code:

When running this code you get the following output:

You can see that the GC thread (actually called Finalizer) is a daemon thread.
 
Gisselle Szaritsa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million I hadn't realized that I could ask the runtime system itself. Thanks again partner, Gisselle
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finalizer is not the g.c. thread. It is the thread that calls the finalize methods. It is an instance of java.lang.ref.Finalizer.FinalizerThread. In the source code for it there is no implementation of the algorithms used in a g.c.
I guess the g.c. is implemented in the same language as the JVM and is part of it.
By the way, ReferHandler is an instance of java.lang.ref.Reference.ReferenceHandler and it enques the registered Reference objects.
Hey Val, for something curious call your code within the finalize method of 10000 thousand garbage collectable objects, and call System.gc()
When the main thread dissapears, the finalizer still goes on a bit, but a thread named DestroyJavaVM shows up for a while.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that useful information Jose. Now I'm screwed up
Sorry Gisselle if I have provided the wrong information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic