Click here to Skip to main content
14,829,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a List<myclass>myList which contains object of type Class Myclass.

I need to remove an entry from myList and want to remove memory allocation from the heap. I implemented IDisposable and Dispose method. but even after calling dospose method (Myclass.Dispose()) I was able to access the object.

public void Dispose()

Please advise. Thanks !
Updated 10-Jun-13 3:47am

1 solution

You don't have to do anything at all. GC works is some object becomes unreachable. This is explained here:[^].

This also explains why you have been able to access the object. The mere fact that your code had a reference to it makes it impossible to be garbage collected. And why do you think calling GC.SuppressFinalize(this); could possibly clean up memory? it does just the opposite… :-)

You should understand that System.IDisposable is itself unrelated to garbage collection. I know where you come from: you read about some technique in the documentation to GC.SuppressFinalize, which you did not really understand:[^].

But who told you that you should use something like that? Here is the simplest and most effective advice: don't touch GC at all.


Please see my past answers:
Garbage collectotion takes care of all the memory management[^],
deferring varirable inside the loop can cuase memory leak?[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
Memory management in MDI forms[^],
Memory leak in WPF DataBinding[^].

Also, pay attention for some interesting examples of using the using statements and System.IDisposeable in my articles:
Using "using" Statements: DisposalAccumulator[^],
Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].

It's good to implement RAII:[^].

Please see this alternative: Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].

Divymital 10-Jun-13 10:23am
If so, then how shall I dispose this object of class myClass from memory ?
CHill60 10-Jun-13 10:44am
Remove it from the list. As long as there are no other references in scope to that object then GC will take care of it automatically. See the article that SA has referred to
Sergey Alexandrovich Kryukov 10-Jun-13 10:57am
Correct, thank you. 'Dispose' is unrelated to memory. There is no such thing as "dispose from memory". "Disposal" is the abstract notion, it can mean any clean-up action, anything related to your application.
Divymital 10-Jun-13 11:01am
Can I not use Dispose here ? dispose is used to free memory explicitly correct ? If GC is the ultimate process for freeing the memory then why do we have Dispose concept ?
Sergey Alexandrovich Kryukov 10-Jun-13 11:14am
Dispose is unrelated to it. How many times should I explain that? You are lost in pure logic.

Put it this way. Essentially, you are not asking a real question, you are asking something like "why do I think that...?". You first created a misconception, which was influenced by the MSDN article on GC.SuppressFinalize I referenced above. And later, you are using your own misconception to ask further questions.

Formally, the question why do we have Dispose concept it absolutely equivalent to the question "why do we have extension method concept?" or "why do we have ToString concept". The "real" answer would be only: "why not".

Someone would answer you: "Dispose is used to dispose unmanaged resources". In a way, it would be true, as it is really used for this purpose. Only it would not address your concern. The real point of understanding would be realizing the fact that IDisposable is used many unrelated things...

Sergey Alexandrovich Kryukov 10-Jun-13 11:17am
Another point to understand: you cannot free unmanaged memory explicitly. There is no a way. The whole idea is wrong. I could not be done in any safe way, so it never happens, no matter what you do. You have a psychological problem, kind of: your hands are scratching to clean up after yourself, because you just used to it and don't trust, psychologically, automated garbage collection. But you have to.

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