Index: /trunk/src/kWorker/Makefile.kmk
===================================================================
--- /trunk/src/kWorker/Makefile.kmk	(revision 2877)
+++ /trunk/src/kWorker/Makefile.kmk	(revision 2878)
@@ -40,5 +40,6 @@
 kWorker_LIBS.win = \
 	$(TEMPLATE_BIN-STATIC-THREADED_LIBS) \
-	$(PATH_SDK_WINDDK71_LIB_WNET)/ntdll.lib
+	$(PATH_SDK_WINDDK71_LIB_WNET)/ntdll.lib \
+	$(PATH_SDK_WINDDK71_LIB_WNET)/psapi.lib
 kWorker_LDFLAGS.win = \
 	/BASE:0x10000 /DYNAMICBASE:NO /FIXED /SECTION:DefLdBuf,EWR
Index: /trunk/src/kWorker/kWorker.c
===================================================================
--- /trunk/src/kWorker/kWorker.c	(revision 2877)
+++ /trunk/src/kWorker/kWorker.c	(revision 2878)
@@ -32,4 +32,5 @@
 *********************************************************************************************************************************/
 //#undef NDEBUG
+#define PSAPI_VERSION 1
 #include <k/kHlp.h>
 #include <k/kLdr.h>
@@ -47,4 +48,5 @@
 
 #include "nt/ntstuff.h"
+#include <psapi.h>
 
 #include "nt/kFsCache.h"
@@ -5954,16 +5956,21 @@
 
 
-static void kwSandboxCleanup(PKWSANDBOX pSandbox)
-{
-    PKWVIRTALLOC pTracker;
-    PKWLOCALSTORAGE pLocalStorage;
+/**
+ * Does sandbox cleanup between jobs.
+ *
+ * We postpone whatever isn't externally visible (i.e. files) and doesn't
+ * influence the result, so that kmk can get on with things ASAP.
+ *
+ * @param   pSandbox            The sandbox.
+ */
+static void kwSandboxCleanupLate(PKWSANDBOX pSandbox)
+{
+    PROCESS_MEMORY_COUNTERS     MemInfo;
+    PKWVIRTALLOC                pTracker;
+    PKWLOCALSTORAGE             pLocalStorage;
 #ifdef WITH_TEMP_MEMORY_FILES
-    PKWFSTEMPFILE pTempFile;
-#endif
-    PPEB pPeb = kwSandboxGetProcessEnvironmentBlock();
-    pPeb->ProcessParameters->CommandLine = pSandbox->SavedCommandLine;
-    /** @todo lots more to do here!   */
-
-#ifdef WITH_TEMP_MEMORY_FILES
+    PKWFSTEMPFILE               pTempFile;
+
+    /* The temporary files aren't externally visible, they're all in memory. */
     pTempFile = pSandbox->pTempFileHead;
     pSandbox->pTempFileHead = NULL;
@@ -6018,4 +6025,5 @@
     }
 
+
     /* Free the environment. */
     if (pSandbox->papszEnvVars)
@@ -6032,4 +6040,30 @@
         pSandbox->papwszEnvVars[0] = NULL;
     }
+
+    /*
+     * Check the memory usage.  If it's getting high, trigger a respawn
+     * after the next job.
+     */
+    MemInfo.WorkingSetSize = 0;
+    if (GetProcessMemoryInfo(GetCurrentProcess(), &MemInfo, sizeof(MemInfo)))
+    {
+#if K_ARCH_BITS >= 64
+        if (MemInfo.WorkingSetSize >= 512*1024*1024)
+#else
+        if (MemInfo.WorkingSetSize >= 384*1024*1024)
+#endif
+        {
+            KW_LOG(("WorkingSetSize = %#x - > restart next time.\n", MemInfo.WorkingSetSize));
+            //fprintf(stderr, "WorkingSetSize = %#x - > restart next time.\n", MemInfo.WorkingSetSize);
+            g_fRestart = K_TRUE;
+        }
+    }
+}
+
+
+static void kwSandboxCleanup(PKWSANDBOX pSandbox)
+{
+    PPEB pPeb = kwSandboxGetProcessEnvironmentBlock();
+    pPeb->ProcessParameters->CommandLine = pSandbox->SavedCommandLine;
 }
 
@@ -6115,4 +6149,5 @@
             rcExit = 42 + 4;
 
+        /* Clean up essential bits only, the rest is done after we've replied to kmk. */
         kwSandboxCleanup(&g_Sandbox);
     }
@@ -6514,4 +6549,5 @@
                                           cEnvVars, environ);
         KW_LOG(("rcExit=%d\n", rcExit));
+        kwSandboxCleanupLate(&g_Sandbox);
     }
 
@@ -6661,5 +6697,8 @@
                         if (   rc == 0
                             && !g_fRestart)
+                        {
+                            kwSandboxCleanupLate(&g_Sandbox);
                             continue;
+                        }
                     }
                     else
@@ -6670,4 +6709,6 @@
                 rc = kwErrPrintfRc(-1, "Bogus message length: %u (%#x)\n", cbMsg, cbMsg);
         }
+        FlushFileBuffers(hPipe);
+        CloseHandle(hPipe);
         return rc > 0 ? 0 : 1;
     }
