@matheusbsbs / matheus-bagliano / commits / 97bfeb34df
Fork 0

Release pack windows before reporting out of memory.

If we are about to fail because this process has run out of memory we should first try to automatically control our appetite for address space by releasing enough least-recently-used pack windows to gain back enough memory such that we might actually be able to meet the current allocation request. This should help users who have fairly large repositories but are working on systems with relatively small virtual address space. Many times we see reports on the mailing list of these users running out of memory during various Git operations. Dynamically decreasing the amount of pack memory used when the demand for heap memory is increasing is an intelligent solution to this problem. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Dec 24, 2006 at 00:47 UTC 97bfeb34df1aa8a1cf232278624a5a5c924ee380
2 files changed +39 -8
git-compat-util.h
+32 -8
@@ -123,11 +123,17 @@ extern char *gitstrcasestr(const char *haystack, const char *needle);
123 extern size_t gitstrlcpy(char *, const char *, size_t);
124 #endif
125
126 +extern void release_pack_memory(size_t);
127 +
128 static inline char* xstrdup(const char *str)
129 {
130 char *ret = strdup(str);
129 - if (!ret)
130 - die("Out of memory, strdup failed");
131 + if (!ret) {
132 + release_pack_memory(strlen(str) + 1);
133 + ret = strdup(str);
134 + if (!ret)
135 + die("Out of memory, strdup failed");
136 + }
137 return ret;
138 }
139
@@ -136,8 +142,14 @@ static inline void *xmalloc(size_t size)
142 void *ret = malloc(size);
143 if (!ret && !size)
144 ret = malloc(1);
139 - if (!ret)
140 - die("Out of memory, malloc failed");
145 + if (!ret) {
146 + release_pack_memory(size);
147 + ret = malloc(size);
148 + if (!ret && !size)
149 + ret = malloc(1);
150 + if (!ret)
151 + die("Out of memory, malloc failed");
152 + }
153 #ifdef XMALLOC_POISON
154 memset(ret, 0xA5, size);
155 #endif
@@ -149,8 +161,14 @@ static inline void *xrealloc(void *ptr, size_t size)
161 void *ret = realloc(ptr, size);
162 if (!ret && !size)
163 ret = realloc(ptr, 1);
152 - if (!ret)
153 - die("Out of memory, realloc failed");
164 + if (!ret) {
165 + release_pack_memory(size);
166 + ret = realloc(ptr, size);
167 + if (!ret && !size)
168 + ret = realloc(ptr, 1);
169 + if (!ret)
170 + die("Out of memory, realloc failed");
171 + }
172 return ret;
173 }
174
@@ -159,8 +177,14 @@ static inline void *xcalloc(size_t nmemb, size_t size)
177 void *ret = calloc(nmemb, size);
178 if (!ret && (!nmemb || !size))
179 ret = calloc(1, 1);
162 - if (!ret)
163 - die("Out of memory, calloc failed");
180 + if (!ret) {
181 + release_pack_memory(nmemb * size);
182 + ret = calloc(nmemb, size);
183 + if (!ret && (!nmemb || !size))
184 + ret = calloc(1, 1);
185 + if (!ret)
186 + die("Out of memory, calloc failed");
187 + }
188 return ret;
189 }
190
sha1_file.c
+7
@@ -522,6 +522,13 @@ static int unuse_one_window(struct packed_git *current)
522 return 0;
523 }
524
525 +void release_pack_memory(size_t need)
526 +{
527 + size_t cur = pack_mapped;
528 + while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
529 + ; /* nothing */
530 +}
531 +
532 void unuse_pack(struct pack_window **w_cursor)
533 {
534 struct pack_window *w = *w_cursor;