question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App crashes with memory leak when running for a long time

See original GitHub issue

Version

0.2.2

Problem Area

react-native-pytorch-core (core package)

Steps to Reproduce

Following the steps below, the memory heap increases at a pace of about 100MB per hour. We are developing an application that needs to run for long periods of time. Could you please tell me how to avoid this issue.

$ npx react-native init AwesomeTSProject --template react-native-template-typescript
$ cd AwesomeTSProject
$ yarn add react-native-pytorch-core
$ npx pod-install
import * as React from 'react';
import {useCallback} from 'react';
import {LayoutRectangle, StyleSheet} from 'react-native';
import {
  Camera,
  CameraFacing,
  Canvas,
  CanvasRenderingContext2D,
  Image,
  ImageUtil,
} from 'react-native-pytorch-core';

const App = () => {
  const contextRef = React.useRef<CanvasRenderingContext2D>();
  const [layout, setLayout] = React.useState<LayoutRectangle>();

  const handleCapture = useCallback(
    async (image: Image) => {
      const context = contextRef.current;
      if (context != null && layout != null) {
        context.clear();
        const imageWidth = image.getWidth();
        const imageHeight = image.getHeight();
        const scale = Math.min(
          layout.width / imageWidth,
          layout.height / imageHeight,
        );
        context.drawImage(image, 0, 0, imageWidth * scale, imageHeight * scale);
        const wholeImageData = await context.getImageData(
          0,
          0,
          imageWidth * scale,
          imageHeight * scale,
        );
        const wholeImage = await ImageUtil.fromImageData(wholeImageData);
        await wholeImageData.release();
        await wholeImage.release();
        await context.invalidate();
      }
      image.release();
    },
    [contextRef, layout],
  );

  return (
    <>
      <Camera
        onFrame={handleCapture}
        hideCaptureButton={true}
        style={styles.camera}
        facing={CameraFacing.BACK}
      />
      <Canvas
        style={styles.canvas}
        onContext2D={context => {
          contextRef.current = context;
        }}
        onLayout={event => {
          setLayout(event.nativeEvent.layout);
        }}
      />
    </>
  );
};

const styles = StyleSheet.create({
  camera: {
    display: 'none',
  },
  canvas: {
    flex: 1,
  },
});
export default App;

However, the code below is necessary for our application and cannot be removed.

        const wholeImage = await ImageUtil.fromImageData(wholeImageData);
        await wholeImageData.release();
        await wholeImage.release();
        await context.invalidate();
$ vim ./ios/AwesomeTSProject/Info.plist
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) needs access to your Camera.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) needs access to your Microphone.</string>
$ npx react-native start
$ open /Applications/Xcode.app ./ios/AwesomeTSProject.xcworkspace

Expected Results

No response

Code example, screenshot, or link to repository

Related to this issue

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nh9kcommented, Dec 14, 2022

@raedle, thank you for your kindness! I haven’t profiled my app yet. My app also crashes, so I need a memory check. I’ll report back!

0reactions
raedlecommented, Dec 14, 2022

@nh9k, each image has to be released from memory manually. For context, the current implementation holds a reference in the JSContext:

Not releasing the image will keep the reference around.

Tensors are implemented differently. They use jsi::HostObject from the JSI API. This will eventually clean up references when the JavaScript Runtime GC finds unused references (see TensorHostObject.h).

Have you profiled your app to check what leaks memory?

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 steps to fix app memory leaks - Streamlit Blog
1. Identify the memory leak ... A leak happens when your app acquires memory resources and never releases them. It just consumes more...
Read more >
Xcode Memory leak / crash loading - Apple Developer
I have been running into an issue where my SpriteKit game is crashing when run on iOS9. I am using Xcode8-beta5, certainly possible...
Read more >
Preventing and detecting memory leaks in Android apps
Memory leaks can cause your Android app to crash, causing frustration and lower usage. Learn how to fix them in this guide.
Read more >
What Is a Memory Leak and How Do They Happen?
The result is that an app crashes the next time it attempts to use more memory, which can impact on the performance of...
Read more >
Everything you need to know about Memory Leaks in Android.
Memory leaks can lead to lags in the app, ANR errors, and OutOfMemory exceptions. Which ultimately leads to the uninstallation of your app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found