We have an event class which has a reference of a MsgBody class:
public class StatusEvent
{
MsgBody msgBody;
public StatusEvent(MsgBody body)
{
msgBody = body;
}
....
}
public class MsgBody
{
string s1;
string s2;
.....
}
MsgBody class is ONLY used by StatusEvent. No other place a MsgBody is refered. When a StatusEvent instance is created, a MsgBody instance will be created. When a StatusEvent object is ready for cleanup, a MsgBody object will be safe to be cleanup.
But the result is the MsgBody cleanup is way behind the StatusEvent. From one profiler result, I found there ware only 43 Live instances of StatusEvent but there were 9,940 instances of MsgBody!
Is there a way to link them together and make GC cleanup MsgBody when it wipe StatusEvent away?
thanks,
If you want deterministic release of resources, StatusEvent needs to implement IDisposable. In the IDisposable.Dispose method implementation, you can release all the resources held. In your case you can put the statement msgBody = null; to your code.
Are you sure there is no other place where msgBody is being stored?