Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the garbage collector running in a separate process?

Does the garbage collector start in a separate process?

For example:

  1. If we try to measure process time taken by some piece of code and during this the garbage collector starts collecting, will it start on a new process or in the same process?

  2. Is it working like the following?

    //Code (Process 1) --> Garbage Collector Run (Process 1) //Code (Process 1) 

    Or like this?

    //Code (Process 1) --> Garbage Collector Run (Process 2) //Code (Process 1) 
like image 336
Omega Avatar asked Mar 23 '15 10:03

Omega


People also ask

Is garbage collector a process?

Java garbage collection is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

Does garbage collection happen automatically?

In Java, garbage collection happens automatically during the lifetime of a program. This eliminates the need to de-allocate memory and therefore avoids memory leaks. Java Garbage Collection is the process by which Java programs perform automatic memory management.

When and how garbage collector is invoked?

When the JVM doesn't have necessary memory space to run, the garbage collector will run and delete unnecessary objects to free up memory. Unnecessary objects are the objects which have no other references (address) pointing to them.

What does the garbage collector run?

Garbage collection in Java is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short.


1 Answers

The garbage collector runs the thread that triggered garbage collection, on the very same process. It stops all current thread, and executes itself. It doesn't start another process for sure, you would have seen that in Windows.

From MSDN:

Before a garbage collection starts, all managed threads are suspended except for the thread that triggered the garbage collection.

(This applies to workstations only, as pointed out by DrKoch). Servers have a background thread running for garbage collection.

If you search in the referenced documentation for "Concurrent garbage collection", you will the text "GC thread", which supporting this.

You can force to run garabage collection in a separate thread, if you want to. Put this in your app.config:

<configuration>    <runtime>       <gcServer enabled="true"/>    </runtime> </configuration> 

(from this answer)

Also read The .NET Framework 4.5 includes new garbage collector enhancements for client and server apps, as suggested by Adam Houldsworth, about changes in the way the garbage collector works since .NET 4.5.

like image 102
Patrick Hofman Avatar answered Sep 28 '22 03:09

Patrick Hofman