Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Feb 2014 15:47:28 +0100
From:      Andre Albsmeier <mail@ma17.ata.myota.org>
To:        freebsd-hackers@freebsd.org, freebsd-threads@freebsd.org
Subject:   Re: pthread programming eats up resources (My or FreeBSD's fault?)
Message-ID:  <20140219144728.GA3036@schlappy>
In-Reply-To: <20140218180646.GA67861@schlappy>
References:  <20140218180646.GA67861@schlappy>

next in thread | previous in thread | raw e-mail | index | archive | help
[Commenting my own mail from below and CC'ing freebsd-threads.]

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.

On FreeBSD SIZE and RES are about 100MB apart but both are
increasing util 2GB and pth1 dies with 

Cannot map anonymous memory
Out of memory

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?

	-Andre

----- Forwarded message  -----

Well, as these are my first steps regarding thread programming,
it's probably my fault...

Why does this programme slowly grow and grow until it hits
resource limits?

----- snip pth1.c -----

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void* mythread( void* arg )
{
  return NULL;
}

int main( int argc, const char* const argv[] )
{
  pthread_t pthr;
  int i;

  while( 1 ) {

    for( i=1000; i; i-- )
      if( pthread_create( &pthr, NULL, mythread, NULL ) != 0 )
        fprintf( stderr, "pthread_create\n" );
      else
        pthread_detach( pthr );

    putchar( '.' );
    fflush( stdout );
    usleep( 25000 );
  }
}

----- snap -----

Just to be sure I have also created the non-detaching version
which behaves in the same way:

----- snip pth2.c -----

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define M 1000

pthread_t pthr[M];

void* mythread( void* arg )
{
  return NULL;
}

int main( int argc, const char* const argv[] )
{
  int i;

  while( 1 ) {

    for( i=M; i; i-- )
      if( pthread_create( &pthr[i], NULL, mythread, NULL ) != 0 )
        fprintf( stderr, "pthread_create\n" );

    for( i=M; i; i-- )
      if( pthread_join( pthr[i], NULL ) != 0 )
        fprintf( stderr, "pthread_join\n" );

    putchar( '.' );
    fflush( stdout );
    usleep( 25000 );
  }
}

----- snap -----

Compile them using -pthread and watch their ps output in another
window (FreeBSD-9.2 but that shouldn't matter).

So what am I doing wrong here?

Thanks,

	-Andre



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