Index: trunk/psLib/src/sys/psMemory.c
===================================================================
--- trunk/psLib/src/sys/psMemory.c	(revision 30595)
+++ trunk/psLib/src/sys/psMemory.c	(revision 31152)
@@ -27,4 +27,5 @@
 #include <string.h>
 #include <assert.h>
+#include <unistd.h>
 
 #if defined(PS_MEM_BACKTRACE) && defined(HAVE_BACKTRACE)
@@ -1090,2 +1091,35 @@
     return (memBlock1->freeFunc == memBlock2->freeFunc);
 }
+
+bool static dumpMemory = false;
+
+void psMemDumpSetState (bool state) {
+    dumpMemory = state;
+}
+
+void psMemDump(const char *name)
+{
+    if (!dumpMemory) return;
+
+    char filename[1024];	  // don't make your sub-names too long!
+    static int num = 0;		  // Counter, to make files unique and give an idea of sequence
+
+    snprintf (filename, 1024, "memdump_%s_%03d.txt", name, num);
+    FILE *memFile = fopen(filename, "w");
+
+    psMemBlock **leaks = NULL;
+    int numLeaks = psMemCheckLeaks(0, &leaks, NULL, true);
+    fprintf(memFile, "# MemBlock Size Source\n");
+    unsigned long total = 0;            // Total memory used
+    for (int i = 0; i < numLeaks; i++) {
+        psMemBlock *mb = leaks[i];
+        fprintf(memFile, "%12lu\t%12zd\t%s:%d\n", mb->id, mb->userMemorySize, mb->file, mb->lineno);
+        total += mb->userMemorySize;
+    }
+    fclose(memFile);
+    psFree(leaks);
+
+    // fprintf(stderr, "Memdump %s %d: Memory use: %ld, sbrk: %p\n", name, num, total, (void *) sbrk(0));
+    fprintf(stderr, "Memdump %s %d: Memory use: %ld\n", name, num, total);
+    num++;
+}
