[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug #34140] gc not run correctly when allocating threads
From: |
Andy Wingo |
Subject: |
Re: [bug #34140] gc not run correctly when allocating threads |
Date: |
Sat, 03 Sep 2011 13:56:10 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) |
Hi,
On Fri 26 Aug 2011 10:49, Stefan Israelsson Tampe <address@hidden> writes:
> On my machine this crashes after about 4000 iterations due to the oom killer.
> The reason is mainly that stack space is not reclaimed.
>
> (define (f n)
> (let ((t (call-with-new-thread (lambda () 'ok))))
> (join-thread t))
> (if (= (modulo n 30) 0)
> (begin
> (pk n)
> (pk (gc-stats))
> (sleep 1)))
> (f (+ n 1)))
>
> (f 0)
Why is this, I wonder? (Also, how much memory do you have, and what is
your architecture? On my system each thread will allocate 512 KB by
default for a stack; if these thread stacks are not being collected,
4000 of them is 2 GB, which could hit the OOM on many systems.)
Anyway I could not reproduce it with the attached C program. But the C
program isn't quite like what we do with threads, either; I suppose we
could try approximating the C program to the Scheme program. We should
not have to introduce calls to `gc'; if the collector isn't collecting,
that's a problem!
Andy
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define GC_THREADS 1
#define GC_REDIRECT_TO_LOCAL 1
#define GC_NO_THREAD_REDIRECTS 1
#include <gc/gc.h>
#define ALLOC_SIZE (64 * 1024 * sizeof(void*))
static void
fail (char *reason)
{
perror (reason);
exit (1);
}
static void*
thr_func (void* arg)
{
void *mem = GC_malloc (ALLOC_SIZE);
if (!mem)
fail ("malloc failed");
memset (mem, 1, ALLOC_SIZE);
return NULL;
}
int main (int argc, char *argv[])
{
GC_INIT ();
int i, err, limit;
void *res = NULL;
limit = (argc == 2 ? atoi (argv[1]) : 10000);
for (i = 0; i < limit; i++)
{
pthread_t thr;
printf ("Spawning %d\n", i);
if ((err = GC_pthread_create (&thr, NULL, thr_func, NULL)))
fail ("pthread_create");
if ((err = GC_pthread_join (thr, &res)))
fail ("pthread_join");
}
printf ("Success.\n");
return 0;
}
--
http://wingolog.org/