performance impact of circular references

C

colin

Hi,
what is the impact of circular references in c# if any ?
I know c# GC takes care of the hassle of releasing cirular references.

It would be too problomatic to try and implement a dispose mechanism.

I have a large data structure wich at the top I have called "World"
it is read in from files and contains many objects and sub objects each may
reference any other in a circular fashion,
this I have no control over.

each object has to reference the World particularly in order to know where
to look for a file
so it can dynamicaly load referenced objects etc.

I now monitor the GC.GetTotalMemory and it rises to 600Mb,
eventualy faling to 260mb once the GC has run.
however once I have closed the World it stays there
untill I call GC.Collect, however this is quite a slow call,
I was wondering if the circular references make this call more
likely to be necessary.


thanks

Colin =^.^=
 
J

Jon Skeet [C# MVP]

what is the impact of circular references in c# if any ?

None that I'm aware of.
I know c# GC takes care of the hassle of releasing cirular references.
Yup.

It would be too problomatic to try and implement a dispose mechanism.

I have a large data structure wich at the top I have called "World"
it is read in from files and contains many objects and sub objects each may
reference any other in a circular fashion,
this I have no control over.

each object has to reference the World particularly in order to know where
to look for a file so it can dynamicaly load referenced objects etc.

I now monitor the GC.GetTotalMemory and it rises to 600Mb,
eventualy faling to 260mb once the GC has run.
however once I have closed the World it stays there
untill I call GC.Collect, however this is quite a slow call,
I was wondering if the circular references make this call more
likely to be necessary.

It's very rarely a good idea to call GC.Collect() manually - but when
the GC has to collect as far as Gen2 (which is sounds like would be
the case here) it will indeed take a little while. This is just to do
with the amount of data it has to look through, and not really to do
with circular references.

Jon
 
M

Marc Gravell

The only common issue with circular object references is tree-based
serialization (for example, XmlSerializer and DataContractSerializer in
its default mode). Graph-based serializers (BinaryFormatter,
DataContractSerializer with the right options) are fine, though.

Regular GC etc is fine with circular object references.
 
C

colin

thanks for the replies, the biggest problem I was having was that the
some of my objects were being aded to event handlers automatically
such as directx device and had some helper dictionaries wich were static.
so my whole tree structure never got collected,
as I started to open and close a few large trees i soon ran out of memory.
however i managed to turn the automatic event handler off,
and make the dictionaries non static.

took me quite some time to track down the eventhandlers, and find a way to
turn
this feature off. although ive probably got to find out what I need to do
in place of the event handlers but i could at least use weak references
here,
as ive used elsewhere to cache the texture objects etc so i can reuse
existing ones.

My serialization is all binary format and so each object has to have its own
routine.

looks like i might need to investigate an efficient way to make sure
the GC doesnt leave so much uncollected for so long, although for now
I just do it manualy to make sure there are no actual memory leaks,
the taskbar displays the memory used and clicking on it does a colection.

thanks
Colin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top