C#Window Server出现“System.OutOfMemoryException”异常

liyanrong1987 2010-07-17 09:51:59
做了一个WindowServer刚开始在服务器上运行没发生什么事
但每隔一天大概就会增加 1M左右 的内存开销
运行久了就会出现这个“System.OutOfMemoryException”的异常

这得怎么办啊 是不是哪处理的不好 内存溢出?
每天增加的内存又不多 可是这个服务是要求一年从头开尾都是开的
请问各位高手指点 帮忙解决一下

里边大概是这样的
-------------------------------------------------------------------------------------------------
void 开始操作(){

while(true){

读取数据库信息...

调用异步方法处理一些后台操作(完成操作)
....

Thread.Sleep(1000);
}
}

void 完成操作(AsyncResult iresult){
...
数据库操作...(自动释放数据连接)

...
}
-------------------------------------------------------------------------------------------------
...全文
699 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
superzengxian 2010-09-10
  • 打赏
  • 举报
回复
使用了多线程,在start方法里报错了,提示内存一出,怎么回事呢?
宇峰科技 2010-07-26
  • 打赏
  • 举报
回复
debug一下吧,太长了,不太愿意看啊
liyanrong1987 2010-07-26
  • 打赏
  • 举报
回复

终究还是沉了
liyanrong1987 2010-07-24
  • 打赏
  • 举报
回复
Come on!!
liyanrong1987 2010-07-22
  • 打赏
  • 举报
回复
别沉下去好不。。。
ad13824127631 2010-07-20
  • 打赏
  • 举报
回复
看看先~
liyanrong1987 2010-07-20
  • 打赏
  • 举报
回复
源代码大概是这样的 麻烦 各位帮忙瞧瞧


partial class MSGSendService : ServiceBase {
/// <summary>
/// 检查间隔时间(单位毫秒)
/// </summary>
private int ExamineDelayCount {
get {
int time = Config.ExamineDelayCount;
int count = time * 60 * 1000;
return count;
}
}
/// <summary>
/// 触发检查的时间控件
/// </summary>
private System.Timers.Timer ExamineTimer;
// 构造函数
public MSGSendService() {
InitializeComponent();
this.ExamineTimer = new System.Timers.Timer(this.ExamineDelayCount);
this.ExamineTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.ExamineTimer_Elapsed);
}
/// <summary>
/// 服务启动
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args) {
try {
this.SendThreadStart();
} finally {
LogHelper.WriteLog("***************************** Start MSGSend! *****************************");
}
}
/// <summary>
/// 服务停止
/// </summary>
protected override void OnStop() {
try {
this.SendThreadClose();
} finally {
LogHelper.WriteLog("***************************** Stop MSGSend! *****************************");
}
}
#region 按设定间隔时间检查线程是否处于开启状态
private void ExamineTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
//
}
#endregion

#region 定时发送
// 短信服务处理对象
MessageService msgService = new MessageService();
/// <summary>
/// 发送总数量
/// </summary>
public int SendCount { get; private set; }
/// <summary>
/// 下发成功数量
/// </summary>
public int SuccessCount { get; private set; }
/// <summary>
/// 下发错误数量
/// </summary>
public int ErrorCount { get; private set; }
/// <summary>
/// 发送线程
/// </summary>
private Thread SendThread = null;
/// <summary>
/// 是否处于激活状态
/// </summary>
private bool IsAlive { get; set; }
/// <summary>
/// 初始化发送短信线程
/// </summary>
public void SendThread_Init() {
if (this.SendThread == null || !this.SendThread.IsAlive) {
this.IsAlive = true;
ThreadStart starter = delegate { SendMsg(); };
this.SendThread = new Thread(starter);
this.SendThread.IsBackground = true;
this.SendThread.Priority = ThreadPriority.AboveNormal;
this.SendThread.SetApartmentState(ApartmentState.MTA);
this.SendThread.Start();
}
}
/// <summary>
/// 终止发送短信线程
/// </summary>
public void SendThread_Abort() {
lock (this) {
if (this.SendThread != null) {
try {
this.IsAlive = false;
this.SendThread.Abort();
} catch {
this.SendThread = null;
} finally {
GC.Collect();
}
}
// 关闭监听时钟
this.ExamineTimer.Enabled = false;
Thread.Sleep(100);
}
}
/// <summary>
/// 开启发送短信线程
/// </summary>
public void SendThreadStart() {
lock (this) {
if (this.SendThread == null || !this.SendThread.IsAlive) {
this.SendThread_Init();
}
// 激活
this.IsAlive = true;
// 激活监听时钟
//this.ExamineTimer.Enabled = true;
}
}
/// <summary>
/// 关闭发送短信线程
/// </summary>
public void SendThreadClose() {
this.IsAlive = false;
// 关闭监听时钟
this.ExamineTimer.Enabled = false;
}
/// <summary>
/// 重启发送短信线程
/// </summary>
public void SendThreadRestart() {
this.SendThread_Abort();
this.SendThreadStart();
}
/// <summary>
/// 获取待发短信
/// </summary>
private void SendMsg() {
if (null == this.msgService) {
return;
}
//
this.msgService.Url = Config.MessageServiceUrl;
while (true) {
if (this.IsAlive) {
try {
//获取待发对象详细发送清单并进行发送
IList<HX.MIAP.Model.SMSSendDetails> committedList_TD = BLL.BLLSMSSendDetails.GetCommitted(Config.SendPageSize, Config.MinAfter, Config.IsGetSendReport);
for (int i = 0; i < committedList_TD.Count; i++) {
HX.MIAP.Model.SMSSendDetails item = committedList_TD[i];
if (null != item && this.IsAlive) {

if (Config.SendAsync) {
#region 进行异步发送
this.msgService.BeginSendSMS(Config.SystemID, Config.MsgSendSign,
Config.MsgSendSign, new string[] { item.MOB },
item.MsgContent, item.MsgContent,
new AsyncCallback(this.SendMsgCallback), item.ID);
#endregion
} else {
#region 同步发送
string msgId = string.Empty;
string msgError = string.Empty;
bool result = this.msgService.SendSMS(Config.SystemID, Config.MsgSendSign,
Config.MsgSendSign, new string[] { item.MOB },
item.MsgContent, item.MsgContent,
out msgId, out msgError);
// 累计下发数量
this.SendCount++;
if (Config.IsGetSendReport) {
// 更新短信状态
int updateResult = BLL.BLLSMSSendDetails.UpdateState(item.ID, result ? "已发送" : msgError);
}
// 记录错误日志
if (result) {
// 累计下发成功数量
this.SuccessCount++;
LogHelper.WriteLog(string.Format("短信下发成功:手机号码【{0}】内容【{1}】", item.MOB, item.MsgContent));
} else {
LogHelper.WriteLog(string.Format("短信下发时发生错误:错误标识:{0},错误信息{1}", msgId, msgError));
// 累计下发失败数量
this.ErrorCount++;
}
#endregion
}
}
}
committedList_TD = null;
} catch (OutOfMemoryException omex) {
// 发生内存溢出时强制垃圾回收
GC.Collect();
LogHelper.WriteLog("短信下发时发生内存溢出异常!内存已回收!");
} catch (Exception ex) {
LogHelper.WriteLog(string.Format("短信下发时发生异常:{0}", ex.Message));
}
}
// 暂停
Thread.Sleep(Config.SendInterval);
}
}
/// <summary>
/// 异步回调事件(完成短信下发)
/// </summary>
/// <param name="asyncResult"></param>
private void SendMsgCallback(IAsyncResult asyncResult) {
if (!asyncResult.IsCompleted) {
asyncResult.AsyncWaitHandle.WaitOne();
}
if (null != msgService && null != asyncResult) {
string msgId = string.Empty;
string msgError = string.Empty;
int smsdetailsID = Convert.ToInt32(asyncResult.AsyncState);
try {
bool result = msgService.EndSendSMS(asyncResult, out msgId, out msgError);
// 累计下发数量
this.SendCount++;
if (Config.IsGetSendReport) {
// 更新短信状态
int updateResult = BLL.BLLSMSSendDetails.UpdateState(smsdetailsID, result ? "已发送" : msgError);
}
// 记录错误日志
if (result) {
// 累计下发成功数量
this.SuccessCount++;
LogHelper.WriteLog("异步短信下发成功 反馈信息:" + msgError);
} else {
// 累计下发失败数量
this.ErrorCount++;
LogHelper.WriteLog(string.Format("短信下发时发生错误:错误标识:{0},错误信息{1}", msgId, msgError));
}
} catch (OutOfMemoryException omex) {
// 发生内存溢出时强制垃圾回收
GC.Collect();
LogHelper.WriteLog("短信下发时发生内存溢出异常!内存已回收!");
} catch (Exception ex) {
LogHelper.WriteLog(string.Format("短信下发时发生异常:{0}", ex.Message));
}
}
}
#endregion

}
showjancn 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 showjancn 的回复:]
看样子,可能是什么地方导致了死循环。
[/Quote]

看错楼主意思了。
liyanrong1987 2010-07-20
  • 打赏
  • 举报
回复
有没有什么工具 可以实时查看进程详细的内存使用情况的
showjancn 2010-07-20
  • 打赏
  • 举报
回复
看样子,可能是什么地方导致了死循环。
porschev 2010-07-20
  • 打赏
  • 举报
回复
回收资源。。。很多线程应该都没有结束。。。
liyanrong1987 2010-07-20
  • 打赏
  • 举报
回复
一天也就增加1M不到 而且也尝试过用 GC.Collect();可是几乎没有效果

请问一般的内存溢出是怎么引起的?
DreamTiger 2010-07-17
  • 打赏
  • 举报
回复
定期做GC.Collect()释放内存。
李一升 2010-07-17
  • 打赏
  • 举报
回复
进来看看答案
liyanrong1987 2010-07-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mars199 的回复:]
楼主的结贴率不太好
[/Quote]

哈哈 这个。。。
可能是我的提问方式不对 没能吸引人 总得到类似的答案 所以 还请见谅
zhao38322684 2010-07-17
  • 打赏
  • 举报
回复
内存溢出
mars199 2010-07-17
  • 打赏
  • 举报
回复
楼主的结贴率不太好
zhengqian529 2010-07-17
  • 打赏
  • 举报
回复
建议你还是debug一下吧,或者用perfmon去看看线程的调用情况,
灵雨飘零 2010-07-17
  • 打赏
  • 举报
回复
内存溢出内存溢出内存溢出内存溢出内存溢出

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧