473,471 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

About Garbage Collection

<code>

public class MainClass
{
private ArrayList list;

public Class()
{
list = new ArrayList();

AddToList();
ReadFromList();
}

private void AddToList()
{
list.Add(new RecordClass("test1"));
list.Add(new RecordClass("test2"));
list.Add(new RecordClass("test3"));
}

private void ReadFromList()
{
if (((RecordClass)list[0]).Field == "test1") {
// do something
}

if (((RecordClass)list[1]).Field == "test2") {
// do something
}

if (((RecordClass)list[2]).Field == "test3") {
// do something
}
}
}

public class RecordClass
{
private string field;

public RecordClass(string field)
{
this.field = field;
}

public string Field
{
get { return field; }
set { field = value; }
}
}

</code>

Now, when i read the list in ReadFromList(), are the list items
*guaranteed* to reference valid RecordClass instances, or would the
garbage collector free the memory that these references should be
pointing to?

I really can't understand how the GC would work in this case. If the
answer to the above question is "yes", then does the GC know to free
up the memory if I were to call ArrayList.Delete()?

Of course, if the answer to the first question is "no", then I'd have
to use a struct instead of a class for RecordClass, right -- since
it's a value type?

TIA
Nov 15 '05 #1
4 2068
C# Learner,

See inline:

"C# Learner" <cs****@learner.here> wrote in message
news:8n********************************@4ax.com...

Now, when i read the list in ReadFromList(), are the list items
*guaranteed* to reference valid RecordClass instances, or would the
garbage collector free the memory that these references should be
pointing to?
The list items are guaranteed to be there. This is because the
ArrayList is holding references to the items (they are reference types) and
your class instance is holding a reference to the ArrayList. Because of
this, the items are reachable, and not subject to garbage collection. Now,
if your class instance was to be let go, and not reachable, and the other
references are not reachable, then the ArrayList (and the items in the list,
if references are not stored anywhere else that is reachable) will be
eligible for GC>

I really can't understand how the GC would work in this case. If the
answer to the above question is "yes", then does the GC know to free
up the memory if I were to call ArrayList.Delete()?
When you call Delete on the ArrayList, the ArrayList removes the item
from the list, letting go of its reference. If there are no other
references out there, then the item is eligible for GC (when the runtime
determines that a GC is needed)

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Of course, if the answer to the first question is "no", then I'd have
to use a struct instead of a class for RecordClass, right -- since
it's a value type?

TIA



Nov 15 '05 #2
Yes, they will be valid. Your program holds a reference to the arraylist,
and the arraylist holds references to the recordclass objects. The
garbage collector will search for the parent reference of any object and
as long as the topmost reference is still valid, all the other references
will also be valid.

There is no ArrayList.Delete(), but if you set

list = null;

the program will not hold any reference to the arraylist object anymore.
The object will be flagged for termination as well as all the contents of
it, since once the arraylist is gone, there is no valid reference to the
recordclasses either.

Also, if you call list.Clear(); the references to the recordclass objects
will be lost and the objects will be collected.
The ArrayList object will still be valid since list still holds the
reference to it.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabled
Nov 15 '05 #3
C# Learner <cs****@learner.here> wrote:

<snip>

Thanks for the replies.
Nov 15 '05 #4
If you want to know more about how the GC works, here's a pretty good
article on the subject (by Jeffrey Richter):
part 1: http://msdn.microsoft.com/msdnmag/issues/1100/gci/
part 2: http://msdn.microsoft.com/msdnmag/is...2/default.aspx

If you're writing anything that will have long-lived objects, especially if
it is a server (including aspx or middle tier service), it's good to know
how Generations work (part 2 of article)

"C# Learner" <cs****@learner.here> wrote in message
news:81********************************@4ax.com...
C# Learner <cs****@learner.here> wrote:

<snip>

Thanks for the replies.

Nov 15 '05 #5

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

Similar topics

1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
4
by: pachanga | last post by:
After you destroy a Object and its send to the garbage collections, can you retrieve the object back? Also, if you can, can you destroy an object permantly with no trace of it?
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...
2
by: Howard Weiss | last post by:
I am writing a Managed C++ application. In my application, I create a modeless sub-form using New. I retain a pointer to this sub-form in my application. If the user closes this form, my...
23
by: Ray | last post by:
Hello! I've been reading about PyPy, but there are some things that I don't understand about it. I hope I can get some enlightenment in this newsgroup :) First, the intro: <excerpt> "The...
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...
142
by: jacob navia | last post by:
Abstract -------- Garbage collection is a method of managing memory by using a "collector" library. Periodically, or triggered by an allocation request, the collector looks for unused memory...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
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...
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...
1
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.