Rookie problem: spiralling memory usage on inference

username: rfrey
cooked: <p>Hi! I’m transitioning from Tensorflow to PyTorch in my android application, and I’m running into a problem when I do inference - memory usage increases quickly until the app crashes. Android Studio reports that all of the memory usage is Native. I’m clearly abusing the library somewhere, or not releasing something that needs releasing. I’ve looked through most of the sample apps and can’t find any clues.</p>
<p>I’m processing 15 frames per second from a screen recording.</p>
<p>My code looks like this. A “slice” is just a data object holding the bitmaps I’m using (cropped, resized, original capture). I know those are being freed because when I remove the inference step but leave the rest of the image pipeline memory usage is fine.</p>
<pre><code class="lang-auto">class EvaluationFilter: Filter {
    // Lazy initialization so we can ensure the context is set up in the service.
    private val module: Module? by lazy {
        val path = assetFilePath("image_qual3.ptl")
        LiteModuleLoader.load(path)
        //Module.load(assetFilePath("image_qual1.pt"))
    }

    // Perform inference on a screen capture. Called about 15 times per second
    // while the user is recording.
    override fun processSlice(slice: Slice): Slice {
        slice.croppedScaledBitmap?.let { bitmap -&gt;
            val inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,
                TensorImageUtils.TORCHVISION_NORM_STD_RGB,
                TensorImageUtils.TORCHVISION_NORM_MEAN_RGB)
            val output = module?.forward(IValue.from(inputTensor))?.toTensor()
            val scores = output?.dataAsFloatArray
            scores?.let { slice.quality = it[1] }
            Log.d(TAG, "Scores are ${scores?.contentToString()}")
        }
        return slice
    }
}
</code></pre>
<p>Thanks for any help, I’m a bit of a loss. I’m sure it will be a forehead-slapper when it’s revealed.</p>

username: kimishpatel
cooked: <aside class="quote no-group" data-username="rfrey" data-post="1" data-topic="126364">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rfrey/48/40157_2.png" class="avatar"> rfrey:</div>
<blockquote>
<p>Android Studio reports that all of the memory usage is Native</p>
</blockquote>
</aside>
<p>I am not very familiar with android app development. Can you elaborate on what you mean here?</p>
<p>If the issue is memory leak in pytorch runtime, then we may have to figure out where it is coming from. Is this quantized model or floating point model? Do you know?</p>

username: Rafael_Aquino
cooked: <p>Hi, I’m having the same problem with this too, I use Pytorch already for arround 2 years and for this long I have been looking for an answer or some way to release the used memory that increases constantly. Now I am in need to process more frames per second and the app is crashing faster.</p>
<p>I have a simmilar code to the original question from <a class="mention" href="/u/rfrey">@rfrey</a></p>
<p>I figured out that the native memory increases always on the module.forward() call.</p>
<p>I already tryed to reuse the FloatBuffer instance like <a href="https://pytorch.org/tutorials/recipes/mobile_perf.html?highlight=mobile#:~:text=protected%20AnalysisResult%20analyzeImage,from(mInputTensor).toTensor()%3B%0A%7D" rel="noopener nofollow ugc">this</a>, freing/rewinding it, calling tensor.mHybridData.resetNative(); and many other ways… I just couldn’t use imageYUV420CenterCropToFloatBuffer method since I have a Bitmap as image source. I tryed to convert the Bitmap to YUV420, but I just get NV21 format on a byte[] (pytorch requires the YUV Image 3 planes), which is not compatible with pytorch…</p>
<p>Have someone reached a solution for the memory that could please share it with us? It could be also usefull some way to convert the bitmap to YUV420 getting its image planes.</p>

username: km04
cooked: <p>Hello,<br>
Has anyone had any luck with this? I made a similar post profiling the android memory showing the same results (memory leak). I have also tried reuse and various garbage collection methods and models. I use bitmaps as well but tried imageYUV420CenterCropToFloatBuffer and saw the same thing. On the android example there is a similar issue: <a href="https://discuss.pytorch.org/t/android-memory-leak/187760" class="inline-onebox">Android Memory Leak</a></p>