react-native-bottom-sheet icon indicating copy to clipboard operation
react-native-bottom-sheet copied to clipboard

[v4] | [v2] Memory leak, endless consumption when frequently changed state

Open therealkeeg1 opened this issue 1 year ago • 3 comments

Bug

Endless memory consumption when BottomSheet is mounted and a state is frequently changed. Doesn't matter if bottom sheet is open or closed. After 10 minutes the app crashes on my Samsung Galaxy s21. iOS doesn't crash but will lag extremely bad after 10-15 minutes (using 700+ mb of ram, iPhone 15 Pro).

Bottom sheet

Same example code but with the bottom sheet commented out

CommentedOut

Environment info

Library Version
@gorhom/bottom-sheet ^4.6.0
react-native 0.71.1
react-native-reanimated ^3.7.0
react-native-gesture-handler ^2.15.0

Steps To Reproduce

  1. Create a bottom sheet (Doesn't matter if bottom sheet is open or not)
  2. Create a frequently changing state. (For example, a setInterval)
  3. Wait 1-5 minutes

Describe what you expected to happen:

  1. I expected the frequently changing state to have zero effect on the bottom sheet.

Reproducible sample code

const snapPoints = useMemo(() => ['30%'], []);
const bottomSheetRef = useRef<BottomSheet>(null);
const [date, setDate] = useState(0)

useEffect(() => {
    let tmp = setInterval(() => {
        setDate(Date.now())
    }, 100)

    return () => {
        clearInterval(tmp)
    }
}, [])

return (
    <View style={{flex: 1, justifyContent:'center', alignItems:'center'}}>
        <TouchableOpacity onPress={() => {
            bottomSheetRef?.current?.snapToIndex(0)
        }} style={{backgroundColor:'red', padding: 20}}>
            <Text>Open</Text>
        </TouchableOpacity>

        <BottomSheet ref={bottomSheetRef} index={-1} snapPoints={snapPoints}>
            <Text>TEST</Text>
        </BottomSheet>
    </View>
)

therealkeeg1 avatar Feb 08 '24 23:02 therealkeeg1

@therealkeeg1: hello! :wave:

This issue is being automatically closed because it does not follow the issue template.

github-actions[bot] avatar Feb 08 '24 23:02 github-actions[bot]

Similar issue, but reproduced on BottomSheetModal when opening/closing

Sample code:

const Content = () => {
  const [s, setState] = useState(false);
  useEffect(() => {
    setInterval(() => {
      setState(s => !s);
    }, 50);
  }, []);
  return <View />;
};

export const SimpleExample = () => {
  const isOpened = useRef(false);
  const bottomSheetRef = useRef<BottomSheetModal>(null);
  useEffect(() => {
    setInterval(() => {
      if (!isOpened.current) {
        bottomSheetRef.current!.present();
      } else {
        bottomSheetRef.current!.dismiss();
      }
    }, 50);
  }, []);
  const snapPoints = useMemo(() => ['25%', '50%'], []);

  const handleChange = useCallback((index: number) => {
    isOpened.current = index > -1;
  }, []);
  return (
    <GestureHandlerRootView style={styles.wrapper}>
      <BottomSheetModalProvider>
        <View style={styles.container}>
          <BottomSheetModal
            ref={bottomSheetRef}
            snapPoints={snapPoints}
            enablePanDownToClose
            enableDismissOnClose
            onChange={handleChange}>
            <Content />
          </BottomSheetModal>
        </View>
      </BottomSheetModalProvider>
    </GestureHandlerRootView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 24,
  },
  wrapper: {
    flex: 1,
  },
});
Library Version
@gorhom/bottom-sheet ^4.6.1
react-native 0.73.4
react-native-reanimated ^3.7.0
react-native-gesture-handler ^2.15.0

Android studio profiler after 14 minutes test run Снимок экрана 2024-02-20 в 10 48 52

On production app where bottom sheets contain more complex content single bottom sheet open might increase RAM by 20mb

yadormad avatar Feb 20 '24 07:02 yadormad

same issue

KirillRodichevUtorg avatar Feb 20 '24 07:02 KirillRodichevUtorg

You can include V5. I have migrated from react-native-modalize to bottomsheet, and performance is not the same. On certain screens, I have multiple API calls, 5-6 bottom sheet modals, in one word, more complex views than just a link to open a modal and the app is freezing, on performance monitor, JS is stuck to 0 during many seconds and my app is not reactive anymore. I precise that I had not had this problem before. To test, I commented all bottom sheet modals, and it fixes the performance problem. There is something in the source code that consumes a lot of memory. Maybe useless renders. I even logged the onClose method, and I was surprised that it fires on the first render while the bottom sheet is on index -1. Because of this issue, sometimes the enableDynamicSizing doesn't work as expected. The calculation is not good. I don't see a PR to fix this even though this is critical. This problem seems to occur with a minimal example, so I don't think it comes from my code.

TomCorvus avatar Feb 29 '24 20:02 TomCorvus

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Mar 31 '24 09:03 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Apr 06 '24 09:04 github-actions[bot]

@gorhom Any insight?

therealkeeg1 avatar Jun 18 '24 16:06 therealkeeg1

I believe this is still relevant, my app uses multiple bottom sheets and is facing performance issues.

edwinvrgs avatar Jun 18 '24 18:06 edwinvrgs

It all depends on the functionality you need for the modals, but you can code your own modal using the different examples available. you will stay in control and adjust according to your needs. It took me a few hours to get what I want and I have no performance issues. https://docs.swmansion.com/react-native-reanimated/examples/bottomsheet/ https://github.com/software-mansion-labs/reanimated-bottom-sheet-video

TomCorvus avatar Jun 18 '24 20:06 TomCorvus

Why this is getting closed every time? the issue still persists

dhaval97shah avatar Jun 22 '24 07:06 dhaval97shah