title: Create and write an XML file throws the exception -> system out of memory exception?
body: <hr>
  
  <p>I've been developing for some time a small game called 'voxel' in XNA. A .NET C# like Minecraft game.
  I use a simple concept to save and read data from my games to build the world map. Everything is stored in an xml file.</p>
  
  <p>Now I'm trying to load a larger map, and 'paf' an exception is raised during the generation of my map: </p>
  
  <blockquote>
    <p>[<strong>System out of memory exception</strong>]</p>
  </blockquote>
  
  <p>I do not understand why, because after the exception is raised my 
  file is not very heavy, it's about <strong>70 mb</strong>.
  Is it normal to see an exception raise during the generation of an <strong>XML file up to 70 mb</strong>?</p>
  
  <h2>Development Environment used</h2>
  
  <ul>
  <li>Microsoft Windows 7 (x64) bits</li>
  <li>Visual Studio 2012 professionnel Update 3</li>
  <li>8192 Ram</li>
  <li>Intel i5 CPU 2.5 Ghz (4 cpus)</li>
  </ul>
  
  <h2><strong>I did do a test with CLR profiler to check how the garbage collector works:</strong></h2>
  
  <p><img src="https://i.stack.imgur.com/A33zZ.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/uyJle.png" alt="enter image description here"></p>
  
  <h2><strong>The code where i initialize my XmlWritter:</strong></h2>
  
  <pre><code>    private XmlTextWriter myXmlTextWriter ;
  
  
      #region prepareNewWorldXmlFIle
      private void PreparedNewWorldXmlFile()
      {
          Stream fs = new FileStream(currentPath + "World\\world.xml", FileMode.Create);
  
          myXmlTextWriter = new XmlTextWriter(fs,Encoding.ASCII);
          myXmlTextWriter.Formatting = Formatting.Indented;
          myXmlTextWriter.WriteStartDocument(false);
          myXmlTextWriter.WriteComment("World Map ID:");
          //World and his attribute
          myXmlTextWriter.WriteStartElement("World");
          myXmlTextWriter.WriteStartElement("Matrix", null);
          myXmlTextWriter.WriteStartElement("Regions");
      }
      #endregion
  </code></pre>
  
  <h2><strong>The code i use to generate my world map and write an xml file:</strong></h2>
  
  <pre><code>    //Octree calcul and generate map to xml file
      foreach (Region region in arcadia.world.Regions)
      {
          isAllCheckSameIdOctree = false;
          if (isFirstGenerationWorld)
          {
              //Regions and attributes
              myXmlTextWriter.WriteStartElement("Region");
              myXmlTextWriter.WriteAttributeString("id", indexRegion.ToString());
              myXmlTextWriter.WriteAttributeString("min", "x:" + region.PositionMin.X + ";y:" + region.PositionMin.Y + ";z:" + region.PositionMin.Z);
              myXmlTextWriter.WriteAttributeString("max", "x:" + region.PositionMax.X + ";y:" + region.PositionMax.Y + ";z:" + region.PositionMax.Z);
              myXmlTextWriter.WriteStartElement("Structures");
              myXmlTextWriter.WriteAttributeString("type", "cube");
          }
          indexRegion++;
          if (region.Matrice != null)
          {
              //If the node to generate contain minimum a height divisible by 2
              if (((region.PositionMax.Y - region.PositionMin.Y) / 2) &gt; 2)
              {
                  //generate and octree by 8
                  GenerateNodes(region, region.PositionMin, 8);
              }
              else if (((region.PositionMax.Y - region.PositionMin.Y) / 2) &lt;= 2)
              {
                  //generate and octree by 4
                  GenerateNodes(region, region.PositionMin, 4);
              }
              while (!isAllCheckSameIdOctree)
              {
                  if (nodeToRegenerate != null &amp;&amp; needRecurseBuild)
                  {
                      //if the node is greater than 2
                      if (nodeToRegenerate.TotalHeight &gt; 2)
                      {
                          nodeToRegenerate = GenerateNodes(nodeToRegenerate, region, nodeToRegenerate.Position, 8);
                          if (nodeToRegenerate == null)
                          {
                              isAllCheckSameIdOctree = true;
                          }
  
                      }
                      else if (nodeToRegenerate.TotalHeight &lt;= 2)
                      {
                          nodeToRegenerate = GenerateNodes(nodeToRegenerate, region, nodeToRegenerate.Position, 4);
                          if (nodeToRegenerate == null)
                          {
                              isAllCheckSameIdOctree = true;
                          }
                      }
                  }
                  else
                  {
                      isAllCheckSameIdOctree = true;
                  }
              }
              if (isFirstGenerationWorld)
              {
                  myXmlTextWriter.WriteEndElement();//Ferme le noeud Structures
                  myXmlTextWriter.WriteEndElement();//Ferme le noeud Region
                  myXmlTextWriter.Flush();
              }
          }
          else
          {
              if (isFirstGenerationWorld)
              {
                  myXmlTextWriter.WriteEndElement();//Ferme le noeud Structures
                  myXmlTextWriter.WriteEndElement();//Ferme le noeud Region
                  myXmlTextWriter.Flush();
              }
          }
      }
      if (isFirstGenerationWorld)
      {
          myXmlTextWriter.WriteEndElement();//Ferme le noeud Regions
          myXmlTextWriter.WriteEndElement();//Ferme le noeud World
          myXmlTextWriter.Flush();
          myXmlTextWriter.Close();
      }
  </code></pre>
  
  <h2>Exception catching in my generatedNode function , see below for more informations</h2>
  
  <p><img src="https://i.stack.imgur.com/V0uDc.png" alt="enter image description here"></p>
  
  <h2>My generatedNode function recursive where the exception fire with 'system out of memory exception</h2>
  
  <pre><code>#region ReGenerateWorld
      private Node GenerateNodes(Node nodeToRegenerate, Region region, Vector3 position, int countToCut)
      {
          //Relative dimension of the parent octree
          int widthParent = (int)nodeToRegenerate.TotalWidth / 2;
          int heightParent = (int)nodeToRegenerate.TotalHeight / 2;
          int lenghtParent = (int)nodeToRegenerate.TotalLenght / 2;
  
          //Relative dimension of the parent octree
          int widthNode = (widthParent) / (countToCut / (countToCut / 2));
          int heightNode = (heightParent) / (countToCut / (countToCut / 2));
          int lenghtNode = (lenghtParent) / (countToCut / (countToCut / 2));
  
          if (heightNode &lt; 1)
          {
              heightNode = 1;
          }
  
          int refX = (int)position.X / 2;
          int refY = (int)position.Y / 2;
          int refZ = (int)position.Z / 2;
          int indexStartX = 0;
          int indexStartY = 0;
          int indexStartZ = 0;
          int nbrToCut = 0;
          if (heightParent &gt;= 2)
          {
              nbrToCut = ((widthParent / (widthParent / 2))) * ((heightParent / (heightParent / 2))) * ((lenghtParent / (lenghtParent / 2)));
          }
          else
          {
              nbrToCut = 4;
              heightNode = 1;
          }
          //Calculate the number of cubic to cut
  
          //Génére les noeud racine
          int countVertical = 0;
          int calcPosX = 0;
          int calcPosY = 0;
          int calcPosZ = 0;
          int[][][] nodeMatriceWorld = null;
          bool firstTime;
          newNode = null;
          int idGroup = 0;
          bool isSameId = true;
          int idToCheck = 0;
  
          for (int index = 0; (index &lt; nbrToCut) &amp;&amp; (refY &lt; 32); index++)
          {
              indexStartX = refX;
              indexStartY = refY;
              indexStartZ = refZ;
  
              try
              {
                  nodeMatriceWorld = new int[widthNode][][];
                  for (int i = 0; i &lt; widthNode; i++)
                  {
                      nodeMatriceWorld[i] = new int[lenghtNode][];
                      for (int j = 0; j &lt; lenghtNode; j++)
                      {
                          nodeMatriceWorld[i][j] = new int[heightNode];
                      }
                  }
              }
              catch (Exception ex)
              {
                  // OUT OF MEMORY EXCEPTION HERE
                  Console.Out.WriteLine(ex.Message);
              }
              firstTime = true;
              for (int epaisseur = 0; epaisseur &lt; heightNode; epaisseur++, indexStartY++)
              {
                  for (int ligne = 0; ligne &lt; lenghtNode; ligne++, indexStartZ++)
                  {
                      for (int collone = 0; collone &lt; widthNode; collone++, indexStartX++)
                      {
                          if (firstTime)
                          {
                              calcPosX = indexStartX;
                              calcPosY = indexStartY;
                              calcPosZ = indexStartZ;
  
                              firstTime = false;
                          }
                          nodeMatriceWorld[collone][ligne][epaisseur] = matriceWorld[indexStartX][indexStartZ][indexStartY];
                      }
  
                      indexStartX = refX;
                  }
  
                  indexStartZ = refZ;
              }
  
              indexStartY = refY;
              idGroup = matriceWorld[calcPosX][calcPosZ][calcPosY];
              countVertical++;
              if (newNode != null)
              {
                  newNode.Dispose();
              }
  
              newNode = new Node(nodeMatriceWorld, new Vector3(calcPosX, calcPosY, calcPosZ), idGroup, widthNode, heightNode, lenghtNode);
              region.Nodes[idGroup].Add(newNode);
  
              //Regions.Add(new Node(nodeMatriceWorld, new Vector3(calcPosX, calcPosY, calcPosZ), idGroup));
              refX += widthNode;
  
              if (countVertical &gt;= 4)
              {
                  refY = ((int)position.Y / 2) + heightNode;
                  refX = ((int)position.X / 2);
                  refZ = (int)position.Z / 2;
                  countVertical = 0;
              }
              else if (countVertical == 2)
              {
                  refZ = ((int)position.Z / 2) + lenghtNode;
                  refX = ((int)position.X / 2);
  
              }
          }
  
          isSameId = true;
          nodeToRegenerate = null;
          needRecurseBuild = false;
          idToCheck = 0;
          // Check for each octree node if all are the same id
          foreach (List&lt;Node&gt; listNode in region.Nodes)
          {
              foreach (Node node in listNode.Where(m =&gt; m.isGroupSameId == false))
              {
  
                  isSameId = true;
                  idToCheck = node.matriceNode[0][0][0];
                  node.isGroupSameId = true;//Le met a true au depart
                  for (int epaisseur = 0; epaisseur &lt; node.TotalHeight / 2 &amp;&amp; isSameId; epaisseur++)
                  {
                      for (int ligne = 0; ligne &lt; node.TotalLenght / 2 &amp;&amp; isSameId; ligne++)
                      {
                          for (int collone = 0; collone &lt; node.TotalWidth / 2 &amp;&amp; isSameId; collone++)
                          {
                              if (node.matriceNode[collone][ligne][epaisseur] != idToCheck)
                              {
                                  isSameId = false;//si au moin un cube est différent on le marque
                                  node.isGroupSameId = false;
                                  //node.ItemGroup = node.matriceNode[collone, epaisseur, ligne];
                                  nodeToRegenerate = node;
                                  needRecurseBuild = true;
                                  break;
                              }
  
                          }
                      }
  
                  }
  
                  if (!isSameId)
                  {
  
                      break;
                  }
                  else
                  {
                      if (idToCheck != 0)
                      {
                          isSameId = true;
                          node.isGroupSameId = true;
                          node.ItemGroup = idToCheck;
                          node.matriceNode = null;
                          node.Cube = new Primitives3D.Cube(node.ItemGroup, node.Position, new Vector3(0, 0, 0), node.TotalWidth, node.TotalHeight, node.TotalLenght);
                          //Initialise le cube qui représente le noeud
                          node.Cube.BuildCubeStart();
                          if (isFirstGenerationWorld)
                          {
                              myXmlTextWriter.WriteStartElement("Cube");
                              //Structures et ses attributs
                              myXmlTextWriter.WriteAttributeString("id", node.ItemGroup.ToString());
                              myXmlTextWriter.WriteAttributeString("min", "x:" + node.Cube.BoundingBox.Min.X + ";y:" + node.Cube.BoundingBox.Min.Y + ";z:" + node.Cube.BoundingBox.Min.Z);
                              myXmlTextWriter.WriteAttributeString("max", "x:" + node.Cube.BoundingBox.Max.X + ";y:" + node.Cube.BoundingBox.Max.Y + ";z:" + node.Cube.BoundingBox.Max.Z);
  
                              myXmlTextWriter.WriteEndElement();//Ferme le noeud xml cube
                              myXmlTextWriter.Flush();
  
                          }
                          //Ajoute l'id du noeud aux groupe d'id de la region s'il n'y était pas auparavant
                          if (!region.IdFound.Contains(node.ItemGroup) &amp;&amp; node.ItemGroup != 0)
                          {
                              region.IdFound.Add(node.ItemGroup);
                          }
                      }
                      // If the node group is equal to an empty group id -&gt; 0 it removes entire                        else
                      {
                          nodeToDelete = node;
                      }
                  }
              }
              if (!isSameId)
              {
  
                  break;
              }
              //Console.Out.WriteLine("Nodes cout generated : " + Nodes.Count.ToString());
  
          }
          // If a node does not contain all the same id -&gt; go remove it
          if (!isSameId)
          {
              region.Nodes[nodeToRegenerate.ItemGroup].Remove(nodeToRegenerate);
  
          }
          if (nodeToDelete != null)
          {
              region.Nodes[nodeToDelete.ItemGroup].Remove(nodeToDelete);
          }
          nodeToDelete = null;
          //Dispose the resources
          newNode.Dispose();
          nodeMatriceWorld = null;
  
  
          return nodeToRegenerate;
      }
      #endregion
  </code></pre>
  
  <h2><strong>The exception:</strong></h2>
  
  <p><img src="https://i.stack.imgur.com/EsUGD.png" alt="enter image description here"></p>