473,769 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Colle ct() 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 2362
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=Lo adFromFile() 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.Di spose() 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.goo glegroups.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.Colle ct() 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.goo glegroups.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.Colle ct() 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
AddMemoryPressu re/RemoveMemoryPre ssure that can be used when the applciation
expects to use objects that allocate large chunks of unmanaged memory.
--

Stoitcho Goutsev (100)

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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
3386
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 images that have been used and are going to be closed. This is how ever no way to reduce the memory consumption. I have noticed , using the task manager, that garbage collector doesn't actually do any collections unless the computer becomes low...
18
3076
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, when memory becomes limited, the garbage collector is moved up in priority. I believe they must be overly simplified explanations because I would tend to guess that Microsoft would have allowed for more pre-emptive reclaiming of memory to best...
10
2309
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
2210
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 documents that you could point me to help me control the GC, then that would be great also. The .Net GC does not cleanup memory of our service process unless it is forced to by another process that hogs memory. · GC Algorithm - This is an issue...
4
1699
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 fine, but now I am worried about garbage collection. I am concerned that the location of the delegate might be altered as a result of other (unused) objects being garbage collected. This would probably cause undesirable results when the unmanaged DLL...
5
4784
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 call-back mechanism. These calls pass a string that contains a "command" and a pointer to a SafeArray that (depending on the command) either receives data from the ..Net application or provides data to the .Net application. This mechanism is...
28
3187
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 this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions raised (especially due to pointer abuse). Is a GC for C++ just a pipe dream or is there a lot of work...
56
3711
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 = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
0
2738
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 each COM object explicit by a call to Marshal.ReleaseComObject, does the RCW take care of that or does it leaks unmanaged resources?
0
9587
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9993
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.