且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试在 UWP 中初始化 InkCanvas 数组时内存不足,无法继续执行程序

更新时间:2023-01-14 14:25:36

Windows 操作系统将根据 RAM 和操作系统配置平衡位于应用程序的内存.您可以通过 MemoryManager(正如 cFrozenDeath 所说)通过以下方式检查您的应用程序的内存配置:

 public MainPage(){this.InitializeComponent();var AppUsageLevel = MemoryManager.AppMemoryUsageLevel;var AppMemoryLimit = MemoryManager.AppMemoryUsageLimit;}

根据我的测试,您的应用确实内存不足.您可以做一个简单的测试:在每次new InkCanvas() 的 for 循环中,您可以调用 MemoryManager.AppMemoryUsage 并将其与 MemoryManager.AppMemoryUsageLimit 进行比较.>

如果您有任何以这种方式创建 UIElment 的具体原因,请告诉我吗?Win10 1.586 确实提供了一个新的 API TrySetAppMemoryUsageLimit,用于设置应用程序的内存限制.但是,根据内部讨论,此 API 目前仅适用于非常有限的场景,例如移动设备上的 VOIP 应用程序.而且这个 API 的示例代码和文档还没有完全准备好.

Here is my code:

    public sealed partial class MainPage : Page
{
    InkCanvas[] arrInkCanvas = null;
    public MainPage()
    {
        this.InitializeComponent();

        int i = 0;

        arrInkCanvas = new InkCanvas[1000];
        try
        {
            for (i = 0; i < 1000; i++)
            {                    
                arrInkCanvas[i] = new InkCanvas();
            }
        }
        catch (Exception ex)
        {

        }
    }
}

When i run this code in WPF app there's no problem , but in UWP it throws exception : "Insufficient memory to continue the execution of the program."

Is it bugs or something can anyone tells me ?

Thanks.

Windows OS will balance the memory located to the application based on RAM and OS configuration. You can check your app's memory configuration by MemoryManager (just as cFrozenDeath said) in following way:

 public MainPage()
 {
     this.InitializeComponent();

     var AppUsageLevel = MemoryManager.AppMemoryUsageLevel;
     var AppMemoryLimit = MemoryManager.AppMemoryUsageLimit;
  }

From my test, your app does run out of memory. You can do a simple test: In the for loop each time you new InkCanvas(), you can call MemoryManager.AppMemoryUsage and compare it with the MemoryManager.AppMemoryUsageLimit.

Could you let me know if you have any specific reason for creating UIElment in such a way? Win10 1.586 does provide a new API, TrySetAppMemoryUsageLimit, to set the app's memory limitation. However, based on internal discussion, this API only works for very limited scenario right now, such as VOIP application on mobile device. And the sample code and document for this API are not quite ready.