简体   繁体   中英

Is there any limit of bundle in Android?

I want to know whether android bundle's data size has upper limit. I try to post data by bundle which size >80k,and throw android fatal exception.The data is Serializable.

It depends on the purpose of the bundle. The bundle itself is only limited by the amount of memory.

The two main uses for bundles are to pass information between components using intents and to save the state of activities.

1. Intents / Binders

When used to pass information between Android components the bundle is serialized into a binder transaction. The total size for all binder transactions in a process is 1MB. If you exceed this limit you will receive this fatal error "!!! FAILED BINDER TRANSACTION !!!"

It's recommend that you keep the data in these bundles as small as possible because it's a shared buffer, anything more than a few kilobytes should be written to disk.

Reference: https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/jni/android_util_Binder.cpp

ALOGE("!!! FAILED BINDER TRANSACTION !!!");
        // TransactionTooLargeException is a checked exception, only throw from certain methods.
        // FIXME: Transaction too large is the most common reason for FAILED_TRANSACTION
        //        but it is not the only one.  The Binder driver can return BR_FAILED_REPLY
        //        for other reasons also, such as if the transaction is malformed or
        //        refers to an FD that has been closed.  We should change the driver
        //        to enable us to distinguish these cases in the future.

Reference: http://developer.android.com/reference/android/os/TransactionTooLargeException.html

The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size.

2. Saved Instance State ( Activity onSaveInstanceState, onPause etc. )

I found no limit in the size I could store in the bundle used to preserve Activity state. I did some tests and could successfully store about 175mb before I received an out of memory exception trying to allocate the data I was attempting to save.

Update: This research was performed in 2014, newer versions of Android may crash with bundles over 500kb

I think the limit is 500kb. You can save the passed object in a file and send the path of the file in the bundle instead. You can check similar question asked by me at SO

The Binder transaction buffer has a limited fixed size, currently 1MB , which is shared by all transactions in progress for the process. Since this limit is at the process level rather than at the per activity level, these transactions include all binder transactions in the app such as onSaveInstanceState, startActivity and any interaction with the system. When the size limit is exceeded, a TransactionTooLargeException is thrown.

For the specific case of savedInstanceState, the amount of data should be kept small because the system process needs to hold on to the provided data for as long as the user can ever navigate back to that activity (even if the activity's process is killed). We recommend that you keep saved state to less than 50k of data.

Parcelable and Bundles

Yes it has, and now in android Nougat it will crash if you exceeded the limit roughly(500Kb).

android nougat issue

Yes it has 1MB limit.

You can use Singleton class to pass data.

根据Google Android API ,日期应小于 50K。

I think that the maximum bundle size is 1024 KiloBytes. In order to transfer large objects among activities, you should try other ways (memory cache, local storage, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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