473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Use of Garbage Collection?

Hi, I want to know what action is performed when we call Dispose() method. Is Object frees all resources quickly on Dispose() call or Dispose() marks the Object is ready for garbage collection. And What happened when we set Object reference to NULL. Actually I have Windows form application in .NET 2.0. And I want to call garbage collector after certain time is passed(For Example after 5 mins) to collect all unreferenced Object.
Jun 30 '10 #1
2 1748
GaryTexmo
1,501 Expert 1GB
I believe it just marks it as ready for garbage collection. Same for when you just set something to null (assuming no other references to that memory are present).

Here's a reference to Microsoft's doc: http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx

Typically, it's actually best to not collect garbage yourself... just let the framework do it's thing. It usually tries to do this at an opportune time so as not to interrupt other processes in cases where there is a lot of memory to free up.

You can interact directly with the GC class, and manually specify garbage collection by calling GC.Collect() but the downside here is that I think it performs garbage collection for your entire system (ie, other processes running .NET) so you run the risk of delaying other programs. I'm not 100% sure about this so you may want to verify. It will definitely slow down your own program though, if there is a lot of memory to free.

Unless you're working in a limited memory environment or allocating an extremely large amount of memory, I wouldn't worry about it. That said, if you do need to tinker, look into the GC class. I thought there were methods in there to configure it, but maybe not? Check it out anyway :)

If you did need to collect every 5 minutes manually, you could always use a Timer object.
Jun 30 '10 #2
The garbage collector in .NET works by traversing all of the currently referenced objects by starting at object references stored in static fields and the stack, then recusively following references in member fields of any of these. Anything that it doesn't eventually reach using this algorithm will be marked as eligible for collection.

The Dispose method is primarily used for releasing unmanaged resources an instance has created. Calling Dispose will not force immediate collection of a managed object, nor will it necessarily mark that object as safe for collection. That will only happen when the object is no longer referenced.

The primary reason to implement or call Dispose is when you're working with some type of resource that needs to be released deterministically, like a file stream. For example, calling Dispose on a FileStream will close the file handle, but will not cause the FileStream instance to be collected immediately.

Note that GC.Collect() will force an immediate collection of all unreferenced objects, but this is generally considered a bad practice unless you are using it for some diagnostic/debugging purpose.

For more information on the purpose of Dispose and IDisposable, check out MSDN: http://msdn.microsoft.com/en-us/libr...5e(VS.71).aspx
Jul 9 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
2
by: Oculus | last post by:
Before I get into the question -- I know .NET isn't the right solution for this app but it's part of my clients requirements and writing this in C++ isn't an option. That being said -- my app is a...
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
55
by: jacob navia | last post by:
Tired of chasing free(tm) bugs? Get serious about C and use lcc-win32. The garbage collector designed by Boehm is the best of its class. Very simple: #define malloc GC_malloc #define free(a)...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.