Unloading a DLL that was dynamically loaded

Hello,

I have just finished a DLC solution for our game. It allows you to download mini games that include code. This code extends interfaces that as in the Main project but has a whole bunch of extended functionality.

Everything is working we can hit download and the bundle is downloaded from the internet and the .dll is loaded and the prefabricated game objects constructed and the classes loaded in the .dll are associated with the gameObject.

In short everything is working perfectly.

Below is the function that is part of the main application that loads the .dll into memory.

	public void LoadDll(){
		assembly=Assembly.Load(dllFile.bytes);
	}

My question is does the assembly member of this object require a cleanup? I Destroy the GameObject instance when the .dll is finished and the user quites the mini game. I am unsure if this cleans up the memory used by the .dll in the assembly. Will it be garbage collected later and full cleanup happen?

Is there further code I need to execute to perform a solid cleanup? The game will allow many different mini games to be played downloaded and released accumulating memory could be an issue I would like to avoid.

Thanks

Hey Travis,

Based on what I’ve read about assemblies in the .NET framework, unless mono or unity implements the assemblies differently, no, that will not unload the code in the assemblies, though if you dereference all your GameObjects, they will all be freed.

Normally, to successfully cleanup assemblies, you have to create a separate AppDomain to manage objects in, which you then have to destroy to unload the assemblies. Do remember that unless your games are a /lot/ of code, if you can ensure you unload all your textures and models, you may not be using enough memory with code to bother with the hassle.

I don’t have much experience with this, but I understand it can be very messy. I’m actually interested in finding out myself if there’s a simple solution to unloading assemblies, or anything simple about AppDomains, so if I find out more, I’ll let you know.

Best of luck,

Zarenor