Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am facing a problem to dispose a object. I created demo application for disposing object. when application is executing it is taking around 5 mb,once i click on button memory usage is raising upto 40mb.If i am clicking number of times memory is usage is increasing.I have taken all the measures to dispose the object after using.

i am putting my code below.


C#
private void button_Click(object sender, EventArgs e)
        {
            using (DataTable dt = new DataTable())
            {
                dt.Columns.Add("Name", typeof(string));
                using (CheckingPerformance objCheckingPerformance = new CheckingPerformance())
                {
                 objCheckingPerformance.method(objCheckingPerformance.method_row(dt));

                }
            }
        }



C#
public class CheckingPerformance:IDisposable
    {
        public DataTable dt = null;
        public DataRow dr = null;
        private bool _disposed;

        public DataTable method(DataTable pdt)
        {
            return pdt;
            
        }

        public DataTable method_row(DataTable dt)
        {
            
                for (int i = 0; i <= 200000; i++)
                {

                    dt.Rows.Add("admin" + i.ToString());
                }
                return dt;
            
        }

            

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // Dispose managed resources
                    //if (_stream != null) _stream.Dispose();
                    if (dt != null) dt = null;
                    if (dr != null) dr = null;
                }
 
                // Dispose unmanaged resources
                //_handle = IntPtr.Zero;
 
                //_stream = null;
                _disposed = true;
            }
        }
 
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~CheckingPerformance()
        {
            Dispose(false);
        }

    }



Please let me know where i am doing mistake.
Posted
Updated 19-May-12 7:43am
v2

That's the problem. What you consider an error is a behavior.
If you want to force garbage collection (for tests) use GC.Collect(). If even with that the memory keeps increasing, then your problem is not related to dispose, but to having non-collectible objects (if you still have variables holding the objects).
But you should not call GC.Collect unless it is for tests purposes or if you are pretty sure it is needed.

In fact, I can show this example to you:
Imagine that you open thousands of files in exclusive mode and puts them into a list.
If you clear the list without disposing the objects, those files will still be opened, and will be closed at the next Garbage Collection (with by default you don't know when it will happen).
If you Dispose all of the files without clearing the list, then the files will be closed (and can be read/written by other programs) but the managed memory used to represent them will still be allocated (and will not be deallocated even if a garbage collection is done).
If you: Clear the list and then call GC.Collect (independently if you dispose before or not) then all the files will be closed and their memory will be reclaimed.

The Dispose, in fact, only allow you to close them immediately, without forcing a garbage collection (as calling the Garbage Collection manually causes a lot of problems regarding to new collections).

So, to test: If you call GC.Collect, the memory should be recovered. And, even with GC.Collect it is possible that some mb are never deallocated, and this is not a bug, is how the .Net manages its memory useage. Those never deallocated memory blocks are reserved to new allocations.
 
Share this answer
 
Comments
Sander Rossel 19-May-12 14:34pm    
My 5 for a good explanation. I know someone who thinks memory management is a non-issue in .NET because .NET does all the work for you.
I recently found out how wrong they were! :D
Member 4586137 19-May-12 14:46pm    
Hi Paulo,

In my case how i can deallocate memory,It is keeping around 40 mb.If you can show me any good example it will be very helpful.
Paulo Zemek 19-May-12 14:59pm    
Probaly you did everything right and those 40mb will be reutilised for new allocations. There is no problem.
I don't know how you are doing your tests... but even when disposing objects, some resources (the memory fot the objects themselves) are kept in memory until the GC decides to collect them.
What Dispose really does is to avoid unmanaged resources to be kept active for more time than needed (for example, a database connection is closed immediately), but the memory used for the .Net class that represents the collection will not be immediately reclaimed.
In the case of the DataTable, all datatable rows will still be in memory until the garbage collector decides to run.
 
Share this answer
 
Comments
Member 4586137 19-May-12 14:12pm    
Hi Paulo,
How i can dispose all datatable rows forcefully in my posted code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900