Slove在c#中的WindowsFormsApplication1.exe中发生类型为'System.OutOfMemoryException'的未处理异常 [英] Slove the An unhandled exception of type 'System.OutOfMemoryException' occurred in WindowsFormsApplication1.exe in c#
问题描述
我的代码如:
my code like as:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
filename = Path.GetFileName(openFileDialog.FileName);
path = Path.GetDirectoryName(openFileDialog.FileName);
textBox1.Text = path + "\\" + filename;
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
filename = Path.GetFileName(openFileDialog.FileName);
path = Path.GetDirectoryName(openFileDialog.FileName);
textBox2.Text = path + "\\" + filename;
}
}
private void button3_Click(object sender, EventArgs e)
{
string s = ExtractTextFromPdf(textBox1.Text);
string s1 = Extract(textBox2.Text);
d = CalculateSimilarity(s, s1);
d = Math.Round(d, 0);
label1.Visible = true;
label1.Text = Convert.ToString(d);
}
double CalculateSimilarity(string source, string target)
{
if ((source == null) || (target == null)) return 0.0;
if ((source.Length == 0) || (target.Length == 0)) return 0.0;
if (source == target) return 1.0;
int stepsToSame = ComputeLevenshteinDistance(source, target);
double ds =(1.0 - ((double)stepsToSame / (double)Math.Max(source.Length, target.Length)))*100;
return ds;
}
int ComputeLevenshteinDistance(string source, string target)
{
if ((source == null) || (target == null)) return 0;
if ((source.Length == 0) || (target.Length == 0)) return 0;
if (source == target) return source.Length;
int sourceWordCount = source.Length;
int targetWordCount = target.Length;
// Step 1
if (sourceWordCount == 0)
return targetWordCount;
if (targetWordCount == 0)
return sourceWordCount;
int[,] distance = new int[sourceWordCount + 1, targetWordCount + 1];
// Step 2
for (int i = 0; i <= sourceWordCount; distance[i, 0] = i++) ;
for (int j = 0; j <= targetWordCount; distance[0, j] = j++) ;
for (int i = 1; i <= sourceWordCount; i++)
{
for (int j = 1; j <= targetWordCount; j++)
{
// Step 3
int cost = (target[j - 1] == source[i - 1]) ? 0 : 1;
// Step 4
distance[i, j] = Math.Min(Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), distance[i - 1, j - 1] + cost);
}
}
return distance[sourceWordCount, targetWordCount];
}
我的应用主题是每当比较pdf文件时的比例。这就是为什么我
已经阅读了pdf文件的内容并存储在字符串之后我要比较
两个字符串使用LevenshteinDistance alogrithem.at我发现错误的时间
方法-ComputeLevenshteinDistance。
错误的判断如下:
My application theme is geting percentage whenever compare pdf files. thats why i
have read the content of pdf files and stored in strings after that i want to compare
the two strings using LevenshteinDistance alogrithem.at the time i have error occured
in method -ComputeLevenshteinDistance.
error statment like as:
int[,] distance = new int[sourceWordCount + 1, targetWordCount + 1];
最后错误是
WindowsFormsApplication1.exe中出现未处理的'System.OutOfMemoryException'类型的异常。
请帮帮我。谢谢你
Finally error is
An unhandled exception of type 'System.OutOfMemoryException' occurred in WindowsFormsApplication1.exe.
please help me.thank u
推荐答案
你有调试吗?
似乎sourceWordCount或targetWordCount太大了。
Have you debug it?
It seems like sourceWordCount or targetWordCount are too big.
您正在使用的Levenshtein算法的实现需要(FileLength1 * FileLength2)字节的内存。根据您要比较的PDF文件的大小,这显然会很快导致OutOfMemory-Exception,尤其是当您以32位进程运行应用程序时。
看看这个替代Levenshtein-Implementation,正如作者声称的那样,只需要2 * Min(StrLen1,StrLen2)字节:快速,内存效率高的Levenshtein算法 [ ^ ]
The implementation of the Levenshtein Algorithm that you're using requires (FileLength1 * FileLength2) bytes of memory. Depending on the size of PDF files you're trying to compare, this obviously can quickly result in a OutOfMemory-Exception, especially if you're running your application as a 32 bit process.
Take a look at this alternative Levenshtein-Implementation which, as the author claims, only requires 2*Min(StrLen1,StrLen2) bytes: Fast, memory efficient Levenshtein algorithm[^]
这篇关于Slove在c#中的WindowsFormsApplication1.exe中发生类型为'System.OutOfMemoryException'的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!