← Back to context

Comment by glenngillen

1 day ago

I'd been running into a lot of disk pressure on my system from agents running. I discovered two main causes:

- my approach to using git worktrees for all work. Not entirely surprising, the duplicated build artifacts in each worktree being the main culprit here. There were some limited gains to be had by just being more efficient with how builds were managed but ultimately I just had to limit the number of concurrent features I was working on in a couple of particularly large repos.

- more insidious was the fact I was actually hitting memory limits more than I realised and causing my mac to have to swap heavily. The system was surprisingly still quite performant while this was happening... until it wasn't. Unfortunately as best I can tell the only "fix" when this happens is to restart the machine. There's no way to have the system reclaim any of the swap space once it's written to disk. Which if you have it thrash a couple of times a day (especially easy if you leave something running overnight) adds up very quickly.

It's so goddamn bad, I wish it was easier to turn all of this garbage off. I used to spend time defining parallel work lanes to issue to subagents as frontloaded development work with set validation checkpoints and it worked great. Now attempting to do this with 8, 16 subagents will balloon out into hundreds of gigs of fucking mess

Copy on write worktrees do absolutely nothing to solve this, either. The build artifacts alone will do it

I'll take a stab in the dark here and say that if you're using Docker (or Rancher, etc..) I found they aren't releasing memory properly and caused a similar issue to what you're describing.

Switching to Orbstack solved the problem for me because of their memory management efforts. Just wanted to mention it in case it leads to an easy fix for someone else too.

  • same here — not trying to shill OrbStack, but switching to it has "magically" solved weird problems that would otherwise have consumed my actual time to debug

    • It does seem like magic but I hope it's excellent engineering. It was valuable enough for me to start paying for it.

switching to copy on write worktrees is the biggest game changer it should become common knowledge pretty soon and it's already available on most operating systems

  • You also need a deduplication, because with simple copy on write you will have a copy for writing the same - change a byte in a file, get a copy, then change it back, get a duplicate.

    Deduplication in ZFS needs 5G of RAM for 1TB of logical data - if you expect your agents to produce 2TB of data, you should provide 10G of RAM for deduplication.

    • Why are your agents expected to produce 2TB of data? Not criticizing, I'm curious. Outside of classifiers I don't have any of my agents produce data, they produce scripts to produce data so it can be deterministic

  • He's saying the build artifacts themselves are consuming the space

    • thanks to the parent you're replying to, I discovered there's numerous projects that try to retrofit CoW into worktrees + also fill in the gaps for mostly-shared data like build artifacts! I've decided to take lane (https://github.com/lukeed/lane) for a spin as I type to see how it goes.

My work provides me a Mac laptop plus remote Windows VMs and I've completely stopped using my Mac for agent builds/tests - I give them ssh access to WSL instances and they can go hog wild on the VMs.

It's all a bit adhoc - agents still need to pull (or I need to keep the main enlistment up to date on these machines) - I'm thinking of using containers or something else like github actions keep things cleaner, but right now it's "good enough".

Regardless I'm pretty happy with how it's working so far. Previously I'd be letting agents do work in the background and they'd kick off builds in the middle of a meeting or something and I'd start dropping video streams until I killed everything.

On Linux I’ve taken to disabling swap before doing memory heavy operations. Claude will carelessly run benchmarks and tasks which gobble up all available memory. When that happens, I’m not sure why but my system becomes unresponsive and I can’t kill the process. If I disable swap, the oom killer takes care of whatever Claude was doing and Claude figures it out after the fact.

  • You can use systemd-oomd to force OOM killing without having to completely disable swap. It's quite handy for this especially since you can tell it to aim its OOM heavy artillery at only certain tasks (i.e. only processes that are descendants of claude-cli.)

  • One thing to also look out for is some distros will but /tmp in RAM, and claude loves to fill that

If git used reflink / CoW when available it would make this a lot faster.

tho, I also met this problem and found a solution for me - develop all in one worktree on a single branch, then cherry-pick to the second worktree to the needed branch.

So, only first worktree is built: - less space needed. - less time spent on build, which is usually caused by switching branches. - you are not required to merge/rebase of master branch to solve conflicts, which also cause long time rebuilds.

I ran into similar issues. Sometimes though there are good language specific solutions. For Rust based worktrees you should definitely check out https://mr-boxington.jdx.dev/ .

Regarding swapping, are you sure it's not just from ram backed tmpfs partition? For me that was it and most of the swap was from /tmp. So I created an agressive job that runs frequently to delete all tmp files not used in the last ~2 hours without any command holding handles to it.

The other thing I found was that there were a bunch of leftover processes from prior coding agents forking commands and just leaving them. So I had to build a system into my agent spawner to automatically launch each agent session in a cgroup whose processes all get killed on task completion.

For the first part you might be interested in Anthropic recently creating their own version control system that's server based like SVN, and it seems to ease their memory pressure too. It's called `scion`.