Click here to Skip to main content
15,905,229 members
Home / Discussions / C#
   

C#

 
GeneralRe: root in managed heap Pin
Jordanwb20-Apr-08 4:06
Jordanwb20-Apr-08 4:06 
GeneralRe: root in managed heap Pin
Luc Pattyn20-Apr-08 2:37
sitebuilderLuc Pattyn20-Apr-08 2:37 
GeneralRe: root in managed heap Pin
George_George20-Apr-08 3:16
George_George20-Apr-08 3:16 
GeneralRe: root in managed heap Pin
Luc Pattyn20-Apr-08 3:38
sitebuilderLuc Pattyn20-Apr-08 3:38 
GeneralRe: root in managed heap Pin
George_George20-Apr-08 4:42
George_George20-Apr-08 4:42 
GeneralRe: root in managed heap Pin
Luc Pattyn20-Apr-08 13:03
sitebuilderLuc Pattyn20-Apr-08 13:03 
GeneralRe: root in managed heap Pin
George_George20-Apr-08 19:20
George_George20-Apr-08 19:20 
GeneralRe: root in managed heap Pin
Luc Pattyn20-Apr-08 22:42
sitebuilderLuc Pattyn20-Apr-08 22:42 
Hi George,

an object is alive if there is a (theoretical) probability that one of the process threads
will touch the object. Now where is a thread's knowledge about objects? it is in the data
it knows to access, i.e. the stack (containing stack frames from earlier methods that lead
to the invocation of the current method, and containing local variables for the current
method). It is not in other data, the thread would not know how to find other data, remember
there is no such thing as global variables in OO, so everything the thread is supposed to
work on must be given to it or created by it.

Of course, as soon as an object A is marked "alive", all its data members must also be
inspected; object A could contain references to other objects B, C, ... and being alive itself,
those would automatically be alive too.
Example: a local variable of type List may be passed in as an argument to a method,
and the List may contain any number of Control objects; each of those Controls in turn
would have names, texts, fonts, whatever.

Typical app example: a windows app starting up has one thread, no objects (apart from the
command line args that are passed to static main). It creates a new MyForm
then passes it to Application.Run to make it visible and give it a message pump.
The form may create new threads and pass parameters to them, etc. So new objects get
created and most of the time somehow linked to existing objects through the class members
of MyForm and others.

But there is no magic way to keep objects alive; if none of the threads has any reference
to a specific object, then none of them will be able to touch it ever again, hence the
object is dead.
Well actually there are some methods such as GC.KeepAlive() to mark objects for special cases,
e.g. when the managed world wants to pass an object to unmanaged (native) code and wants
to object to stay alive even when the managed side no longer holds a reference to it.

Final remark: the cyclic reference example I gave earlier (A holds ref to B, B holds ref to A,
none of the threads knows about the existence of A and B, hence both A and B are dead)
proves that you cannot simply walk the heap and decide about live and dead for objects,
since doing so would keep A and B alive no matter what.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.


GeneralRe: root in managed heap Pin
George_George20-Apr-08 23:46
George_George20-Apr-08 23:46 
GeneralRe: root in managed heap Pin
Luc Pattyn21-Apr-08 0:32
sitebuilderLuc Pattyn21-Apr-08 0:32 
GeneralRe: root in managed heap Pin
George_George21-Apr-08 1:08
George_George21-Apr-08 1:08 
GeneralRe: root in managed heap Pin
Luc Pattyn21-Apr-08 1:23
sitebuilderLuc Pattyn21-Apr-08 1:23 
GeneralRe: root in managed heap Pin
George_George21-Apr-08 2:04
George_George21-Apr-08 2:04 
GeneralRe: root in managed heap Pin
Luc Pattyn21-Apr-08 4:00
sitebuilderLuc Pattyn21-Apr-08 4:00 
GeneralRe: root in managed heap Pin
George_George21-Apr-08 4:16
George_George21-Apr-08 4:16 
GeneralFileStream.Read(); Pin
Ian Uy19-Apr-08 18:33
Ian Uy19-Apr-08 18:33 
GeneralRe: FileStream.Read(); Pin
Zoltan Balazs20-Apr-08 0:11
Zoltan Balazs20-Apr-08 0:11 
GeneralRe: FileStream.Read(); Pin
Ian Uy20-Apr-08 1:14
Ian Uy20-Apr-08 1:14 
GeneralComboBox not properly adding items Pin
Jordanwb19-Apr-08 14:10
Jordanwb19-Apr-08 14:10 
GeneralRe: ComboBox not properly adding items Pin
Luc Pattyn19-Apr-08 15:12
sitebuilderLuc Pattyn19-Apr-08 15:12 
GeneralRe: ComboBox not properly adding items Pin
Jordanwb19-Apr-08 15:53
Jordanwb19-Apr-08 15:53 
GeneralRe: ComboBox not properly adding items Pin
Luc Pattyn19-Apr-08 16:09
sitebuilderLuc Pattyn19-Apr-08 16:09 
GeneralRe: ComboBox not properly adding items Pin
Jordanwb19-Apr-08 16:27
Jordanwb19-Apr-08 16:27 
GeneralRe: ComboBox not properly adding items Pin
Luc Pattyn19-Apr-08 22:59
sitebuilderLuc Pattyn19-Apr-08 22:59 
GeneralRe: ComboBox not properly adding items Pin
Jordanwb20-Apr-08 4:09
Jordanwb20-Apr-08 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.