Middle
What is the TransactionTooLargeException exception?
sobes.tech AI
Answer from AI
The TransactionTooLargeException occurs when data transmitted between application components (for example, from an Activity to a Service or between processes) via Binder exceeds the allowable size limit.
Binder is an inter-process communication (IPC) mechanism in Android. It has a fixed buffer for data transfer. The size of this buffer is limited, usually up to 1 MB (plus some overhead). When attempting to transfer data through Binder and the data size exceeds this limit, the system throws a TransactionTooLargeException.
This most commonly happens when:
- Transferring large objects via
Intent(e.g., serialized data, Bitmap). - Working with AIDL services, where methods return or accept large amounts of data.
- Using
Bundleto pass data between components.
Ways to resolve the issue:
- Transfer only necessary data.
- Store large data in memory or on disk and pass only a reference to it (e.g., URI, identifier).
- Use other data transfer mechanisms for large data volumes (e.g., local sockets, media provider).
- Split large data into smaller parts for transmission over multiple transactions.
// Example that may lead to TransactionTooLargeException
Intent intent = new Intent(this, TargetActivity.class);
Bundle bundle = new Bundle();
byte[] largeData = new byte[1024 * 1024 * 2]; // 2MB of data
bundle.putByteArray("data", largeData);
intent.putExtra("bundle", bundle);
startActivity(intent); // Exception may be thrown here