cycleleak.nim 972 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. discard """
  2. outputsub: "no leak: "
  3. retries: 2
  4. """
  5. type
  6. Module = object
  7. nodes*: seq[PNode]
  8. id: int
  9. PModule = ref Module
  10. Node = object
  11. owner* {.cursor.}: PModule
  12. data*: array[0..200, char] # some fat to drain memory faster
  13. id: int
  14. PNode = ref Node
  15. var
  16. gid: int
  17. when false:
  18. proc finalizeNode(x: PNode) =
  19. echo "node id: ", x.id
  20. proc finalizeModule(x: PModule) =
  21. echo "module id: ", x.id
  22. proc newNode(owner: PModule): PNode =
  23. new(result)
  24. result.owner = owner
  25. inc gid
  26. result.id = gid
  27. proc compileModule: PModule =
  28. new(result)
  29. result.nodes = @[]
  30. for i in 0..100:
  31. result.nodes.add newNode(result)
  32. inc gid
  33. result.id = gid
  34. var gModuleCache: PModule
  35. proc loop =
  36. for i in 0..1000:
  37. gModuleCache = compileModule()
  38. gModuleCache = nil
  39. GC_fullCollect()
  40. if getOccupiedMem() > 9_000_000:
  41. echo "still a leak! ", getOccupiedMem()
  42. quit(1)
  43. echo "no leak: ", getOccupiedMem()
  44. loop()