尝试在 UWP 中初始化 InkCanvas 数组时内存不足,无法继续执行程序 [英] Insufficient memory to continue the execution of the program when trying to initialize array of InkCanvas in UWP

查看:27
本文介绍了尝试在 UWP 中初始化 InkCanvas 数组时内存不足,无法继续执行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 公共密封部分类 MainPage : Page{InkCanvas[] arrInkCanvas = null;公共主页(){this.InitializeComponent();int i = 0;arrInkCanvas = 新 InkCanvas[1000];尝试{对于 (i = 0; i <1000; i++){arrInkCanvas[i] = new InkCanvas();}}捕获(异常前){}}}

当我在 WPF 应用程序中运行此代码时没有问题,但在 UWP 中它抛出异常:内存不足,无法继续执行程序."

是bug还是有人可以告诉我的?

谢谢.

解决方案

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.

这篇关于尝试在 UWP 中初始化 InkCanvas 数组时内存不足,无法继续执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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