The prints said memory grows. The debugger said why.
Training a trading model, an AI agent spent four rounds on print statements chasing a memory leak. Given a debugger through an MCP tool, it found the cause in one session, and the fix was 16 lines.
I was training a trading model, and the training job kept getting killed. The machine had 121 GB of memory free, shared by the CPU and the GPU. With two jobs running at once, used memory climbed to 98 to 107 GB and the watchdog killed the run after five and a half minutes. Once, with three jobs, the kernel’s out-of-memory killer stepped in and took desktop processes with it.
The job is Java, running PyTorch through DJL (Deep Java Library). An AI agent did the work. I want to show the two routes it took, because the difference between them is the whole point.

Route one: print statements
The agent did what it is trained to do. It guessed, changed something, and added a print to see what happens. A developer would have opened the debugger and set a breakpoint, or simply started the profiler.
Four rounds of that. It moved the check into a sub-manager. It added a print of how many arrays DJL was managing. It padded every batch to a fixed size. It set GPU allocator options.
The print gave real numbers: 61,181 arrays at step 15, 185,697 at step 75. Plus 31,000 arrays and 4.6 GB every fifteen steps. The three fixes did nothing. A 150-step test still went from 22 GB to 52 GB.
So after four rounds we knew one thing for certain: memory grows. We did not know why. Every round cost an edit, a rebuild and a run, and every edit was one more thing to take out again later.
Route two: the debugger
For a developer this is the obvious step. The debugger is part of the IDE: set a breakpoint, run, look. An agent has none of that. It works in a terminal, where no debugger is waiting, and it learned from code, which is full of print statements and contains no debugger sessions at all. So out of the box an agent prints. It does not reach for a debugger, because it has none and has hardly ever seen one used.
This agent got one through an MCP tool (Model Context Protocol, the standard way an agent calls outside tools): a debugger and a profiler it could drive itself, on the running job. No code change at all.
- Launch the real run under the debugger, held before its first instruction.
- One breakpoint at the early-stopping check, the periodic test of whether training should stop. It runs every ten steps.
- At the first stop, ask the program itself: the long-lived memory manager held 30,784 arrays.
- Look at the newest of them. Their shapes all started with 6. A training step works on 4 days of market data. Early stopping evaluates 6. These arrays came from the check, not from training.
- At the third stop, the same question: 92,322. Exactly three times the first reading. The training steps in between had added nothing.
About fifteen calls to the debugger and the profiler. That was the diagnosis.
The cause, and the fix
Early stopping loaded its six evaluation days once, into the long-lived memory manager. DJL attaches every result to the manager of the array it was computed from. So each check left about 30,800 arrays, about 4.6 GB, on a manager that never closes. A 400-step run with a check every 25 steps has 16 checks: about 74 GB. That matches the kills.
It also explains why the first guess failed. Moving the work into a sub-manager could not help, because the input arrays still lived on the long-lived one, and the results followed them there.
Once the cause was known, the fix was small: each check now loads its six days inside its own scope, evaluates there, and the scope frees everything when it closes. One file, 16 lines added, 3 removed.
Did it work
The same question at the same breakpoint, after the fix: 744 arrays at the first stop, 1,476 at the second. The jump of 30,800 per check is gone.
Two 400-step jobs at once, the setup that used to reach 107 GB, now peak at 36 GB together. The full sweep, 15 runs two at a time over 107 minutes, finished without a single kill. Available memory never fell below 85 GB.
What I will not claim: a small leak is left, about 700 small arrays per ten steps, under a gigabyte over a whole run by estimate. It is not fixed and not measured. And I have not proven there is no other leak. The machine-level numbers simply stay flat.
What the difference was
Prints answer the question you already know how to ask: how big is it now? You have to guess where to put them, and when the guess is wrong you rebuild and guess again.
A debugger lets you ask the running program the question you did not know you had. Which arrays? How many per check? From which operation? Here the answer was in the shape of an array, a 6 where a 4 belonged. No print would have shown that unless someone had already suspected it.
For a person at an IDE the debugger is one click away, and a person who sees a 6 where a 4 belongs stops. For an agent the debugger is not there unless someone gives it one, and the habit it brings is the print, because the print is what it saw in the code it learned from. Give it a debugger through MCP, and when it adds its second print, point it there.