Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the .NET Garbage Collector's stop-the-world effect halt or delay the execution of unmanaged threads and timer callbacks?

I have an application that needs to have code executed at very precise intervals. For that purpose I also need Windows' scheduler resolution increased to 1ms via timeBeginPeriod.

To do that, I have a native C++ dll that handles all the time critical things via callbacks that get raised from a TimerQueueTimer at an interval of 1ms (all native code).

The application itself is a .NET application (C#, so pure CLR). The native dll uses buffering, so the C# code itself does not need to be time critical as long as it gets some execution time every 50 ms or so.

When the garbage collector strikes, would that also halt or delay the execution of any timer callbacks in my program? (In other words: Does the non-deterministicness of a .NET program even spread to native code fragments used by it?) I haven't found an answer to this precise question. A link to the MSDN documentation would be most reassuring.

I'm using .NET 4.0

like image 706
dialer Avatar asked May 20 '13 18:05

dialer


People also ask

Does garbage collector clean unmanaged objects?

So, the garbage collector is nothing but a background thread that runs continuously. Checks for unused managed objects clean those objects and reclaims the memory. Now, it is important to note that the garbage collector cleans and reclaims unused managed objects only. It does not clean unmanaged objects.

What does the garbage collection in .NET do?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

Does .NET have automatic garbage collection?

Automatic memory management is made possible by Garbage Collection in . NET Framework. When a class object is created at runtime, certain memory space is allocated to it in the heap memory.

How is garbage collector invoked in C#?

You can force garbage collection either to all the three generations or to a specific generation using the GC. Collect() method. The GC. Collect() method is overloaded -- you can call it without any parameters or even by passing the generation number you would like to the garbage collector to collect.


1 Answers

My intuition says no - the CLR will only affect managed threads.

  1. The CLR doesn't need to stop unmanaged threads in order to collect garbage.
  2. The CLR shouldn't be aware to non-CLR threads in the process: it would violate isolation. Think of an IIS server, or an SQL server - both host the CLR, but I can't imagine the CLR freezing all threads in the process... Even if WIN32 API functions are used by the user's managed code (stored-proc / web application) to try and manipulate the unmanaged threads in the process, the managed code being hosted shouldn't even have the security permissions needed to manipulate the host's non-CLR threads (to Windows, the CLR is "yet another COM object").

I can't find an MSDN confirmation, but you can try to prove that an unmanaged thread is running during garbage collection: have an unmanaged thread constantly tracing, and have a GC notification mechanism tracing. See that the managed thread's trace continues during the garbage collection process.

(Also worth noticing is that there are several GC "modes", which behave differently)

like image 159
M.A. Hanin Avatar answered Sep 27 '22 19:09

M.A. Hanin