0

I have one single configuration XML file which can be deserialised into different instances, each of which takes only a portion of this XML.

class A
{
    public string A { get; set; }
}

class B
{
    public string B { get; set; }
}

public static T Xml2Cls<T>(string filename)
{
    XDocument doc = XDocument.Load(filename);
    var root = new XMLRootAttribte(doc.Root.Name.LocalName);
    var serializer = new XMLSerializer(typeof(T), root);
    using (var reader = doc.Root.CreateReader())
    {
        return (T)serializer .Deserialize(reader);
    }
}

// if I keep calling the following for say 1000 times
// all these instances don't seem to get garbage collected
var a = Xml2Cls<A>("a.xml");
var b = Xml2Cls<B>("a.xml");

<root>
    <a>...</a>
    <b>...</b>
</root>

Any ideas why those instantiated objects are not garbage collocted? I tried moving the return out of the using block but it doesn't make a difference.

EylM
  • 5,967
  • 2
  • 16
  • 28
Antediluvian
  • 653
  • 1
  • 6
  • 18
  • 3
    So what is the *actual* question? It should be clearly summarized in the title and expanded/clarified outside of the code block. – user2864740 Jul 09 '19 at 23:23
  • Are you getting some sort of error? You didn't make it clear what the issue is. "Any ideas" is not much use as a question if you don't explain the problem. – ADyson Jul 09 '19 at 23:23
  • Where do you get the idea something isn't garbage collected? You aren't looking at the Memory column in Task Manager are you? That [doesn't show memory in active use](https://stackoverflow.com/a/4863113/22437). – Dour High Arch Jul 09 '19 at 23:40
  • @DourHighArch the memory monitoring in visual studio – Antediluvian Jul 09 '19 at 23:55
  • 1
    Why do you believe `var a = …` somehow should be instantly garbage collected? Or you are asking about some other objects ? (Very unclear at this point what exactly is the problem) – Alexei Levenkov Jul 10 '19 at 00:29
  • @AlexeiLevenkov those code are in a function in the real code. So as soon as that function returns, they should be destroyed. Or if there is a delay for GC, they should be collected 1 minute or so later. But the VS memory profiling shows that the memory keeps increasing even after 1 minute. – Antediluvian Jul 10 '19 at 00:38
  • 2
    "So as soon as that function returns, they should be destroyed."???? Can you show [mcve] that explains why you expect that behavior of GC? Where "collected 1 minute or so later" comes from? (I don't think there any configuration you can set to make GC to behave that way - so clearly you have code that force GC - make sure to include that in sample) - again showing good self-contained example would be really helpful. – Alexei Levenkov Jul 10 '19 at 00:48
  • As far as I know garbage collection are more dependant on memory usage than elapsed time. – Phil1970 Jul 10 '19 at 01:48
  • @AlexeiLevenkov Sorry I can't put all the code up here because the whole thing is just too big. I don't have any code to force GC. To put it another way, if I keep it running for 1 hour, the memory usage will always be increasing with all code commented out but only the `Xml2Cls`. – Antediluvian Jul 10 '19 at 01:48
  • @Phil1970 The whole thing finally got an `Out of Memory` exception. – Antediluvian Jul 10 '19 at 01:49
  • Then either you keep reference to all loaded documents or those are very large files (say hundreds of megabytes). To get an `Out of memory` you really need to have a lot of big objects. – Phil1970 Jul 10 '19 at 01:52
  • 1
    @Antediluvian https://www.bing.com/search?q=c%23+xmlserializer+outofmemory - should be duplicate of https://stackoverflow.com/questions/2805738/net-outofmemoryexception-on-xmlserializer-serialize (but I already used up my vote) – Alexei Levenkov Jul 10 '19 at 01:53
  • @Phil1970 no... just call XmlSerialization enough times. I was completely confused about the way OP phrased the question and it looked like a,b… are not getting collected (which makes no sense) or they have strange expectation of GC... – Alexei Levenkov Jul 10 '19 at 01:54
  • @AlexeiLevenkov That helped. I didn't know that it was the problem of the serialiser. Thanks a lot. – Antediluvian Jul 10 '19 at 01:58
  • 1
    You don't provide real code. If you have 1000 variables a, b,... and each string is a 1MB file, then it it possible to get out-of-memory because you have 1GB of memory just for that (a 32 bit application would have 2GB available for user in theory. It could be a bit less in practice) – Phil1970 Jul 10 '19 at 01:59
  • @Phil1970 The XML itself is only 4K. For a 1M string or whatever, I would rather use streams instead. – Antediluvian Jul 10 '19 at 02:04

0 Answers0