• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads and Garbage Collection

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a million I hadn't realized that I could ask the runtime system itself. Thanks again partner, Gisselle
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic