git clone fails with "fatal: Out of memory, malloc failed" error

Using the fact that a Git repo is self-contained and can just be copied, we only need some way to make a copy and convert from bare to non-bare for our first few attempts.

Cloning is still the best form of initial copying, if that option is possible (see work-arounds 1 and 2). If not, we can just straight copy from the server, if access is available, and convert it manually (see #3). Failing that, perhaps copying/cloning a smaller piece of the repo will work?

Work-around 1 - clone onto USB from server

Get on the server (if you have access), clone to a usb drive folder (as a non-bare repo), stick USB into target machine, and just move/copy it to the final location you want the repo. Git command should be:

git clone /path/to/bare/repo /local/repo/folder

I think this will not copy any orphan commits (those that will eventually be garbage-collected).

Work-around 2 - Clone to another PC, then copy

Clone repo onto another PC, copy/move folder somehow to target PC (USB, network connection, etc.). Similar to #1

Work-around 3 - Copy from Server, convert from bare

This assumes access to the server, perhaps through a network share rather than directly (otherwise use #1). Copy the bare repo locally, then do something like this to convert it to a non-bare repo.

Work-around 4a - Clone only one branch at a time

It appears you can clone only a single branch at a time, which may fit under the limits causing your memory issue:

git clone [url/folder] -b master --single-branch [local repo folder]

Note you could create new branches on the server that are a bit back in the history, to pull less at once, but based on the answer in the above link, you may have to jump through some hoops to remove the "single-branchedness" of your new repo.

Work-around 4b - Shallow Repo

Use --depth <depth> and/or other shallow tags to make a local repo with a limited commit history depth. From git clone --help man page:

--depth Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass --shallow-submodules.

--shallow-since= Create a shallow clone with a history after the specified time.

--shallow-exclude= Create a shallow clone with a history, excluding commits reachable from a specified remote branch or tag. This option can be specified multiple times.

--[no-]single-branch Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at. Further fetches into the resulting repository will only update the remote-tracking branch for the branch this option was used for the initial cloning. If the HEAD at the remote did not point at any branch when --single-branch clone was made, no remote-tracking branch is created.

Work-around 4c - Clone/Checkout only some files at a time

See this answer and it's link for instructions on how to pull down just part of a repo at a time. Ugly, but could work to clone part at a time.


Also check ulimit. On AIX, I have (with no malloc failed issue):

> ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) unlimited
pipe size            (512 bytes, -p) 64
stack size              (kbytes, -s) unlimited
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited

A user having this malloc failed issue has:

> ulimit -a
core file size          (blocks, -c) 1048575
data seg size           (kbytes, -d) 131072
file size               (blocks, -f) 1048575
max memory size         (kbytes, -m) 32768
open files                      (-n) 2000
pipe size            (512 bytes, -p) 64
stack size              (kbytes, -s) 32768
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited

If you are on Linux and have root access, the simple workaround of adding virtual memory could be the easiest solution. On most Linux systems you can do the following (as root) to add 4GiB of virtual memory, and then try your git clone again after:

dd if=/dev/zero of=/var/swap.img bs=1M count=4096
chmod 0600 /var/swap.img
mkswap /var/swap.img
swapon /var/swap.img

This ended up being the simplest workable solution for me when I ran into this issue.