helo. I'm new to c# and I got the big picture with GC deleting objects that can't be reached by my code. The problem new is that I'm working with some big (7-40 MB) objects , the good thing is that I don't need them all at once so is ther any posible way to delet an object? I tried this:
- create big object X
- use big object X
X = null; /*so I don't have any referance to it any more*/
GC.Collect();
GC.WaitForPendingFinalizers(); - create next big object ....`
Does this guarantee that the big object will be deleted after the GC.WaitForPendingFinalizers();
exits?
I know that I should impromve my design to make the objects smaler but believe me I tried it and it makes the logic to complicated. If there is no other way I'll do it the the code will get 2 or 3 times bigger.
thanks!
Without mensioning your grammar skills, I would say there is no possible way to be 100% sure that the object gets deleted when you want it to.
The GC frees memory as it wants, calling GC.Collect does not free every object without a reference immediately.
It does get deleted, but not at that point. Calling GC.Collect whitout knowing what it actually does is never a good idea! Leave it alone, GC knows what it does.
You don't need to do this. Avoid calling GC.Collect
manually. So if you don't need the objects at the same time, simply leave them out of scope and construct the new object. The garbage collector is smart enough and it will clean them when necessary. For example if you have plenty of free memory it might not bother but if memory starts running low it will collect them. It is highly optimized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With