Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Feb 2014 23:08:24 +0800
From:      Erich Dollansky <erichsfreebsdlist@alogt.com>
To:        Andre Albsmeier <mail@ma17.ata.myota.org>
Cc:        freebsd-hackers@freebsd.org, freebsd-threads@freebsd.org
Subject:   Re: pthread programming eats up resources (My or FreeBSD's fault?)
Message-ID:  <20140219230824.0f2ba24b@X220.alogt.com>
In-Reply-To: <20140219144728.GA3036@schlappy>
References:  <20140218180646.GA67861@schlappy> <20140219144728.GA3036@schlappy>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

as I understand your program, it creates 1000 threads, waits a but and
then starts again creating 1000 threads until something kill it.=20

=46rom my point of view, your program depends very much on the default
settings of the environment. If the environment allows the immediate
execution of the new thread, you will never get many threads.
Otherwise, the number of threads hanging around could add up. It also
depends on the number of CPUs/cores your system has.

But your are right, it should not crash on a modern machine but it
still could use some amount of memory.

Erich

On Wed,
19 Feb 2014 15:47:28 +0100 Andre Albsmeier <mail@ma17.ata.myota.org>
wrote:

> [Commenting my own mail from below and CC'ing freebsd-threads.]
>=20
> I have tested my code below on a Linux box (3.2.0) and here the
> behaviour is different and seems correct: While watching with
> top(1), VIRT climbs up to a few GB and collapses then to a few
> MB (somehow reminding me of some kind of garbage collection).
> Important thing is that RES always stays below 1MB.
>=20
> On FreeBSD SIZE and RES are about 100MB apart but both are
> increasing util 2GB and pth1 dies with=20
>=20
> Cannot map anonymous memory
> Out of memory
>=20
> So the question is: Is my programme buggy and Linux works around
> this bug or is there some kind of memory leak in the pthreads
> code in FreeBSD?
>=20
> 	-Andre
>=20
> ----- Forwarded message  -----
>=20
> Well, as these are my first steps regarding thread programming,
> it's probably my fault...
>=20
> Why does this programme slowly grow and grow until it hits
> resource limits?
>=20
> ----- snip pth1.c -----
>=20
> #include <stdio.h>
> #include <unistd.h>
> #include <pthread.h>
>=20
> void* mythread( void* arg )
> {
>   return NULL;
> }
>=20
> int main( int argc, const char* const argv[] )
> {
>   pthread_t pthr;
>   int i;
>=20
>   while( 1 ) {
>=20
>     for( i=3D1000; i; i-- )
>       if( pthread_create( &pthr, NULL, mythread, NULL ) !=3D 0 )
>         fprintf( stderr, "pthread_create\n" );
>       else
>         pthread_detach( pthr );
>=20
>     putchar( '.' );
>     fflush( stdout );
>     usleep( 25000 );
>   }
> }
>=20
> ----- snap -----
>=20
> Just to be sure I have also created the non-detaching version
> which behaves in the same way:
>=20
> ----- snip pth2.c -----
>=20
> #include <stdio.h>
> #include <unistd.h>
> #include <pthread.h>
>=20
> #define M 1000
>=20
> pthread_t pthr[M];
>=20
> void* mythread( void* arg )
> {
>   return NULL;
> }
>=20
> int main( int argc, const char* const argv[] )
> {
>   int i;
>=20
>   while( 1 ) {
>=20
>     for( i=3DM; i; i-- )
>       if( pthread_create( &pthr[i], NULL, mythread, NULL ) !=3D 0 )
>         fprintf( stderr, "pthread_create\n" );
>=20
>     for( i=3DM; i; i-- )
>       if( pthread_join( pthr[i], NULL ) !=3D 0 )
>         fprintf( stderr, "pthread_join\n" );
>=20
>     putchar( '.' );
>     fflush( stdout );
>     usleep( 25000 );
>   }
> }
>=20
> ----- snap -----
>=20
> Compile them using -pthread and watch their ps output in another
> window (FreeBSD-9.2 but that shouldn't matter).
>=20
> So what am I doing wrong here?
>=20
> Thanks,
>=20
> 	-Andre
> _______________________________________________
> freebsd-threads@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-threads
> To unsubscribe, send any mail to
> "freebsd-threads-unsubscribe@freebsd.org"




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140219230824.0f2ba24b>