Red hat 6.1 contains memory leak under pthread?

xu sun <[email protected]> Thu, 12 Jan 2012 10:26:34 +0800
Newsgroups gmane.linux.redhat.devel
Message-ID <CAGt8XdBf06RHy5xDFsNsSABRYf=FcHs+V2fUyYR2QU+cZanYYA@mail.gmail.com>
--===============6446385465192439347==
Content-Type: multipart/alternative; boundary=e89a8f6438c221328304b64b7a6a

--e89a8f6438c221328304b64b7a6a
Content-Type: text/plain; charset=ISO-8859-1

hi all.

Just see the following code I provide:

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

void * f(void * arg)
{
     /*
      * Only request 8 bytes. Call free() or not as your
      * will.
      */
     malloc(8);

     /* Prevent the thread exit(). */
     while (1)
        sleep(10000);
}

int main()
{
     pthread_t  id;
     int                i = 0;

     /*
      * Type ENTER key to create one thread. Just
      * watch `top' command to see VIRT column's value
      * change.
      */
     getchar();

     while (1)
     {
        printf ("create %d threads\n", ++i);
        pthread_create(&id, NULL, f, NULL);
        getchar();
     }
     return 0;
}

Complie it: gcc a.c -lpthread -Wall
Run ./a.out, and open `top' command to watch VIRT of a.out.
I saw some difference between RH 6.1 and RH 5.4. its behaviors are
different.

1)
CPU: x86_64
OS kernel: Linux  2.6.32-131.0.15.el6.x86_64
OS release: Red Hat Enterprise Linux Server release 6.1 (Santiago)
gcc: gcc (GCC) 4.4.5 20110214 (Red Hat 4.4.5-6)

a) Run ./a.out
b) Every type ENTER key and one thread will be created, the VIRT of a.out
will increase about 70MB
c) When I create 16 threads, the VIRT of a.out has already be up to 1190MB
d) But when the number of created thread is up to about 24 threads, and
again create thread via typing
    ENTER key, the increase of VIRT of a.out is 10MB.

2)
CPU: x86_64
OS kernel: Linux localhost.localdomain 2.6.18-164.el5
OS release: Red Hat Enterprise Linux Server release 5.4 (Tikanga)
gcc: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)

Do the same step as above. But the increase of  the VIRT of a.out is 10MB
but not 70MB at any step.

3)
CPU: x86_32
OS kernel: Linux  2.6.32-131.0.15.el6.i686
OS release: Red Hat Enterprise Linux Server release 6.1 (Santiago)
gcc: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)

Do the same step as above. But the increase of  the VIRT of a.out is 10MB
but not 70MB at any step.

SO, why the behavior is different. And I think, if the thread only request
8 bytes memory space, the OS
should not give him 70MB. If some applicate need create a thread pool
contain 16 threads under x86_64
with RH6.1, and any request has not processed, the virtual memory has
already been used 1GB. In fact,
under x86_64 with RH5.4 only 300MB virtual memory is enough at the
beginning of the application running.

So any configuration should be done to prevent a thread use much more
virtual memory(10MB, but not 70MB).

--e89a8f6438c221328304b64b7a6a
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

hi all.<br><br>Just see the following code I provide:<br><br>#include &lt;s=
tdlib.h&gt;<br>#include &lt;pthread.h&gt;<br>#include &lt;unistd.h&gt;<br>#=
include &lt;stdio.h&gt;<br><br>void * f(void * arg)<br>{<br>=A0=A0=A0=A0 /*=
<br>=A0=A0=A0=A0=A0 * Only request 8 bytes. Call free() or not as your<br>
=A0=A0=A0=A0=A0 * will.<br>=A0=A0=A0=A0=A0 */<br>=A0=A0=A0=A0 malloc(8);<br=
><br>=A0=A0=A0=A0 /* Prevent the thread exit(). */<br>=A0=A0=A0=A0 while (1=
)<br>=A0=A0=A0=A0=A0=A0=A0 sleep(10000);<br>}<br><br>int main()<br>{<br>=A0=
=A0=A0=A0 pthread_t=A0 id;<br>=A0=A0=A0=A0 int=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0 i =3D 0;<br>
<br>=A0=A0=A0=A0 /*<br>=A0=A0=A0=A0=A0 * Type ENTER key to create one threa=
d. Just<br>=A0=A0=A0=A0=A0 * watch `top&#39; command to see VIRT column&#39=
;s value<br>=A0=A0=A0=A0=A0 * change.<br>=A0=A0=A0=A0=A0 */<br>=A0=A0=A0=A0=
 getchar();<br><br>=A0=A0=A0=A0 while (1)<br>=A0=A0=A0=A0 {<br>=A0=A0=A0=A0=
=A0=A0=A0 printf (&quot;create %d threads\n&quot;, ++i);<br>
=A0=A0=A0=A0=A0=A0=A0 pthread_create(&amp;id, NULL, f, NULL);<br>=A0=A0=A0=
=A0=A0=A0=A0 getchar();<br>=A0=A0=A0=A0 }<br>=A0=A0=A0=A0 return 0;<br>}<br=
><br>Complie it: gcc a.c -lpthread -Wall<br>Run ./a.out, and open `top&#39;=
 command to watch VIRT of a.out. <br>I saw some difference between RH 6.1 a=
nd RH 5.4. its behaviors are different.<br>
<br>1)<br>CPU: x86_64<br>OS kernel: Linux=A0 2.6.32-131.0.15.el6.x86_64<br>=
OS release: Red Hat Enterprise Linux Server release 6.1 (Santiago)<br>gcc: =
gcc (GCC) 4.4.5 20110214 (Red Hat 4.4.5-6)<br><br>a) Run ./a.out<br>b) Ever=
y type ENTER  key and one thread will be created, the VIRT of a.out will in=
crease about 70MB<br>
c) When I create 16 threads, the VIRT of a.out has already be up to 1190MB<=
br>d) But when the number of created thread is up to about 24 threads, and =
again create thread via typing<br>=A0=A0=A0 ENTER key, the increase of VIRT=
 of a.out is 10MB.<br>
<br>2)<br>CPU: x86_64<br>
OS kernel: Linux localhost.localdomain 2.6.18-164.el5<br>
OS release: Red Hat Enterprise Linux Server release 5.4 (Tikanga)<br>
gcc: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)<br><br>Do the same step as=
 above. But the increase of=A0 the VIRT of a.out is 10MB but not 70MB at an=
y step.<br>
<br>3)<br>
CPU: x86_32<br>

OS kernel: Linux=A0 2.6.32-131.0.15.el6.i686<br>

OS release: Red Hat Enterprise Linux Server release 6.1 (Santiago)<br>

gcc: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)<br>
<br>Do the same step as above. But the increase of=A0 the VIRT of a.out is =
10MB but not 70MB at any step.<br><br>SO, why the behavior is different. An=
d I think, if the thread only request 8 bytes memory space, the OS<br>shoul=
d not give him 70MB. If some applicate need create a thread pool contain 16=
 threads under x86_64<br>
with RH6.1, and any request has not processed, the virtual memory has alrea=
dy been used 1GB. In fact,<br>under x86_64 with RH5.4 only 300MB virtual me=
mory is enough at the beginning of the application running.<br><br>So any c=
onfiguration should be done to prevent a thread use much more virtual memor=
y(10MB, but not 70MB).<br>

--e89a8f6438c221328304b64b7a6a--


--===============6446385465192439347==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Redhat-devel-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/redhat-devel-list
--===============6446385465192439347==--