简体   繁体   English

C#中调用Web服务的非托管泄漏

[英]Unmanaged Leak in Calling Web service in C#

I need help with my code. 我的代码需要帮助。

Ive been trying to use RedGate to monitor the memory usage of my application, after hours of testing It pointed out the unmanaged code of my application, and I could only think of my webservice calling as the only or somewhat unmanaged part of my code. 经过数小时的测试,我一直试图使用RedGate监视应用程序的内存使用情况它指出了应用程序的非托管代码,并且我只能将Web服务调用视为代码中唯一或有些不受托管的部分。 Ive been debugging for hours and cant seem to find out where or what really happened. 我已经调试了几个小时,似乎无法找出真正发生的地方或发生的事情。 below is my code. 下面是我的代码。

private void btnstart_Click(object sender, EventArgs e)
{
    btnPressed = !btnPressed //boolean
    if(btnPressed)
    {
        myTask = Task.Factory.StartNew(() => 
        {
            do {
                _checkMatches(token.Token);
            } while (token.IsCancellationRequested != true);
        },token.Token);
    }
    else
    {
        token.Cancel();

        try {
            Task.WaitAny(myTask);
        }
        catch(Exception Error) {
            //Put to logs
        }
        finally
        {
            if(myTask.isCancelled || myTask.IsCompleted || myTask.IsFaulted)
            {
                myTask.Dispose();
            } 
        }
    }
}

private void _checkMatches(CancellationToken token) 
{
    try
    {
        if(token.IsCancellationRequested)
        {
            token.ThrowIfCancellationRequested();
        }
        EndpointAddressBuilder ServiceEndPoint = new EndpointAddressBuilder(//Read Endpoint From Notepad);
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });//just bypassing validation for test purposes

        // WebService is an ASMX web service
        using (WebService.SoapClient client = new WebService.SoapClient()) 
        {
            WebService.checkResponse clientRes = client.checkClient();
            if(clientRes.response == 1 || clientRes.response == 2) {
                //Put to Logs
            }
        }
    }
    catch(Exception error){
        //Put to logs
    }
}

I cant seem to find any error in this code. 我似乎找不到此代码中的任何错误。 Can Someone Help me with what is the problem why my unmanaged code is leaking? 有人可以帮我解决我的非托管代码泄漏的问题是什么吗? or could someone suggest what tools to be used or even suggest to find the leaking part of my code? 还是有人建议使用什么工具,甚至建议找到我的代码的泄漏部分? Any Help would be great. 任何帮助都会很棒。

If you are using RedGate memory profiler it should tell you more than just pointing unmanaged resources. 如果您使用的是RedGate内存探查器,它不仅可以指向非托管资源,还可以告诉您更多信息。 Because you are not directly creating any unamanged resources, most likely your memory leak is caused by a managed object or an event subsciption(or callback subscription). 因为您不是直接创建任何未使用的资源,所以内存泄漏很可能是由托管对象或事件订阅(或回调订阅)引起的。

Standard .net managed objects which have unmanaged resources (ie : WebService.SoapClient) have implemented the finalizer(destructor) in order to get rid of its unmanaged resources in any case if Dispose is not called. 如果未调用Dispose,则具有非托管资源的标准.net托管对象(即WebService.SoapClient)已实现了finalizer(destructor)以便摆脱其非托管资源。 If you use a standard .net managed object which has unmanaged resources and don't call the dispose (this is bad), still it will release unamanged resources when it is going through the finalization. 如果您使用具有非托管资源的标准.net托管对象,并且不调用dispose(这很糟糕),则在完成终结时,它仍会释放未使用的资源。 Generally this shouldn't cause unmanaged resource memory leak. 通常,这不应导致非托管资源内存泄漏。

How to check if there is any memory leak: 如何检查是否有内存泄漏:

Execute your memory consumption logic a few times. 几次执行您的内存消耗逻辑。 Go to the Instance list and see if there is any instances are growing. 转到“实例”列表,查看是否有任何实例在增长。 Select the 'Objects with Source' option in order to get rid of heaps of system object. 选择“带有源的对象”选项以摆脱系统对象的堆。 You should see more instances that there should be if there is any memory leak. 如果出现内存泄漏,您应该看到应该有更多的实例。

If there is any growing instances, pick one and see the objects retention graph which will show you exactly which instance holds the reference. 如果有任何增长的实例,请选择一个实例并查看对象保留图,该图将向您确切显示哪个实例保存了引用。

And also, make sure that you have implemented IDisposable properly and dispose all disposable objects and unsubscribe from all event subscriptions. 另外,请确保已正确实现IDisposable并处置所有一次性对象,并取消所有事件订阅的订阅。

Have a look at below walkthroughs 看看下面的演练

http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/walkthrough http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/ http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/ 演练http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM