Android Activity 内存消耗 [英] Android activity memory consumption

查看:47
本文介绍了Android Activity 内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 4 个活动的 Android 应用程序.第一个是基本的启动画面,第二个是登录,第三个是主要的,第四个是额外的绘图.

I have one Android application that has 4 activities. First is splash screen basically, second is login, third is main and forth is one extra for drawing.

在真实设备上运行该应用程序 - 2014 年的三星 Galazy 选项卡或使用 Android 4.4 的 smth 运行出错并在登录活动后崩溃.它说内存不足错误.主要活动包含多个编辑文本字段和按钮,还有单选选项和使字段可见和不可见.

Running that application on real device - Samsung Galazy tab from 2014 or smth with Android 4.4 runs to error and crashes after login activity. It says out of memory error. Main activity contains of multiple edit text fields and buttons, with also radio choice and making fields visible and invisible.

当我在登录后设置绘图活动时,它会正确登录,当从绘图到主时,它第一次崩溃,但第二次运行.

When I set drawing activity after login, it will log in correctly and when going from drawing to main, it crashes first time, but runs second.

所以我删除了启动和登录活动,然后应用程序运行顺利 - 没有滞后或任何东西.可以去画画然后回来没有问题.所有功能都有效.

So I removed splash and login activity and then application runs smoothly - no lagging or anything. Can go to drawing and come back without problems. All functions work.

我的问题是,为什么会发生这种情况?为什么它可以在没有飞溅和登录的情况下运行,但会因这两个而崩溃.我认为这两个内存不重.那平板电脑可能有问题,还是我应该查看内存消耗?对我来说似乎很奇怪,它可以在没有这两个的情况下打开主要活动.

My question is, why is this happening? Why it can run without splash and login, but crashes with these two. I assume these two aren't heavy on memory. Can it be problem with that tablet or maybe I should look over memory consumption? Just seems weird for me that it can open up that main activity without these two.

推荐答案

Out of Memory 异常是应用程序非常常见的问题.它可以根据 RAM 大小更改设备.从这里告诉您问题的原因是不可能的.

Out of Memory exception is quite common problem for applications. It can change device to device depending on their RAM size. To telling what causes the problem for you from here is impossible.

设备内存不足,未正确使用内存(您在活动中使用的图像,保存的变量的引用,而在屏幕上的活动中不需要)以及其他因素可能会导致此问题.

Low memory of the device, not using memory properly (The images you use in the activities, The references of the variable which hold, while not needed in activity which is on the screen) and other factors can cause this problem.

你应该怎么做?

  1. 优化您的材料,尝试使用较低分辨率的图像,在更改活动或片段之前使您的变量为空,以使系统回收它们持有的内存.

  1. Optimize your materials, try to use lower resolution images, Make your variables null before changing activity or fragment to make system reclaim the memory they hold.

使用 Leak Canary 查看应用是否存在内存泄漏(https://square.github.io/leakcanary/)

Use Leak Canary and find out if there is a memory leak in the app(https://square.github.io/leakcanary/)

使用 Android Studio Profiler 监控应用的内存使用情况,找出代码的哪一部分使用的内存最多.

Use Android Studio Profiler to monitor memory usage of the app to find out which part of the code uses memory most.

了解权衡之后,您可以调用 System.gc() 垃圾收集器来回收未使用的内存,然后再执行繁重的任务.

After understanding trade-off you can call System.gc() the garbage collector to reclaim unused memory before going to heavy task.

如果其中任何一个没有帮助,您可以将下面的代码添加到您的清单文件中(在应用程序标签之间).但是您应该注意使用它的性能权衡.

if any of these not helps you can add the code at the below, to your manifest file(between application tag). But you should be aware of the performance trade-off to use this.

<application            
android:largeHeap="true"  

这篇关于Android Activity 内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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