gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

memory leak

Open ohroy opened this issue 1 year ago • 10 comments

What happened?

memory leak After a period of time, all memory will be eaten. 64G full

What did you expect to happen?

no memory leak

Client information

│ About Gemini CLI                                                      │
│                                                                       │
│ CLI Version             0.1.7                                         │
│ Git Commit              55cc809                                       │
│ Model                   gemini-2.5-pro                                │
│ Sandbox                 no sandbox                                    │
│ OS                      darwin                                        │
│ Auth Method             OAuth                                         │
│ GCP Project             googo-461406

m3 max 64gb

Login information

No response

Anything else we need to know?

No response

ohroy avatar Jul 01 '25 01:07 ohroy

Hey @ohroy, Could you please provide more information about your session? For example: what kind of jobs were you running? Any chance you could provide a stack trace?

vachan-shetty avatar Jul 01 '25 13:07 vachan-shetty

Hello, my appearance here is that doing any refactoring, generation, etc. of the code will cause a memory leak. Here's an example

Can you help me generate a unit test for @xxx.go? ......

This work went well, so I didn't close gemini-cli. After a while, maybe a few hours, the computer becomes very slow. Looking at the activity monitor, wezterm has a memory footprint of more than 60GB. Wezterm only runs gemini-cli.

It stands to reason that nodejs is supposed to be automatic garbage collection, and I don't know why this is happening.

ohroy avatar Jul 02 '25 01:07 ohroy

you should try again after updating from head. I'm pretty sure this was a leak from the telemetry / session state service that was refactored. It was counting tokens as they streamed and they seemed to bounce around.

acoliver avatar Jul 02 '25 17:07 acoliver

How many GB of memory was Gemini CLI using when it crashed? By Default, Gemini CLI is limited to 4GB of memory (node default). To raise the limit you can add autoConfigureMaxOldSpaceSize: true to your ~/.gemini/settings.json. As mentioned in https://github.com/google-gemini/gemini-cli/issues/1740 we are considering making this the default option so please do give us feedback if that mitigates the issue for you.

jacob314 avatar Jul 09 '25 15:07 jacob314

Look into how telemetry is bouncing around and not always being consumed especially when tool calls fail. I think you'll find the leak. It happens much faster with the throttle problems. I inadvertently fixed it in my fork (while adding multi-provider and fixing your rate limiting). The answer isn't to just make it bigger.

El mié, 9 jul 2025 a la(s) 12:51 p.m., Jacob Richman ( @.***) escribió:

jacob314 left a comment (google-gemini/gemini-cli#2782) https://github.com/google-gemini/gemini-cli/issues/2782#issuecomment-3053162039

How many GB of memory was Gemini CLI using when it crashed? By Default, Gemini CLI is limited to 4GB of memory (node default). To raise the limit you can add autoConfigureMaxOldSpaceSize: true to your ~/.gemini/settings.json. As mentioned in #1740 https://github.com/google-gemini/gemini-cli/issues/1740 we are considering making this the default option so please do give us feedback if that mitigates the issue for you.

— Reply to this email directly, view it on GitHub https://github.com/google-gemini/gemini-cli/issues/2782#issuecomment-3053162039, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAKILN5NWBV2XF3V3O6BZD3HU3BVAVCNFSM6AAAAACAPU7PQWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTANJTGE3DEMBTHE . You are receiving this because you commented.Message ID: @.***>

acoliver avatar Jul 10 '25 15:07 acoliver

Look into how telemetry is bouncing around and not always being consumed especially when tool calls fail. I think you'll find the leak. It happens much faster with the throttle problems. I inadvertently fixed it in my fork (while adding multi-provider and fixing your rate limiting). The answer isn't to just make it bigger.

El mié, 9 jul 2025 a la(s) 12:51 p.m., Jacob Richman ( @.***) escribió: …

Can you share a branch where you believe you accidentally fixed the issue? Expect there are multiple real memory bloat and/or leak issues that we should fix. Interested in resolving the real issues as well as unblocking more users by removing the arbitrary 4gb limit when we can.

jacob314 avatar Jul 11 '25 16:07 jacob314

https://github.com/google-gemini/gemini-cli/pull/3405 makes it much better but If I disable telemetry completely like I did in my repo then the process seems to stop growing. (I'm still working on fixing the "loop" and making it work more agentically so I can't yet run as many requests as I want but it seems to work).

acoliver avatar Jul 11 '25 19:07 acoliver

#3405 makes it much better but If I disable telemetry completely like I did in my repo then the process seems to stop growing. (I'm still working on fixing the "loop" and making it work more agentically so I can't yet run as many requests as I want but it seems to work).

how much memory usage growth are you seeing with telemetry on? Keep in mind that code like telemetry may be allocating some memory that will be GCed as soon as the next GC occurs so you don't need to worry just because memory usage is increasing slightly.

jacob314 avatar Jul 11 '25 19:07 jacob314

I didn't run a profiler or heap analysis yet. I just noticed it busted faster with telemetry on.

...also react components aren't unmounted as far as I can tell.

acoliver avatar Jul 11 '25 20:07 acoliver

we've looked at things with chrome devtools and there aren't any obvioius react related memory leaks at least for the simple scenarios we have tested. if you see one where we are failing to handle lifecycles correctly in react do let me know. if you include multiple heap snapshots that is particularly actionable. I generally use the 3 snapshot technique discussed here. https://addyosmani.com/blog/taming-the-unicorn-easing-javascript-memory-profiling-in-devtools/

jacob314 avatar Jul 11 '25 20:07 jacob314