Cleanup unmanaged objects
Object is backbone of object oriented languages. All codes and programs under this structure will follow object lifecycle. Object creation and destruction is very basic idea behind this concept.
MSDN defines object in a simple way "object is an entity, any live or non living thing is object"
but in our practically it's a instance of class.
basically a thing which represent a class, called object.
.Net framework and Object creation
The .NET Framework provides garbage collector which takes care of memory managment tasks like memory allocation and deallocation. When we create NEWobject,
the CLR will search space in managed heap to allocate memory for newly created, The memory allocation continue till space is available in the managed heap,
when memory get's full, garbage collector perform a collection operation to free some memory. it checks for those objects who are no longer used by application, GC will collect those object and
freed up memory for upcoming objects.
Managed and Unmanaged objects
Roughly we can divide objects in to two parts a managed and unmanaged objects
Managed objects are those for which GC having all knowledge. mainly all system defined and inbuilt classes, structures, namespaces are under managed objects
GC completely unaware about these objects and unable to freed up memory allocated by these objects
such as Memory stream, COM Objects, automation objects like word, excel
The GC does not know about unmanaged resources, if you do not clean them up explicitly in your code then you will end up with memory leaks and locked resources.
Disposing unmanaged objects
Let's overcome this issue.
We can use Idisposable interface to dispose all managed and unmanaged resources to freed up memory for other resources
The idea behind the IDisposable interface is to clean up resources in a Highly skilled fashion and clean up unmanaged resources. Even if you do not have unmanaged resources in your code,
but you used heavy managed objects which consumes large memory like images, network connections, fleshy loops etc. if you no longer need these objects then flush them from memory
This code is placed inside the Dispose functions. we can also use IDisposable interface. you should call the Dispose() method when you are finished with the object.
Once you called Dispose() you can stop the GC from calling the dispose function in the finalizer because this is unnecessary overhead on system. we can tell GC not call dispose method
by calling GC.SupressFinalize(this) method.Which make sure that the object will not put in GC's dispose Queue.
If you use unmanaged objects like Image or IOStreams then it has already Dispose function and a Finalizer methods implemented inside, just do not forget to call them
Implementing a way to Dispose Objects
When you create any class, you can use two methods to dispose objects means to freeing memory used by unmanaged resources.
The way are:
1. Use of finalizer (also called Destructor) as a member of your class
2. Implementing the System.IDisposable interface
Let's discuss them in Detail
Constructors usually in action when an instance of a class is created. Conversely, finalizer is called before an object is destroyed by the garbage collector.
However a Finalizer seem to be a great place to free unmanaged resources and perform a general cleanup.
Here is example to implement Finalize
But there is problem with C# finalizer, finalizer may delays the final removal of an object from memory. Objects that do not under finalizer are removed from memory in single GC Cycle,
but objects that are under finalizer need to call two GC cycle, The first pass calls the finalizer without removing the object, and the second cycle actually deletes the object.
that may be impact on performance.
The recommended alternative to finalizer is using System.IDisposable interface.
The IDisposable is basically a interface that ensures a deterministic level for freeing memory from unmanaged resources and avoids the garbage collector–related problems occurs in
case of finalizer.
The IDisposable interface declares a single method named Dispose(), which is without parameters and returns void.
Consider following example in which IDisposable is implemented
Here object directly call Dispose() method and should explicitly free all unmanaged resources. In this way, the Dispose() method provides precise control over when unmanaged resources are
Suppose that you have call Dispose method using class instance like following
Unfortunately, above code fails to free the resources if an exception occurs during processing. so you should write the code as follows using a try...catch...finally block
see the following code
in above code ,
The finally get's executed always regardless of exception occured or not. So here Dispose will call and any resources consumed by Unmanaged class are always freed,
mamory management is big ocean
so this is about short article on Cleanup unmanaged objects, How ever mamory management is big ocean, we can not cover it in single article,This article gives you short idea of it.
we will go in depth step by step in upcoming articles.
Suggestion and Queries always welcome
Thanks to giving valuable information for disposing unmanaged code in c#.
i have created my application in wpf here am found Ram memory increasing problem is load xaml file because using few staticresource and load some style in xaml file.
i want make sure how xaml staticresource and style resource dispose.
Thanks and regards,
Basically Animation control in WPF takes lot memory and can not release it after use you should not use it until it is very much required.
One of the posssible reason is virtualization get turned off due to this it's virtual space get full.
as you have stated that you have take heavy Usage of Static Resources so there are chances of getting virtual sapce full
put following code in XAML file
- Make sure ScrollViewer.CanContentScroll=True
- Make sure VirtualizingStackPanel.IsVirtualizing=True
- Avoid Grouping
- Keep VirtualizingStackPanel as your default panel
more ever you can check following link it will help you to minimize your memory resources