后台线程增加内存使用 [英] Background thread increases memory usage

查看:63
本文介绍了后台线程增加内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要每 30 秒检查一次数据库,以检查特定记录是否已更新.我为此使用后台线程.问题是当我在内存分析器中检查它时,应用程序的内存使用量不断增加.这是代码

I have an application which need to check db for every 30secons to check a particular record has updated or not. I use background thread for that. The problem is memory usage of application is continuously increase when I check it in memory profiler. Here is the code

Form1:Base

Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Check));
t.IsBackground = true;
t.Start();

public void Check() {
            bool isUpdate = false;
            while (!isUpdate)
            {
                DataTable _data = CheckRecord();
                if (_data.Rows.Count > 0)
                {
                    isUpdate = true;
                }
                else
                {
                    Thread.Sleep(30000);
                }
            }
        }

public DataTable CheckRecord(){
return CHANNEL.GetData();
}

Form1 继承基础表单

Form1 inherit base form

Base:Form

ChannelFactory<ServiceClass> temp= new ChannelFactory<ServiceClass>("AAA");
ServiceClass ss=null;

public ServiceClass CHANNEL
{
    get {
        if(ss==null)
            ss=temp.CreateChannel();
        return ss;
    }
    }

推荐答案

根据提供的信息,无法回答您的问题.但是,要解决内存泄漏问题,您有以下几种选择:

With the provided information it is not possible to answer your question. However, to troubleshoot memory leaks you have a few options:

  • 如果删除线程执行的代码,内存使用量的增加是否会消失?如果没有,那么您必须在代码中的其他地方寻找泄漏.

  • Does the memory usage increase go away if you remove the code executed by the thread? If not then you have to look for a leak somewhere else in your code.

泄漏很可能不是托管内存,因为它是垃圾收集的.您可以通过监视性能计数器.NET CLR Memory -> # Bytes in all Heaps 来验证这一点.如果此计数器在您的程序运行时显着增加,那么您就在泄漏"托管内存,因为您有一些活动数据结构在大小不断增长但不符合垃圾收集条件.

Most likely the leak is not managed memory because it is garbage collected. You can verify this by monitoring the performance counter .NET CLR Memory -> # Bytes in all Heaps. If this counter increases significantly while your program is running you are "leaking" managed memory in the sense that you have some active data structure that keeps growing in size while not being eligible for garbage collection.

假设您已经排除了受管理的泄漏",那么您将不得不寻找不受管理的泄漏.您是否编写代码调用一些需要您清理的互操作 API?在您的代码中,唯一的可能性似乎是 CheckRecord,您应该检查它以了解它是否是泄漏的来源.

Assuming that you have ruled out a managed "leak" then you will have to look for an unmanaged leak. Does you code call some interop API that requires that you clean up? In your code the only possibility seems to be CheckRecord which you should inspect to learn if that is the source of the leak.

查看您的代码,似乎您有错误或有错字:

Looking at your code it seems that either you have a bug or there is a typo:

public void Check() {
  bool isUpdate = false;
  while (isUpdate) {
    ...
  }
}

因为 isUpdatefalse 永远不会进入 while 循环,Check 将立即返回并终止线程.

Because isUpdate is false the while loop will never be entered and Check will return immediately terminating the thread.

这篇关于后台线程增加内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆