views:

69

answers:

1

Hi I have a SharePoint WebPart that I made and I override the Render method. My question is how long does an object live before the GC cleans it up? I created a StringReader object in the Render function, I am just wondering will it be disposed of once the page has rendered or will I have to explicitly call the .Close method.

Could this cause the SharePoint server to goto 99%? (w3wp.exe process went to 99% CPU)

Thank you.

+2  A: 

It's better (read:practically mandatory) to call Close. The GC will get it one way or another, but its nondeterministic.

Could this cause the SharePoint server to goto 99%? (w3wp.exe process went to 99% CPU)

Unlikely perhaps briefly, but regardless of what the w3wp process is doing, you want to explicitly release disposable resources whenever you can. When dealing with object that implement IDisposable, think of the GC as a safety net.

Greg Dean
'nondeterministic' is true but hardly the issue here. But leaving it for the GC is inefficient.
Henk Holterman
OP: "My question is how long does an object live before the GC cleans it up?"A: NondeterministicI'd say it's precisely the issue.
Greg Dean