Skip to content

Excessive RAM usage when syncing large folders #4976

Description

@calmh

When syncing we build a queue of the files to sync, then sort it according to the desired criteria (size, time, random, etc.). For millions of files this can use a lot of RAM. In some cases we don't really care too much about the exact order and it would be better to use less RAM. For this, we could have a limit on the queue size. When doing a pull and iterating over the database / need list, we stop iterating once the limit is reached. The pull proceeds as normal. When complete, we do another pass and grab more needed files, repeat until done.

Activity

  1. added
    enhancement
    on May 30, 2018
  2. imsodin commented on May 30, 2018

    @imsodin
    Member

    I think this will come with a slight disadvantage in terms of renames?

    I have a almost finished (pre-polish) branch where I implement disk-spilling slices and maps to restrict RAM usage (for both scanning and pulling). I already started working on it before you posted on the forum, that you consider that overkill (from memory, maybe it was something else). Would such an option be considered, i.e. should I invest some more time to roughly finish it for review?

  3. calmh commented on May 30, 2018

    @calmh
    MemberAuthor

    Yeah it would make us miss renames if there are a lot of changes and the split happens between the files. I guess that would happen if a large directory is renamed for example.

    I guess disk spilling is fine too - but there's a lot of special case sorting etc going on for the pulling. I guess we can implement sort.Interface on top of them. Are they FileInfo (FileIntf?) specific or is there a lot of type assertion going on?

  4. calmh commented on May 30, 2018

    @calmh
    MemberAuthor

    I have the implementation for this done and it's small :)

  5. imsodin commented on May 30, 2018

    @imsodin
    Member

    Ok, then probably don't delay for my stuff, because merging till next Tuesday is hardly realistic.

    I probably shouldn't start describing it too much, that would just cause confusion. The caller needs to pass a type implementing an interface defined by the disk spilling code. I pushed the code if you want to have a quick look at it, interfaces should be final. However please do not judge that code (nor commit history), it was never meant to be public in this form x)
    Value: https://github.com/imsodin/syncthing/blob/diskOverflow-nextgen/lib/diskoverflow/common.go#L19
    SortValue: https://github.com/imsodin/syncthing/blob/diskOverflow-nextgen/lib/diskoverflow/sorted.go#L36

    The following links are broken (left here for Github support ;) ):
    Value: master...imsodin:diskOverflow-nextgen#diff-8d3f8add196bc2edef7d73cdf08e37daR41
    SortValue: master...imsodin:diskOverflow-nextgen#diff-c7f82e327453e4ad6fde542f76465758R59

    And there is a predifened type fulfilling the interface for file infos.

  6. AudriusButkevicius commented on May 30, 2018

    @AudriusButkevicius
    Member

    Shows nothing to compare. anyways, the crux of the rename problem comes from scans, and we should fix it there. Disk spilling thing will solve that hopefully, and then we can augment the puller to support that.

  7. imsodin commented on May 31, 2018

    @imsodin
    Member

    I updated the links (and wrote to github support, something is broken here). Anyway, the takeaway for me is that a disk-overflow type is probably good to have mid to long term, but this issue can be solved quickly without it - i.e. it's worth cleaning up for preliminary review, but no rush.

  8. calmh commented on Jun 1, 2018

    @calmh
    MemberAuthor

    @imsodin was looking at your branch but there's no definition for Value, I think checking is missing a file or so? This is kind of central as it defines how to interact with this thing :)

  9. added a commit that references this issue on Jun 1, 2018
    82c5fc0
  10. imsodin commented on Jun 1, 2018

    @imsodin
    Member

    I indeed forgot to git add a crucial file - symptom of hardly writing any original code at all :)
    I cleaned it up and filed a (wip) PR, referencing a branch is too cumbersome: #4980

  11. icolwell commented on Mar 11, 2019

    @icolwell

    Did this fix make it into syncthing yet? Definitely would love to use this feature as I am currently experiencing excessive RAM usage and don't know how else to limit the RAM.

  12. sifourquier commented on May 7, 2019

    @sifourquier

    Hi
    I think i have a bug due to this issue

    I syncronise the source of QT and syncthing use 13G ram (i have 16G ram)

  13. wiwie commented on Oct 18, 2019

    @wiwie

    I may also be affected by this issue. I'm syncing a folder of 114GB, containing about 8k files. Memory usage on a NAS is maxing out at 1GB. I feel this is definitely too much for 8k files. Has this been merged into master? Is there a flag I need to set somewhere, to limit the queue size?

  14. jeffbl commented on Oct 24, 2019

    @jeffbl

    I also don't know if this is my issue, but I'm also having RAM issues, with very large temporary spikes. On a 4GB Raspberry pi 4 running nothing but syncthing and a tmux remote console session, it is crashing:

    runtime: out of memory: cannot allocate 40370176-byte block (2855698432 in use)
    fatal error: out of memory

    Most of the time when I check it is using less than a GB of RAM. I have the order for all folders on this device set to "random" so I would expect it not to do any sorting, but the first post above implies that it actually does a sort when "random" is selected? Can it just skip the sort on "random", if this could be contributing to this issue?

  15. Leicas commented on Oct 24, 2019

    @Leicas

    I think I have the same issue, on my machine Syncthing used more than 4GB of RAM and made the full system unresponsive. I was syncing a folder of 200GB with mainly 4GB files (50files total). I have other shares but there was no activity on them at the time, and usage is back to 500MB now.

  16. 10 remaining items

  17. Leicas commented on Nov 4, 2019

    @Leicas

    I'm actually still having the issue with the database tuning set to small.
    It's happening every time I'm syncing a bunch of ~4GB files on my local 1Gb/s network.
    AFAIK it wasn't happening before version 1.3.0 and I was already syncing the same folder.

  18. imsodin commented on Nov 4, 2019

    @imsodin
    Member

    I completely forgot about the comments in this issue. I am fairly sure it is completely unrelated to the original issue in this thread, but a problem with our pooling in the protocol package, which was not noticeable until a change in go1.13 (or our package is fine, and there is a regression in go1.13, but I'd expect it's our package).

    Please do not respond to this issue about the recent memory usage problem, but use the following forum thread: https://forum.syncthing.net/t/large-mem-usage-on-sync-regardless-of-db-tuning/14015
    I intend to investigate further in the near future and open a ticket once the problem is located.

  19. added a commit that references this issue on Nov 4, 2019
  20. abs0 commented on May 3, 2022

    @abs0

    Pretty sure I've also hit this - in trying to convert a periodic rsync between one primary and two copies to syncthing, and having to stop syncthing due to memory usage (it reached 16.3g VIRT 10.2g RES)

    The single folder to sync had:

    • 39,978 directories
    • 17,897,687 files
    • 1.2TB usage

    It counts up to 31,999 directories, then the status remains unchanging while the memory usage keeps climbing. I've tried setting caseSensitiveFS true without apparent effect.

    I have syncthing in use in a couple of other cases - but with nothing like the number of files the largest having filers including (10.5TB, 377 directories, 21,231 files) and (0.5TB, 3,816 directories, 49,906 files) and with a steady state of ~1GB SIZE and ~280MB RES. Those are under NetBSD, and the new setup is under Linux, but I don't think the OS is likely to be relevant here.

    Going to try to break it down into subfolders and then try to boostrap the sync of each in turn to see if that avoids the issue

    Thanks

    David

  21. AudriusButkevicius commented on May 3, 2022

    @AudriusButkevicius
    Member

    You might also want to disable progress updates (check the docs how)

  22. calmh commented on May 4, 2022

    @calmh
    MemberAuthor

    @abs0 If you see an issue and can reproduce it, take a heap profile (https://docs.syncthing.net/users/profiling.html) and we can look at it. This issue, as originally described at least, is really about the case where we do an initial sync of a gazillion files, which doesn't sound like what you're doing.

  23. Avamander commented on May 29, 2022

    @Avamander

    I think I've stumbled upon this issue with Syncthing on ZFS scanning a lot of tiny files in one share (~600GB in total). There's about 6GB free memory when Syncthing isn't running on average, but Syncthing gets OOM killed. Probably because ARC cache (which Syncthing starts to inflate) isn't freed quickly enough? Maybe it would be possible to have a special mode to do things slower but avoid using a lot of memory that could lead to an OOM condition?

  24. calmh commented on May 29, 2022

    @calmh
    MemberAuthor

    Again, as noted above, a memory profile would help narrow down what you're suffering from.

  25. imsodin commented on May 6, 2023

    @imsodin
    Member

    @hohmd
    That's scanning not syncing, and there are knobs to reduce/remove that memory usage. However the issue tracker isn't the place for questions/support. Please use the forum - https://forum.syncthing.net/ - for support, first by searching for any similar issues and then by posting a new one when required. The forum has a lot more eyes and friendly people on it than the issue tracker.

    Thanks!

  26. nazar256 commented on Jun 28, 2023

    @nazar256

    I faced the same problem with large folders so the service was freezing at 700MiB docker container limit and abusing storage intensively. After upgrading the syncthing image to make sure it was compiled with go 1.19 I added GOMEMLIMIT environment variable and set desired soft memory limit. After that it works just fine without any noticeable performance impact. Feels like I can freely put much lower values like 128MiB, but I haven't fully tested that yet, since I'm totally fine with 512.
    I believe for older syncthing versions compiled with Go prior to 1.19 as a workaround you can experiment with values for GOGC. The lower (try 10-20) the less memory consumption will be but the more frequently garbage collector will be invoked which may hit the performance on too low values.

  27. added theissue type on May 30, 2025
  28. gaby commented on Apr 14, 2026

    @gaby

    We are running into this issue too. We have one syncthing container per NAS, and they start using 20-30GB of RAM each.

    Using latest v2 release as of today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions