I have a Unity WebGL application that loads and displays models from asset bundles and is basically a 3D viewer. The models/asset bundles are very large (152,696 KB). Luckily I only need to load one at a time.
It takes some time to load the model but is acceptable. I want to be able to unload/load individual models as needed.
When the user loads another model, the current model is destroyed via Destroy(model). After several model loads/unloads the browser will run out memory. Is there something I need to do to prevent out-of-memory error? I also call a GC.Collect() after destroying the previous model.
Below is my load routine:
IEnumerator DownloadBundle() {
loadingTxt.text = “Loading " + AssetName + " …”; //modelLoading = true; // Download the file from the URL. It will not be saved in the Cache
using (WWW www = newWWW(BundleURL)) {
yieldreturn www;
if (www.error != null)
Debug.Log (“WWW download had an error:” + www.error); //throw new Exception(“WWW download had an error:” + www.error);
bundle = www.assetBundle;
if (AssetName == “”)
model = Instantiate(bundle.mainAsset) asGameObject; //This is not working
else
model = Instantiate(bundle.LoadAsset(AssetName) asGameObject); // Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
loadingTxt.text = “”; //modelLoading = false;
SetupModel ();
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
This app is mainly run from Firefox and Chrome.
I would appreciate any feedback, thanks in advance.
We have a similar issue. We have a base binary and each “application” is just a collection of asset bundles. What helps us is loading a blank scene in between each “application” load to clear out memory. However that seems less practical for your situation.
Even that’s not foolproof though (well for us at least, but that could still be our issue not Unity’s, still investigating). If you can get the webgl profiler to work (a big if), you can use the memory tool (https://bitbucket.org/Unity-Technologies/memoryprofiler/overview) to better figure out what’s being allocated.
The second article contains this gem (under section 4.2.2) which might be pertinent to you ( @dbond ):
I’m not sure if this true for webgl too, but we’ve switched over to using UnityWebRequest. I haven’t done any profiling yet so I can’t make any claims, but something I thought you might also want to try.
Kognito, thanks for the info. I tried the Web Request method for loading asset bundles and it does work in WebGL. But I still get out-of-memory errors after loading several large asset bundles one at a time. However, your first message about loading a scene in between loading asset bundles is a winner. It does seem to trigger a memory garbage collection that allows me to keep loading large bundles one at a time. So the solution, at this point in time, is I have a button in an external GUI to load the models (only one model will be loaded). Then I added another button to unload the model which calls for a new scene to be loaded and then reloads the main scene to display the new model.
Again thanks for your help, it is much appreciated!