C#程序内存自动增大
刚学C#,做了一个小程序,程序是想周期的读取Txt文件内容到Label.txt上.程序是可以运行,但是通过任务管理器发现程序所占用内存在慢慢的变大,求解决办法?using...
刚学C#,做了一个小程序,程序是想周期的读取Txt文件内容到Label.txt上.程序是可以运行,但是通过任务管理器发现程序所占用内存在慢慢的变大,求解决办法?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace WFNotes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Start06");
//防止程序重复启动
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(this.ProductName);
if (myProcesses.Length > 1)
{ Application.Exit(); }
//定义窗口在屏幕上的位置
this.Left = System.Windows.Forms.SystemInformation.VirtualScreen.Width - this.Width-10;
this.Top = 10;
//定义Timer触发条件
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream("//Ctunasfile01/ctu_mes 2/rep/function/command/NewNotes/New.txt",FileMode.Open);
StreamReader fr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));
label1.Text = fr.ReadToEnd() + "";
fr.Dispose();
fs.Dispose();
fr.Close();
fs.Close();
}
catch(Exception ex)
{
Application.Exit();
}
} 展开
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace WFNotes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Start06");
//防止程序重复启动
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(this.ProductName);
if (myProcesses.Length > 1)
{ Application.Exit(); }
//定义窗口在屏幕上的位置
this.Left = System.Windows.Forms.SystemInformation.VirtualScreen.Width - this.Width-10;
this.Top = 10;
//定义Timer触发条件
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream("//Ctunasfile01/ctu_mes 2/rep/function/command/NewNotes/New.txt",FileMode.Open);
StreamReader fr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));
label1.Text = fr.ReadToEnd() + "";
fr.Dispose();
fs.Dispose();
fr.Close();
fs.Close();
}
catch(Exception ex)
{
Application.Exit();
}
} 展开
1个回答
展开全部
有个内存手动回收机制,你可以定时调用ClearMemory()一次
#region 内存回收
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 释放内存
/// </summary>
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
App.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
#endregion
#region 内存回收
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 释放内存
/// </summary>
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
App.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
#endregion
追问
我的程序怎么改呢?
追答
新建一个类,例如类名为Clear,把上面的代码复制过去,然后新建一个timer或者直接在timer1中调用Clear.ClearMemory();即可,调用的时间间隔可调整为5分钟左右即可,最好别太短。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询