我正在处理Levenshtein的差异,并且在处理int [11000,11000]的矩阵时我得到了OutOfMemoryException。从我所知道的一切都不会占用太多内存,但我仍然收到OutOfMemoryException。 如果我的计算是正确的,它有点不到一半的演出,我检查了并且有足够的公羊支持这个。
可能是什么问题,我在阵列上达到了最大值吗?
如何在不知道它们有多大的情况下处理大型数组?也许是内存映射文件?
更新
这是代码,在第10行抛出异常。
private int LevenshteinDifference(string source, string target)
{
// Check for degenerate cases.
int sourceLength = source != null ? source.Length : 0;
int targetlength = target != null ? target.Length : 0;
if (sourceLength == 0 && targetlength == 0) return 0;
if (sourceLength == 0) return targetlength;
if (targetlength == 0) return sourceLength;
// Do the calculations.
int[,] distance = new int[sourceLength + 1, targetlength + 1];
// Fill first row and column of the matrix with consecutive integers starting at 0 position.
for (int x = 0; x <= sourceLength; x++) distance[x, 0] = x;
for (int y = 0; y <= targetlength; y++) distance[0, y] = y;
// We must fill in the remaining cells.
for (int x = 1; x <= sourceLength; x++)
{
for (int y = 1; y <= targetlength; y++)
{
// Cost is equal to 0 if x and y match, otherwise 1.
int cost = source[x - 1] == target[y - 1] ? 0 : 1;
// min(min(left+1, top+1), diagonal+cost)
distance[x, y] = Math.Min(Math.Min(distance[x - 1, y] + 1, distance[x, y - 1] + 1), distance[x - 1, y - 1] + cost);
}
}
// Return the bottom-right most cell value. This is the total cost to effectively make source equal the target.
return distance[sourceLength, targetlength];
}