473,395 Members | 2,222 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,395 software developers and data experts.

Re: memory managment

On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <fo*****@free.fr.invalid>
wrote:
Hello,

When a control is disposed, should it first set its Tag property (if
present) to null ?
If the control will soon be eligible for garbage collection, then no.
If the control won't be eligible for garbage collection soon, then
setting the Tag property to null will allow the object references by
Tag to be collected sooner (assuming there are no other references to
that object).
>I use a commercial control and I saw (with a .NET memory profiler) that
the objects I put in this property were not collected even when the
control is disposed (until I set the tag
property to null)
Calling Dispose has no effect on garbage collection. An object is
eligible for garbage collection when no live object has a reference it
regardless of whether or not Dispose has been called. Dispose simply
frees any resources that the object is holding. For UI controls
Dispose releases the associated Windows control. In no case does
Dispose cause the object to be garbage collected. If Dispose isn't
called explicitly, it will be called when the object is garbage
collected. Explicitly calling Dispose is simply a way to free
resources in a deterministic way, since the object may not be garbage
collected for a very long time.

If you aren't holding any references to the control (it is removed
from its container collection, any event handlers it added for other
objects have been removed, you don't have any variables that hold
reference to it, etc.) then it is eligible for garbage collection and
so is the object that its Tab property references.
Jun 27 '08 #1
2 1011
On Thu, 12 Jun 2008 13:15:03 +0200, "Fred" <fo*****@free.fr.invalid>
wrote:
>"Jack Jackson" <jj******@cinnovations.neta écrit dans le message de
news:19********************************@4ax.com.. .
>On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <fo*****@free.fr.invalid>
wrote:
>>Hello,

When a control is disposed, should it first set its Tag property (if
present) to null ?

If the control will soon be eligible for garbage collection, then no.
If the control won't be eligible for garbage collection soon, then
setting the Tag property to null will allow the object references by
Tag to be collected sooner (assuming there are no other references to
that object).
>>>I use a commercial control and I saw (with a .NET memory profiler)
that
the objects I put in this property were not collected even when the
control is disposed (until I set the tag
property to null)

Calling Dispose has no effect on garbage collection. An object is
eligible for garbage collection when no live object has a reference it
regardless of whether or not Dispose has been called. Dispose simply
frees any resources that the object is holding. For UI controls
Dispose releases the associated Windows control. In no case does
Dispose cause the object to be garbage collected. If Dispose isn't
called explicitly, it will be called when the object is garbage
collected. Explicitly calling Dispose is simply a way to free
resources in a deterministic way, since the object may not be garbage
collected for a very long time.

If you aren't holding any references to the control (it is removed
from its container collection, any event handlers it added for other
objects have been removed, you don't have any variables that hold
reference to it, etc.) then it is eligible for garbage collection and
so is the object that its Tab property references.


Thank you Göran and Jack,

I omitted to say that I was calling GC.Collect in my tests before to
make a memory snapshot. I know that Dispose won't cause the object to be
garbage collected immediatly.

So :
1) Not settings the Tag property to null, I can see in the memory the
disposed control and some of my classes instances (after GC.Collect).
2) Setting the Tag proprty to null, every thing disappear (after
GC.Collect)

According to your explainations, it means, I think, that there were
somewhere some references to the control in my classes (I will have a
look and correct this). A kind of circular reference that I broke
setting the Tag property to null and permit the garbage collector to do
its job ?

So, about my question. Isn't it a best practice, if I develop a custom
control, to set all it's possible extern references (as the Tag
property) to null when disposing ?
If you forced a garbage collection and the control did not get
collected, then yes that implies that something still references it.
There are some tools that will give you information about what is
keeping objects alive.

It certainly won't hurt to null out references, but in general it
should not be necessary. It might help to hide other problems, which
could be either a good thing or a bad thing depending on your point of
view.
Jun 27 '08 #2
Fred wrote:
I omitted to say that I was calling GC.Collect in my tests before to
make a memory snapshot. I know that Dispose won't cause the object to be
garbage collected immediatly.

So :
1) Not settings the Tag property to null, I can see in the memory the
disposed control and some of my classes instances (after GC.Collect).
2) Setting the Tag proprty to null, every thing disappear (after
GC.Collect)
Calling GC.Collect doesn't ensure that all collectable objects are
collected. The GC still decides how extensive the collection should be,
and if it's worth it at all. If there is too little that would be
collected, it might decide to skip the collection altogether. So,
calling GC.Collect can not really be used as a diagnostic tool.
According to your explainations, it means, I think, that there were
somewhere some references to the control in my classes (I will have a
look and correct this). A kind of circular reference that I broke
setting the Tag property to null and permit the garbage collector to do
its job ?
Circular references is not a problem at all for the GC. It looks for
active references, so two objects that reference each other but are not
referenced from somewhere else, are still recognised as unreachable.
So, about my question. Isn't it a best practice, if I develop a custom
control, to set all it's possible extern references (as the Tag
property) to null when disposing ?
That depends on the situation. In most cases it's not worth the trouble,
as the referene will soon be unreachable anyway.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #3

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

Similar topics

1
by: D. Buck | last post by:
Details: MS SQL 2000 dual Intel 1.2 GHz processors. 1 GB RAM 2.1 GB dB Dynamic Memory Managment. No other apps running on this server. First question: Since I have Dynamic Memory Managment...
3
by: Yves Dhondt | last post by:
Hello, I'm looking for information about the loading/unloading of unmanaged dlls. Using the debugger I think it works as follows : the unmanaged DLL is loaded as soon as a call to one of it's...
2
by: Matthias Kwiedor | last post by:
Hi! I have a small ;) application which needs just to load up the Main Form 40MB (there is a IE Instance implementet). After that i managed with own Memory Managment to need just 10 to 20MB more...
7
by: Casey Leamon | last post by:
I've been noticing that all of my .NET apps seem to progressivly use more and more memory. Even after several reworks of the code I can only manage to slow the growth. Is there some Garbage...
1
by: LP | last post by:
Hello .NET experts A group of develpers here are having some arguements about different ways to write program with minimal memory footprint. One method in buisness class creates an instance of...
1
by: Ashish | last post by:
hi all we have tested out web site for performance and we observed that the asp.net process does disposes memory ( as opposed to my earlier observation ), i was just curious if the asp.net...
18
by: CJ Taylor | last post by:
If I wanted to find out exactly how much memory a particular variable was using, how would I go about that. Or where to look for information would be appreciated. Just need a class/namespace ...
13
by: Nikolay Petrov | last post by:
I've got this issue: When I start my application it takes a lot of memory. I guess this is caused by CLR. That's fine. But every time the app performs an action (open new form, load data in...
1
by: bsimon | last post by:
I've read in the doc that every memory allocation done in a spi memory context was freed at the end of the context (after spi_finish) What happens if the context is is initialized inside another...
2
by: Daniel | last post by:
Hi, I have a question regarding the memory managment in stl stacks. I want to use stacks to store a very large amount of numbers (some millions), thus I'm interested in how the stack behaves...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.