This is the fix for the dreaded memory leak, which isn't a memory leak
at all (which kind of explains why it was so hard to find when I was
looking for leaks).
A clue to the problem is that you can make the leak go away by setting
`gc-cons-percentage' to zero.
I instrumented recompute_need_to_garbage_collect() in gc.c to report the
total memory usage at GC and the percentage used whenever the
percentage-based counting should kick in. It kicked in when I'd
expected: with the default setting for gc-cons-percentage of 40 and a
gc-cons-threshold of 40000000, when the total memory consumed
approximated 70Mb. It soon became obvious that something was very wrong:
total gc usage: 56471356; GC percentage used: -5
[...]
total gc usage: 56471356; GC percentage used: -1
total gc usage: 56471356; GC percentage used: 0
total gc usage: 56471356; GC percentage used: 1
[...]
(consing-since-gc was rising all along, at about 75000000 at this
point, but I wasn't printing it out in this debugging dump.)
The problem is numeric overflow in the percentage-comparison code:
(!total_gc_usage_set ||
(100 * consing_since_gc) / total_gc_usage >=
gc_cons_percentage)
Obviously if consing-since-gc is bigger than about 20Mb (since this is a
*signed* integer) we're pretty much dead; each time around, we allow the
heap to bloat more and more, and GC less and less often: heap
fragmentation just makes things worse, because memory's vanishingly
rarely going to get given back to the OS as long as GCs get less and
less frequent like this.
(This also explains why this leak takes some time to kick in: XEmacs has
to load enough to be governed by gc-cons-percentage rather than
gc-cons-threshold in the first place, particularly if you've got it set
as high as I have. It turns up less often when not using X because not
loading the X stuff means that the total_gc_usage takes longer to reach
that threshold.)
2006-12-29 Nix <nix(a)esperi.org.uk>
* gc.c (recompute_need_to_garbage_collect): Avoid numeric
overflow in percentage calculation.
Index: 21.5/src/gc.c
===================================================================
--- 21.5.orig/src/gc.c 2006-12-29 18:21:43.000000000 +0000
+++ 21.5/src/gc.c 2006-12-29 22:11:22.000000000 +0000
@@ -314,12 +314,12 @@
(consing_since_gc > gc_cons_threshold
&&
#if 0 /* #### implement this better */
- (100 * consing_since_gc) / total_data_usage () >=
- gc_cons_percentage
+ ((double)consing_since_gc) / total_data_usage()) >=
+ ((double)gc_cons_percentage / 100)
#else
(!total_gc_usage_set ||
- (100 * consing_since_gc) / total_gc_usage >=
- gc_cons_percentage)
+ ((double)consing_since_gc / total_gc_usage) >=
+ ((double)gc_cons_percentage / 100))
#endif
);
recompute_funcall_allocation_flag ();
_______________________________________________
XEmacs-Patches mailing list
XEmacs-Patches(a)xemacs.org
http://calypso.tux.org/cgi-bin/mailman/listinfo/xemacs-patches