Open SomaKishimoto opened 2 years ago
@SomaKishimoto, I can't repro the issue with the provided code and an iPhone Xs (see screenshots at the start of the memory measurement and after 1 hour continuously running the code).
@raedle , thanks for trying to repro the issue. my answers below
What iOS device was used in the experiment?
- device : iPhone SE 2nd
- OS : 16.11
What JavaScript runtime is enabled in the app (e.g., Hermes or JSC)?
setting
Memory heap seems to increase slightly once every 10 minutes. Do you use Expo? I create empty project with bare react native. If it's possible, could you try to repro this issue with bare react native?
(Stopped with an error unrelated to this issue [error.txt] )
Hi, @raedle, Does not doing image.release() or tensor release(current not implement) properly cause a memory leak?
@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?
@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!
Hi, @raedle, i found out what leaks memory!!!
This code causes app crash when capture under about 100 times
on Android.
A large image size
causes a memory leak rapidly.
For example, the size of an image are 3000x4000
.
This example size is captured image size using react-native-camera
package.
for-loop
was used instead of pressing the capture button 100 times.
Here is the code crashes the app on the 37
th i variable using for-loop when tested by Galaxy A33
.
Galaxy S9+
crashes at 80
th i variable in for-loop.
for (let i=0; i<100; i++){
const width = image.getWidth();
const height = image.getHeight();
const blob = media.toBlob(image);
let tensor = torch.fromBlob(blob, [height, width, 3]);
tensor = tensor.permute([2, 0, 1]);
const newTensor = tensor.div(255);
}
Here is a Snack for test. :cry:
Thanks for investigating in the issue, @nh9k.
My hunch is that it is not a memory leak per se, but the Hermes/JSC garbage collector will not run in fast enough intervals to free memory of variables that are out of scope (i.e., the tensor
variables inside the for-loop aren't GC'ed fast enough and the system eventually runs out of memory).
This seems like a valid case for introducing manual memory management options to PlayTorch (although this will break with idiomatic JavaScript).
cc @chrisklaiber @justinhaaheim
Thanks @raedle. My app crashes are also occurs about 27th capture on Galaxy A33, it is similar to for-loop of test.
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.
However, the code below is necessary for our application and cannot be removed.
Expected Results
No response
Code example, screenshot, or link to repository
Related to this issue