Memory not freed in Webgl

This is how we load Unity in JavaScript.

Unity Loading…

// Preparing Unity Preferences
const unityConfig: UnityConfig = {
	loaderUrl: `${UNITY_BASE_URI()}/Build/Build.loader.js`,
	dataUrl: `${UNITY_BASE_URI()}/Build/Build.data.unityweb`,
	frameworkUrl: `${UNITY_BASE_URI()}/Build/Build.framework.js.unityweb`,
	codeUrl: `${UNITY_BASE_URI()}/Build/Build.wasm.unityweb`,
	streamingAssetsUrl: `${UNITY_ASSET_URI()}/StreamingAssets`,
	// cacheControl: (url: string) => {
	// 	return "no-store";
	// },
	companyName: "company",
	productName: "productName",
	productVersion: "0.1.0.0",
	webglContextAttributes: {
		...Configure.useWebGLOptions
	}
};
// Preparing Unity Libraries
const { unityProvider, isLoaded, loadingProgression, unload, sendMessage, addEventListener, removeEventListener } =
		useUnityContext(unityConfig);
// page rendering
<UnityStyled
	ref={unityCanvas}
	className="unity-player"
	tabIndex={-1}
	unityProvider={unityProvider}
	devicePixelRatio={window.devicePixelRatio}
	matchWebGLToCanvasSize={true}
	style={{
		visibility: isLoaded ? "visible" : "hidden",
		width: "100%",
		height: "100%",
		zIndex: "auto"
	}}
/>
// Unity Unload process
1. If you want to stop Unity activation by flow of service process, call unity.unload(), return Promise()
2. Once unity is guaranteed that unload() has completed, remove the unity.canvas DOM.


We are handling it like this but the memory is still increasing every time we load.
I am waiting for your solution. First of all, thank you.

you have to release memory by javascript.

Can you share your unloading code? We found out that Development builds might currently have an issue regarding this (due to Emscripten debug code it injects) but Release builds should be fine.

1 Like
import { UnityConfig } from "react-unity-webgl/distribution/types/unity-config";

const effector = {
		onMount() {
			EventHandler.emit("UnityDomMount");
			if (unityCanvas.current) {
				unityCanvas.current.onmousedown = async (ev: MouseEvent) => await fn.request_pointer_lock(ev);
				unityCanvas.current.onmouseup = (ev: MouseEvent) => fn.release_pointer_lock(ev);
			}`Preformatted text`
		},
		onUnmount() {
			return () => {
				/**
				 * @link https://react-unity-webgl.dev/docs/api/unload
				 * ! NOTICE
				 * Normally it was possible to unmount the Unity Application and
				 * unmount the containing component right after the unload function was invoked,
				 * but due to a bug in newer Unity versions this is no longer possible when using a build made with
				 * Unity 2021.2 or later.
				 * It is still possible to unload the Unity Application, but the canvas has to be kept mounted until
				 * the promise is resolved.
				 * As of writing this, the issue has not been fixed, but it is possible to unmount the Unity Application
				 * manually by halting the navigation to the next page.
				 *
				unityLoaded &&
				unload().then(() => {
					setUnityLoaded(false);
				});
				*/
				EventHandler.emit("UnityDomUnmount");
			};
		},
		onFullScreen() {
			console.debug(`${LOGTAG} Fullscreen`, isFullScreen);
			unityCommand.screenModeChanged(isFullScreen ? 1 : 0);
		},
		onUnityLoaded() {
			if (isLoaded) {
				console.log(`${LOGTAG} Unity loaded`);
				setUnityLoaded(true);

				eventQ.processAllEvents();

				if (!Configure.useUnityLogger) {
					unityCommand.startLogger({ start: false });
				}
			}
		},
		onLoadingProgress() {
			EventHandler.fire("$UnityPlayer::Loading$", isLoaded ? 100 : Math.round(loadingProgression * 100));
		}
	};

	useEventHandlers(
		{
			[EventID.CLOSE_UNITY_PLAYER]() {
				unityLoaded &&
					timer(0).subscribe(async () => {
						if (state) {
							await unload();
							setUnityLoaded(false);
							console.debug(LOGTAG, `Unity real unload() success`);
							setIsActiveUnity(false);
						}
					});
			},