Skip to content

High memory usage #1541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ebak opened this issue Jun 30, 2024 · 18 comments
Closed

High memory usage #1541

ebak opened this issue Jun 30, 2024 · 18 comments
Labels
enhancement New feature or request

Comments

@ebak
Copy link

ebak commented Jun 30, 2024

Description

I have recently tried out the stable tool-chain (installed with swiftly on Manjaro Linux) with VSCode and its Swift plugin.
The memory usage of sourcekit-lsp easily freezes out the machine.

The machine has 16 CPU threads (8 core with 2 HW threads), but only 8GB RAM.
As I suspect the number of sourcekit-lsp threads scales up based on the number of CPU threads.

Is it possible to explicitly limit the sourcekit-lsp threads?
(Linux allows to turn off some cores, but that is not too convenient.)

Or would it be possible to also consider available RAM too when spawning sourcekit-lsp threads?
E.g. for 8GB of RAM only spawn 3 threads, even if the machine have 256 CPUs.

@ebak ebak added the enhancement New feature or request label Jun 30, 2024
@ahoppen
Copy link
Member

ahoppen commented Jun 30, 2024

Synced to Apple’s issue tracker as rdar://130844901

@ahoppen
Copy link
Member

ahoppen commented Jul 2, 2024

Hey @ebak,

A couple of questions:

  • How much memory does sourcekit-lsp use on your machine? I would be expecting something <500MB on start up, fairly independent of your CPU core count.
  • Does the memory usage of SourceKit-LSP grow over time or does it go up immediately after launching
  • Does this also reproduce with a Swift 6 toolchain. We have made significant changes to SourceKit-LSP between 5.10 and 6.0, so the issue might already be fixed.
  • Does the memory usage go up on every project that you open or only on specific ones? If so, would you be able to share the project?

@ebak
Copy link
Author

ebak commented Jul 2, 2024

Hi,

Now I don't have possibility to try it on the machine with 8 CPUs, maybe next week.

How much memory does sourcekit-lsp use on your machine? I would be expecting something <500MB on start up, fairly independent of your CPU core count.

I attach screenshots of htop output, as I see at startup it only consumes 2.8% (I don't know what htop means about this percentage.)

Screenshot from 2024-07-02 10-57-56

Does the memory usage of SourceKit-LSP grow over time or does it go up immediately after launching

Grows over time. As I see build and source browsing is required for it.

Screenshot from 2024-07-02 11-49-07

Does this also reproduce with a Swift 6 toolchain. We have made significant changes to SourceKit-LSP between 5.10 and 6.0, so the issue might already be fixed.

Not yet tried out, maybe I'll do when I have possibility to play it around with the system with 8 CPUs.

Does the memory usage go up on every project that you open or only on specific ones? If so, would you be able to share the project?

Recently I was working with this project (QtTest):
QtTest.zip

@ebak
Copy link
Author

ebak commented Jul 2, 2024

Just by editing the same code, the memory usage quickly goes up to 45% (3.6GB maybe):

Screenshot from 2024-07-02 15-31-16

Maybe unrelated but VSCode usually forgets about the dependent packages:

VSCode-Screenshot from 2024-07-02 15-36-06

The dependencies (Qlift, Math) are sometimes shown up there, sometimes not.

Sad the 8GB RAM is hardly enough for IDE assisted development.
In my case the opened apps almost use up all free memory (Gnome, 2 terminals, 2 file managers, a light weight editor (gedit), an ebook reader for the Swift book, VSCode with Swift instrumentation).
Opening up a Firefox with 1-2 tabs would easily freeze out the system.

@ahoppen
Copy link
Member

ahoppen commented Jul 4, 2024

I have been able to reproduce the memory growth by working on the SourceKit-LSP codebase in VS Code on Ubuntu 22.04.

@ebak
Copy link
Author

ebak commented Jul 4, 2024

Thanks! With the 6.0 or the 5.10 toolchain? Maybe if this issue doesn't exist in 6.0, this issue could be closed and wait for 6.0 to get stable. (Currently I cannot test.)

@ahoppen
Copy link
Member

ahoppen commented Jul 15, 2024

I saw the memory issue with a 6.0 toolchain.

ahoppen added a commit to ahoppen/swift that referenced this issue Aug 5, 2024
Checking if this allows us to reproduce the memory issues from swiftlang/sourcekit-lsp#1541 on macOS.
@iMostfa
Copy link
Contributor

iMostfa commented Sep 28, 2024

i think that this issue is happening more after updating to Xcode 16.
it keeps growing until the whole device get freezed.

image

@iMostfa
Copy link
Contributor

iMostfa commented Sep 30, 2024

i downloaded Xcode 15, copied the sourceKit inside it, then placed it inside Xcode 16, and it appears to be working, and fixed my issue [until now]

@ahoppen
Copy link
Member

ahoppen commented Oct 2, 2024

Oh, that’s interesting that you’re also seeing excessive memory usage on macOS. I haven't seen that before.

Could you try reproducing the problem, run leaks --outputGraph=/tmp sourcekit-lsp and then share the .memgraph file that is being generated the command? I’m hoping that will show us where the memory allocations are from.

@iMostfa
Copy link
Contributor

iMostfa commented Oct 2, 2024

@ahoppen i will try, when it happens i'm running against time to kill SourceKit-lsp before my device is freezed 🥶

i can also confirm that since i started using sourcekit-lsp from Xcode 15 the issue isn't appearing

@ahoppen
Copy link
Member

ahoppen commented Oct 2, 2024

I think it would be valuable to see the graph when SourceKit-LSP is at ~2GB. That’s already more than it should take and should contain the culprit who’s using the memory.

@iMostfa
Copy link
Contributor

iMostfa commented Oct 8, 2024

@ahoppen
i just shared it with u over email, thanks for u

@ahoppen
Copy link
Member

ahoppen commented Oct 9, 2024

Thanks for sharing the memgraph, @iMostfa 🙏🏽 With it, it was obvious that the failure cause is the same one as #1720.

ahoppen added a commit to ahoppen/sourcekit-lsp that referenced this issue Oct 9, 2024
…yncQueue`

Adding an item to `AsyncQueue<Serial>` is linear in the number of pending queue items, thus adding n items to an `AsyncQueue` before any can execute is in O(n^2). This decision was made intentionally because the primary use case for `AsyncQueue` was to track pending LSP requests, of which we don’t expect to have too many pending requests at any given time.

`SourceKitIndexDelegate` was also using `AsyncQueue` to track the number of pending units to be processed and eg. after indexing SourceKit-LSP, I have seen this grow up to ~20,000. With the quadratic behavior, this explodes time-wise.

Turns out that we don’t actually need to use a queue here at all, an atomic is sufficient and much faster.

Independently, we should consider mitigating the quadratic behavior of `AsyncQueue<Serial>` or `AsyncQueue` in general.

Fixes swiftlang#1541
rdar://130844901
@ahoppen
Copy link
Member

ahoppen commented Oct 29, 2024

I tracked down the memory leak to a bug in libdispatch. The issue should be fixed by swiftlang/swift-corelibs-libdispatch#850.

@m-buczek
Copy link

This error prevents me from working on Xcode 16.2.

@ahoppen Is there a chance to fix this bug in Xcode 16.x or do we have to wait for Xcode 17?

I can confirm that the workaround provided by @iMostfa works (thanks a lot!).

More detailed instructions:

  1. Disable System Integrity Protection: Apple Documentation
  2. Get Xcode 15.4: Download Link (requires login to a developer account) and unzip archive.
  3. Replace sourcekit-lsp binary:
sudo cp /<PATH_TO_XCODE_15.4>/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/

Please note that Xcode will only work with System Integrity Protection disabled.

@iMostfa
Copy link
Contributor

iMostfa commented Jan 13, 2025

@m-buczek it's interesting that you needed SIP to be disabled, for me it worked without disabling it.
btw @ahoppen can you confirm to us if the changes to sourcekit-lsp were merged to Xcode[or swift which later merged to Xcode]

@ahoppen
Copy link
Member

ahoppen commented Jan 13, 2025

Thanks for also confirming that my PR above fixes the issue. The fix will be included in Swift 6.1. I can’t speak for Xcode’s release schedule.

@ahoppen ahoppen closed this as completed Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants