tcoopman

tcoopman

I was reading the docs on Agent and noticed there is no mentions of hibernation at all.
Given that Agents are just wrappers around GenServers I was kinda surprised .

So I read through the code, but hibernate is not used at all.

Wouldn’t it make sense to support hibernate on Agent? I noticed that hibernation reduce memory consumption often a lot.

Showing Posts 1 to 9

dimitarvp

dimitarvp

You would think so, yep, but I’d say if you have enough Agents in memory that hibernating them would help you then you have a huge design problem. I mean, you would probably need 500K - 1M agents for this to make a tangible difference. And you absolutely should not have so many agents.

tcoopman

tcoopman OP

I find that a bit of a weird argument.

You can make the same argument for GenServers and LiveViews.
GenServers support hibernate and LiveView has a default hibernation after 15s.

tcoopman

tcoopman OP

One more thing. It’s actually trivial to write an agent that grows huge in size if not hibernated and where hibernation makes it tiny again.

Agent.start_link(fn ->
  x = fetch_something_big()
  calculate_something_small_from(x)
end, name: :foo)

This will create a huge Agent named foo, although it might just have a tiny state.
After hibernation this would become tiny again.

You could of course trigger :erlang.garbage_collect on it manually to get the same result.

krasenyp

krasenyp

Yes, but the idea of an Agent is not to fetch huge data in it. It’s for simple concurrent state management. You’d fetch the data and then update the Agent’s state. API’s should be hard to misuse.

derek-zhou

derek-zhou

IMHO, Agent is just GenServer with training wheels. I moved to full GenServer for all Agents except the trivial ones. Maybe the core team think adding advanced features to a “training-wheel” API is not worth their time?

tcoopman

tcoopman OP

That’s not an idea I read in the docs.

The first function blocks the agent. The second function copies all the state to the client and then executes the operation in the client. One aspect to consider is whether the data is large enough to require processing in the server, at least initially, or small enough to be sent to the client cheaply. Another factor is whether the data needs to be processed atomically: getting the state and calling do_something_expensive(state) outside of the agent means that the agent’s state can be updated in the meantime. This is specially important in case of updates as computing the new state in the client rather than in the server can lead to race conditions if multiple clients are trying to update the same state to different values.
(from Agent — Elixir v1.20.2)

The docs say to consider if the data is large enough to require processing in the server.
It doesn’t say that you should not process large data in an agent.

Anyway, I don’t mind too much that Agent doesn’t have hibernate. I seem to be the first person missing this, and it’s easy enough to work around.

I was a bit surprised about it though, because it’s easy to have big stacks that can be a lot smaller after garbage collection. Anyway, it might not be an issue at all and might be cleaned up once garbage collections kicks in.

D4no0

D4no0

Completely agree, there is absolutely zero benefit from using an Agent over a Genserver besides those 2 callbacks that dump the state.

dimitarvp

dimitarvp

Another angle to consider is that I personally would reach for ETS if load and/or payload size(s) start growing.

jeffheigl

jeffheigl

@tcoopman - Agent supports hibernate_after, I just tried and confirmed it works (using :observer I looked that the Processes → Current Function).

When you call Agent.start_link just pass in the hibernate_after option in miliseconds.

Looking at the code, Agent.start_link will pass through the options to GenServer.start_link

https://github.com/elixir-lang/elixir/blob/main/lib/elixir/lib/agent.ex#L276

— All posts loaded —

Where Next? Top

Trending in Discussions Top

wmnnd
Hey folks, I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Ganna
Hi everyone, I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
johnkirkham
A note on form. Joe Armstrong, co-creator of the Erlang programming language, died in April 2019. What follows is a work of narrative non...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews