yasirkula / UnityNativeCamera

A native Unity plugin to take pictures/record videos with device camera on Android & iOS
MIT License
615 stars 71 forks source link

open the camera to take a photo,the memory rise 300M #114

Closed tianqi1994 closed 10 months ago

tianqi1994 commented 10 months ago

Just like the title says,When I opened the camera to take a photo, and then returned to Unity, I found that the memory increased by nearly 300M, however, I didn't do anything with callback in Unity, every time I opened the photo, it increased by nearly 300M, increasing all the time

yasirkula commented 10 months ago

Did the profiling tool give any more info about the increased memory?

tianqi1994 commented 10 months ago

Did the profiling tool give any more info about the increased memory?

I'm sorry I didn't find any information,
public static void OpenPhoto(Action callBack, bool isShowToast = true) { NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((string path) => { if (!string.IsNullOrEmpty(path) && callBack != null) { callBack(NativeGallery.LoadImageAtPath(path, markTextureNonReadable : false)); } }); CheckPermission(permission, isShowToast); } I just call OpenPhoto, and the callBack passes null, and the photo goes up by 300M

yasirkula commented 10 months ago

I have a theory. That's memory that will be collected by Android's native garbage collector. The memory footprint comes from the camera Intent and the Fragment created by NativeCamera. If my theory is correct, then the memory usage should drop back to normal levels after launching NativeCamera a few times. Otherwise, either the app will crash or it will consistently restart the Unity app after opening the camera a few times.

tianqi1994 commented 10 months ago

Yes, I just tested it and the unity app crashed after taking many photos in a row

tianqi1994 commented 10 months ago

I found that the problem is in the LoadImageAtPath API, if you do not call it, the memory will not grow

yasirkula commented 10 months ago

You said that you didn't do anything with the callback so I assumed LoadImageAtPath wasn't called in your test scenario. In this case, the loaded textures are increasing the memory usage. You should call Destroy(oldTexture) when you no longer need a procedural texture (one that is returned by LoadImageAtPath), otherwise it will continue consuming memory.

tianqi1994 commented 10 months ago

I have found my problem, I did not pass in the maxSize parameter, resulting in the default maxSize is the maximum value obtained, and it does not need such a large size in practical applications

yasirkula commented 10 months ago

Happy to hear that 🌷 In that case, I'm closing the Issue.