cycleleak.nim 948 B

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