net-lisias-ksp / KSP-Recall

Recall for KSP blunders, screw ups and borks.
GNU General Public License v2.0
25 stars 2 forks source link

Refunding is triggering a nasty memory leak on this.part.Modules.Add #23

Closed Lisias closed 3 years ago

Lisias commented 3 years ago

This is a follow up from issue #21

TL;DR : Refunding was creating and destroying the fake Resource under demand. This was triggering a nasty bug on Unity's GC under heavy load, since this crappy excuse of a Game Engine is using spinlocks to halt the threads when the GC is working. This completely trashes the whole system: 8 threads waiting a spinlock and an i7 with 4 cores and 8 hyper-threads just gets down on its knees in pain.

bully-scholarship-edition-thumb04

Empirical tests pinpoint the this.part.Resources as the culprit by the memory leak.

Lisias commented 3 years ago

Jesus... The memory leak persists even by quitting the current savegame and going back to the Main Menu... #facePalm

Lisias commented 3 years ago

And... YEAH. I was right

There's something smelling funny on this.part.Resources.Add(PartResource). Brute forcing my way using this.part.Resources.Add(int, PartResource) solved the memory leak and also that pesky log messages on KSP.log like this one:

[LOG 00:02:31.256] [PartSet]: Failed to add Resource 1566956177 to Simulation PartSet:60079 as corresponding Part Mk0 Liquid Fuel Fuselage-4274492751 SimulationResource was not found.
Lisias commented 3 years ago

Fixed on commit https://github.com/net-lisias-ksp/KSP-Recall/commit/8395611d80ec7d6dd443732bb873f0dcdd26ba2a