Hello,
I'm using nim to make a dynamic link library (dll) to be used by a python program on Windows, and having a trouble.
I want to reuse the nim program, and I have to use not ptr object type but ref object type it will be managed by GC.
(below is an example to the last.)
type
Hoge = ref object
val: int
var hoge: Hoge
The python program call the dll's functions many time and hoge has to be constructed then.
In the python program, a memory shortage problem is occurred. I think it is caused because the dll doesn't free the memory.
So I tried the below code it is run at the end of the function so that GC collects hoge object.
hoge = nil
but there are no improvement...
There are a possibility that the cause isn't the memory occupied by dll, but please leave that aside for now.
I want to know two points below:
Compiling is executed by the below code:
nim c --app:lib test.nim
and functions which will be called by the python program have the below pragma:
{.cdecl, exportc, dynlib.}
I'm Japanese so when my English is strange please excuse me. If you know anything about this, Could you give me your advice?
Thank you.
Using Nim's Garbage Collector in a DLL is not easy -- do you have seen this thread:
https://forum.nim-lang.org/t/4580#28874
Araq mentions --gc:regions
Thank you for your reply, Stefan_Salewski. And I'm sorry that my reply was too late.
I tried using --gc:regions. However, probably due to that gc works fine, the speed of the execution of my program became very slow.
So after all I rewrote the program so that it works without gc (for example, making initializing of the structure only once and reusing it, avoiding using string, and so on). And it worked fine.
Thank you again.
Fine that it now works for you.
Generally allocating heap objects in a narrow loop can make programs slow, that is not really a GC issue, but due to the many allocations. I read that some other languages like Java or Python cache allocations, but I think current Nim does not much caching for allocations. Note that while seqs and strings are stack objects in recent Nim, they have memory allocated on heap for storing content. Maybe soon you can use Nim's new Destructors.