What resources have to be manually cleaned up in C# and what are the consequences of not doing so?
For example, say I have the following code:
myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
// Use Brush
If I don't clean up the brush using the dispose method, I'm assuming the garbage collector frees the memory used at program termination? Is this correct?
What other resources do I need to manually clean up?
Open Disk Cleanup by clicking the Start button . In the search box, type Disk Cleanup, and then, in the list of results, select Disk Cleanup. If prompted, select the drive that you want to clean up, and then select OK. In the Disk Cleanup dialog box in the Description section, select Clean up system files.
Windows suggests different types of files you can remove, including recycle bin files, Windows Update Cleanup files, upgrade log files, device driver packages, temporary internet files, and temporary files.
(If more than one disk or disk partition exists on the computer, the user is first asked to choose a drive before this dialog is displayed.) The disk cleanup manager is part of the operating system. It displays the dialog box shown in the preceding illustration, handles user input, and manages the cleanup operation.
If you don't dispose something, it'll be cleaned up when the garbage collector notices that there are no more references to it in your code, which may be after some time. For something like that, it doesn't really matter, but for an open file it probably does.
In general, if something has a Dispose method, you should call it when you've finished with it, or, if you can, wrap it up in a using
statement:
using (SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
{
// use myBrush
}
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