C#COM对象不会离开内存

时间:2014-09-03 18:30:11

标签: c# thread-safety

我有10个线程,每个线程创建一个我的对象的新实例。调用此对象的2个方法并且线程的循环寿命结束,问题是它保留在内存中。

这一小时最终会发生一次内存不足。

我有以下代码:

        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);
        new Thread(Thread_).Start();
        Thread.Sleep(1000);

void Thread_()
{
    spdNFeX _spdNFeX = new spdNFeX();
    try
    {
        String[] Chave = (String[])Chaves.ToArray(typeof(string));

        _spdNFeX.LoadConfig("");
        Random Rand = new Random();
        _spdNFeX.DiretorioLog = "C:\\Users\\Joao\\Documents\\Visual Studio 2012\\Projects\\TesteNFe\\TesteNFe\\bin\\Debug\\" + Convert.ToString(Rand.Next(1, 999) + "\\");

        for (int i = 0; i < Chave.Length; i++)
        {
            _spdNFeX.ConsultarNF(Chave[i]);
            Thread.Sleep(5000);
        }
    }
    finally {
        _spdNFeX = null;
    }

}

我进行了以下测试,但没有成功。

测试1

finally {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_spdNFeX);
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

测试2

finally {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(_spdNFeX);
        _spdNFeX = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

测试3

public class ComDisposer : IDisposable
{
    private List<Object> _comObjs;

    public ComDisposer()
    {
        _comObjs = new List<Object>();
    }

    ~ComDisposer()
    {
        Dispose(false);
    }

    public T Add<T>(T o)
    {
        if (o != null && o.GetType().IsCOMObject)
            _comObjs.Add(o);
        return o;
    }

    public void Clear()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            for (int i = _comObjs.Count - 1; i >= 0; --i)
                Marshal.FinalReleaseComObject(_comObjs[i]);
            _comObjs.Clear();
        }
    }

    void IDisposable.Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}        

void Thread_()
{
    using (ComDisposer cd = new ComDisposer())
    {
        spdNFeX _spdNFeX = cd.Add(new spdNFeX());
        try
        {
            String[] Chave = (String[])Chaves.ToArray(typeof(string));

            _spdNFeX.LoadConfig("");
            Random Rand = cd.Add(new Random());
            _spdNFeX.DiretorioLog = "C:\\Users\\Joao\\Documents\\Visual Studio 2012\\Projects\\TesteNFe\\TesteNFe\\bin\\Debug\\" + Convert.ToString(Rand.Next(1, 999) + "\\");

            for (int i = 0; i < Chave.Length; i++)
            {
                _spdNFeX.ConsultarNF(Chave[i]);
                Thread.Sleep(5000);
            }
        }
        finally
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用System.Runtime.InteropServices.Marshal.FinalReleaseComObject