EngineSim objects are now pooled.
EngineSim objects are now pooled.

--- /dev/null
+++ b/KerbalEngineer/Helpers/Pool.cs
@@ -1,1 +1,53 @@
+namespace KerbalEngineer.VesselSimulator
+{
+    using System.Collections.Generic;
 
+    public class Pool<T> where T : new()
+    {
+        private static List<T> available = new List<T>();
+        private static List<T> inUse = new List<T>();
+
+        public static int PoolCount
+        {
+            get
+            {
+                return available.Count + inUse.Count;
+            }
+        }
+
+        public static T GetPoolObject()
+        {
+            T obj;
+            if (available.Count > 0)
+            {
+                obj = available[0];
+                available.RemoveAt(0);
+            }
+            else
+            {
+                obj = new T();
+            }
+
+            inUse.Add(obj);
+            return obj;
+        }
+
+        public static void Release(T obj)
+        {
+            if (inUse.Contains(obj))
+            {
+                inUse.Remove(obj);
+                available.Add(obj);
+            }
+        }
+
+        public static void ReleaseAll()
+        {
+            for (int i = 0; i < inUse.Count; ++i)
+            {
+                available.Add(inUse[i]);
+            }
+            inUse.Clear();
+        }
+    }
+}

--- a/KerbalEngineer/KerbalEngineer.csproj
+++ b/KerbalEngineer/KerbalEngineer.csproj
@@ -199,6 +199,7 @@
     <Compile Include="UIControls\WindowObject.cs" />
     <Compile Include="VesselSimulator\AttachNodeSim.cs" />
     <Compile Include="VesselSimulator\EngineSim.cs" />
+    <Compile Include="Helpers\Pool.cs" />
     <Compile Include="VesselSimulator\PartSim.cs" />
     <Compile Include="VesselSimulator\ResourceContainer.cs" />
     <Compile Include="VesselSimulator\SimManager.cs" />

--- a/KerbalEngineer/VesselSimulator/EngineSim.cs
+++ b/KerbalEngineer/VesselSimulator/EngineSim.cs
@@ -26,10 +26,10 @@
     using Helpers;
     using UnityEngine;
 
-    public class EngineSim
+    public class EngineSim : Pool<EngineSim>
     {
-        public double actualThrust = 0;
-        public List<AppliedForce> appliedForces;
+        public double actualThrust = 0.0;
+        public List<AppliedForce> appliedForces = new List<AppliedForce>();
         public bool isActive = false;
         public double isp = 0;
         public PartSim partSim;
@@ -41,7 +41,7 @@
         public Vector3 thrustVec;
         private readonly ResourceContainer resourceConsumptions = new ResourceContainer();
 
-        public EngineSim(PartSim theEngine,
+        public EngineSim Init(PartSim theEngine,
             double atmosphere,
             float machNumber,
             float maxFuelFlow,
@@ -61,9 +61,14 @@
         {
             StringBuilder buffer = null;
 
+            isp = 0.0;
+            maxMach = 0.0f;
+            actualThrust = 0.0;
             partSim = theEngine;
             isActive = active;
             thrustVec = vecThrust;
+            resourceConsumptions.Reset();
+            appliedForces.Clear();
 
             double flowRate = 0.0;
             if (partSim.hasVessel)
@@ -134,7 +139,7 @@
                 MonoBehaviour.print(buffer);
             }
 
-            appliedForces = new List<AppliedForce>();
+            appliedForces.Clear();
             double thrustPerThrustTransform = thrust / thrustTransforms.Count;
             foreach (Transform thrustTransform in thrustTransforms)
             {
@@ -142,6 +147,8 @@
                 Vector3d position = thrustTransform.position;
                 appliedForces.Add(new AppliedForce(direction * thrustPerThrustTransform, position));
             }
+
+            return this;
         }
 
         public ResourceContainer ResourceConsumptions

--- a/KerbalEngineer/VesselSimulator/PartSim.cs
+++ b/KerbalEngineer/VesselSimulator/PartSim.cs
@@ -196,7 +196,7 @@
 
                         Vector3 thrustvec = this.CalculateThrustVector(vectoredThrust ? engine.thrustTransforms : null, log);
 
-                        EngineSim engineSim = new EngineSim(this,
+                        EngineSim engineSim = EngineSim.GetPoolObject().Init(this,
                             atmosphere,
                             (float)mach,
                             engine.maxFuelFlow,
@@ -230,7 +230,7 @@
 
                         Vector3 thrustvec = this.CalculateThrustVector(vectoredThrust ? engine.thrustTransforms : null, log);
 
-                        EngineSim engineSim = new EngineSim(this,
+                        EngineSim engineSim = EngineSim.GetPoolObject().Init(this,
                             atmosphere,
                             (float)mach,
                             engine.maxFuelFlow,

--- a/KerbalEngineer/VesselSimulator/ResourceContainer.cs
+++ b/KerbalEngineer/VesselSimulator/ResourceContainer.cs
@@ -134,7 +134,7 @@
 
         public void Reset()
         {
-            this.resources = new Hashtable();
+            this.resources.Clear();
         }
 
         public void Debug()

--- a/KerbalEngineer/VesselSimulator/SimManager.cs
+++ b/KerbalEngineer/VesselSimulator/SimManager.cs
@@ -287,6 +287,7 @@
             try
             {
                 Stages = (simObject as Simulation).RunSimulation();
+
                 if (Stages != null && Stages.Length > 0)
                 {
                     if (logOutput)
@@ -367,7 +368,8 @@
                 // This call doesn't ever fail at the moment but we'll check and return a sensible error for display
                 if (simulation.PrepareSimulation(parts, Gravity, Atmosphere, Mach, dumpTree, vectoredThrust))
                 {
-                    ThreadPool.QueueUserWorkItem(RunSimulation, simulation);
+                    //ThreadPool.QueueUserWorkItem(RunSimulation, simulation);
+                    RunSimulation(simulation);
                 }
                 else
                 {

--- a/KerbalEngineer/VesselSimulator/Simulation.cs
+++ b/KerbalEngineer/VesselSimulator/Simulation.cs
@@ -144,6 +144,8 @@
             activeEngines.Clear();
             drainingResources.Clear();
 
+            EngineSim.ReleaseAll();
+
             // A dictionary for fast lookup of Part->PartSim during the preparation phase
             partSimLookup.Clear();
 

 Binary files a/Output/KerbalEngineer/KerbalEngineer.dll and b/Output/KerbalEngineer/KerbalEngineer.dll differ