473,396 Members | 1,966 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.

Object Lifecycle in CSharp?

Hi:

Anybody knows about object life cycle in CSharp?

Any online articles?

Please let me have.

Thanks
Nov 15 '05 #1
8 7066
What are you looking for specifically??
"Abdolhosein Vakilzadeh Ebrahimi" <va********@kzlabs.com> wrote in message
news:b9**************************@posting.google.c om...
Hi:

Anybody knows about object life cycle in CSharp?

Any online articles?

Please let me have.

Thanks

Nov 15 '05 #2
Look for something on Garbage collection in the MSDN online library and
you'll find just what you're looking for.

cheers

-Andre

Abdolhosein Vakilzadeh Ebrahimi wrote:
Hi:

Anybody knows about object life cycle in CSharp?

Any online articles?

Please let me have.

Thanks


Nov 15 '05 #3
Hi Abdolhosein,

In a nutshell -

An object exists and is alive and usable as soon as it is created and
for as long as there's something holding a reference to it.

When all references have been set to null, the object still exists in
memory but is NOT alive and usable. It is awaiting garbage collection.

If two objects reference each other and nothing else does, the pair will
exist and be alive but NOT usable. They will wait together for the program
to terminate. Similarly with any closed group of mutually-referencing
objects.

A freed object will continue to occupy memory until it is removed by the
garbage collector (GC) - which will happen when the GC gets around to it.
This is not guaranteed and the program may terminate before the object is
freed.

At termination, objects aren't freed per se, but the system will reclaim
all the program's memory anyway, so there's no problem.

The main consideration is when an object holds references to unmanaged
resources, eg Windows handles, bitmaps, etc. These MUST be freed (the
reference set to null) when the object has been finished with, and before it
is released. The GC doesn't know about these resources (which is partly why
they are unmanaged!).

Hope that's useful.

Regards,
Fergus.
Nov 15 '05 #4
Hi Fergus
If two objects reference each other and nothing else does, the pair will
exist and be alive but NOT usable. They will wait together for the program
to terminate. Similarly with any closed group of mutually-referencing

objects.

Are you sure of this? A good garbage collector should detect this and nuke
the pair. If you are correct, I need to fix some of my code :)

I'm pretty sure the Java GC does this - it was improved sustantially in
V1.3. Programs that consumed memory in v1.2 no longer had a problem in v1.3.
I assumed it was because the garbage collector had become more intelligent.

Regards

Ron
Nov 15 '05 #5
See inline ****
Fergus Cooney wrote:

|| If two objects reference each other and nothing else does, the
|| pair will exist and be alive but NOT usable. They will wait together
|| for the program to terminate. Similarly with any closed group of
|| mutually-referencing objects.
||
**** This is totally wrong, if the pair has lost it's root it will be collected at the first GC run (or the second when there are
pending finalizers).

|| A freed object will continue to occupy memory until it is
|| removed by the garbage collector (GC) - which will happen when the
|| GC gets around to it. This is not guaranteed and the program may
|| terminate before the object is freed.

**** What do you mean here with "a freed" object?

||
|| At termination, objects aren't freed per se, but the system will
|| reclaim all the program's memory anyway, so there's no problem.
||
|| The main consideration is when an object holds references to
|| unmanaged resources, eg Windows handles, bitmaps, etc. These MUST be
|| freed (the reference set to null) when the object has been finished

**** Releasing unmanaged resources is somewhat more complicated than simply setting a reference to null.
Willy.
Nov 15 '05 #6
Hi Ron,

Thanks for your question.

No, I'm not certain. This was my understanding when I first learned
about it - way back. If things have changed since then, that's great.

If you're dumping "closed-loop" object groups, I reckon you should "fix"
your code anyway - it'll raise the "pride level" if nothing else? :-).

Regards,
Fergus
Nov 15 '05 #7
Abdolhosein Vakilzadeh Ebrahimi,
If by 'object life cycle', your asking how the Garbage collector works, see
the following two articles:

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/

Hope this helps
Jay
"Abdolhosein Vakilzadeh Ebrahimi" <va********@kzlabs.com> wrote in message
news:b9**************************@posting.google.c om...
Hi:

Anybody knows about object life cycle in CSharp?

Any online articles?

Please let me have.

Thanks

Nov 15 '05 #8
Hi Willy,

I said (shortened)
|| If two objects reference each other and nothing else does,
the
|| pair will wait together for the program to terminate.
Ron asked me if I'm sure. No. I was when I first learned this stuff. If
it's now wrong - great, we have a better GC. I prefer to write my code
as if the GC is dumb in this respect - it feels cleaner.

You asked what I meant by "freed". I'm talking about managed objects and
mean that they are unreferenced. I'm not talking about system resources,
memory, etc.

You said
|| Releasing unmanaged resources is somewhat more complicated than
simply setting a reference to null.
My post starts with the line:
|| In a nutshell -
The post is introductory and intended to be read as a list of bullets -
not full-on explanations. However, it would have been better if I'd implied
that there's more to it.

The article that Michael mentioned,
http://msdn.microsoft.com/msdnmag/issues/1100/gci/, written by Jeffrey
Richter, is excellent and goes into lots of detail about garbage collection,
especially Finalization, - but suprisingly little about your point
(unmanaged resources). I think you should do a short post to explain further
and give examples, Willy. Go for it. ;-)

Regards,
Fergus

ps. In part 2 of the article there's a link to Figure 1 which uses
JavaScript. It failed on my machine. If it fails for you too, the full link
is http://msdn.microsoft.com/msdnmag/is...I2/figures.asp.
Nov 15 '05 #9

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

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
4
by: Richard | last post by:
Office Addins are easy in .NET. Try this from Visual Studio's main menu: File New Project "Other Projects" Extensibility Projects Shared Addin Run the wizard and you're off...
3
by: GoodMorningSky | last post by:
I have long term question about object serialization. Object serialization is used in many ways. In .net I heard XML is used for object serialization. I understand how object values are serialized...
4
by: dx | last post by:
If not is it appropriate to place an EnsureChildControls within an objects OnInit method? TIA
0
by: Epson Barnett | last post by:
I'm new to ASP.NET and have run across a problem several times while working on web apps. I often need to create content dynamically (based on an event) which contains dynamically created...
19
by: Mark Rae | last post by:
Hi, Is it possible to have programmatic access to the Page object in Application_BeginRequest, or is it too early in the lifecycle...? E.g. to be able to change a page's MasterPage...
1
by: =?Utf-8?B?QW5kZXJ3IE1pbGxlcg==?= | last post by:
Apologies for posting in this forum, I don't see a more appropriate group... I'm trying to understand the timeline for Window 2003 ending mainstream support. I have consulted the MS lifecycle...
2
by: Gary W. Smith | last post by:
I have a page that inherits from a base page that is currently overriding all of the On* events. For the most part I'm accomplishing everything I set out to do with the inheritance, but I wanted...
2
by: bsmith95610 | last post by:
Hello, I am trying to learn how the asp. Net lifecycle of a web page works. I am a jr. programmer and am just starting to learn C#. Does anyone know of any websites or tutorials that explain...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.