This project has moved. For the latest updates, please go here.

Densematrix overflow exception and outofmemory exception

Mar 21, 2013 at 4:12 PM
Hi all,

I was trying to work with very large matrices in C# using Math.Net library when I met the following exceptions.
private static DenseMatrix OutOfMemoryTest()
        {
            var testMat = new DenseMatrix(1000000, 7500, 10.5);
            return testMat;
        }
If I tried to create a large matrix like above, a exception was thrown.
Arithmetic operation resulted in an overflow.
private static DenseMatrix OutOfMemoryTest()
        {
            var testMat = new DenseMatrix(100000, 7500, 10.5);
            return testMat;
        }
If I cut the row numbers to 100000, the library threw an OutOfMemory Exception.
Exception of type 'System.OutOfMemoryException' was thrown.
Finally, if I even cut the row number to 10000, but change the return value to double array.
private static double[,] OutOfMemoryTest()
        {
            var testMat = new DenseMatrix(10000, 7500, 10.5);
            return testMat.ToArray();
        }
It did allow me to successfully create the matrix but failed to return the array.
Exception of type 'System.OutOfMemoryException' was thrown.
I am wondering if I misused the library or is there anything about memory I mislook? Or is it simply the library's problem?

Am I missing anything here? Please help. Thanks all.

Ben
Mar 21, 2013 at 5:27 PM
Edited Mar 21, 2013 at 5:38 PM
Hi,

I believe that maximum size of a .NET object is 2GB (though there is an option in the 64-bit version of .NET 4.5 to increase that size - I've never used it). The first two versions above put you well over 2GB. In the last version, the array and the matrix would take about 575MB each. However, each block of memory needs to be contiguous.
Also, is your matrix really dense or can you use the a sparse matrix?

Regards,
Marcus
Marked as answer by cdrnet on 10/3/2013 at 5:45 PM
Mar 21, 2013 at 5:35 PM
cuda wrote:
Hi,

I believe that max maximum size of a .NET object is 2GB (though there is an option in the 64-bit version of .NET 4,5 to increase that size - I've never used it). The first two
versions above put you well over 2GB. In the last version, the array and the matrix would take about 575MB each. However, each block of memory needs to be contiguous.
Also, is your matrix really dense or can you use the a sparse matrix?

Regards,
Marcus
Thank you for your response. Yes my matrix is dense but I should be able to split them into smaller ones. Thank you for reminding me of the object size limit, which I overlooked previously.

-Ben