App crashes with memory leak when running for a long time
See original GitHub issueVersion
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:
- Created 10 months ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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!
@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 (seeTensorHostObject.h
).Have you profiled your app to check what leaks memory?