material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.31k stars 3.06k forks source link

[ BottomNavigationView] The BottomNavigationView component takes up too much memory! #4243

Open crowforkotlin opened 2 months ago

crowforkotlin commented 2 months ago

Description: After the component was declared in XML and referenced the menu, the memory increased by about 25-35MB! When I deleted the menu declaration, the memory returned to normal. also I checked the size of the menu icon and it is only a few dozen KB.

Expected behavior:

image image

Source code:

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        style="@style/Widget.Material3.BottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@android:color/transparent"
        app:itemBackground="?attr/colorSurfaceContainer"
        app:labelVisibilityMode="selected"
        app:menu="@menu/main_container_bottom_menu" />
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/main_menu_homepage"
        android:icon="@android:drawable/sym_action_chat"
        android:title="chat"
        app:showAsAction="always" />

    <item
        android:id="@+id/main_menu_discovery_comic"
        android:icon="@android:drawable/sym_action_chat"
        android:title="chat"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/main_menu_bookshelf"
        android:icon="@android:drawable/sym_action_chat"
        android:title="chat"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/main_menu_anime"
        android:icon="@android:drawable/sym_action_chat"
        android:title="chat"
        app:showAsAction="ifRoom" />

</menu>

Minimal sample app repro: https://github.com/crowforkotlin/Issues-BottomNavigationView Android API version: 34 Material Library version: 1.12.0

Device: Xiaomi、Pixel6 API23

crowforkotlin commented 1 day ago

I also found the same problem with Compose's bottom navigation, why loading a menu consumes so much memory. This wastes a lot of space on some low-configuration machines, and I have been investigating these problems before, after all, it is easy to OOM on low-configuration machines