How to examine the size of the Bundle object in onSaveInstanceState?
Asked Answered
S

2

4

I am using Android Studio 3.0.1 which allows to inspect the Bundle object in onSaveInstanceState:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

To do so add a breakpoint and once the debugger stops right click the Bundle object as shown in the screenshot and select "Show Bundle Objects..." from the context menu.

Android Studio - Show Bundle Objects

Then a window opens to list the Bundle objects as shown in this screenshot:

Android Studio - Bundle objects

Is there a way to find out how much memory the whole Bundle and it's children occupy? I want to identify the bigger chunks to optimize and avoid TransactionTooLargeException to be thrown on Android >= 7.

Something like Ubuntu Disk Usage Analyzer would be helpful - see screeshot:

Disk Usage Analyzer

Snowbound answered 4/12, 2017 at 12:6 Comment(0)
D
8

You can also use this one :-

=> just pass the Bundle here

public int  getBundleSizeInBytes(Bundle bundle  ) {
            Parcel parcel = Parcel.obtain();
            parcel.writeValue(bundle);
            byte[] bytes = parcel.marshall();
            parcel.recycle();
            return bytes.length;
}

Then use android.text.format.Formatter.formatFileSize(activityContext, bytes) to output nicely.

Durkheim answered 4/12, 2017 at 12:17 Comment(0)
S
2

The TooLargeTool is great for analysing bundle size when diagnosing TransactionTooLargeException.

Sardinia answered 31/1, 2019 at 13:33 Comment(1)
Very useful to analyse bundle. Thank you.Bedwarmer

© 2022 - 2024 — McMap. All rights reserved.