Fox-Connely
Fox-Connely

Reputation: 101

Why would GC.AddMemoryPressure deadlock / hang?

Solved: It turns out that the affected machine had .NET 4.5 beta installed.


I have a C# application using .NET4 that deadlocks / hangs indefinitely in a call to

GC.AddMemoryPressure(12000000)

Since it is an in production app I can't really share any code - however I would like to get some hints on what might make GC.AddMemoryPressure hang.

Upvotes: 4

Views: 342

Answers (3)

Tigran
Tigran

Reputation: 62276

Hint from documentation:

You must ensure that you remove exactly the amount of pressure you add. Failing to do so can adversely affect the performance of the system in applications that run for long periods of time.

That means that you have to be very careful in balancing between these two calls:

AddMemoryPresure and RemoveMemoryPreasure

It's, by the way, suggested to use in case when you allocate a large amount of unmanaged resources, in this way signaling GC about the fact that it has to take in a count also that amomunt of memory.

In vast magiority of cases the simple Dispose(), Finalize() correct management would be enough.

Upvotes: 2

Fox-Connely
Fox-Connely

Reputation: 101

Thanks for your input.

It turns out that the affected machine had .NET 4.5 beta installed.

It seems that it wasn't deadlocked since one core was fully saturated. The call to AddMemoryPressure seemed to end up in the function clr!CNameSpace::GcScanRoots that never returned.

Uninstalling 4.5 beta and installing .NET 4 seems to have fixed the problem.

If this was truly the problem then I sure hope MS fix it before release.

Upvotes: 2

iefpw
iefpw

Reputation: 7052

I wouldn't mess with manual GC. Make sure your resources are disposed of and finished after they are used. GC manually can screw up the system. .NET manages GC pretty well as long as objects are deleted in the program.

Upvotes: 1

Related Questions