views:

59

answers:

2

After some experimenation using CLR Profiler, I found that:

Node[,] n = new Node[100,23]; //'84,028 bytes, is not placed in LOH
Node[,] n = new Node[100,24]; //'86,428 bytes, is 

    public struct Node {
        public int Value;
        public Point Point;
        public Color Color;
        public bool Handled;
        public Object Tag;
    }

During run-time, how do I know an array of structures (or any array) was allocated in the Large Object Heap (LOH)?

+2  A: 

Any object larger than 85,000 bytes will be stored on the LOH. Here is a great blog post about .Net Memory Management.

Matt Dearing
+1  A: 

From your comments, I don't think you actually need to know whether the object is going to go on the LOH or not. Whether or not that's the actual cause of your application's slowdown is kind of irrelevant when all you really want to do is display a warning to the user when they enter a value that's "too big".

So I would suggest something a bit simpler: just use a bit of trial-and-error to determine the cut-off value. If they enter a size over your trial-and-error value, display the warning.

As for your actual performance problems, instead of allocating one big two-dimensional array, you could simply allocate a bunch of "smaller" one dimensional arrays. Instead of:

Node[,] n = new Node[100,100]; // this will go the LOH

You'd do this:

Node[][] n = new Node[100][];
for(int i = 0; i < n.Length; i++) {
    n[i] = new Node[100];  // none of these will be on the LOH
}

You'd still have the same number of nodes in total, but nothing would go on the LOH. Personally, I think you will probably find that the performance is actually not going to be all that much different, but it might be worthwhile to just give it a try.

Dean Harding