【问题标题】:UWP Memory Leak by calling ServiceReference (C#)通过调用 ServiceReference (C#) 导致 UWP 内存泄漏
【发布时间】:2016-08-31 11:19:44
【问题描述】:

我有一个问题:

  1. 我创建了一个默认的 ServiceReference

    [OperationContract]
    string GetData(int value);
    

    实施:

    public string GetData(int value) { return string.Format("You entered: {0}", value); }

  2. 将 ServiceReference 添加到新的 UWP 项目中 (Add Reference Pic.)

  3. 在我调用的 UWP 主构造函数中:

    void Test() { ServiceReference.Service1Client h = new ServiceReference.Service1Client(); for (int i = 0; i < 10000; i++) { var u = h.GetDataAsync(5); } }

    或者:

    async void Test() { ServiceReference.Service1Client h = new ServiceReference.Service1Client(); for (int i = 0; i < 10000; i++) { var u = await h.GetDataAsync(5); } }

  4. 问题: WCF 服务有效。 UWP 应用程序有效。但是进程内存永远不会减少。 Memory Pic. 垃圾收集器什么也不做。

    当我打电话时:

    GC.Collect(2); GC.WaitForPendingFinalizers();

    它改变“无”(没有上升那么多)。但是进程内存永远不会减少。

    在内存不足的设备上,当内存已满时,应用程序将崩溃。(树莓派 2 / 3)。

    我做错了什么。

Raspberry pi 2 / 3 场景:应用程序在日常循环中调用 ServiceReference。该应用程序全年运行。有时应用程序会因为内存已满而崩溃。

【问题讨论】:

  • 您只发布了部分代码,但没有遗漏 dispose 或将您的客户端放入 using 块中?
  • ServiceReference 没有 dispose 函数。在 Using-Block 中只能使用 IDisposable 对象。

标签: c# visual-studio wcf memory-leaks garbage-collection


【解决方案1】:

(1) 设置“ServiceReference.Service1Client h = new ServiceReference.Service1Client();” 到全局参数

或 (2)h.Dispose()到底

【讨论】:

  • 全局选项工作。 ServiceReference 没有 dispose 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 2021-04-09
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2017-02-20
相关资源
最近更新 更多