summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-02-10 09:22:28 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-02-10 09:22:28 +0100
commit0bb18d1821183b08079e8cff03498454ff0b36f1 (patch)
treeadc3515b64c7114b270f4da2d781f4c920a8d983
parent601dc2098df48838f044fa2e68d8227a164a0fc8 (diff)
downloadNim-0bb18d1821183b08079e8cff03498454ff0b36f1.tar.gz
introduce nimMaxHeap define to fight memory overcommit
-rw-r--r--lib/system/alloc.nim5
-rw-r--r--tools/nimsuggest/nimsuggest.nim.cfg6
2 files changed, 11 insertions, 0 deletions
diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index a124d7537..f28a124d2 100644
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -292,10 +292,15 @@ proc writeFreeList(a: MemRegion) =
               it, it.next, it.prev, it.size)
     it = it.next
 
+const nimMaxHeap {.intdefine.} = 0
+
 proc requestOsChunks(a: var MemRegion, size: int): PBigChunk =
   when not defined(emscripten):
     if not a.blockChunkSizeIncrease:
       let usedMem = a.currMem # - a.freeMem
+      when nimMaxHeap != 0:
+        if usedMem > nimMaxHeap * 1024 * 1024:
+          raiseOutOfMem()
       if usedMem < 64 * 1024:
         a.nextChunkSize = PageSize*4
       else:
diff --git a/tools/nimsuggest/nimsuggest.nim.cfg b/tools/nimsuggest/nimsuggest.nim.cfg
index 949bd18e8..6525a206a 100644
--- a/tools/nimsuggest/nimsuggest.nim.cfg
+++ b/tools/nimsuggest/nimsuggest.nim.cfg
@@ -8,6 +8,12 @@ path:"$lib/packages/docutils"
 
 define:useStdoutAsStdmsg
 define:nimsuggest
+# die when nimsuggest uses more than 4GB:
+@if cpu32:
+  define:"nimMaxHeap=2000"
+@else:
+  define:"nimMaxHeap=4000"
+@end
 
 #cs:partial
 #define:useNodeIds