473,396 Members | 1,997 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,396 software developers and data experts.

Garbage Collector and unmanaged resources

There are various contradictory newsgroup postings on this issue, but I
would really like a definitive answer from the .NET gurus out there?

We have various WinForms that contain multiple Icons and Bitmaps that
are loaded onto the form dynamically, eg control.Icon = ;. Should we
explicitly Dispose of these unmanaged resources when the form closes OR
should we leave it up to the garbage collector to remove these (and
will it definitively remove them?).

Currently, we are NOT disposing of them. However, we are experiencing
intermittent GDI errors, eg A generic error occurred in GDI+,
particularly on terminal server environments AND the GDI count (and
memory usage) grows as the user uses the system, but does not reduce
when the user closes the forms, despite forcing a call to
System.GC.Collect() when closing the form. Perhaps these issues are not
even related?

My understanding was that the recommended way was to let the garbage
collector sort things out, but I am now starting to wonder if this is
correct with applications that use a lot of unmanaged resources.

Apr 4 '06 #1
4 2342
GC calls finalizers, not Dispose. And the way GC works, there is no
guarantee when it will do it.
GC might not even fire after a long time, so the unmanaged resources remain
there for a long time.

This is why there is a need for an explicit release of unmanaged resources,
called dispose pattern,
embodied by the Dispose method.

WinForms runtime does call Dispose, when the form is closed (expect some
cases for MDI applications)
for all the controls it has, so you do not need to call Dispose explicitly.

If you do control.Icon=LoadFromFile() you create a new Icon object that is
holding a OS handle in it.
If you repeate the instruction, you in effect, create a new object with OS
handle in it but you leave the previous
object 'hanging' around. The previous object is could be marked unreacheable
so it will be collected someday, and finalized
someday+later. And it's only on someday+later that the OS handle will be
released.

You is why should call Control.Icon.Dispose() before assigning the new icon
for it (if it's not null).
This way you release the object and it's handle immediatly.

Whenever feasible, Dispose correctly (=explicitly and early). It helps to
keep the environment clean.

Laura

"PromisedOyster" <Pr************@hotmail.com> ha scritto nel messaggio
news:11**********************@j33g2000cwa.googlegr oups.com...
There are various contradictory newsgroup postings on this issue, but I
would really like a definitive answer from the .NET gurus out there?

We have various WinForms that contain multiple Icons and Bitmaps that
are loaded onto the form dynamically, eg control.Icon = ;. Should we
explicitly Dispose of these unmanaged resources when the form closes OR
should we leave it up to the garbage collector to remove these (and
will it definitively remove them?).

Currently, we are NOT disposing of them. However, we are experiencing
intermittent GDI errors, eg A generic error occurred in GDI+,
particularly on terminal server environments AND the GDI count (and
memory usage) grows as the user uses the system, but does not reduce
when the user closes the forms, despite forcing a call to
System.GC.Collect() when closing the form. Perhaps these issues are not
even related?

My understanding was that the recommended way was to let the garbage
collector sort things out, but I am now starting to wonder if this is
correct with applications that use a lot of unmanaged resources.

Apr 4 '06 #2
PromisedOyster,

You should dispose bitmaps and icons as soon as you finish using them.
However for icons that are loaded as a control's icon I don't think you
should worry disposing them explicitly.

Keep in mind that modeless forms (shown with Form.Show) are disposed upon
close where modal dialogs (shown with Form.ShowDialog) are not and need to
be disposed manually otherwise they will be lurking alive in the memory and
probably leaking resources. If you are not reusing modal dialogs dispose
them when the user closes a dialog and the data is extracted.
--
HTH
Stoitcho Goutsev (100)

"PromisedOyster" <Pr************@hotmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
There are various contradictory newsgroup postings on this issue, but I
would really like a definitive answer from the .NET gurus out there?

We have various WinForms that contain multiple Icons and Bitmaps that
are loaded onto the form dynamically, eg control.Icon = ;. Should we
explicitly Dispose of these unmanaged resources when the form closes OR
should we leave it up to the garbage collector to remove these (and
will it definitively remove them?).

Currently, we are NOT disposing of them. However, we are experiencing
intermittent GDI errors, eg A generic error occurred in GDI+,
particularly on terminal server environments AND the GDI count (and
memory usage) grows as the user uses the system, but does not reduce
when the user closes the forms, despite forcing a call to
System.GC.Collect() when closing the form. Perhaps these issues are not
even related?

My understanding was that the recommended way was to let the garbage
collector sort things out, but I am now starting to wonder if this is
correct with applications that use a lot of unmanaged resources.

Apr 4 '06 #3
I agree with Stoitcho, I think. :-)

If the Designer has generated the code to assign an Icon to a control
or a form, then I don't believe that it bothers to put code in the
form's Dispose method to dispose of those icons.

However, if you're loading the icon or image yourself, then you should
put appropriate code in your form's Dispose method to dispose of them.
As Laura said, the earlier you Dispose of them, the better, from a
memory perspective.

Apr 4 '06 #4
There is even more to that. Objects like bitmaps for example keeps almost
all of their wheight in the unmanaged heap. The GC doesn't care about it, it
cares only for the managed heap where the small Bitamap object leaves (all
its data is in the unamaned heap). So it is possible that unreferenced
bitmaps doesn't put preasure on the managed heap, thus the GC won't kick in
and objects won't be finalized.

To solve this problem in .NET2.0 GC class introduces new pair of methods
AddMemoryPressure/RemoveMemoryPressure that can be used when the applciation
expects to use objects that allocate large chunks of unmanaged memory.
--

Stoitcho Goutsev (100)

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
I agree with Stoitcho, I think. :-)

If the Designer has generated the code to assign an Icon to a control
or a form, then I don't believe that it bothers to put code in the
form's Dispose method to dispose of those icons.

However, if you're loading the icon or image yourself, then you should
put appropriate code in your form's Dispose method to dispose of them.
As Laura said, the earlier you Dispose of them, the better, from a
memory perspective.

Apr 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all...
18
by: Rein Petersen | last post by:
Is there any way to adjust thread priority for the garbage collector? Would be nice if I could tune the thread priority rules for the garbage collector too... From my readings of the process,...
10
by: Ray5531 | last post by:
Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself?? Is there away to to do so? Thanks
18
by: Larry Herbinaux | last post by:
I'm having issues with garbage collection with my long-standing service process. If you could review and point me in the right direction it would be of great help. If there are any helpful...
4
by: R. MacDonald | last post by:
Hello, all, I have a .NET application (VB) that passes the address of a delegate to unmanaged code in a DLL. The unmanaged code then uses the delegate as a call-back. This seems to work...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
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...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
0
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.