Jump to content

Help with OutOfMemory exception

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Chris

Chris

    Newbie

  • Members
  • Pip
  • 5 posts
I seem to be getting this error on a quad tree I am working on even with relatively small depths. I have searched through google with little luck on why this happens in c#. If I give it a max level (depth) of 10, it works, but if I go much higher I get the error, and this is before I add any content to the nodes. I'm using VS2010 and XNA 4.0.

Quote

A first chance exception of type 'System.OutOfMemoryException' occurred in QuadTree.exe

public enum Slot

    {

        NW = 0,

        NE = 1,

        SW = 2,

        SE = 3

    }


    class QuadTree<T> where T : class

    {

        private byte _maxLevels;

        private QuadNode _root = null;


        public QuadTree(byte maxLevels)

        {

            _maxLevels = maxLevels;

            _root = new QuadNode(null, -100.0, -100.0, 200.0, 0, _maxLevels);

        }


        class QuadNode

        {

            private QuadNode[] _children = null;

            private QuadNode _parent = null;

            private List<T> _content = new List<T>();

            private byte _level;

            private double _x, _y, _size;


            public QuadNode(QuadNode parent, double x, double y, double size, byte level, byte maxLevels)

            {

                _parent = parent;

                _level = level;

                _x = x;

                _y = y;

                _size = size;


                if (_level < maxLevels)

                {

                    _children = new QuadNode[4];

                    _buildChildren(maxLevels);

                }

            }


            private bool _buildChildren(byte maxLevels)

            {

                double size = _size / 2;

                double x = _x;

                double y = _y;

                byte newLevel = _level;

                newLevel++;


                _children[(int)Slot.NW] = new QuadNode(this, x - (size / 2), y + (size / 2), size, newLevel, maxLevels);

                _children[(int)Slot.NE] = new QuadNode(this, x + (size / 2), y + (size / 2), size, newLevel, maxLevels);

                _children[(int)Slot.SW] = new QuadNode(this, x - (size / 2), y - (size / 2), size, newLevel, maxLevels);

                _children[(int)Slot.SE] = new QuadNode(this, x + (size / 2), y - (size / 2), size, newLevel, maxLevels);


                return true;

            }

        }



    }


#2
Chris

Chris

    Newbie

  • Members
  • Pip
  • 5 posts
Well, it is weird and hard to figure out how to workaround the problem. Out of my 16gb of ram, I have about 2.5gb in use before I run the program. Running the program gets to about 3.7gb use and then throws the exception. Altering the pagefile makes no difference either.

A 10 depth is probably enough for what I need, but am concerned that it may throw the exception on lower depths on a different machine. I rewrote the algorithm for C++, and although it kinda froze, it didn't throw any errors.

---------- Post added at 07:13 PM ---------- Previous post was at 05:38 PM ----------

Strange.

If I make a list of Quad Trees, with each item in the list creating a tree with a depth of 10, I get no errors, even though it is obviously using more memory to store the total. My memory usage is only going up to about 20-30mb, which is a figure that sounds about right, considering the lack of content.

Does compiling in debug mode cause some wild acid test which may explain why if I go to too high of a depth, the program seems to thrash some resources and use about a gb more memory? I'm still pretty new to C# memory management and other C# 'under the bonnet' processes.

#3
Chris

Chris

    Newbie

  • Members
  • Pip
  • 5 posts
Fixed.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users