git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [patch] git: fix memory leak #2 in read-cache.c

From
Linus Torvalds <torvalds@osdl.org>
Date
Apr 14, 2005, 15:11 UTC
Message-ID
<Pine.LNX.4.58.0504140804480.7211@ppc970.osdl.org>
In-Reply-To
<20050414123934.GA15420@elte.hu>
On Thu, 14 Apr 2005, Ingo Molnar wrote:
> 
> this patch fixes a memory leak in read-cache.c: when there's cache entry 
> collision we should free the previous one.

As you already noticed "read_cache()" normally just populates the active-cache with pointers to the mmap'ed "active" file.

Whether that is a good idea or not, I do not know. But I do know that the active file is the single biggest file we work with (ie 1.6MB in size), so since _most_ tools just read it and modify a very small number of entries, it seemed like a good idea.

In other words, if the common case is that we update a couple of entries in the active cache, we actually saved 1.6MB (+ malloc overhead for the 17 _thousand_ allocations) by my approach.

And the leak? There's none. We never actually update an existing entry that was allocated with malloc(), unless the user does something stupid. In other words, the only case where there is a "leak" is when the user does something like

	update-cache file file file file file file .. 
with the same file listed several times.

And dammit, the whole point of doing stuff in user space is that the kernel takes care of business. Unlike kernel work, leaking is ok. You just have to make sure that it is limited enough to to not be a problem. I'm saying that in this case we're _better_ off leaking, because the mmap() trick saves us more memory than the leak can ever leak.

(The command line is limited to 128kB or so, which means that the most files you _can_ add with a single update-cache is _less_ than the mmap win).

It was _such_ a relief to program in user mode for a change. Not having to care about the small stuff is wonderful.

		Linus
Previous: Martin SchlemmerNext: Ingo Molnar
Message 20 of 22 in “git: fix memory leak in checkout-cache.c”
  1. git: fix memory leak in checkout-cache.cIngo Molnar, Apr 14, 2005
  2. git: fix memory leak #2 in checkout-cache.cIngo Molnar, Apr 14, 2005
  3. git: cleanup in ls-tree.cIngo Molnar, Apr 14, 2005
  4. git: fix memory leaks in read-tree.cIngo Molnar, Apr 14, 2005
  5. git: fix rare memory leak in rev-tree.cIngo Molnar, Apr 14, 2005
  6. git: report parse_commit() errors in rev-tree.cIngo Molnar, Apr 14, 2005
  7. git: fix memory leak in show-diff.cIngo Molnar, Apr 14, 2005
  8. git: fix overflow in update-cache.cIngo Molnar, Apr 14, 2005
  9. cleanup: read_sha1_file() -> malloc_read_sha1_file()Ingo Molnar, Apr 14, 2005
  10. git: fix 1-byte overflow in show-files.cIngo Molnar, Apr 14, 2005
  11. Petr BaudisApr 17, 2005
  12. Ingo MolnarApr 18, 2005
  13. git: fix memory leaks in update-cache.cIngo Molnar, Apr 14, 2005
  14. git: clean up add_file_to_cache() in update-cache.cIngo Molnar, Apr 14, 2005
  15. git: fix memory leak #3 in update-cache.cIngo Molnar, Apr 14, 2005
  16. git: fix memory leaks in read-cache.cIngo Molnar, Apr 14, 2005
  17. git: fix memory leak #2 in read-cache.cIngo Molnar, Apr 14, 2005
  18. Ingo MolnarApr 14, 2005
  19. Martin SchlemmerApr 14, 2005
  20. Linus TorvaldsApr 14, 2005
  21. Ingo MolnarApr 14, 2005
  22. git: fix memory leak in write-tree.cIngo Molnar, Apr 14, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.