开发者

Is this a memory leak or unreleased resources

I'm not quite sure if this is a memory leak o开发者_开发问答r if the resources are not being released for GC, but it looks suspect since I'm referencing a property from another object.

I deserialize an XML file into an object. I then use this object to populate some properties of another object. Does doing this keep this deserialized-file-into-a-class-object in memory somewhere:

 void Load() {
   MyClass deserializedClass = Helper.GetDeserializedFileFromXml(path, type);
   SetProperty(deserializedClass)
 }

 // MyProperty is a List<ADifferentClass>;
 void SetProp(MyClass myClass) {
    MyProperty = myClass.MyProperty;
 }

UPDATE

I realized after reading the comments below that the crucial part (and why I asked this is) is that the property is a reference type, a List of a different class.


The class shouldn't stick around, unless the value of the proprety references back to the class somehow. The file itself doesn't stick around (unless GetDeserializedFileFromXml does something unexpected).


To explain further, if your class is like this:

public class MyClass {
   public String MyProperty { get; set; }
}

then assigning the property to MyProperty won't cause MyClass to stick around. If, on the other hand, it's something like:

public class MyClass {
    public MyClass() {
        MyProperty = new HoldsAReference(this);
    }
    public HoldsAReference MyProperty { get; private set; }
}

then your class will stick around.


So, the answer was yes, because my property is referencing another classes (deserialized from XML) property that is a reference type. The deserialized class hangs around.


First thing is for you to understand Reference and Value types works.

From there, it is basic understanding, that there is no relation between parent object and object inside of any of parent object's properties. They are completly different entities, that can be anywhere in memory with only reference from one to another.

So if there is even single reference to object in property(except the object, who owns this property), but there is no reference to parent object fron anywhere else, then the parent object will be marked for garbage collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜