C# - High memory consumption

Asked

Viewed 593 times

2

I’m having memory consumption issues with an application that receives real-time news alerts. Whenever a news arrives, when opening this news, the memory consumption grows, but does not decrease in the same proportion when the form is closed. I tried to call the GC explicitly when closing the form but it seems that did not solve.

GC.Collect();
GC.WaitForPendingFinalizers();

Comparing the consumption recorded by Diagnostic tools, I can see that consumption is always increasing and never returns close to the original level when the application was initialized.

I don’t know what points of code I should check to try to decrease memory consumption.

  • Calling GC doesn’t make it release memory directly, makes it CHECK for unused objects and finally frees up memory used by them. If calling the GC does not work, he must be understanding that the objects are being used.

2 answers

0

The GC (Garbage Colector) only liberates memory of Managed code and/or Managed Resources, where in cases that are not included in this scenario it is necessary to implement routines to free "manually" the memory.

The objects you are using in the news collection process may be using resources that cannot be released by GC, as well as files, network connections, database connections, etc.

The solution is to implement, for example, the Disposable Pattern, where it allows memory and object to be correctly released.

Another way is to make one override to the destructive method (~MeuMetodo()) and wipe the memory there.

More information on this subject can be found in the Microsoft:

Cleaning Up Unmanaged Resources

Or a good article on the subject at Codingame:

Garbage Collection and C#

  • I tried to implement the Idispoable interface and to overload the destructor method, but I could observe that in no time it goes through the Dispose method or the destructor. Apparently, while these objects are in memory inside the Workspace that controls the windows, it has not gone through these methods.

0

You are probably not cleaning the objects relating to news story as the form is closed.

The Garbage Collector (GC) will clean objects that are not being referenced and promote to another generation old objects that are still being referenced.

To identify what these objects are, run a Memory Profiler. Recommend ANTS: https://www.red-gate.com/products/dotnet-development/ants-memory-profiler/index

Free for 14 days. In some iterations you will be able to identify which is the object in question.

  • Is there any opensource tool that shows me which objects consume the most memory?

  • There is a tool from Microsoft itself that can help: the CLR Memory Profiler: https://www.microsoft.com/en-us/download/details.aspx?id=16273, it will give an indication of how memory grows and the use of GC but does not show exactly what the object is. With VS 2015 Update 1 or later you can try memory profiler embedded with VS: https://docs.microsoft.com/en-us/visualstudio/profiling/memory-usage?view=vs-2015.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.