管理内存如何解决C#中没有足够的内存异常

时间:2012-05-01 19:26:50

标签: c# memory-management

我的内存是4GB(2.99GB可用,但我不知道为什么)。 当使用Bluebit Library来处理项目C#Visual Studio 2010中的Matrix时,以及在Windows Form Application中执行代码并且在连接数据库之后,出现NotEnoughMemoryException(没有足够的内存来继续执行)并且物理内存使用的是2.14GB,但在控制台应用程序中单独使用代码时不会出现任何异常。

StreamWriter aWr = new StreamWriter("aMatrix.mtx");
Matrix aMatrix = new Matrix(termsList.Count, Docs.Count);
/////load aMatrix from DataBase and aMatrix[8200, 40]

SVD svd = new SVD(aMatrix);
StreamWriter sWr = new StreamWriter("sMatrix.mtx");
Matrix S = svd.S.SubMatrix(0, k - 1, 0, k - 1);
for (int row = 0; row < S.Rows; row++)
{
        for (int col = 0; col < S.Cols; col++)
            sWr.Write(S[row, col] + "\t");
        sWr.Write("\n");
}
sWr.Close();

StreamWriter uWr = new StreamWriter("uMatrix.mtx");
Matrix U = svd.U;
nt endRaws = U.Rows - 1;
U = U.SubMatrix(0, endRaws, 0, k - 1);
for (int row = 0; row < U.Rows; row++)                                                      
{
    for (int col = 0; col < U.Cols; col++)
        uWr.Write(U[row, col] + "\t");
    uWr.Write("\n");
}
uWr.Close();

StreamWriter vWr = new StreamWriter("vMatrix.mtx");
Matrix V = svd.V;
endRaws = V.Rows - 1;
V = V.SubMatrix(0, endRaws, 0, k - 1);
for (int row = 0; row < V.Rows; row++)
{
    for (int col = 0; col < V.Cols; col++)
        vWr.Write(V[row, col] + "\t");
    vWr.Write("\n");
}
vWr.Close();

StreamWriter vtWr = new StreamWriter("vtMatrix.mtx");
Matrix VT = new Matrix(V.Cols, V.Rows);
for (int row = 0; row < V.Rows; row++)
    for (int col = 0; col < V.Cols; col++)
        VT[col, row] = V[row, col];

for (int row = 0; row < VT.Rows; row++)
{
    for (int col = 0; col < VT.Cols; col++)
        vtWr.Write(VT[row, col] + "\t");
    vtWr.Write("\n");
}
vtWr.Close();

我如何管理我的记忆?

1 个答案:

答案 0 :(得分:0)

您需要在x64模式下构建应用,以便每个进程使用超过2Gb的内容,请参阅Why am I getting an Out Of Memory Exception in my C# application?