12

Is garbage collector a daemon (background) thread?

Thanks.

bunty
  • 2,665
  • 8
  • 30
  • 27
  • i think it must be implemented as a native thread – Suraj Chandran Mar 07 '11 at 11:04
  • @sean....thanx..for correction... – bunty Mar 07 '11 at 11:04
  • 1
    @Suraj Chandran: No, a garbage collector doesn't need to live in a separate thread at all. In fact, it's quite complicated to do it that way (although it has some advantages like no interruption of the program like normal "stop-the-world" collectors do). – DarkDust Mar 07 '11 at 11:07
  • Related? http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java – Buhake Sindi Mar 07 '11 at 11:12
  • @DarkDust, actually nowadays the GC and the JIT are run in background high-priority threads. GC in background threads have way too many benefits to pass by. – bestsss Mar 08 '11 at 22:34
  • @bestsss: Good to know, but I assume you talk about Sun's (Oracle's) reference implementation. I guess not every Java implementation does threaded GC. – DarkDust Mar 09 '11 at 06:31
  • 1
    @DarkDust, actually most modern JVMs would take benefit of concurrent GC, provided the system has spare cores. To put it simply: it won't be possible to properly manage large heaps w/o. The pauses will become intolerant. – bestsss Mar 09 '11 at 11:02

5 Answers5

7

I will assume yes, Garbage collector thread is a daemon thread. Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation or other requests for the java runtime system.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 5
    From Java Concurrency In Practice by Goetz et al: "When the JVM starts up, all the threads it creates (such as garbage collector and other housekeeping threads) are daemon threads, except the main thread." – John Jun 23 '11 at 21:25
  • 5
    A daemon thread is simply a thread that does not force the JVM to keep running. i.e. There are two types of threads: non-deamon threads, and daemon threads. Non-deamon threads perform your important work, and daemon threads perform housekeeping. When all non-deamon threads finish running, the JVM shuts down and kills all daemon threads automatically. That's what you wanted when you created the daemon thread, right? Just some housekeeping task that's meant to support the non-daemon threads. – Ken Bloom Aug 23 '11 at 16:56
  • 1
    If the garbage collector is a daemon thread and might be terminated when all the non-daemon threads finish running, does that mean all references may not have been garbage-collected and might still be occupying memory? – ion20 May 28 '17 at 19:10
  • 1
    @ion20 when the JVM shuts down, it frees all memory it ever occupied. No references exist anymore. So, no – xeruf Feb 23 '18 at 03:04
2

It's not a thread from a java.lang.Thread perspective at least.

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • what you mean by it's not from java.lang.Thread ? user space multitask or native implementation. – Dead Programmer Mar 07 '11 at 11:22
  • I mean it's not a thread that you can see and manage in Java unlike other ordinary (includes daemon) threads which are merely instances of `java.lang.Thread`. So, I have indirectly implied that it's a native implementation abstracted from the user. – adarshr Mar 07 '11 at 11:24
0

Yes: http://www.javaperspective.com/daemon-threads.html : (Daemon threads are considered as threads that run in the background and they are generally used as service providers for user threads. For example, the Java garbage collector is a daemon thread)

TT_ stands with Russia
  • 1,785
  • 3
  • 21
  • 33
0

on jdk 1.8 following threads are listed with

ThreadMXBean mxbean = ManagementFactory.getThreadMXBean();
    for(long id:mxbean.getAllThreadIds())
        System.out.println(mxbean.getThreadInfo(id));

Output -

  1. "Attach Listener" Id=5 RUNNABLE
  2. "Signal Dispatcher" Id=4 RUNNABLE
  3. "Finalizer" Id=3 WAITING on java.lang.ref.ReferenceQueue$Lock@63947c6b
  4. "Reference Handler" Id=2 WAITING on java.lang.ref.Reference$Lock@2b193f2d
  5. "main" Id=1 RUNNABLE

There is no GC thread. It can safely be said garbage collection process is native.

-3

A daemon thread is also a thread that continues to run even after the JVM exits. From Oracle documentation When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: •The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. •All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

So if GC is a daemon thread, it should be a native thread spawned by the java run time, but can continue to run after he JVM exits

srajan
  • 185
  • 2
  • 8
  • 2
    "A daemon thread is also a thread that continues to run even after the JVM exits." No, there can't be Java threads after the JVM exits. – xeruf Feb 23 '18 at 03:05