memory leak - dynamically build destroy web player in an html page

Hi,

I’m having a memory leak on my project (web player non-streamed) so i started investigating into it and I’ve found out that with a completly new and empty project (not a single scene/file in the project) a web player build leaves a ~0.5/1MB leak in the system memory every single time you create and destroy the unity object (according to the task manager, tested on firefox&chrome)

I think the problem could be in the way i create/destroy the unity object.

My unity web game is part of a dynamic page so I’m creating the unity object when I need to play the game, destroy it straight after the player finish the session and then rebuild it again when needed etc… always without leaving the current page.

I’m currently filling/hiding a div using jscript/jquery

to create the object I use:

$(‘#’ + unity_div_id).html(objectHTML.toString());

where objectHTML is e StringBuilder with all the “<object id=UnityObject … > … ” appended

and I destroy it with a simple

$(‘#’ + unity_div_id).html(“”);

is it the correct way to do this?
do i need to call any function in the unity game?

I’m actually having a 4/8MB leak in my game (I make an “intensive” use of assetbundles so i wrote a “Destructor” in my main “Game” object

void OnDisable () {
	if (!gameObject.active) 
	{
//		Debug.LogError("OnDestroy start");
		for (int i = (int)ASSET_BUNDLE_ID.N_BUNDLES-1; i>=0; i--)
		{
			if (m_asset_bundles[i]!=null)
			{
				m_asset_bundles[i].Unload(true);
				m_asset_bundles[i] = null;
			}
		}
		System.GC.Collect();
//		Debug.LogError("OnDestroy end");
	}
}

where m_asset_bundles is an array containing all the loaded bundles.

I’m sure that this function gets called when I hide the UnityObject with the jquery posted above but not all the memory get released (I’ve also tried with OnApplicationQuit on the same object without any luck, it gets called before the destructor so i prefer to leave the unload there).

Any suggestions will really be appreciated.

Thank you

I’ve mailed the support and, just for future reference as someone else could bump in this too, here is the official answer to this issue:

…cut…
I have just done a test myself on 4 different browsers and did find that there seems to be a problem deallocating memory. I have filed a bug report and it is now assigned to developers.
In the meantime what you can do is instead of destroying the Unity plugin, you can just hide it temporarily and show it when you need to. This is a temporary solution as the bug should be fixed.
…cut…

new problem hiding the object…

I’ve mailed the support again, reporting it here too:

I’ve tried hiding the unity object but when I show it again something weird happens, it’s like the game is restarted and all the awake functions are called again,.
My game’s flow is:

Main Level → Wait for Level Request from the web page → execute level → Wait for level Request → go back to the web page (hide unity object)

where “Main Level” should be called only once when the unity object is created, after that it’s a loop on the “wait-> execute” sequence every time the object is shown.

The issue is that also if I’m only hiding the object (I can see it in the DOM) when I show it again it starts back from “Main Level” as it has just been created (calling all the awake functions) instead of continuing from “Wait for level Request” where it was when I hid it.

Is that normal?

ok, I’ve got the answer from the support on this too and that’s the normal behaviour when hiding… to solve the problem the only way is to resize the player object in the web page instead of hiding it