C# Garbage Collector Begin/End Callback [duplicate] - c#

This question already has answers here:
Is there an event for when garbage collection occurs in .NET?
(5 answers)
Closed 7 years ago.
I was just wondering if there is an overridable callback for the garbage collector begin/end in the .NET runtime/C#. I will also state that I have no intentions of attempting to control the GC, I am just curious.
And if not, what would be the best way to replicate this behavior?

You can't get notification directly in manged code because managed code is suspended (or at least not guaranteed to run your thread in case of background GC) during GC.
Options:
You can get approximate notification with GC.RegisterForFullGCNotification as shown in
Garbage Collection Notifications article.
You can watch performance counters related to GC if you need approximate time.
You can also capture ETW events to analyze behavior off-line - Garbage Collection ETW Events. There are many links on how to do that - Windows Performance Toolkit and SO questions like Consuming "Event Tracing for Windows" events.
If really necessary you can host CLR yourself and than you'll get notifications about GC - see CLR Indie Out:Hosting CLR and CLR Hosting - part 3. The ultimate method you want when hosting CLR to capture start/end of GC - IHostGCManager::SuspensionStarting.

Related

How to know when gc working [duplicate]

This question already has an answer here:
Monitoring Garbage Collector in C#
(1 answer)
Closed 5 years ago.
After I handle all memory leaks in my application I experience a time to time freeze , most of the freeze happens on weak PC's
I thinking it's exactly the time when GC starts to work.
can I monitor GC invoke time to be sure that that's the problem
Thanks.
There are a number of performance counter related to GC: https://msdn.microsoft.com/en-us/library/x2tyfybc(v=vs.71).aspx
In your case, you should watch the value of # Gen 2 Collections and see if it increases when you experience the freeze. Note that it could also be gen 0 or 1 collections, but they're much less likely to cause a noticeable freeze.

Restart Application If using too much RAM [duplicate]

This question already has answers here:
How to get memory available or used in C#
(6 answers)
Closed 9 years ago.
I'm creating a server (Console App), but after doing some long-term testing I found out it grows eating RAM. For the local test suite, I am not working with much RAM
(8GB-DDR3 #2400MHz)Is there a way (In Program.cs, I assume) to restart the program if it is using over 'x' amount of RAM? Also, one way could be a timed loop/checkup?
You can use GC.GetTotalMemory. It returns an approximate value (long) of how much memory your program has allocated.
You can create a Timer object and make this comparison under the Tick event handler.
For more information, you can look here: http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx
I agree with what others have said about fixing your memory leak.
If you want to restart your program, create a second application that monitors the first process. Then, when memory gets too high in your original app, safely shut it down and allow the second application to launch it again.

C# lock vs Java syncronized - Is there any difference in runtime? [duplicate]

This question already has answers here:
Are there any differences between Java's "synchronized" and C#'s "lock"?
(3 answers)
Closed 9 years ago.
I'm wondering if there is any difference in runtime at lock vs syncronized.
I have learnd that syncronized is a slow operation and outdated at Java.
Today I saw the lock at C# and I'm wondering if they are the same and lock is something I "want" to avoid same as in Java or maybe he is much faster and I want to use it...
Thanks!
1 synchronized is not outdated, java.util.concurrent.locks package simply provides extended functions which are not always needed.
2 Locking is done at CPU level and there is no difference between Java and C# in this regard
see http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
... special instructions, called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others. These memory barriers are usually performed when lock and unlock actions are taken; they are invisible to programmers in a high level language.

What's the benefit of using a weak reference in .NET? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Weak references
I understand the concept of a weak reference, but I am unable to find where I should use a weak reference in C#.
A good example of where to use a WeakReference would be when implementing the EventAggregator pattern.
Say you have the code
eventAggregator.Subscribe<AnEventType>(this.DoSomethingDelegate);
then you will specifically ahve to unsubscribe later if you don't want to have a potential memory leak. See Explicitly Removing Event Handlers for more info.
If however the internals of the EventAggregator hold on to the DoSomethingDelegate using a weak reference, then no unsubscription is necessary.
For further learning, I suggest taking a look at the implementation of EventAggregator in the Microsoft Practices library using ILSpy. This internally uses a WeakReferenceDelegate type which wraps a Weakdelegate and allows subscription without explicit unsubscription and no chance of a memory leak.
Best regards,

.NET framework issue? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# memory leak?
Sorry if this is not the right place to post this, seeing as it's not strictly code related.
Basically, since the beginning of this work, a program which has worked flawlessly before has suddenly started throwing Out of Memory exceptions. After checking out the usage, I discovered the memory usage increases by a ridiculous 1mb per second! This happens on a part of the program that iterates over a collection, and calls many different methods - so it seems like these objects are staying in memory even after use.
What does this issue sound like to everyone else? I've been debugging the code, ensuring nothing's left open and all is disposed, but to no avail. I'm wondering if perhaps the .NET framework on the computer it runs from has become corrupt, and the garbage collection isn't running.
I plan to test on another computer later to hopefully prove this theory
Garbage collection will only clear up objects in the heap with no references left to them. Although you've ensured you're disposing everything it'll also be an idea to specifically null any of these references to objects after use.
Perhaps you can post the troublesome code?
Have a look at dotTrace tool. 10 days trial version is available.
Using it you can track what keeps the object reference to objects that should be removed from memory.

Categories