Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android内存限制测试问题?_Java_C#_Android_Memory_Xamarin.android - Fatal编程技术网

Java Android内存限制测试问题?

Java Android内存限制测试问题?,java,c#,android,memory,xamarin.android,Java,C#,Android,Memory,Xamarin.android,我在某个地方读到一个Android应用程序的内存限制为16MB(对于某些设备可能更高)。所以,从网络上加载一些数据(如图像)时,情况似乎是这样 在我的虚拟设备(Android版Visual Studio Emulator)上,内存限制约为46MB。我猜这是因为在抛出OutOfMemoryException之前可以加载的图像数量是46个图像(每个图像的大小约为1MB) 起初,我认为只有当图像显示在ImageView(包装在ListView或RecyclerView项中)上时,内存才可能超过。但是,

我在某个地方读到一个Android应用程序的内存限制为16MB(对于某些设备可能更高)。所以,从网络上加载一些数据(如图像)时,情况似乎是这样

在我的虚拟设备(Android版Visual Studio Emulator)上,内存限制约为46MB。我猜这是因为在抛出OutOfMemoryException之前可以加载的图像数量是46个图像(每个图像的大小约为1MB)

起初,我认为只有当图像显示在ImageView(包装在ListView或RecyclerView项中)上时,内存才可能超过。但是,无论是否显示加载的图像,仍然会引发异常(我可以在输出窗口中看到它)

因此,我想通过加载某种类型的本地列表来重现OutOfMemoryException,例如
list
,其中项目数约为1300000(应该超过46MB)。但是,不会引发异常。我甚至尝试将项目数量增加到13万个,但仍然保持不变,没有抛出任何异常。代码如下:

//the local list is declared inside my main Activity class
List<int> _ints = new List<int>();
for(var i = 0; i < 130000000; i++) {
    _ints.Add(i);
}
//本地列表在我的主活动类中声明
列表_ints=新列表();
对于(变量i=0;i<130000000;i++){
_内加(i);
}
看来我对OutOfMemoryException的原因理解错误。你能给我解释一下吗?或者如果可能的话,给我一些示例代码或建议来重现OutOfMemoryException,但不要从网络上下载任何东西,我的意思是尽可能简单。

public void OOMTest()抛出异常{
public void OOMTest() throws Exception {
    int count = 20;
    System.out.println("\n=================> started..\n");
    for (int itrator = 1; itrator < 100; itrator++) {
        System.out.println("Iteration " + itrator + " Free Mem: "
                + Runtime.getRuntime().freeMemory());
        int[] memoryFillIntVar = new int[count];
        count = count * 5;
        System.out.println("\nRequired Memory for next loop: " + count);
        Thread.sleep(1000);
    }
}
整数计数=20; System.out.println(“\n===================>已启动..\n”); 对于(int-itrator=1;itrator<100;itrator++){ System.out.println(“迭代”+itrator+“自由内存:” +Runtime.getRuntime().freemory()); int[]memoryFillIntVar=新的int[count]; 计数=计数*5; System.out.println(“\n下一个循环需要的内存:“+count”); 睡眠(1000); } }
关于应用程序的堆大小,不同手机的堆大小不同。由制造商设置,最小值通常为16MB。您可以通过在Android清单中标记应用程序来强制在应用程序中使用更大的堆,但这应该作为最后手段,因为这将导致系统为了获得可用内存而杀死后台的其他应用程序


在您的情况下,请注意,即使图像未显示,如果另一个对象持有对它的引用,它也可能仍在应用程序内存中。Android中的图像加载和管理由于堆的内存限制,通常会出现问题。为了解决这个问题,Facebook编写了自己的图像加载库,它使用不同的堆来加载内存,避免使用应用程序中其他对象使用的空间。如果你处理大量的图片,考虑在你的应用程序中介绍。

< P> <代码> OutOfMeMyExtExe> /Cord>你是从java端来的。您不能仅使用C#对象来复制它

您需要分配大的Java对象。它不能处理很多小对象,因为可以从C#代码中引用多少Java对象是有限制的

Android.Graphics.Bitmap
是一个很好的候选者:

List<Bitmap> _ints = new List<Bitmap>();
for (var i = 0; i < 130000000; i++)
{
    _ints.Add(Bitmap.CreateBitmap(1000, 1000, Bitmap.Config.Argb8888));
}
List_ints=newlist();
对于(变量i=0;i<130000000;i++)
{
_Add(Bitmap.CreateBitmap(10001000,Bitmap.Config.argb888));
}
产生的Java异常(包装在mono异常中):

Java.Lang.OutOfMemoryError:无法分配4000012字节的分配,其中包含219956个可用字节和214KB,直到OOM
0中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]处

在中的Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod(Java.Interop.JniObjectReference类型,Java.Interop.JniMethodInfomethod,Java.Interop.JniArgumentValue*args)[0x00082]中,特别是在章节

中,图像在内存中的大小不是1MB,图像在内存中的实际大小可以计算为:

假设您有一个图像
2000x1250
。它需要
4x2000x1250
字节的内存。 每个像素都有一部分红、绿、蓝和阿尔法通道信息

因此,实际占用内存的大小类似于
4x2000x1250=10000000字节->9.5MB

加载图像的最佳方法是使用库,库会很好地处理位图的回收、加载图像、缓存图像等问题

这是我最喜欢的收藏,你可以尝试使用任何适合你需要的。我总是喜欢第一个


你的解释对我来说很有意义,这似乎是最好的答案,所以我很快就会接受。谢谢:)。顺便说一句,我的实际问题是加载大量图像。但是我不需要把它们全部展示出来。目前的问题是在显示足够多的图像(通过滚动列表)后,没有显示下一个图像(通过继续滚动)(我怀疑内存是否已满)。我没有尝试使用图像加载器库,因为我想先尝试自己解决这个问题,以帮助更好地理解这个问题。如果你对这个解决方案有更多的建议,我会非常感激:)添加了指向xamarin文档的链接。顺便问一下,异常发生在哪里?你确定它在位图分配中吗?我不是很确定,但在图像开始不显示的滚动位置,我可以在输出中看到一条消息,如
OutOfMemoryException
。但奇怪的是,在尝试调试堆内存之后,图像开始没有在明显不同的点显示(一个点约为123MB,另一个点约为85MB)。所以这让我很困惑,线程(加载图像)似乎运行良好(退出代码=0),但实际上没有加载图像(我使用async/await)。所以我开始觉得这个问题可能不会
Java.Lang.OutOfMemoryError: Failed to allocate a 4000012 byte allocation with 219956 free bytes and 214KB until OOM
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <368820a9888f43ddb85d18e87189adbf>:0
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfomethod, Java.Interop.JniArgumentValue* args) [0x00082] in <a043032cf94a485190047a14918b9f60>:0
    at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters) [0x00019] in <a043032cf94a485190047a14918b9f60>:0
    at Android.Graphics.Bitmap.CreateBitmap (System.Int32 width, System.Int32 height, Android.Graphics.Bitmap+Config config) [0x0005e] in <3140893b79904cefbb68181cd89998e0>:0
    at SomeActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00024] in <efd0cb86bcd8422d9a46a0a8a1804b4d>:0
    at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <71c3e52f1b484794bca1cdfb1b8b1fdb>:0
    at (wrapper dynamic-method) System.Object:46337ec2-c41a-4805-8728-4ab907d01a03 (intptr,intptr,intptr)
    --- End of managed Java.Lang.OutOfMemoryError stack trace ---
  java.lang.OutOfMemoryError: Failed to allocate a 4000012 byte allocation with 219956 free bytes and 214KB until OOM
      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
      at android.graphics.Bitmap.nativeCreate(Native Method)
      at android.graphics.Bitmap.createBitmap(Bitmap.java:812)
      at android.graphics.Bitmap.createBitmap(Bitmap.java:789)
      at android.graphics.Bitmap.createBitmap(Bitmap.java:756)
      at md53b9888b0c3fe89a24cb980ab2ea2a8a2.SomeActivity.n_onCreate(Native Method)
      at md53b9888b0c3fe89a24cb980ab2ea2a8a2.someActivity.onCreate(SomeActivity.java:32)
      at android.app.Activity.performCreate(Activity.java:5990)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)