android 应用程序的内存分配问题
Memory allocation problems with android application
我有一个执行图像分析的 android 应用程序,它由 IntentService
管理 - 该过程每次需要几秒钟,并且工作准确快速。
但是当这个过程在应用程序中重复大约 50 次(如图所示)时,它开始变得非常慢,直到应用程序和设备变得无法使用。当设备重新启动并再次打开应用程序时,它 运行 一切如常。
使用 Android Studio 检查我可以看到每次我 运行 分析应用程序的内存分配每次都增加大约 1MB。所以当它崩溃时显然 运行 内存不足。
我在完成分析并查看结果以尝试修复后台活动时使用了此标志;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
效果微乎其微,据我所知 IntentService 自行管理关闭。所以不确定我还能做些什么来尝试减少内存分配或至少清除分配并停止添加?
更多详情:
- 应用程序正在使用基于Google Camera2
的相机实现
- 分析是通过 IntentService 使用 C++ 库完成的
您似乎没有正确处理资源(变量、图像文件等),并在您的应用程序中造成内存泄漏。
您可以在此博客的此处 Written by Johan 中找到有关处理应用程序内存泄漏的信息,或查看此 SO 问题。
Avoid memory leaks on Android
如果内存泄漏是在 c++ 库中产生的,那么您可以在调试模式下轻松找到泄漏内存的资源。
在结果 activity 之后,您应该按照 Grisgram 的建议调用垃圾收集器并关闭所有未使用的资源。
如果你能在问题中提供堆栈跟踪就好了。
我想对 Ali786 的回答进行补充
Intent Services 并不是要重复的事情的最佳选择。下次您调用该服务时,它会进入队列。
Intent Services 的工作方式类似于 HandlerThreads。他们有自己的 MessageQueues,在您使用 Intent 启动服务后,它将等待上一个。
在 UI 线程上 运行 的正常服务正在 运行 并行。
我不确定你将分析的信息发送到你的activity后是否正在做某事,但如果你做了,意图服务不会死,下一个必须等待。 Intent 服务不是与您的 UI 线程通信的最佳选择,Asynctask 可能更适合您的情况。如果您给我们更多信息(代码),也许我们可以给您更准确的答案。
希望这对您有所帮助!
有一件事可能是这样的,如果你的工作完成后的服务意图可能是你不破坏服务
检查设置 运行 服务列表 运行 您应用程序的服务
尝试使用 leakCanary https://github.com/square/leakcanary to find out what is causing the leak and use a weakReference https://developer.android.com/reference/java/lang/ref/WeakReference.html to allow it to be garbage collected when necessary. It may also be that the device you are using does not have enough memory to hold 50 high res images in memory at the same time. You could try lowering the resolution of the images if you are keeping them in memory and be sure you are recycling bitmaps https://developer.android.com/topic/performance/graphics/manage-memory.html
我也会考虑使用 threadPoolExecutor 而不是 intent 服务,它们更易于配置 https://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html
我有一个执行图像分析的 android 应用程序,它由 IntentService
管理 - 该过程每次需要几秒钟,并且工作准确快速。
但是当这个过程在应用程序中重复大约 50 次(如图所示)时,它开始变得非常慢,直到应用程序和设备变得无法使用。当设备重新启动并再次打开应用程序时,它 运行 一切如常。
使用 Android Studio 检查我可以看到每次我 运行 分析应用程序的内存分配每次都增加大约 1MB。所以当它崩溃时显然 运行 内存不足。
我在完成分析并查看结果以尝试修复后台活动时使用了此标志;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
效果微乎其微,据我所知 IntentService 自行管理关闭。所以不确定我还能做些什么来尝试减少内存分配或至少清除分配并停止添加?
更多详情:
- 应用程序正在使用基于Google Camera2 的相机实现
- 分析是通过 IntentService 使用 C++ 库完成的
您似乎没有正确处理资源(变量、图像文件等),并在您的应用程序中造成内存泄漏。
您可以在此博客的此处 Written by Johan 中找到有关处理应用程序内存泄漏的信息,或查看此 SO 问题。
Avoid memory leaks on Android
如果内存泄漏是在 c++ 库中产生的,那么您可以在调试模式下轻松找到泄漏内存的资源。
在结果 activity 之后,您应该按照 Grisgram 的建议调用垃圾收集器并关闭所有未使用的资源。
如果你能在问题中提供堆栈跟踪就好了。
我想对 Ali786 的回答进行补充
Intent Services 并不是要重复的事情的最佳选择。下次您调用该服务时,它会进入队列。 Intent Services 的工作方式类似于 HandlerThreads。他们有自己的 MessageQueues,在您使用 Intent 启动服务后,它将等待上一个。
在 UI 线程上 运行 的正常服务正在 运行 并行。
我不确定你将分析的信息发送到你的activity后是否正在做某事,但如果你做了,意图服务不会死,下一个必须等待。 Intent 服务不是与您的 UI 线程通信的最佳选择,Asynctask 可能更适合您的情况。如果您给我们更多信息(代码),也许我们可以给您更准确的答案。 希望这对您有所帮助!
有一件事可能是这样的,如果你的工作完成后的服务意图可能是你不破坏服务
检查设置 运行 服务列表 运行 您应用程序的服务
尝试使用 leakCanary https://github.com/square/leakcanary to find out what is causing the leak and use a weakReference https://developer.android.com/reference/java/lang/ref/WeakReference.html to allow it to be garbage collected when necessary. It may also be that the device you are using does not have enough memory to hold 50 high res images in memory at the same time. You could try lowering the resolution of the images if you are keeping them in memory and be sure you are recycling bitmaps https://developer.android.com/topic/performance/graphics/manage-memory.html
我也会考虑使用 threadPoolExecutor 而不是 intent 服务,它们更易于配置 https://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html