我目前在c#中的内存使用量正在挣扎。字节的空闲内存[]
我目前正在使用的工具能够上传和下载文件。为此,它使用字节数组作为文件内容的缓冲区。在上传或下载操作后,我可以放弃WebResponse和Stream(+ Reader/Writer)对象,但字节数组永远保持在内存中。它超出了范围,我甚至'空'它,所以我猜垃圾收集从未运行。
在搜索过程中,我发现了很多文章,建议永远不要手动运行GC,但有一个简约的后台应用程序,不断占用100或甚至1000 MB的RAM(使用时间越长,什么都不过分。
那么,如果不推荐使用GC,那么在这种情况下还能做什么呢?
编辑3 /解决方案:我结束了使用从文件I/O填充数据的16kb字节缓冲区。之后,将缓冲区内容写入RequestStream,并采取进一步的操作(更新进度条等)。
编辑2:这似乎与LOH有关。我会在星期五进行测试,然后在这里记录结果。
编辑:这是代码,也许我错过了一个参考?
internal void ThreadRun(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
UploadItem current = Upload.GetCurrent();
if (current != null)
{
string localFilePath = current.src;
string fileName = Path.GetFileName(localFilePath);
elapsed = 0;
progress = 0;
try
{
string keyString = Util.GetRandomString(8);
worker.ReportProgress(0, new UploadState(0, 0, 0));
FtpWebRequest req0 = Util.CreateFtpsRequest("ftp://" + m.textBox1.Text + "/" + keyString, m.textBox2.Text, m.textBox3.Text, WebRequestMethods.Ftp.MakeDirectory);
req0.GetResponse();
FtpWebRequest req1 = Util.CreateFtpsRequest("ftp://" + m.textBox1.Text + "/" + keyString + "/" + fileName, m.textBox2.Text, m.textBox3.Text, WebRequestMethods.Ftp.UploadFile);
worker.ReportProgress(0, new UploadState(1, 0, 0));
byte[] contents = File.ReadAllBytes(localFilePath);
worker.ReportProgress(0, new UploadState(2, 0, 0));
req1.ContentLength = contents.Length;
Stream reqStream = req1.GetRequestStream();
Stopwatch timer = new Stopwatch();
timer.Start();
if (contents.Length > 100000)
{
int hundredth = contents.Length/100;
for (int i = 0; i < 100; i++)
{
worker.ReportProgress(i, new UploadState(3, i * hundredth, timer.ElapsedMilliseconds));
reqStream.Write(contents, i * hundredth, i < 99 ? hundredth : contents.Length - (99 * hundredth));
}
}
else
{
reqStream.Write(contents, 0, contents.Length);
worker.ReportProgress(99, new UploadState(3, contents.Length, timer.ElapsedMilliseconds));
}
int contSize = contents.Length;
contents = null;
reqStream.Close();
FtpWebResponse resp = (FtpWebResponse)req1.GetResponse();
reqStream.Dispose();
if (resp.StatusCode == FtpStatusCode.ClosingData)
{
FtpWebRequest req2 = Util.CreateFtpsRequest("ftp://" + m.textBox1.Text + "/storedfiles.sfl", m.textBox2.Text, m.textBox3.Text, WebRequestMethods.Ftp.AppendFile);
DateTime now = DateTime.Now;
byte[] data = Encoding.Unicode.GetBytes(keyString + "/" + fileName + "/" + Util.BytesToText(contSize) + "/" + now.Day + "-" + now.Month + "-" + now.Year + " " + now.Hour + ":" + (now.Minute < 10 ? "0" : "") + now.Minute + "\n");
req2.ContentLength = data.Length;
Stream stream2 = req2.GetRequestStream();
stream2.Write(data, 0, data.Length);
stream2.Close();
data = null;
req2.GetResponse().Dispose();
stream2.Dispose();
worker.ReportProgress(100, new UploadState(4, 0, 0));
e.Result = new UploadResult("Upload successful!", "A link to your file has been copied to the clipboard.", 5000, ("http://" + m.textBox1.Text + "/u/" + m.textBox2.Text + "/" + keyString + "/" + fileName).Replace(" ", "%20"));
}
else
{
e.Result = new UploadResult("Error", "An unknown error occurred: " + resp.StatusCode, 5000, "");
}
}
catch (Exception ex)
{
e.Result = new UploadResult("Connection failed", "Cannot connect. Maybe your credentials are wrong, your account has been suspended or the server is offline.", 5000, "");
Console.WriteLine(ex.StackTrace);
}
}
}
你怎么知道的字节数组还活着的记忆是什么? – BrokenGlass
你必须有一个你没有擦除的内存的引用。您不必擦除字节数组的内容,必须删除对它的所有引用,并且GC将重新使用该内存。 –
一些相关的代码可能会有所帮助。有可能你正在做一些让GC更难回收内存的问题(比如在某处保留引用)。 –