Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does the .net garbage collector run?

When I debug .net code, how can I know when the garbage collector runs?

I do not wish to control when the garbage collector runs. I only wish to be able to know when it is running. I have some code that is running out of resources. I know that the resources are not being used; I want to know when the GC is running to free them.

Oh, and the resources I have in mind are connections from a Sql connection pool, not memory :-)

like image 237
Vivian River Avatar asked Jun 21 '10 21:06

Vivian River


People also ask

What triggers garbage collection C#?

Garbage collection occurs when one of the following conditions is true: The system has low physical memory. The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.

How does .NET garbage collector work?

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.

When should you run garbage collector?

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.

In what cases do garbage collectors run?

When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.


1 Answers

garbage collection does not free up your pooled database connections. connections are returned to the pool when you close/dispose the connection object. the connection pool itself manages when it needs to open more connections and close them again based on how hard you are hitting your pool and your configuration of the pool in the connection string. garbage collection is not related to this in any way other than reclaiming the memory allocated to instances of SqlConnection. you are barking up the wrong tree in asking about garbage collection and getting (and accepted) answers that don't have anything to do with what you are after (if i understand your question correctly).

like image 83
Dave Rael Avatar answered Nov 05 '22 17:11

Dave Rael