Skip to content

Adding large files exhausts memory #1851

Closed

Description

@johnepinterest

This line in the lfs/pointer_clean.go copyToTemp function causes the entire file to be read in to memory:

by, rerr := ioutil.ReadAll(buf)

For large files this can cause the process to consume an enormous amount of memory. In fact, on my particular system (Mac OS X), adding half a dozen 4-6 GB files caused it to run out of memory because it couldn't add more swap space because it had filled up the disk (the disk was quite full already).

I ended up having to git add files one at a time. It seems like adding multiple files at once compounds the problem.

This really needs to be re-written so that it doesn't require the entire file to be in memory. From my simple reading of the code, I can't see any reason why it needs to do this, and if it truly does, mmap()ing the file is probably a much better idea.

Activity

  1. ttaylorr commented on Jan 11, 2017

    @ttaylorr
    Contributor

    Hi @johnepinterest, thanks for opening this. What you're pointing out is my fault, and definitely a regression. Sorry about that.

    The reason that we read from the buffer is to check if the contents of the file is already an LFS pointer, which we should write back to Git directly if asking to be cleaned during a check-in. Prior to LFS v1.5.4, the amount of data that was buffered was limited at blobSizeCutoff, which is 512 bytes (see: lfs/pointer.go at v1.5.3).

    Behavior was added in v1.5.4 to enable LFS to stream the entire contents of a malformed pointer during checkouts. However, it looks like I forgot to cap the amount of data read from the buf, as you pointed out in the line above. Luckily, this only has measurable impact against large malformed pointers. Going forward, I think the following patch can safely be applied, and should be released as v1.5.5.

    diff --git a/lfs/pointer_clean.go b/lfs/pointer_clean.go
    index 83cc725e..4928c444 100644
    --- a/lfs/pointer_clean.go
    +++ b/lfs/pointer_clean.go
    @@ -5,7 +5,6 @@ import (
     	"crypto/sha256"
     	"encoding/hex"
     	"io"
    -	"io/ioutil"
     	"os"
     
     	"github.com/git-lfs/git-lfs/config"
    @@ -79,7 +78,11 @@ func copyToTemp(reader io.Reader, fileSize int64, cb progress.CopyCallback) (oid
     	}
     
     	ptr, buf, err := DecodeFrom(reader)
    -	by, rerr := ioutil.ReadAll(buf)
    +
    +	by := make([]byte, blobSizeCutoff)
    +	n, rerr := buf.Read(by)
    +	by = by[:n]
    +
     	if rerr != nil || (err == nil && len(by) < 512) {
     		err = errors.NewCleanPointerError(ptr, by)
     		return
  2. ttaylorr commented on Jan 12, 2017

    @ttaylorr
    Contributor

    I went ahead and applied the above patch in #1852, which is open for review.

  3. ttaylorr commented on Jan 12, 2017

    @ttaylorr
    Contributor

    Git LFS v1.5.5 has been released which resolves this issue. Thanks again!

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions