简体   繁体   English

获取此异常会强制 android 应用程序在启动时崩溃

[英]Getting this exception which forces the android app to crash on start

11-23 10:38:04.430 2095-2095/? I/art: Not late-enabling -Xcheck:jni (already on)
11-23 10:38:04.430 2095-2095/? I/art: Late-enabling JIT
11-23 10:38:04.433 2095-2095/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
11-23 10:38:04.830 2095-2095/com.example.somanshusrivastava.carpiko W/System: ClassLoader referenced unknown path: /data/app/com.example.somanshusrivastava.carpiko-2/lib/x86
11-23 10:38:05.697 2095-2106/com.example.somanshusrivastava.carpiko I/art: Background partial concurrent mark sweep GC freed 96(3KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 2.581ms total 140.616ms
11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.827 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc sticky concurrent mark sweep GC freed 79(11KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 5.901ms total 13.614ms
11-23 10:38:05.836 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.850 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc partial concurrent mark sweep GC freed 23(1008B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 2.153ms total 10.247ms
11-23 10:38:05.853 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.867 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 15(12KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.431ms total 13.789ms
11-23 10:38:05.868 2095-2095/com.example.somanshusrivastava.carpiko I/art: Forcing collection of SoftReferences for 49MB allocation
11-23 10:38:05.869 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.881 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.407ms total 11.758ms
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko W/art: Throwing OutOfMemoryError "Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM"
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.892 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc sticky concurrent mark sweep GC freed 3(608B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.389ms total 8.964ms
11-23 10:38:05.894 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.903 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.387ms total 8.867ms
11-23 10:38:05.904 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.916 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.416ms total 10.922ms
11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: Forcing collection of SoftReferences for 49MB allocation
11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.928 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.350ms total 10.018ms
11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko W/art: Throwing OutOfMemoryError "Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM"
11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko D/skia: --- allocation failed for scaled bitmap
11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko D/AndroidRuntime: Shutting down VM
11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.somanshusrivastava.carpiko, PID: 2095
                                                                                      java.lang.OutOfMemoryError: Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM
                                                                                          at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                                          at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                                          at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                                          at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                                                                                          at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                                                                                          at android.content.res.Resources.loadDrawable(Resources.java:2540)
                                                                                          at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                                          at android.widget.ImageView.(ImageView.java:152)
                                                                                          at android.widget.ImageView.(ImageView.java:140)
                                                                                          at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:58)
                                                                                          at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:54)
                                                                                          at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
                                                                                          at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                                          at com.example.somanshusrivastava.carpiko.MainActivity.onCreate(MainActivity.java:24)
                                                                                          at android.app.Activity.performCreate(Activity.java:6237)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-23 10:38:07.560 2095-2102/com.example.somanshusrivastava.carpiko W/art: Suspending all threads took: 13.233ms
11-23 10:38:09.537 2095-2102/com.example.somanshusrivastava.carpiko W/art: Suspending all threads took: 9.664ms
11-23 10:38:11.140 2095-2095/? I/Process: Sending signal. PID: 2095 SIG: 9

Whenever I try to run the AVD , my app unexpectedly closes and this is the error I get.每当我尝试运行AVD 时,我的应用程序都会意外关闭,这是我得到的错误。 This is the logcat that is generated.这是生成的 logcat。 It is showing a FatalException and a MemoryException.它显示了 FatalException 和 MemoryException。 Is it an AVD error or code error?是 AVD 错误还是代码错误? Please help.请帮忙。

You need to understand the concept of Bitmap and memory你需要了解Bitmap和内存的概念

Let's say you want to display an image on your screen that you just took with your camera.假设您想在屏幕上显示您刚用相机拍摄的图像。 The total memory needed for this is calculated with the following formula: memory_needed_in_bytes = 4 * image_width * image_height;为此所需的总内存使用以下公式计算:memory_needed_in_bytes = 4 * image_width * image_height;

Why 4?为什么是4? Well, the most common / recommended bitmap configuration is ARGB_8888.嗯,最常见/推荐的位图配置是 ARGB_8888。 That means that for each pixel we draw, we need to keep 8 bits (1 byte) for the alpha, the red, the greed and the blue channel in memory, in order to properly display it.这意味着对于我们绘制的每个像素,我们需要在内存中为 alpha、红色、贪婪和蓝色通道保留 8 位(1 个字节),以便正确显示它。 There are alternatives, like the RGB_565 configuration that requires half the memory than ARGB_8888, but loses the transparency and the color precision (while maybe adding a green tint).有替代方案,例如 RGB_565 配置需要的内存是 ARGB_8888 的一半,但会失去透明度和颜色精度(同时可能会添加绿色调)。

Let's assume you have a brand new device with full HD screen and 12 MP camera.假设您有一台配备全高清屏幕和 12 MP 摄像头的全新设备。 The picture you just took is 4000x3000 pixels large and the total memory needed to display it is: 4 bytes * 4000 * 3000 = 48 MB您刚刚拍摄的图片大小为 4000x3000 像素,显示它所需的总内存为:4 字节 * 4000 * 3000 = 48 MB

48 megabytes of your RAM just for a single image!? 48 MB 的 RAM 仅用于单个图像!? That's a lot!好多啊!

Now let's take the screen resolution into consideration.现在让我们考虑屏幕分辨率。 You are trying to show a 4000x3000 image on a screen that has 1920x1080 pixels, in worst case scenario (displaying the image full screen) you shouldn't allocate more than 4 * 1920 * 1080 = 8.3 MB of memory.您试图在具有 1920x1080 像素的屏幕上显示 4000x3000 图像,在最坏的情况下(全屏显示图像),您不应分配超过 4 * 1920 * 1080 = 8.3 MB 的内存。

Always follow the Android programming tips for displaying bitmaps efficiently:始终遵循 Android 编程技巧以有效地显示位图:

Measure the view you're showing your images in. Scale / crop the large image accordingly.测量显示图像的视图。相应地缩放/裁剪大图像。 Show only what can be displayed.只显示可以显示的内容。

The exception thrown is known as OutOfMemoryError .抛出的异常称为OutOfMemoryError The error occurs when (obviously) you run out of memory!当(显然)内存不足时会发生错误! Here is the documentation for it:这是它的文档:

Thrown when a request for memory is made that can not be satisfied using the available platform resources.当使用可用平台资源无法满足内存请求时抛出。 Such a request may be made by both the running application or by an internal function of the VM.此类请求可由正在运行的应用程序或 VM 的内部功能发出。

More information here:更多信息在这里:

http://developer.android.com/reference/java/lang/OutOfMemoryError.html http://developer.android.com/reference/java/lang/OutOfMemoryError.html

So, you should allocate more memory to your emulator.因此,您应该为模拟器分配更多内存。 But I think the main reason is your app uses too much memory.但我认为主要原因是您的应用程序使用了太多内存。 Did you use a super big image?你用的是超大图吗?

android:largeHeap="true"机器人:大堆=“真”

You shouldn't use this, this is not a fix for the problem, this is a temporary problem fix.你不应该使用它,这不是解决问题的方法,这是一个临时的问题修复方法。 What this do is increase the memory of the emulator/phone, it will work but eventually it will crash again because you did not fix the memory problem这样做是增加模拟器/手机的内存,它会工作,但最终它会再次崩溃,因为你没有解决内存问题

将此ligne添加到您的清单文件中:

 android:largeHeap="true"

if you are using RXJAVA and are only observing the "Single" type of observable than this is prone to happen where you are emitting large number of images or data set of any kind:如果您使用 RXJAVA 并且只观察“单一”类型的可观察对象,那么在您发出大量图像或任何类型的数据集时很容易发生这种情况:

try using toFlowable before .subscribe在 .subscribe 之前尝试使用 toFlowable

https://medium.com/@gabrieldemattosleon/fundamentals-of-rxjava-with-kotlin-for-absolute-beginners-3d811350b701 https://medium.com/@gabrieldemattosleon/fundamentals-of-rxjava-with-kotlin-for-absolute-beginners-3d811350b701

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM