【问题标题】:Xamarin.Forms App Crash in AndroidAndroid 中的 Xamarin.Forms 应用程序崩溃
【发布时间】:2017-03-06 00:36:22
【问题描述】:

用户打开应用时会导致应用崩溃的问题是什么?

我与 HockeyApp 集成,显示错误:

VMRuntime.newNonMovableArray java.lang.OutOfMemoryError: 无法分配 63701004 字节分配,其中 16777056 个可用字节和 41MB 直到OOM

Xamarin caused by: java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte
allocation with 16777056 free bytes and 41MB until OOM
dalvik.system.VMRuntime.newNonMovableArray(Native Method)
android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
android.graphics.BitmapFactory.decodeStreamInternal()BitmapFactory.java:639
android.graphics.BitmapFactory.decodeStream()BitmapFactory.java:615
android.graphics.BitmapFactory.decodeStream()BitmapFactory.java:653

回答 我已经用

解决了问题
  1. 更新 Xamarin.Forms Nuget。
  2. 删除解决方案中的包文件。
  3. 再次构建解决方案。

【问题讨论】:

  • OutOfMemoryError 不言自明
  • 你能告诉我们错误发生的相关代码吗?
  • @zett42 更新说明
  • 您可以尝试在Android Project -> Properties -> Android Options -> Linker 中设置Heap size 1G
  • @ChandreshKhambhayata 链接器设置为无?

标签: xamarin xamarin.android xamarin.forms out-of-memory


【解决方案1】:

在您的清单中添加这些行 android:hardwareAccelerated="false" , android:largeHeap="true" 它可能会解决您的问题。

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

【讨论】:

    【解决方案2】:

    我有同样的问题。事实证明,多次更改解决了这个问题。

    首先,我确保我的应用可以处理大内存堆。这是您可以在应用清单中设置/更改的设置:

    <application
        ...
        android:largeHeap="true"> <!-- This line does the trick. -->
    

    其次,我确保尽可能强制使用小图像尺寸。就我而言,我决定将最大分辨率限制为 720 像素或屏幕分辨率,无论 更小。结果我调整了大图像的大小:

    int maxImageSideLength = Math.Min(720, Math.Max(myScreenHeight, myScreenWidth));
    // see tutorials how to resize the image now
    

    最后,我确保在分配新图像之前处理分配给图像视图的图像位图(已用内存)。我不确定这是否真的有必要,因为我不敢相信分配一个新的图像位图没有被正确清理,但我把它留在了我的代码中,我仍然对一个流畅运行的应用程序感到满意。示例:

    Bitmap resizedImage = ResizeImage(fileName, maxImageSideLength);
    imageView.SetImageBitmap(null); // this is to free allocated memory
    imageView.SetImageBitmap(resizedImage);
    GC.Collect(); // dispose of the Java side bitmap
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2019-01-22
      • 2012-10-04
      • 1970-01-01
      相关资源
      最近更新 更多