打开 Winform 窗口后,内存一直增加,关闭窗口后,内存不会还原,使用Dispose();
没有效果
比如第一段代码中使用 Winform 显示图片
另外找到 .NET API 中,GC.Collect();
和SetProcessWorkingSetSize
函数,会降低内存显示,实际上是转移到虚拟内存中,效果非常不好
是否有好的办法关闭 form 窗口后,清理内存的呢
var Forms = forms.System.Windows.Forms;
var form = new Forms.Form;
form.StartPosition = Forms.FormStartPosition.Manual;
form.FormBorderStyle = Forms.FormBorderStyle.None;
form.TopMost = true;
form.ControlBox = false;
form.ShowInTaskbar = false;
form.Margin = new Forms.Padding(0);
form.Padding = new Forms.Padding(1);
form.MinimumSize = new Size(10, 10);
form.Size = new Size(10, 10);
form.Location = new Point(100, 100);
form.AutoSize = true;
form.GetType().GetProperty("DoubleBuffered",
host.flags(clr.System.Reflection.BindingFlags.NonPublic,
clr.System.Reflection.BindingFlags.Instance))
.SetValue(form, true);
form.BackColor = drawing.System.Drawing.SystemColors.HotTrack;
var pic = new Forms.PictureBox;
pic.Name = 'pic';
pic.SizeMode = Forms.PictureBoxSizeMode.Zoom;
pic.Size = new Size(450, 500);
pic.BackColor = drawing.System.Drawing.SystemColors.Control;
pic.Location = new Point(0, 0);
pic.Image = img;
form.Controls.Add(pic);
var autoSize = form.Size;
var t = (form.ClientSize.Width - pic.Width - form.Padding.Horizontal);
autoSize.Width -= t;
autoSize.Height -= t;
form.AutoSize = false;
form.Size = autoSize;
pic.Dock = Forms.DockStyle.Fill;
var form_shown =
form.Shown.connect(
function (sender, args) {
form.TopMost = true;
form.Activate();
});
var pic_doubleClick =
pic.DoubleClick.connect(
function (sender, args) {
form.Dispose();
pic.Dispose();
Forms.Application.Exit();
form.Close();
});
Forms.Application.Run(form);
img.Dispose();
使用SetProcessWorkingSetSize
form.Close();
FlushMemory();
function FlushMemory()
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
if (System.Environment.OSVersion.Platform == System.PlatformID.Win32NT)
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.