Unload asset library

How do I unload a library load by this command: AssetLibrary.loadFromFile

You can call library.unload, but assets will also remain in memory if they are still in use

I made a small test and seems that the library is not beeing unloaded.

Assets.loadLibrary('assets/swf/apresentacao.bundle').onComplete(function(library){
			trace(Assets.list());
			library.unload()
			trace(Assets.list());
		});

The 2 trace results are the same, before and after calling library.unload().
I checked the unload method of AssetLibrary.hx and its is empty.
Am I doing something wrong? Thanks.

Oh, if you use Assets.loadLibrary, you should use Assets.unloadLibrary. I think it would work like this:

Assets.loadLibrary ("assets/swf/apresentacao.bundle").onComplete (function (_) {
    trace (Assets.list ());
    Assets.unloadLibrary ("assets/swf/apresentacao.bundle");
    trace (Assets.list ());
});

Thanks. that way it works.

1 Like

Doesn’t work for me…
I’m loading assets though AssetManifest.loadFromFile(), then calling Assets.registerLibrary("name", loadedLibrary).
Calling Assets.unloadLibrary("name") will not free any memory, and calling trace(Assets.list()) later will show that all resources are still available (even if not used anywhere).

Tested in Chrome.

Oh, I just noticed that trace(Assets.list()) shows all assets potentially available (regardless if the have been loaded).

I have a question then… After I load a library of images & sounds from the web, how will Lime store the resources in memory: as raw bitmaps / audio, or in their initial compressed states (jpg, png, mp3, ogg…)?

It stores them uncompressed

I am worried that memory from loaded assets is never released in HTML5.
After a library is loaded, RAM and GPU memory used by the browser tab will grow, but after calling Assets.unloadLibrary() it is will not drop neither instantly nor a few minutes later.

The code is:

AssetLibrary.loadFromManifest(am).onComplete(loadComplete);
private function loadComplete(al:AssetLibrary):Void
{
	Assets.registerLibrary("myLibrary", al);
	Assets.unloadLibrary("myLibrary");
}

I also wonder if I can dispose of Lime assets one by one, as something like Assets.getBitmapData("library:assets/image.png").dispose() will not remove the loaded image.

What about memory if you use AssetLibrary.loadFromFile but do not call registerLibrary? Does it get GC’ed later?

If I don’t call registerLibrary, I cannot call unloadLibrary, right?
I tried loading from file and from the manifest again without calling registerLibrary afterwards and just waiting for a few minutes to see RAM changes, but looks like there is no difference.

Edit: Not calling registerLibrary, but calling loadedLibrary.unload doesn’t have effect either.

Edit 2: I’m curious about unloadLibrary code:

if (library != null)
{
	cache.clear(name + ":");//HTML5 libraries don't seem to have cache, so this will be a loose run
	library.onChange.remove(library_onChange);
	library.unload();//this function is a placeholder
}

I might be missing something, but I couldn’t find what marks loaded assets for GC.

This will fix cache clearing problems in OpenFL and make it possible to correctly reload sounds:

In src\openfl\utils\Assets.hx:

	public static function unloadLibrary(name:String):Void {
		/*add this line*/cache.clear(name + ":");//call of OpenFL version of cache.clear method has been missing
		#if lime
		LimeAssets.unloadLibrary(name);
		#end
	}

@singmajesty, can you include this fix in the next version?

Thanks! Great find

I just added this commit:

and I also realized there’s a potential issue here with the prefix on the “default” library… I don’t know if it’s caching using “” or “default” so I added this commit but there may still need to be more clarity on this