0
Not a Bug

FlowMachine not cleaning up when destroyed

Dave Conley 5 months ago updated by Lazlo Bonin (Lead Developer) 4 months ago 4 1 duplicate

I've got a major issue in my app with memory. We have our app setup with asset bundles and separate scenes. When we load into a new scene we throw out the old scene and unload the asset bundles for that scene.

The bolt graph macro that is attached to the FlowMachine is not destroyed when the scene is unloaded. I tested a similar setup with a test component and a test scriptable object that is referenced by that component. Similarly, the scriptable object is initialised but not destroyed when the component is destroyed. This post on the Unity forums suggest it should be done manually https://answers.unity.com/questions/639852/does-unity-call-destroy-on-a-scriptableobject-that.html

The benefit of it not destroying is that the second load of that scene is much faster but the downside is that any assets referenced by that graph, also are not destroyed so we end up with huge memory leaks between scenes.

Bolt Version:
1.4.4
Unity Version:
2018.4.2f1
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

Duplicates 1

Not a Bug

Hi Dave,

Sorry you're experiencing this issue.

I believe Bolt and Unity are behaving normally in the scenario you describe. Assets referenced in components do not get destroyed when their components get destroyed; for example, a material does not get destroyed when a renderer that uses it gets destroyed. Likewise, a macro does not get destroyed when a machine that uses it gets destroyed.

You could manually destroy the macro, although I must admit I never tested asset-destruction in any Unity project ever, so I'm not sure how the build would cope with that. For example, what happens if you reload the scene later, does Unity auto-recreate it?

Regarding the load time issue, that is indeed Bolt's most major performance problem that we are addressing radically in Bolt 2. We are abandoning Full Serializer entirely in favor of Odin Serializer, which is orders of magnitudes faster and leaner on memory on all operations (serialization and deserialization). In the future then, you should see much less of a lag spike while loading macros.

I hope that clarifies things! Let me know if I can help any further.

But that's what I'm saying. I can't destroy the macro because I don't have a reference to it as far as I know. I'm unaware of any method or property on the Flow Graph that returns the macro asset that I could then call destroy on.

Ah I see. That is by design, unfortunately. 

You would have to write a C# helper method that gets the macro off the current machine. Thankfully it's pretty easy:

Destroy(gameObject.GetComponent<FlowMachine>().nest.macro);

Please keep me updated with your findings!

Bolt Version:1.3

maybe cos of your version / it was fixed in 1.4 as i remember