Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is memory allocated by malloc garbage collected?

Tags:

malloc

julia

I am guessing (hoping) the answer is never. That such memory must be explicitly freed.

For example if if I wrote:

julia> x = Libc.malloc(1_000_000)
Ptr{Void} @0x0000000002f6bd80

julia> x = nothing

have I just leaked ~1MB of memory?

However I am not 100% certain this is true, because the docs don't mention it at all.

help?> Libc.malloc(3)
malloc(size::Integer) -> Ptr{Void}

Call malloc from the C standard library.

like image 682
Lyndon White Avatar asked Oct 20 '25 16:10

Lyndon White


1 Answers

Yes, you are correct.

Julia is designed to seamlessly interoperate with C on a low level, so when you use the C wrapper libraries, you you get C semantics and no garbage collection.

The docs for Libc.malloc is not written to teach C, but could be improved to mention Libc.free, in case anyone gets confused.

like image 172
ivarne Avatar answered Oct 23 '25 06:10

ivarne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!