Click here to Skip to main content
16,006,535 members
Home / Discussions / C#
   

C#

 
GeneralRe: debuging timers with in a windows service application Pin
Dave Kreskowiak13-Sep-04 6:14
mveDave Kreskowiak13-Sep-04 6:14 
GeneralRe: debuging timers with in a windows service application Pin
karanba13-Sep-04 20:47
karanba13-Sep-04 20:47 
GeneralNetwork Machine Names from Mapped Drive Letter Pin
ribsmac13-Sep-04 4:17
ribsmac13-Sep-04 4:17 
GeneralRe: Network Machine Names from Mapped Drive Letter Pin
Heath Stewart13-Sep-04 6:25
protectorHeath Stewart13-Sep-04 6:25 
GeneralRe: Network Machine Names from Mapped Drive Letter Pin
Dave Kreskowiak13-Sep-04 6:50
mveDave Kreskowiak13-Sep-04 6:50 
GeneralMemory/Resource Leaks in .NET Pin
Barry Etter13-Sep-04 3:57
Barry Etter13-Sep-04 3:57 
GeneralRe: Memory/Resource Leaks in .NET Pin
Steve Maier13-Sep-04 4:38
professionalSteve Maier13-Sep-04 4:38 
GeneralRe: Memory/Resource Leaks in .NET Pin
Heath Stewart13-Sep-04 6:10
protectorHeath Stewart13-Sep-04 6:10 
Yes there is such a use, but a CLR profiler is a better choice. Many classes in the .NET BCL (and third-party libraries) rely on native resources. These are unmanaged resources because the CLR does not manage them. File handles are a good example. FileStream (actually, any String) implements IDisposable because there may be a file handle (there is for a FileStream) or some other native resource that must be closed when finished. You must call the IDisposable.Dispose implementation when finished with such an object. If you are a class designer, you should follow the dispose pattern discussed in the .NET Framework SDK and other articles:
class MyObject : IDisposable
{
  ~MyObject()
  {
    Dispose(false);
  }
  void IDisposable.Dispose()
  {
    Dispose(true);
  }
  protected virtual void Dispose(bool disposing)
  {
    if (disposing)
    {
      // Release managed resources.
    }
    // Release unmanaged resources.
  }
}
Unmanaged resources will be freed aventually. There is no gaurantee of when the GC runs, however, and you should almost never call GC.Collect (repeated calls can greatly hinder the performance of your application). This is why IDisposable exists. The using block statement is good to make sure that - even in case of exception - your objects are disposed:
using (FileStream file = File.Open("file.txt", FileMode.Open))
{
  // Do something with 'file'.
}
Is the same as:
FileStream file = File.Open("file.txt", FileMode.Open));
try
{
  // Do something with 'file'.
}
finally
{
  if (file != null) ((IDisposable)file).Dispose(0;
}
So, exceptions will still be thrown, but at least the file is disposed and the native file handle is released.

So, in a round-about way, you should be able to see that while objects are garbage collected, resources they use (i.e., unmanaged resources) are not. Using a profile will help you find those problems, although it's a good rule of thumb to always dispose of any object that implements IDisposable anywhere up the class inheritance hierarchy.

Profilers can also help you find bottlenecks in your code; any platform is susceptible to that since it's typically human fault.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles]
GeneralDataGridCell Change Color Pin
Member 119819213-Sep-04 3:33
Member 119819213-Sep-04 3:33 
GeneralRe: DataGridCell Change Color Pin
sreejith ss nair13-Sep-04 3:41
sreejith ss nair13-Sep-04 3:41 
GeneralRe: DataGridCell Change Color Pin
Member 119819213-Sep-04 4:20
Member 119819213-Sep-04 4:20 
Generalurgent about server.Transfer Pin
shambho13-Sep-04 3:27
shambho13-Sep-04 3:27 
GeneralRe: urgent about server.Transfer Pin
sreejith ss nair13-Sep-04 3:43
sreejith ss nair13-Sep-04 3:43 
GeneralAdding help Pin
Diego F.13-Sep-04 2:14
Diego F.13-Sep-04 2:14 
GeneralRe: Adding help Pin
sreejith ss nair13-Sep-04 2:39
sreejith ss nair13-Sep-04 2:39 
GeneralRe: Adding help Pin
Diego F.13-Sep-04 2:48
Diego F.13-Sep-04 2:48 
GeneralRe: Adding help Pin
sreejith ss nair13-Sep-04 2:52
sreejith ss nair13-Sep-04 2:52 
GeneralRe: Adding help Pin
Diego F.13-Sep-04 3:00
Diego F.13-Sep-04 3:00 
GeneralRe: Adding help Pin
Dave Kreskowiak13-Sep-04 4:09
mveDave Kreskowiak13-Sep-04 4:09 
GeneralC# Is it possible to create ms.excel file in VS.Net (C#) Pin
adnanh7512-Sep-04 23:53
adnanh7512-Sep-04 23:53 
GeneralRe: C# Is it possible to create ms.excel file in VS.Net (C#) Pin
sreejith ss nair13-Sep-04 0:16
sreejith ss nair13-Sep-04 0:16 
GeneralRe: C# Is it possible to create ms.excel file in VS.Net (C#) Pin
adnanh7513-Sep-04 0:20
adnanh7513-Sep-04 0:20 
GeneralRe: C# Is it possible to create ms.excel file in VS.Net (C#) Pin
sreejith ss nair13-Sep-04 0:43
sreejith ss nair13-Sep-04 0:43 
GeneralRe: C# Is it possible to create ms.excel file in VS.Net (C#) Pin
David Salter13-Sep-04 0:19
David Salter13-Sep-04 0:19 
GeneralTHANKS :) Pin
adnanh7513-Sep-04 0:46
adnanh7513-Sep-04 0:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.