Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

reactjs - Slider taking too much RAM, which leads to memory leaks

I have this component to track current time of an audio file:

const SeekBar = (props) => {
  const [isSeeking, setIsSeeking] = useState(false);
  const [seek, setSeek] = useState(0);
  const [position, setPosition] = useState(0)

  useEffect(() => {
    const loop = setInterval(() => setPosition(position => position + 1), 1000)
    return () => clearInterval(loop)
  }, [])
  const { duration } = props

  return (
    <View style={myStyle.seekBar}>
      <View style={myStyle.seekBarContainer}>
        <Text
          style={styles.seekBarTime}
        >
          {position}
        </Text>
        <Slider
          minimumValue={0}
          maximumValue={duration}
          thumbTintColor='#000000'
          minimumTrackTintColor='#000000'
          maximumTrackTintColor='#808080'
          step={1}
          value={isSeeking === true ? seek : position}
          onValueChange={(value) => {
            setIsSeeking(true);
            setSeek(value);
          }}
          onSlidingComplete={async (value) => {
            setSeek(value);
            setIsSeeking(false)
            setPosition(value)
          }}
        />
        <Text>{duration}</Text>
      </View>
    </View>
  )
};

These lines of code seem to work well:

enter image description here

Except one thing: the RAM of device is increasing more and more while the progressing bar is updating:

enter image description here

If the time is long enough (10 hours or more), the device will run out of RAM

I guess the mechanism of re-render cause that things (not sure)

So, how can i reduce the amount of RAM the component uses?

question from:https://stackoverflow.com/questions/65936355/slider-taking-too-much-ram-which-leads-to-memory-leaks

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...