10 Replies Latest reply on Jul 27, 2011 2:13 PM by Echysttas

    Allowing XML to be interpreted causes a 30 MB Memory Leak

    Echysttas Level 1

      Hello everybody ::- ). Please, if you are adept with E4X and XML in AS3, take a look at this CRAZINESS.

       

      I have a 420 KB XML. Fairly large, yes. Its structure is fairly simple:

       

      <Nodes>
        <Node ID="1">
          <Element Name="A" />
          <Element Name="B" />
        </Node>
      </Nodes>

       

      I have between 2 to 10 Elements in a Node and about 50 Nodes in total.

       

      I am storing all the individual "Element" collections into a Dictionary, where the Key is the value of the Node ID attribute. Don't ask why I did it like this, okay? I had a good reason for it.

       

      All right, so we got a Dictionary with about 50 keys, each of them containing values which are XMLLists of 2 to 10 Elements.

       

      So, the big XML is split into more XMLLists and stored into the Dictionary like this:

       

      for each (var obj: XML in objects)
        _SomeDictionary[int(obj.@ID)] = obj.Element;

      Now here comes the weird part:

       

      1. Run Application.
      2. Initialize some objects using 10 of the 50 Dictionary Values.
      3. Memory Consumption: 36 MB.

       

      ---

       

      1. Run Application.
      2. Initialize objects using ALL 50 of the Dictionary's Values.
      4. Memory Consumption: 47 MB.

       

      But... if I do it like this:

       

      for each (var obj: XML in objects)
        _SomeDictionary[int(obj.@ID)] = String(obj.Element);

       

      I get this:

       

      1. Run Application.
      2. Initialize objects using ALL 50 of the Dictionary's Values, and prior to initialization, cast the Dictionary's Value back to XMLList.
      4. Memory Consumption: 16 MB.

       

      So I shaved 30 MB of memory just by casting the XMLList to a String. Why does this happen? Eventually, inside the Initialization function which processes the Dictionary's Value, an XML List will ALWAYS be created.

       

      Second question: when NOT casting to String, WHY does the memory in