# Huge possible memory leak while cherry-picking.

9 messages from 2013-08-09 to 2013-08-13. Participants: Лежанкин Иван, Felipe Contreras, René Scharfe, Junio C Hamano.
Thread: https://gitlist.dev/t/34652

## Лежанкин Иван, 2013-08-09 12:13

Subject: Huge possible memory leak while cherry-picking.
Message-ID: <CAJc7LbpRuqug9pLFVVg=XMvJ9u_P0ZVSy2MVBDaCVkjvfKnfJw@mail.gmail.com>
URL: https://gitlist.dev/e/CAJc7LbpRuqug9pLFVVg%3DXMvJ9u_P0ZVSy2MVBDaCVkjvfKnfJw%40mail.gmail.com

```
Hi,

I have tried to cherry-pick a range of ~200 commits from one branch to
another. And you can't imagine how I was surprised when the git
process ate 8 Gb of RAM and died - before cherry-picking was complete.

I downloaded git sources from master and built it with gperftools
support (-ltcmalloc). After running `git cherry-pick <some hash>` with
a heap-leak checker enabled I got this:

> Have memory regions w/o callers: might report false leaks
> Leak check _main_ detected leaks of 42838782 bytes in 257986 objects

These objects are allocated at

> read-cache.c:1340: struct cache_entry *ce = xmalloc(cache_entry_size(len));

After looking in the code, I found a comment in the function `static
void remove_dir_entry(...)`:

/*
 * Release reference to the directory entry (and parents if 0).
 *
 * Note: we do not remove / free the entry because there's no
 * hash.[ch]::remove_hash and dir->next may point to other entries
 * that are still valid, so we must not free the memory.
 */

So, this objects are never freed - by design?
Is it a real issue, or do I just misunderstand something?

```

## Felipe Contreras, 2013-08-09 20:39

Subject: Re: Huge possible memory leak while cherry-picking.
Message-ID: <CAMP44s282DD+tQUgVHawdRDJayjTxMjOu_R3robbCVhkbksEtQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s282DD%2BtQUgVHawdRDJayjTxMjOu_R3robbCVhkbksEtQ%40mail.gmail.com
In-Reply-To: <CAJc7LbpRuqug9pLFVVg=XMvJ9u_P0ZVSy2MVBDaCVkjvfKnfJw@mail.gmail.com>

```
On Fri, Aug 9, 2013 at 7:13 AM, Лежанкин Иван <abyss.7@gmail.com> wrote:
> I have tried to cherry-pick a range of ~200 commits from one branch to
> another. And you can't imagine how I was surprised when the git
> process ate 8 Gb of RAM and died - before cherry-picking was complete.

Try this:
http://article.gmane.org/gmane.comp.version-control.git/226757

-- 
Felipe Contreras

```

## Лежанкин Иван, 2013-08-12 10:04

Subject: Re: Huge possible memory leak while cherry-picking.
Message-ID: <CAJc7Lbrmsna4u4s+fdCGZ7jn9HzgZkinL3tbjbjcuw40Of5umg@mail.gmail.com>
URL: https://gitlist.dev/e/CAJc7Lbrmsna4u4s%2BfdCGZ7jn9HzgZkinL3tbjbjcuw40Of5umg%40mail.gmail.com
In-Reply-To: <CAMP44s282DD+tQUgVHawdRDJayjTxMjOu_R3robbCVhkbksEtQ@mail.gmail.com>

```
Thank you, it works very well!
Will this patch go to upstream?

Also, there is still some unexpected memory consumption - about 2Gb
per ~200 commits, but it's bearable.
I will do a futher investigation.

Felipe, should I exclude you from my futher reports on possible memory leaks?

On 10 August 2013 00:39, Felipe Contreras <felipe.contreras@gmail.com> wrote:
> On Fri, Aug 9, 2013 at 7:13 AM, Лежанкин Иван <abyss.7@gmail.com> wrote:
>> I have tried to cherry-pick a range of ~200 commits from one branch to
>> another. And you can't imagine how I was surprised when the git
>> process ate 8 Gb of RAM and died - before cherry-picking was complete.
>
> Try this:
> http://article.gmane.org/gmane.comp.version-control.git/226757
>
> --
> Felipe Contreras

```

## Felipe Contreras, 2013-08-12 10:09

Subject: Re: Huge possible memory leak while cherry-picking.
Message-ID: <CAMP44s1CAMPWXDSAc7WHahmrKRrB8aG_H9fnXAMi2LFOGy5EdA@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s1CAMPWXDSAc7WHahmrKRrB8aG_H9fnXAMi2LFOGy5EdA%40mail.gmail.com
In-Reply-To: <CAJc7Lbrmsna4u4s+fdCGZ7jn9HzgZkinL3tbjbjcuw40Of5umg@mail.gmail.com>

```
On Mon, Aug 12, 2013 at 5:04 AM, Лежанкин Иван <abyss.7@gmail.com> wrote:
> Thank you, it works very well!
> Will this patch go to upstream?

Ask Junio.

> Also, there is still some unexpected memory consumption - about 2Gb
> per ~200 commits, but it's bearable.
> I will do a futher investigation.

Can you post some valgrind log? Or even better, a way to reproduce?

> Felipe, should I exclude you from my futher reports on possible memory leaks?

Exclude me?

-- 
Felipe Contreras

```

## Felipe Contreras, 2013-08-12 10:34

Subject: Re: Huge possible memory leak while cherry-picking.
Message-ID: <CAMP44s3SMwQseKbQ8dvYKK41WCWwejRhTqBmJQPCJP2XNG1mLQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s3SMwQseKbQ8dvYKK41WCWwejRhTqBmJQPCJP2XNG1mLQ%40mail.gmail.com
In-Reply-To: <CAJc7Lbrmsna4u4s+fdCGZ7jn9HzgZkinL3tbjbjcuw40Of5umg@mail.gmail.com>

```
On Mon, Aug 12, 2013 at 5:04 AM, Лежанкин Иван <abyss.7@gmail.com> wrote:
> Thank you, it works very well!
> Will this patch go to upstream?

Ask Junio.

> Also, there is still some unexpected memory consumption - about 2Gb
> per ~200 commits, but it's bearable.
> I will do a futher investigation.

Can you post some valgrind log? Or even better, a way to reproduce?

> Felipe, should I exclude you from my futher reports on possible memory leaks?

Exclude me?

-- 
Felipe Contreras

```

## René Scharfe, 2013-08-13 18:27

Subject: [PATCH] unpack-trees: plug a memory leak
Message-ID: <520A7AAE.6010309@web.de>
URL: https://gitlist.dev/e/520A7AAE.6010309%40web.de
In-Reply-To: <CAMP44s1CAMPWXDSAc7WHahmrKRrB8aG_H9fnXAMi2LFOGy5EdA@mail.gmail.com>

```
From: Felipe Contreras <felipe.contreras@gmail.com>

Before overwriting the destination index, first let's discard its
contents.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
Tested-by: Лежанкин Иван <abyss.7@gmail.com> wrote:
---
Felipe sent this patch as part of multiple series in June, but it can
stand on its own.  This version is trivially rebased against master.
The leak seems to have been introduced by 34110cd4 (2008-03-06,
"Make 'unpack_trees()' have a separate source and destination index").

 unpack-trees.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/unpack-trees.c b/unpack-trees.c
index bf01717..1a61e6f 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1154,8 +1154,10 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
 
 	o->src_index = NULL;
 	ret = check_updates(o) ? (-2) : 0;
-	if (o->dst_index)
+	if (o->dst_index) {
+		discard_index(o->dst_index);
 		*o->dst_index = o->result;
+	}
 
 done:
 	clear_exclude_list(&el);
-- 
1.8.3.3

```

## Junio C Hamano, 2013-08-13 21:12

Subject: Re: [PATCH] unpack-trees: plug a memory leak
Message-ID: <7va9klwb03.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7va9klwb03.fsf%40alter.siamese.dyndns.org
In-Reply-To: <520A7AAE.6010309@web.de>

```
René Scharfe <l.s.r@web.de> writes:

> From: Felipe Contreras <felipe.contreras@gmail.com>
>
> Before overwriting the destination index, first let's discard its
> contents.
>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> Tested-by: Лежанкин Иван <abyss.7@gmail.com> wrote:
> ---
> Felipe sent this patch as part of multiple series in June, but it can
> stand on its own.  This version is trivially rebased against master.
> The leak seems to have been introduced by 34110cd4 (2008-03-06,
> "Make 'unpack_trees()' have a separate source and destination index").

It was lost in the follow-up discussion and I missed it.

I assume that this is signed-off by you as a forwarder?  I'd prefer
to even mark it Reviewed-by: you.

Thanks.

>  unpack-trees.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/unpack-trees.c b/unpack-trees.c
> index bf01717..1a61e6f 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -1154,8 +1154,10 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
>  
>  	o->src_index = NULL;
>  	ret = check_updates(o) ? (-2) : 0;
> -	if (o->dst_index)
> +	if (o->dst_index) {
> +		discard_index(o->dst_index);
>  		*o->dst_index = o->result;
> +	}
>  
>  done:
>  	clear_exclude_list(&el);

```

## René Scharfe, 2013-08-13 21:32

Subject: Re: [PATCH] unpack-trees: plug a memory leak
Message-ID: <520AA5FE.1090208@web.de>
URL: https://gitlist.dev/e/520AA5FE.1090208%40web.de
In-Reply-To: <7va9klwb03.fsf@alter.siamese.dyndns.org>

```
Am 13.08.2013 23:12, schrieb Junio C Hamano:
> René Scharfe <l.s.r@web.de> writes:
>
>> From: Felipe Contreras <felipe.contreras@gmail.com>
>>
>> Before overwriting the destination index, first let's discard its
>> contents.
>>
>> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
>> Tested-by: Лежанкин Иван <abyss.7@gmail.com> wrote:
>> ---
>> Felipe sent this patch as part of multiple series in June, but it can
>> stand on its own.  This version is trivially rebased against master.
>> The leak seems to have been introduced by 34110cd4 (2008-03-06,
>> "Make 'unpack_trees()' have a separate source and destination index").
>
> It was lost in the follow-up discussion and I missed it.

I had forgotten about it as well, until Felipe mentioned it again.

> I assume that this is signed-off by you as a forwarder?  I'd prefer
> to even mark it Reviewed-by: you.

Right, I did review the patch and you can tag it as such.

Thanks,
René

```

## Junio C Hamano, 2013-08-13 21:50

Subject: Re: [PATCH] unpack-trees: plug a memory leak
Message-ID: <7v1u5xw99t.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v1u5xw99t.fsf%40alter.siamese.dyndns.org
In-Reply-To: <520AA5FE.1090208@web.de>

```
René Scharfe <l.s.r@web.de> writes:

>> It was lost in the follow-up discussion and I missed it.
>
> I had forgotten about it as well, until Felipe mentioned it again.
>
>> I assume that this is signed-off by you as a forwarder?  I'd prefer
>> to even mark it Reviewed-by: you.
>
> Right, I did review the patch and you can tag it as such.

OK, will queue and soon merge to and cook in 'next'.

Thanks, everybody.

```
