简体   繁体   English

将一个Android应用程序划分为两个进程,以便可以将一个进程用于位图操作,以避开OutOfMemoryErrors

[英]Divide an Android Application into two processes, so that one can be used for Bitmap operations in order to sidestep OutOfMemoryErrors

As far as I know we can optionally assign different processes for logically separated parts of our app in Android, and as far as I know, the new process will have a new part of the heap set aside for it. 据我所知,我们可以为Android中应用程序中逻辑上分开的部分选择分配不同的进程,据我所知,新进程将为其保留堆的新部分。

So lets say our app utilizes 90-100 mb of the 128 mb heap and we need to carry out an operation with a 50mb bitmap. 因此,可以说我们的应用程序利用了128 mb堆中的90-100 mb,我们需要使用50 mb位图进行操作。

There is no time to rewrite, refactor and optimize the code, so that the app uses significantly less RAM. 没有时间重写,重构和优化代码,因此该应用程序使用的RAM少得多。

Can we assign a different process ID for the Activity that handles those heavyweight Bitmap operations, so we avoid an OutOfMemoryError and what are the cons of that, if it is possible? 我们是否可以为用于处理那些重量级位图操作的Activity分配不同的进程ID,所以我们可以避免OutOfMemoryError,如果可能的话,这样做有何弊端?

I've already read this: What are pros and cons of using multiple processes within android application but it is not as specific as my question. 我已经读过这篇文章: 在android应用程序中使用多个进程的优缺点是什么,但是它不像我的问题那么具体。

Here: 这里:

http://developer.android.com/training/articles/memory.html#MultipleProcesses http://developer.android.com/training/articles/memory.html#MultipleProcesses

There is a mention that this might actually increase the RAM footprint, but they havent elaborated on it. 有人提到这实际上可能会增加RAM占用空间,但是他们还没有详细说明。

Can we assign a different process ID for the Activity that handles those heavyweight Bitmap operations 我们是否可以为活动分配一个不同的进程ID,以处理那些重量级的位图操作

You can run that activity in a separate process, following the instructions from your second link , using the android:process attribute in the manifest. 您可以按照清单2中android:process属性,按照第二个链接的说明 ,在单独的流程中运行该活动。

so we avoid an OutOfMemoryError 所以我们避免了OutOfMemoryError

That's not a guarantee. 那不是保证。 In fact, given the following quoted statement, it's not even likely. 实际上,考虑到以下引用的语句,这种可能性很小。

So lets say our app utilizes 90-100 mb of the 128 mb heap 因此,可以说我们的应用利用了128 mb堆中的90-100 mb

You have no guarantee of getting anything more than 16MB. 您不能保证获得超过16MB的内存。 Even with android:largeHeap="true" , you may not get any more heap space than you would have without that attribute. 即使使用android:largeHeap="true" ,也可能无法获得比没有该属性时更多的堆空间。 Do not assume that either process will necessarily have 50MB of heap space, unless you control the hardware. 不要以为任一过程必然具有的堆空间为50MB,除非你控制硬件。

what are the cons of that, if it is possible? 如果可能的话,这样做有何弊端?

It may not help, unless nothing in your first process has need of the bitmap in question. 除非您的第一个过程中没有任何必要的位图,否则它可能无济于事。 So, for example, if your first process is taking a camera photo using the Camera API, you would have the whole bitmap in the first process anyway (or crash trying). 因此,例如,如果您的第一个流程是使用Camera API拍摄相机照片,则无论如何,您都会在第一个流程中拥有整个位图(或尝试崩溃)。 OTOH, if the bitmap is only being downloaded by the first process, and you're doing that the right way (streaming the results to a file), then it may help with your issue. OTOH,如果仅在第一个过程中下载了位图,并且您以正确的方式(将结果流式传输到文件中)进行了处理,则可能会有助于解决问题。

However, it does mean that the bitmap-processing activity is going to be a bit slow to launch, as it will have to read the bitmap off of disk, or do the downloading itself, as deemed appropriate. 但是,这确实意味着位图处理活动的启动速度会有些慢,因为它必须从磁盘上读取位图,或者在认为适当的情况下自行下载。

Any non-IPC communications that the activity would need (eg, event bus, singleton caches) will not be there. 活动将需要的任何非IPC通信(例如,事件总线,单例缓存)将不存在。

Processes get terminated to free up system RAM when they are in the background. 进程在后台时会终止以释放系统RAM。 And, in your case, that could be either process. 而且,就您而言,这可能是任何一个过程。 You will need to be able to handle the cases where the user leaves your app (eg, HOME button) and one or both of your processes had been terminated in the interim. 您将需要能够处理用户离开您的应用程序(例如,HOME按钮)并且其中一个或两个进程已在此期间终止的情况。 It's also not out of the question that your first process will be terminated while your second process is in the foreground -- I haven't checked process importance values in this scenario, where both processes belong to the same Android app. 同样,当您的第二个进程处于前台时,您的第一个进程将被终止也不是没有问题的-在这种情况下,我没有检查两个进程属于同一Android应用程序的进程重要性值。

And, as noted previously, you have no assurance that a completely clean process is capable of loading a 50MB bitmap into the Dalvik/ART heap. 并且,如前所述,您不能保证完全干净的过程能够将50MB的位图加载到Dalvik / ART堆中。

There is a mention that this might actually increase the RAM footprint, but they havent elaborated on it. 有人提到这实际上可能会增加RAM占用空间,但是他们还没有详细说明。

RAM footprint != heap space. RAM占用量=堆空间。 RAM footprint means the total amount of system RAM associated with your app. RAM占用空间是指与您的应用程序关联的系统RAM总量。 While both processes are running, you will be using more system RAM than you would by doing the same work with just one process. 当两个进程都在运行时,与仅通过一个进程执行相同的工作相比,您将使用更多的系统RAM。 That bitmap-processing process will be around while the activity is in the foreground and for a while after that, before Android eventually terminates it to free up system RAM. 该位图处理过程将在活动处于前台时进行,大约一会儿之后,在Android最终终止它以释放系统RAM之前。

There is no time to rewrite, refactor and optimize the code, so that the app uses significantly less RAM. 没有时间重写,重构和优化代码,因此该应用程序使用的RAM少得多。

Unless you control the hardware, you don't really have a choice. 除非您控制硬件,否则您别无选择。 The only way you can be fairly sure that you can process 50MB of data in RAM is via the NDK, as native allocations ( malloc() and friends) are from system RAM, not the Dalvik/ART heap, and therefore do not count against that heap limit. 您可以完全确定可以在RAM中处理50MB数据的唯一方法是通过NDK,因为本机分配( malloc()和friends)来自系统RAM,而不是Dalvik / ART堆,因此不计入该堆限制。

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

相关问题 Android中的操作顺序,为什么会这样呢? - Order of Operations in android, why are so going on? 是否可以将Android应用程序分为两个APK文件? - Is it possible to divide an Android application into two APK files? android中可以使用的最大进程数是多少? - What is the maximum number of processes that can be used in android? 我如何“重置”画布(以及用于绘制的位图),以便可以将新的位图绘制到画布中? - how can i “reset” the canvas (and the bitmap used to draw into) so i can draw a new bitmap into the canvas? 如何在Android中的一项活动上执行两项操作 - How to perform two operations on one activity in android 如何在Android的第一张图片的选定点上将两个位图彼此合并? - How can i merge two bitmap one over another at selected point on the first image in android? 是否可以在Android应用程序中包含/链接并使用为Linux构建的共享库(.so)文件? - Can a shared library (.so) file built for linux be included/linked and used in an Android application? 有没有办法可以找出哪个ndk版本用于编译android .so共享库? - Is there a way one can find out which ndk version was used to compile android .so share library? Android/Kotlin 中的操作顺序 - Order of operations in Android/Kotlin Android:如何将一个活动分为两个独立的活动? - Android: how to divide one activity into two separate activities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM