Closed juzt3 closed 6 months ago
In Nim? If you manually want to deallocate memory you didn't allocate before you need to Zero it completely. Or you remove all references to it since apparently there still are references to the buffer.
Afaik Nims own dealloc only works with memory you explicitly allocated with alloc.
As a last resort you can also importc "free" but I wouldnt advice .
Since im unsure if you want to use the Nim Code or the "inteded" Python conversion, it would helpful if you provide more info on what you want to do, how you actually see that its not getting deallocated etc. Best answers to problems you usually get if you explain:
The issue is related to #51. I'm reading memory regions, and I want to free the memory used after reading each region becose I'm getting an "out of memory" error.
Using pyMemoryEditor doesn't give me this error and it handles memory efficiently but I have a different problem with it.
Out of Nim experience "out of Memory" doesn't necessarily mean that something didn't get freed properly. It may also happen if your loop is wrong. If you use the Python part, since you didnt answer the questions, i would advice to single step through the code while taking an actual look at the RAM usage.
I'm actually looking at RAM usage with pyMeow compared to pyMemoryEditor and I can see how the last free memory after reading it.
Believe me I have looked at it for hours and tried different things
Ok, fair enough. Yet you still didn't answer the important questions. Are you using Nim or Python?
i'm using Python, sorry for mixing things. I was asking for Nim becase your implementation is in Nim
Its @qb-0 actually, im just a contributor.
Ok so, r_bytes is internally calling (in Nim) readSeq.
A Nim Sequence is like a Python List. Nimpy also converts a Sequence to a List. In the source, readSeq uses Nims internel "newSeq" which allocates a new Sequence of type "T" (the type you insert, in that case a uint8) of size size.
So here you are indeed correct that the code allocates memory (explicitly).
pyMeow is getting compiled with the flag --gc:refc
. Which is "garbage collected: reference counted".
As a band-aid without changing the Nim code, you could try changing the garbage collection. There is also currently a Nim bug starting with 2.0.0 about mem allocation.
I would try to remove --gc:refc
and add --d:useMalloc
and recompile the source
https://github.com/qb-0/pyMeow/blob/053268ed25f7c6a61d195db2262adcaa37999382/pyMeow.nim.cfg#L6
It didn't helped much. Now instead of getting an out of memory error it just crashes. I'm limiting the size of the reading and it works better but the memory usage only goes up slower until it fills but without crashes
Update:
--gc:refc
is indeed better at handleing memory. i think this memory region reading needs to be optimized. So my fault
Meh, thats unfortunate. Even if i would expose a proc to deallocate the sequence to you from Nim to Python, qb would need to check and accept it before. So i guess you need to wait until qb responded. Maybe he has additional ideas
Edit: did read your message before editing.
In that case, awesome
when i use r_bytes the memory used in nim doesn't get freed, is it possible to fix that?