TLabAltoh / TLabWebView

Plug-in for WebView that runs in Unity and can display WebView results as Texture2D
https://tlabgames.gitbook.io/tlabwebview/
MIT License
5 stars 4 forks source link

Webview Texture Leaking and Crash #5

Open firdiar-mita opened 1 month ago

firdiar-mita commented 1 month ago

I got crash when testing in android. the crash caused by out of memory.

how to reproduce

doing this several times, will continously bloat up the memory then crashed image

This is my solution image

appearently if we create new Texture2D() in Runtime, we need to explicitly destroy it when no longer use it, otherwise it'll be memory leak. normally it will be cleaned up everytime the scene is changed (*using single mode) but in my case, i've always loading additive scene

I see that we already released the texture in the java lib, but idk, above solution just works for me. image

TLabAltoh commented 1 month ago

Hi, thank you for reporting bug and suggestion. As you say, I have to release not only native texture but also Unity's texture 2D. I have included your suggestion in latest update. Thank you very much.


ps: Maybe my latest update not seems apropriate. I need to release shared texture on rendering thread

firdiar-mita commented 1 month ago

Thanks for the update!

firdiar-mita commented 1 month ago

@TLabAltoh using latest version, i got freeze, when destroying webview but I didn't investigate further about the cause

Note: I did update the VKeyboard

TLabAltoh commented 1 month ago

Thanks for the info. In the latest update, I also updated the native plugin to change the process of release native resources. If you have not merged the diff of the native plugin, please check it. Or is it already checked?

(Please forget above. I just assumed that you manually merged the change from this repo. But I leave above just to be sure).

Additinaly, if you could, please let me know your project settings. I want to know graphics api (OpenGLES or Vulkan?) and multithreaded rendering option and unity version.

Below is the small update of my java plugin.

/**
 * I need to call this function on unity's render thread because
 * releaseSharedTexture() call GLES or Vulkan function and it
 * needs to be called on render thread.
 */
public void Destroy() {

    releaseSharedTexture(); // I have moved the release texture resource process to here.

    final Activity a = UnityPlayer.currentActivity;
    a.runOnUiThread(() -> {
        if (mWebView == null) {
            return;
        }

        mWebView.stopLoading();
        mGlLayout.removeView(mWebView);
        mWebView.destroy();
        mWebView = null;

        UnityPlayer.currentActivity.unregisterReceiver(
                mOnDownloadComplete);
    });
}

I will merge other issues recommendations later...

firdiar-mita commented 1 month ago

@TLabAltoh hello sorry for my late reply i update the package by delete everything and add the new one

related to freeze, this is the logs

2024/07/22 14:43:24.673 20086 20233 Error Unity UnityException: Destroy can only be called from the main thread.
2024/07/22 14:43:24.673 20086 20233 Error Unity Constructors and field initializers will be executed from the loading thread when loading a scene.
2024/07/22 14:43:24.673 20086 20233 Error Unity Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
2024/07/22 14:43:24.673 20086 20233 Error Unity   at (wrapper managed-to-native) UnityEngine.Object.Destroy(UnityEngine.Object,single)
2024/07/22 14:43:24.673 20086 20233 Error Unity   at UnityEngine.Object.Destroy (UnityEngine.Object obj) [0x00007] in /home/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:423 
2024/07/22 14:43:24.673 20086 20233 Error Unity   at TLab.Android.WebView.TLabWebView.DestroyNativePlugin (System.Int32 id) [0x00061] in D:\Work\XXXX\Assets\Plugins\TLabWebView-master\Runtime\TLabWebView.cs:146 
2024/07/22 14:43:24.673 20086 20233 Error Unity   at (wrapper native-to-managed) TLab.Android.WebView.TLabWebView.DestroyNativePlugin(int)
2024/07/22 14:43:24.673 20086 20233 Error Unity UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
2024/07/22 14:43:24.673 20086 20233 Error Unity UnityEngine.DebugLogHandler:LogException(Exception, Object)
2024/07/22 14:43:24.673 20086 20233 Error Unity UnityEngine.Logger:LogException(Exception, Object)
2024/07/22 14:43:24.673 20086 20233 Error Unity UnityEngine.Debug:LogEx

i think unity texture can only being removed in unity mainthread

firdiar-mita commented 1 month ago

although i fixed the destroy problem, the leaking problem are back

image

i think this worser? i just start 2 webview, unload the scene, and load again, then it's crashing

2024/07/22 15:04:46.710 23786 24023 Error Unity Could not allocate memory: System out of memory!
2024/07/22 15:04:46.710 23786 24023 Error Unity Trying to allocate: 16767092B with 16 alignment. MemoryLabel: Serialization
2024/07/22 15:04:46.710 23786 24023 Error Unity Allocation happened at: Line:72 in ./Runtime/Core/Containers/Vector.h
2024/07/22 15:04:46.710 23786 24023 Error Unity Memory overview
2024/07/22 15:04:46.710 23786 24023 Error Unity 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_TEMP_TLS ] used: 141248B | peak: 0B | reserved: 6881280B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_MEMORYPROFILER ] used: 2441316B | peak: 2449408B | reserved: 3145728B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_DEFAULT ] used: 156483420B | peak: 0B | reserved: 184578048B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_TEMP_JOB_1_FRAME ] used: 0B | peak: 0B | reserved: 0B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_TEMP_JOB_2_FRAMES ] used: 0B | peak: 0B | reserved: 0B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_TEMP_JOB_4_FRAMES (JobTemp) ] used: 0B | peak: 0B | reserved: 6291456B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_TEMP_JOB_ASYNC (Background) ] used: 22837212B | peak: 0B | reserved: 58720256B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_GFX ] used: 67784572B | peak: 0B | reserved: 117444608B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_CACHEOBJECTS ] used: 754150190B | peak: 0B | reserved: 789327872B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_TYPETREE ] used: 3344316B | peak: 0B | reserved: 6291456B 
2024/07/22 15:04:46.710 23786 24023 Error Unity [ ALLOC_PROFILER ] used: 446776B | peak: 34821100B | reserved: 50331648B 
TLabAltoh commented 1 month ago

Thank you for the information. I'll review what you pointed out and make some updates accordingly.

(I'm letting you know that I've seen your message)

TLabAltoh commented 1 month ago

Temporary update, I merged your suggestion of releasing texture on main thread on destroy process and I updated native plugin (I found that some of my code was failing to free memory of some resources). Currently not checked app crash on load scene at runtime, so I set about it later. I published current once.

ps: I found multi instance problem in latest update