react-native: built-in fetch: constant polling causes memory leak (Android)

Description

We have the kiosk-like app built for android only. The application makes constant polling for the data once per minute and the response (JSON part) would be quite massive. We’ve noticed a memory leak every time on built-in fetch invocation. Due to the long-running nature of the app (about 20 hours per day), we have crashes and lags because of the constant increasing of memory consumption.

React Native version:

System: OS: macOS Mojave 10.14.6 CPU: (8) x64 Intel® Core™ i7-4770HQ CPU @ 2.20GHz Memory: 162.09 MB / 16.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 10.15.3 - /usr/local/bin/node Yarn: 1.19.0 - /usr/local/bin/yarn npm: 6.4.1 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Managers: CocoaPods: 1.7.5 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2 Android SDK: API Levels: 23, 25, 26, 27, 28 Build Tools: 23.0.1, 25.0.0, 25.0.2, 26.0.1, 26.0.2, 26.0.3, 28.0.3 System Images: android-22 | Google APIs Intel x86 Atom, android-25 | Intel x86 Atom, android-27 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom, android-R | Google APIs Intel x86 Atom Android NDK: Not Found IDEs: Android Studio: 3.4 AI-183.5429.30.34.5452501 Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild Languages: Java: 1.8.0_211 - /usr/bin/javac Python: 2.7.10 - /usr/bin/python npmPackages: @react-native-community/cli: Not Found react: 16.11.0 => 16.11.0 react-native: 0.62.2 => 0.62.2 npmGlobalPackages: react-native: Not Found

Steps To Reproduce

  1. build a simple app with polling, which uses a fetch. To get the result faster use the endpoint which returns massive JSON data and increase polling frequency (once per 10 seconds for example)
  2. run application and use “adb shell dumpsys meminfo com.yourapp” to get info about application memory consumption (run the command sequentially once per minute and observe constant increasing of memory consumption)

Expected Results

fetch should release memory once there is no link to the parsed JSON response (response.json()) - in simple words, garbage collected.

Snack, code example, screenshot, or link to a repository:

  function startUpdating() {
      const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
      while (true) {
        getMoviesFromApi()
        await delay(10000);
      }
    }

function getMoviesFromApi() {
  return fetch('https://reactnative.dev/movies.json') // endpoint which returns massive data
    .then((response) => response.json())
    .then((json) => {
      return json.movies;
    })
    .catch((error) => {
      console.error(error);
    });
}

startUpdating()

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 16

Most upvoted comments

I have similar problem, I download and save around 2400 binary files to android storage, but after some time app is crash with memory error text

" Your app just crashed. See the error below. java.lang.OutOfMemoryError: Failed to allocate a 510992 byte allocation with 160880 free bytes and 157KB until OOM, target footprint 536870912, growth limit 536870912"

Simple app code

import { View } from 'react-native'
import { useEffect } from 'react'

export default () => {
    useEffect(() => {
        const fetchData = async () => {
            const a = await fetch(
                'https://someurl.com/speedtest/500kb'
            )
        }

        setInterval(fetchData, 400)
    }, [])
    return <View></View>
}