How to examine the size of the Bundle object in onSaveInstanceState?

三世轮回 提交于 2019-12-24 05:56:56

问题


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.

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

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:


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/47633002/how-to-examine-the-size-of-the-bundle-object-in-onsaveinstancestate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!