做了一个WindowServer刚开始在服务器上运行没发生什么事
但每隔一天大概就会增加 1M左右 的内存开销
运行久了就会出现这个“System.OutOfMemoryException”的异常这得怎么办啊 是不是哪处理的不好 内存溢出?
每天增加的内存又不多 可是这个服务是要求一年从头开尾都是开的
请问各位高手指点 帮忙解决一下 里边大概是这样的
-------------------------------------------------------------------------------------------------
void 开始操作(){    while(true){       读取数据库信息...       调用异步方法处理一些后台操作(完成操作)
       ....       Thread.Sleep(1000);
    }
}void 完成操作(AsyncResult iresult){
    ...
    数据库操作...(自动释放数据连接)
    
    ...
}
-------------------------------------------------------------------------------------------------

解决方案 »

  1.   

    建议你还是debug一下吧,或者用perfmon去看看线程的调用情况,
      

  2.   

    一天也就增加1M不到 而且也尝试过用 GC.Collect();可是几乎没有效果请问一般的内存溢出是怎么引起的?
      

  3.   

    源代码大概是这样的 麻烦 各位帮忙瞧瞧
        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    }
      

  4.   

    debug一下吧,太长了,不太愿意看啊
      

  5.   

    使用了多线程,在start方法里报错了,提示内存一出,怎么回事呢?