C# 即使使用语句,应用程序进程也会变得更大
似乎我有内存泄漏,因为我看到进程每分钟都在变大 请协助:-C# 即使使用语句,应用程序进程也会变得更大,c#,.net,wpf,memory-leaks,C#,.net,Wpf,Memory Leaks,似乎我有内存泄漏,因为我看到进程每分钟都在变大 请协助:- while (true) { using (Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { using (Graphics graphics = Gr
while (true)
{
using (Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
try
{
graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bitmap.Save(@path + System.Environment.MachineName + ".jpg", ImageFormat.Jpeg);
}
catch (Exception) // Handle file save problems
{
if (File.Exists("path.save"))
{
File.Delete("path.save");
}
}
}
}
Thread.Sleep(15000);
}
在运行了24小时后,我发现内存列在几个小时后就稳定了,正如你们大多数人所说的,我再也不用担心这个列了:-
谢谢你的提示,我现在知道的更多了 GC以确定性方式运行。一旦运行,它将释放内存。你确定内存永远不会被释放吗?应用程序是否因内存不足而崩溃?为什么要使用Thread.Sleep?这是个坏习惯。其次,为什么它在一个while循环中一次又一次地做同样的事情?正如前面所说,使用块将处理有问题的对象,GC将在需要运行时运行,并在需要时清理对象。这也可能是因为您在其他地方保留了一个引用,导致它无法清理,但仍然存在around@RohitVats,感谢您的投入,看起来它在4000K时开始变大,而不是一小时后的7000K。我不知道我应该等多久才能获释…@Ahmedilyas,为什么这是一种坏习惯?我尝试过使用定时器,但过程变为145000K,不知道为什么。原始代码将bmp放在循环之外,导致它是12000K,在我使用using语句之后,它变成4000K,因此我认为使用它并在循环中进行处理可能是一个好主意。主要的想法是每15秒捕获一次屏幕并覆盖上一个屏幕截图文件。你是如何测量内存的?