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

GC collection question...



I have an Object " MyEmploye " :



Class MyEmploye

{

private string Name;

private string Surname;

}


Instances of this object are referenced by instances of an ArrayList "MyArrayList" . One instance of MyArrayList is referenced by the "Tag"' property of each Item of a ListView "MyListView".



"Reference" tree is :

- ListView

- ListViewItems

- Tag

- MyArrayList

- MyEmploye





My question is :



When I call "MyListView.Dispose();" does the GC free the memory allocated for the all the instances of "MyEmploye" objects ?



Any help will be appreciated,



Thanks,



Steph.
Nov 17 '05 #1
10 1630


Steph. wrote:
Class MyEmploye
Please, don't use My-prefix for your classes/variables. just call them
what they are.
When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?


No, not *exactly*. The GC will free that memory whenever it sees fit.
That shoudn't bother you, that's how a GC works.

If Dispose() clears MyListView.Items then the references to MyEmploye
objects are gone and they should be elegible for garbage-collection.

If Dispose() does *not* clear MyListView.Items, the MyEmploye objects
become elegible for GC when MyListViewItem becomes elegible for GC,
which is usually very soon after .Dispose() since you cannot do anything
with it.

The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #2


Steph. wrote:
Class MyEmploye
Please, don't use My-prefix for your classes/variables. just call them
what they are.
When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?


No, not *exactly*. The GC will free that memory whenever it sees fit.
That shoudn't bother you, that's how a GC works.

If Dispose() clears MyListView.Items then the references to MyEmploye
objects are gone and they should be elegible for garbage-collection.

If Dispose() does *not* clear MyListView.Items, the MyEmploye objects
become elegible for GC when MyListViewItem becomes elegible for GC,
which is usually very soon after .Dispose() since you cannot do anything
with it.

The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3
Thanks for the Help !!

:-)

Steph.

"Helge Jensen" <he**********@slog.dk> wrote in message news:eo*************@TK2MSFTNGP11.phx.gbl...
Steph. wrote:
Class MyEmploye
Please, don't use My-prefix for your classes/variables. just call them
what they are.
When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?


No, not *exactly*. The GC will free that memory whenever it sees fit.
That shoudn't bother you, that's how a GC works.

If Dispose() clears MyListView.Items then the references to MyEmploye
objects are gone and they should be elegible for garbage-collection.

If Dispose() does *not* clear MyListView.Items, the MyEmploye objects
become elegible for GC when MyListViewItem becomes elegible for GC,
which is usually very soon after .Dispose() since you cannot do anything
with it.

The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #4
Thanks for the Help !!

:-)

Steph.

"Helge Jensen" <he**********@slog.dk> wrote in message news:eo*************@TK2MSFTNGP11.phx.gbl...
Steph. wrote:
Class MyEmploye
Please, don't use My-prefix for your classes/variables. just call them
what they are.
When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?


No, not *exactly*. The GC will free that memory whenever it sees fit.
That shoudn't bother you, that's how a GC works.

If Dispose() clears MyListView.Items then the references to MyEmploye
objects are gone and they should be elegible for garbage-collection.

If Dispose() does *not* clear MyListView.Items, the MyEmploye objects
become elegible for GC when MyListViewItem becomes elegible for GC,
which is usually very soon after .Dispose() since you cannot do anything
with it.

The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #5

"Helge Jensen" <he**********@slog.dk> wrote in message
news:eo*************@TK2MSFTNGP11.phx.gbl...


Steph. wrote:
Class MyEmploye
Please, don't use My-prefix for your classes/variables. just call them
what they are.


I think he don't in the real world. The My-prefix is however excellent in
example-code to show that the type used is not in any framework but written
by the author. Don't discourage that!
When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?


No, not *exactly*. The GC will free that memory whenever it sees fit. That
shoudn't bother you, that's how a GC works.

If Dispose() clears MyListView.Items then the references to MyEmploye
objects are gone and they should be elegible for garbage-collection.

If Dispose() does *not* clear MyListView.Items, the MyEmploye objects
become elegible for GC when MyListViewItem becomes elegible for GC, which
is usually very soon after .Dispose() since you cannot do anything with
it.


The above is such a oversimplification that is yeilds no facts. Useless
explaination in my humble opinion.
The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).
This is true though
--
Helge Jensen


- Michael S
Nov 17 '05 #6

"Helge Jensen" <he**********@slog.dk> wrote in message
news:eo*************@TK2MSFTNGP11.phx.gbl...


Steph. wrote:
Class MyEmploye
Please, don't use My-prefix for your classes/variables. just call them
what they are.


I think he don't in the real world. The My-prefix is however excellent in
example-code to show that the type used is not in any framework but written
by the author. Don't discourage that!
When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?


No, not *exactly*. The GC will free that memory whenever it sees fit. That
shoudn't bother you, that's how a GC works.

If Dispose() clears MyListView.Items then the references to MyEmploye
objects are gone and they should be elegible for garbage-collection.

If Dispose() does *not* clear MyListView.Items, the MyEmploye objects
become elegible for GC when MyListViewItem becomes elegible for GC, which
is usually very soon after .Dispose() since you cannot do anything with
it.


The above is such a oversimplification that is yeilds no facts. Useless
explaination in my humble opinion.
The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).
This is true though
--
Helge Jensen


- Michael S
Nov 17 '05 #7


Michael S wrote:
I think he don't in the real world. The My-prefix is however excellent in
example-code to show that the type used is not in any framework but written
by the author. Don't discourage that!
Maybe OP will comment on that?
The above is such a oversimplification that is yeilds no facts. Useless
explaination in my humble opinion.
OP explicitly stated which references there were to the instances.
The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).

This is true though


Thank you very much.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #8


Michael S wrote:
I think he don't in the real world. The My-prefix is however excellent in
example-code to show that the type used is not in any framework but written
by the author. Don't discourage that!
Maybe OP will comment on that?
The above is such a oversimplification that is yeilds no facts. Useless
explaination in my humble opinion.
OP explicitly stated which references there were to the instances.
The only reason you would even *want* to Dispose() Control instances are
that they use some unmanaged resources, which would be nice to have
available again soon (win32-handles, gfx-resources, ...).

This is true though


Thank you very much.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #9
Hi Steph.

I would like to give another perspective on GC's and how they work.
Memory-management comes in three flawors; direct and explicit,
reference-counted and implicit; and garbage collected.

- Direct explicit de-allocation:
This is typically for pointer driven langs like c and pascal, not to mention
asm. It's fast but dangerous. What ever memory you allocate you must free
yourself. This is also why C++ and Delphi-code gets bloated with a lot of
try/finally clauses. If you forget to free an object you got a leak.

pros: fast!
cons: very tedious and very error-prone.
- Reference-counted implicit
This is a technique used in COM, and by so, this is stuff you may use in
Delphi and C++, but is mandatory in VB. For COM the IUnknown interface
sports the AddRef and Release methods. VB-code gets injected with calls to
all COM-objects AddRef method on assignment or when sent as parameters. And
when a sub or function goes out of scope it calls Release on those
interfaces. AddRef just increases a counter while Release does a simple if
myrefcount = 0 then killme.

pros: makes for easy coding
cons: hurts performance. COM and VB is not known for it's high speed. =)

Also, ref-count cannot handle circular references (AddRef's) and it's still
possible to create memory leaks by creating islands of object-groups.
- Garbage collection
Now a GC is another beast. It manages the entire heap and does so by keeping
track of all 'roots'. Every application has one root, the rest of them are
the current references on the stack. The GC can kick in at any time but
typically does so when it's first generation gets full (gen0).

When the GC kicks in, it will make a pretty cynical and dreadful assumption:

- All Objects Are Dead!

Then it will loop from it's first root and make an object graph of all
objects that can be reached.
The ones that cannot be reached are now very much dead. Note how this solves
the circular reference problem described above. All objects that can be
reached will be moved to gen1 of the GC. Later, if gen1 gets full it will
collect to gen2.

The generations are currently at gen0 of 256KiB, a gen1 of 2MiB and a gen2
of 16MiB. gen2 can increase in size, but when before it tries to allocate
more space, it tries to move objects back to gen0 and gen1 a few times and
hope most of them get killed. It's a rather nifty system.

pros: makes for easy coding
cons: not predictable. You never know when it will start its run and when
done. Can't work with real-time data.

But, as objects may use 'external' resources like open files on disk,
database connections and some GUI controls, we need IDispose to make sure to
free those resources, as it can take forever before the GC frees your
object. milliseconds, seconds, days, months... You just don't know.

Anyways, few objects needs this kinda cleansing. FxCop can tell you if you
forgot to call Dispose() on a object and this will come as a vanilla-feature
in future versions of Visual Studio (While I think it should have been there
from day one).

A 'good' .NET developer knows that some objects needs Disposing, a 'great'
C# dev does this with the using-statement. A 'awesome' dev doesn't forget to
run FxCop before the release to find the places he forgot and/or can ignore.
=)

Happy Allocating
- Michael S

"Steph." <st***@nomail.com> wrote in message
news:OG*************@TK2MSFTNGP12.phx.gbl...
I have an Object " MyEmploye " :

Class MyEmploye
{
private string Name;
private string Surname;
}

Instances of this object are referenced by instances of an ArrayList
"MyArrayList" . One instance of MyArrayList is referenced by the "Tag"'
property of each Item of a ListView "MyListView".

"Reference" tree is :
- ListView
- ListViewItems
- Tag
- MyArrayList
- MyEmploye
My question is :

When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?

Any help will be appreciated,

Thanks,

Steph.
Nov 17 '05 #10
Hi Steph.

I would like to give another perspective on GC's and how they work.
Memory-management comes in three flawors; direct and explicit,
reference-counted and implicit; and garbage collected.

- Direct explicit de-allocation:
This is typically for pointer driven langs like c and pascal, not to mention
asm. It's fast but dangerous. What ever memory you allocate you must free
yourself. This is also why C++ and Delphi-code gets bloated with a lot of
try/finally clauses. If you forget to free an object you got a leak.

pros: fast!
cons: very tedious and very error-prone.
- Reference-counted implicit
This is a technique used in COM, and by so, this is stuff you may use in
Delphi and C++, but is mandatory in VB. For COM the IUnknown interface
sports the AddRef and Release methods. VB-code gets injected with calls to
all COM-objects AddRef method on assignment or when sent as parameters. And
when a sub or function goes out of scope it calls Release on those
interfaces. AddRef just increases a counter while Release does a simple if
myrefcount = 0 then killme.

pros: makes for easy coding
cons: hurts performance. COM and VB is not known for it's high speed. =)

Also, ref-count cannot handle circular references (AddRef's) and it's still
possible to create memory leaks by creating islands of object-groups.
- Garbage collection
Now a GC is another beast. It manages the entire heap and does so by keeping
track of all 'roots'. Every application has one root, the rest of them are
the current references on the stack. The GC can kick in at any time but
typically does so when it's first generation gets full (gen0).

When the GC kicks in, it will make a pretty cynical and dreadful assumption:

- All Objects Are Dead!

Then it will loop from it's first root and make an object graph of all
objects that can be reached.
The ones that cannot be reached are now very much dead. Note how this solves
the circular reference problem described above. All objects that can be
reached will be moved to gen1 of the GC. Later, if gen1 gets full it will
collect to gen2.

The generations are currently at gen0 of 256KiB, a gen1 of 2MiB and a gen2
of 16MiB. gen2 can increase in size, but when before it tries to allocate
more space, it tries to move objects back to gen0 and gen1 a few times and
hope most of them get killed. It's a rather nifty system.

pros: makes for easy coding
cons: not predictable. You never know when it will start its run and when
done. Can't work with real-time data.

But, as objects may use 'external' resources like open files on disk,
database connections and some GUI controls, we need IDispose to make sure to
free those resources, as it can take forever before the GC frees your
object. milliseconds, seconds, days, months... You just don't know.

Anyways, few objects needs this kinda cleansing. FxCop can tell you if you
forgot to call Dispose() on a object and this will come as a vanilla-feature
in future versions of Visual Studio (While I think it should have been there
from day one).

A 'good' .NET developer knows that some objects needs Disposing, a 'great'
C# dev does this with the using-statement. A 'awesome' dev doesn't forget to
run FxCop before the release to find the places he forgot and/or can ignore.
=)

Happy Allocating
- Michael S

"Steph." <st***@nomail.com> wrote in message
news:OG*************@TK2MSFTNGP12.phx.gbl...
I have an Object " MyEmploye " :

Class MyEmploye
{
private string Name;
private string Surname;
}

Instances of this object are referenced by instances of an ArrayList
"MyArrayList" . One instance of MyArrayList is referenced by the "Tag"'
property of each Item of a ListView "MyListView".

"Reference" tree is :
- ListView
- ListViewItems
- Tag
- MyArrayList
- MyEmploye
My question is :

When I call "MyListView.Dispose();" does the GC free the memory
allocated for the all the instances of "MyEmploye" objects ?

Any help will be appreciated,

Thanks,

Steph.
Nov 17 '05 #11

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

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
4
by: bkazlak | last post by:
Hello, I have a quick question might help me understand garbage collection. let's say I'm having a static collection of objects in one class, so this collection should be cached and present...
3
by: JJ | last post by:
Hi, I noticed in a sample app source code that the app made use of a class for example a user class and then had the user objects that got created stuffed into a user collection. I was wondering...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
34
by: Craig Buchanan | last post by:
Which vb.net object is the best match for the vb6 collection class? Specifically, I would like to be able to access the Item property with an index or a key string. I wrote my own class that...
54
by: MLH | last post by:
I use A97 and do not always insert line numbers while writing procedures. I find it necessary to go back and add them later to aid in debugging. Nearly 3 years ago, something was mentioned in...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
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
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...

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.