Posted Monday, December 04, 2006 9:19 AM
|
|
|
|
I'm receiving some strange output from the Matrix Library when it comes to solving the same problem. It is giving me two different answers for the same problem. I cannot tell the cause of the problem so I'm submitting this code for analysis. The form was built using VS 2005 with a single button1 and label1. I can send you the project files if you want.
My Machine Specs:
HP workstation xz6200 Intel Xeon 2.8 Ghz Dual Core w/Hyperthreading
2.5 Gb ram.
Windows XP sp2
VS 2005 Professional
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bluebit.MatrixLibrary;
namespace MatrixDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Matrix SolveProbelemAndDisplay()
{
Matrix A = new Matrix(4, 5);
A[0, 0] = 1; A[0, 1] = 0; A[0, 2] = 1; A[0, 3] = 0; A[0, 4] = 0;
A[1, 0] = 0; A[1, 1] = 1; A[1, 2] = -1; A[1, 3] = 0; A[1, 4] = 0;
A[2, 0] = 0; A[2, 1] = 0; A[2, 2] = -1; A[2, 3] = 1; A[2, 4] = 0;
A[3, 0] = 0; A[3, 1] = 0; A[3, 2] = 1; A[3, 3] = 0; A[3, 4] = 1;
Matrix B = new Matrix(4, 1);
B[0, 0] = 1;
B[1, 0] = 0;
B[2, 0] = 1;
B[3, 0] = 0;
Matrix X = A.Solve(B);
label1.Text = X.ToString();
return X;
}
private void button1_Click(object sender, EventArgs e)
{
Matrix ANS = SolveProbelemAndDisplay();
for (int i = 0; true; )
{
Matrix ANSTemp = SolveProbelemAndDisplay();
if (!ANS.Equals(ANSTemp))
throw new Exception("Answers are not the same");
}
}
}
}
|
|
Posted Monday, December 04, 2006 12:31 PM
|
|
|
|
Hello,
Creating new matrices in loops without disposing them will exhaust system memory.
I would modify the code as follows using the Dispose method to free resources of unused objects:
public Matrix SolveProbelemAndDisplay()
{
Matrix A = new Matrix(4, 5);
A[0, 0] = 1; A[0, 1] = 0; A[0, 2] = 1; A[0, 3] = 0; A[0, 4] = 0;
A[1, 0] = 0; A[1, 1] = 1; A[1, 2] = -1; A[1, 3] = 0; A[1, 4] = 0;
A[2, 0] = 0; A[2, 1] = 0; A[2, 2] = -1; A[2, 3] = 1; A[2, 4] = 0;
A[3, 0] = 0; A[3, 1] = 0; A[3, 2] = 1; A[3, 3] = 0; A[3, 4] = 1;
Matrix B = new Matrix(4, 1);
B[0, 0] = 1;
B[1, 0] = 0;
B[2, 0] = 1;
B[3, 0] = 0;
Matrix X = A.Solve(B);
label1.Text = X.ToString();
A.Dispose();
B.Dispose();
return X;
}
private void button1_Click(object sender, EventArgs e)
{
Matrix ANS = SolveProbelemAndDisplay();
for (int i = 0; true; )
{
Matrix ANSTemp = SolveProbelemAndDisplay();
if (!ANS.Equals(ANSTemp))
throw new Exception("Answers are not the same");
ANSTemp.Dispose();
}
}
Trifon Triantafillidis | Lead Developer |
|
|
|
Posted Monday, December 04, 2006 2:11 PM
|
|
|
|
So from what I gather, the Garbage Collector will not automatically dispose of these objects once they go out of scope.
It does work now that I've manually disposed of each of the datastructs.
|
|
Posted Monday, December 04, 2006 2:40 PM
|
|
|
|
Yes, NML in order to achieve optimal performance uses unmanaged memory for the Matrix object - same as .NET Framework does for the Bitmap object. The Garbage collector is not aware of how much memory is used by each object and becomes lazy in disposing objects.
Trifon Triantafillidis | Lead Developer |
|
|
|
|
|