Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources that have to be manually cleaned up in C#?

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?

like image 549
Gary Willoughby Avatar asked Sep 22 '08 19:09

Gary Willoughby


People also ask

How do I clean my C drive?

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.

What files can I delete from Windows 10 to free up space?

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.

Who is responsible for cleaning disk space on a compute?

(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.


1 Answers

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
}
like image 185
Khoth Avatar answered Oct 04 '22 15:10

Khoth