views:

36

answers:

3

How can I make sure that the objects get disposed that I am adding into the SerializationInfo object?

For example: if I am adding a hashtable to the info object, how do I make sure that after serialization, there is no reference alive to the hastable object of my class and I can free the memory somehow?

info.AddValue("attributesHash", attributesHash, typeof(System.Collections.Hashtable));


Update (code)

[Serializable]
class A : System.Runtime.Serialization.ISerializable
{
    List<int> m_Data = new List<int>();
    //...
    public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
    {
        if (m_Data != null)
        {
            info.AddValue("MyData", m_Data, typeof(List<int>));
            m_Data.Clear();
        }
        m_Data = null; // <-- Will this be collected by GC?
        GC.GetTotalMemory(true); //forced collection
    }
};

My question is: if i make my object null after adding to the info list, will it be freed after serialization is called (when info is destroyed - I hope), or in the line when GC's function is called (which I don't think so)?

If setting 'm_Data = null' will not mark it as garbage, then how would I know that the memory occupied by m_Data has been freed or not?

+1  A: 

"I can free the memory somehow" and "objects get disposed" do not go along very well.

Memory management is done by the GC. As soon as there is no more reference to an object, it gets flagged for garbage collection. So it deals with managed resources.

Disposing, however, is totall different animal and is calling Dispose() on types implementing IDisposable and deals with unmanaged resources such as file handles and windows resources.

You need to make it clear which you one you mean.

Aliostad
Good answer! I know this.. but somehow did not frame the question right. :) All I wanted was to know if the objects added to the info list are kept in memory or not.. if yes, till what moment - till serialization?
Nayan
Thanks! You seem to be trying to add Hashtable which is not implementing IDisposable, is that correct? So are you concerned with IDisposable or GC?
Aliostad
GC, to be precise. See my updated question for example.
Nayan
A: 

You need to make your containing object implement IDisposable. But just because you have become serialized does not mean that you need to also be disposed. The referencing object should then call dispose after serialization if that is what is expected.

As for the deserialized object, it should also be disposed by whatever references it when it is done being used (presumably in another appdomain?). So what this means is that both instances will need to be disposed. If the resource you share is a single instance (such as an IntPtr) then you might need to be more clever about it, such as not disposing of that unmanaged resource with this object but from a higher level.

General rule of thumb: he who creates it, disposes it.

One other common pattern, as described by the IDisposable documentation is to put a call to Dispose() into your objects destructor. This will give you non-deterministic timing for your disposal but is guaranteed to work (assuming you don't have reference leaks).

justin.m.chase
Thanks for suggesting references. But perhaps I did not frame my question right. I have updated the question. All I want is the memory to be freed - whatever way works :)
Nayan
A: 

After much analysis, I don't think GC collects memory when requested... not in the case of GetTotalMemory function, at least.

Setting the object to null marks it as garbage, but it doesn't mean it will be collected immediately.

Nayan
Setting the m_data reference to null does nothing, since info is still in scope, and you've just asked it (via AddValue) to maintain a reference to the same collection that m_data was referencing.
Damien_The_Unbeliever
Exactly! So, when the 'info' collection gets disposed, the last reference to the object should also be destroyed and GC should release the object's memory. That was my question/confusion.
Nayan