Skip to content

getOccupiedMem returns wrong value #24975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
epikur-io opened this issue Jun 2, 2025 · 3 comments
Open

getOccupiedMem returns wrong value #24975

epikur-io opened this issue Jun 2, 2025 · 3 comments

Comments

@epikur-io
Copy link

epikur-io commented Jun 2, 2025

Nim Version

Nim Compiler Version 2.2.4 [Linux: amd64]
Compiled at 2025-04-22
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: f7145dd
active boot switches: -d:release

Description

The following example program just appends objects to a sequence. When checking the amount of occupied memory by calling getOccupiedMem it always returns 0 byte even if gigabytes have been allocated. Strangely when using --mm:refc the problem doesn't occur.

Example code

import std/[strutils, strformat]

type
    Obj = object of RootObj
        name: string

var l = newSeq[Obj]()
var i = 0
while true:
  var t: Obj = Obj(name: fmt"object #{i}")
  l.add(t)
  i.inc
  if i mod 10_0000 == 0: 
    echo $i & ": occupied mem " & $formatSize(getOccupiedMem())
    # ...
    # 4200000: Occupied mem 0B
    # 4300000: Occupied mem 0B
    # 4400000: Occupied mem 0B
    # 4500000: Occupied mem 0B
    # 4600000: Occupied mem 0B
    # ...

Current Output

318867: occupied mem 0B
318868: occupied mem 0B
318869: occupied mem 0B
318870: occupied mem 0B
318871: occupied mem 0B
318872: occupied mem 0B
318873: occupied mem 0B
318874: occupied mem 0B
318875: occupied mem 0B
318876: occupied mem 0B
318877: occupied mem 0B
318878: occupied mem 0B
318879: occupied mem 0B
318880: occupied mem 0B

Expected Output

62287: occupied mem 3.952MiB
62288: occupied mem 3.953MiB
62289: occupied mem 3.954MiB
62290: occupied mem 3.954MiB
62291: occupied mem 3.955MiB
62292: occupied mem 3.956MiB
62293: occupied mem 3.957MiB
62294: occupied mem 3.957MiB
62295: occupied mem 3.958MiB
62296: occupied mem 3.959MiB

Known Workarounds

No response

Additional Information

No response

@Araq
Copy link
Member

Araq commented Jun 2, 2025

Are you sure you don't have -d:useMalloc enabled? With malloc we don't know the consumed memory.

@epikur-io
Copy link
Author

epikur-io commented Jun 2, 2025

@Araq Yes, I tested it with both and this problem also appears when not passing "--d:useMalloc".

Edit:
I'm sorry, but it appears that the code base I'm working on had a useMalloc defined in the nim.cfg.
I'm reading your response as if this is expected behavior when using malloc instead of Nim's alloc, which makes sense. Do you think we should explicitly mention this in the documentation of getOccupiedMem?

@Araq
Copy link
Member

Araq commented Jun 2, 2025

Should be documented but ideally just supported with malloc, though the API for that is non-standard and may not exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants