Coming from an experience as a jvm developer, I keep thinking about objects and memory allocation.
So, if I have a system where I dont create a shared Tesla client with predefined adapters and configuration, but instead relying on each service function to create a new client (with a method), will this cause any type of memory overhead in Elixir ?
For ex I have methods like these
def get_stuff_from_api(stuff_id, options \\ []) do
conn = MyTeslaClientFactory.new(options)
# possibly more logic
MyStuffService.get_stuff(conn, stuff_id)
end
and then picture 30 of these methods. Will this be a problem?
I won’t directly go to your question, but answer a few related ones.
Will it allocate memory (each time)? Yes
Will it require CPU to build the client (each time)? Yes
Where will it allocate memory? In the process calling that code
When will it be deallocated/garbage collected? There are various points in the lifecycle of a process when garbage collection happens per process. There are some cases in OTP where there can be global garbage collection, but those are commonly called out explicitly.
That depends a lot of where you call that function and what you or your projects constraints consider “a problem”. Are you in a memory constraint system? Do you want to expend more cpu (e.g. to fetch the client from elsewhere) to prevent additional memory allocations? How often are those functions called? Always in the same process or in different ones? Are the processes long lived or short lived? Can the client change at runtime? How much cpu is taken up to create the client?
Its for testcontainers package. So not production code. But never mind. Its not a huge problem unless you have 100+ tests i think with each having at last two test cases. Then testcontainers will now create 5-10 clients per test. So 200*10. hehe. But i guess they are cleaned away pretty fast. Problem comes when i need to make disk checks for docker sockets snd other stuff to evaluate correct base url. Which Im not doing atm. I guess this will be easily solved by passing in connection as an optional option
Maybe an genserver with connection as state after init is the proper way to handle this. As excontainers initially did. and then proxy calls to a module
Inter process communcation means data is copied though. If you want to share data as is you can look into persistent_term, including all it’s caveats.