Re: FREE touble

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On Tue, 7 Aug 2007 johny@xxxxxxxxxxxxxx wrote:

> hi, all
> 
> i want to ask, why free function did not do properly as it expected
> (happened in test_malloc.c), the usage memory (using malloc) still exist

  free() actually resets the allocation pointer internally so that
  the "freed" memory can be used in further memory allocations.
  It is not essential that the freed memory be released from the program
  being executed. Hence size of the program in execution should not be
  deciding parameter.

> please help me, i have another program it, doing malloc continously, and
> free it when not needed, this happened several times,
> after some looping, my memory become 0 free, and it got hang.i did some
> investigation, and found there is something strange in free.

  I dont think I will conclude free() is acting strange with "pmap" 
  observations alone. 

>
> anyone got idea what happen and give me a solution?
>

  Btwn a point of suggestion -

  As I understand it, you are saying that

   * Initially memory is allocated to the 1000000 double pointers of 
     type ** _dodol using calloc

   * By means of a loop, you are assigning to each * _dodol a memory
     of size _dodol

   * Only after the above 2 steps are succesfull, free() is tried

  IMHO I would say this to be a rather inefficient parctice particularly 
  when memory as high as 4M is required ! Try to create memory in chunks 
  and free them when required. realloc might become handy.	 
 
> 
> thanks in advance
> Johny Jugianto
> 
> using gcc:
> $ gcc --version
> gcc (GCC) 4.2.0
> 
> here is my machine info
> $ uname -a
> Linux localhost.svr 2.6.9-55.ELsmp #1 SMP Wed May 2 14:28:44 EDT 2007 i686
> athlon i386 GNU/Linux
> 
> 
> -RESULT: pmap for test_malloc.c after ready to insert-----------------
> [nyto@nyto ~]$ pmap 5836
> 5836:   ./test_malloc
> 007ad000     88K r-x--  /lib/ld-2.3.4.so
> 007c3000      4K r----  /lib/ld-2.3.4.so
> 007c4000      4K rw---  /lib/ld-2.3.4.so
> 007c7000   1176K r-x--  /lib/tls/libc-2.3.4.so
> 008ed000      8K r----  /lib/tls/libc-2.3.4.so
> 008ef000      8K rw---  /lib/tls/libc-2.3.4.so
> 008f1000      8K rw---    [ anon ]
> 08048000      4K r-x--  /home/nyto/test_malloc
> 08049000      4K rw---  /home/nyto/test_malloc
> 08dcb000    132K rw---    [ anon ]
> b7ba2000   3920K rw---    [ anon ]
> bfff0000     64K rw---    [ stack ]
> ffffe000      4K -----    [ anon ]
>  total     5424K
> -RESULT: pmap for test_malloc.c after done free-----------------------
> [nyto@nyto ~]$ pmap 5836
> 5836:   ./test_malloc
> 007ad000     88K r-x--  /lib/ld-2.3.4.so
> 007c3000      4K r----  /lib/ld-2.3.4.so
> 007c4000      4K rw---  /lib/ld-2.3.4.so
> 007c7000   1176K r-x--  /lib/tls/libc-2.3.4.so
> 008ed000      8K r----  /lib/tls/libc-2.3.4.so
> 008ef000      8K rw---  /lib/tls/libc-2.3.4.so
> 008f1000      8K rw---    [ anon ]
> 08048000      4K r-x--  /home/nyto/test_malloc
> 08049000      4K rw---  /home/nyto/test_malloc
> 08dcb000  15708K rw---    [ anon ]
> b7ba2000   3920K rw---    [ anon ]
> bfff0000     64K rw---    [ stack ]
> ffffe000      4K -----    [ anon ]
>  total    21000K
> -RESULT: pmap for test_malloc2.c after ready to insert----------------
> [nyto@nyto ~]$ pmap 5841
> 5841:   ./test_malloc2
> 007ad000     88K r-x--  /lib/ld-2.3.4.so
> 007c3000      4K r----  /lib/ld-2.3.4.so
> 007c4000      4K rw---  /lib/ld-2.3.4.so
> 007c7000   1176K r-x--  /lib/tls/libc-2.3.4.so
> 008ed000      8K r----  /lib/tls/libc-2.3.4.so
> 008ef000      8K rw---  /lib/tls/libc-2.3.4.so
> 008f1000      8K rw---    [ anon ]
> 08048000      4K r-x--  /home/nyto/test_malloc2
> 08049000      4K rw---  /home/nyto/test_malloc2
> 087c3000    132K rw---    [ anon ]
> b7baa000   3920K rw---    [ anon ]
> bfee6000   1128K rw---    [ stack ]
> ffffe000      4K -----    [ anon ]
>  total     6488K
> -RESULT: pmap for test_malloc2.c after done free----------------------
> [nyto@nyto ~]$ pmap 5841
> 5841:   ./test_malloc2
> 007ad000     88K r-x--  /lib/ld-2.3.4.so
> 007c3000      4K r----  /lib/ld-2.3.4.so
> 007c4000      4K rw---  /lib/ld-2.3.4.so
> 007c7000   1176K r-x--  /lib/tls/libc-2.3.4.so
> 008ed000      8K r----  /lib/tls/libc-2.3.4.so
> 008ef000      8K rw---  /lib/tls/libc-2.3.4.so
> 008f1000      8K rw---    [ anon ]
> 08048000      4K r-x--  /home/nyto/test_malloc2
> 08049000      4K rw---  /home/nyto/test_malloc2
> 087c3000    132K rw---    [ anon ]
> b7baa000   3920K rw---    [ anon ]
> bfee6000   1128K rw---    [ stack ]
> ffffe000      4K -----    [ anon ]
>  total     6488K
> 
> 
> -FILE: test_malloc.c--------------------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
> 
> #define JUMLAH 1000000
> #define ISI     100
> 
> typedef struct _dodol
> {
>         char *abc;
> } dodol;
> 
> int main()
> {
>         dodol **abc = (dodol**)calloc( JUMLAH, sizeof(dodol*) );
>         char *lala = (char*)malloc(sizeof(char)*ISI +1);
>         int i;
>         char a[10];
> 
>         for(i=0; i<ISI; i++)
>                 lala[i] = 'a';
>         lala[i]=0;
> 
>         printf("ready to insert\n");
>         scanf("%s",a);
> 
>         for(i=0; i<JUMLAH; i++) {
>                 abc[i] = (dodol *)malloc(sizeof(dodol));
>                 //free( abc[i] );
>         }
> 
>         //printf("ready to free\n");
>         //scanf("%s",a);
> 
>         for(i=0; i<JUMLAH; i++) {
>                 free( abc[i] );
>         }
>         //free(abc);
> 
>         printf("done free\n");
>         scanf("%s",a);
> 
>         return 0;
> }
> -FILE: test_malloc2.c-------------------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
> 
> #define JUMLAH 1000000
> #define ISI     100
> 
> typedef struct _dodol
> {
>         char *abc;
> } dodol;
> 
> int main()
> {
>         dodol **abc = (dodol**)calloc( JUMLAH, sizeof(dodol*) );
>         char *lala = (char*)malloc(sizeof(char)*ISI +1);
>         int i;
>         char a[10];
> 
>         for(i=0; i<ISI; i++)
>                 lala[i] = 'a';
>         lala[i]=0;
> 
>         printf("ready to insert\n");
>         scanf("%s",a);
> 
>         for(i=0; i<JUMLAH; i++) {
>                 abc[i] = (dodol *)malloc(sizeof(dodol));
>                 free( abc[i] );
>         }
> 
>         //printf("ready to free\n");
>         //scanf("%s",a);
> 
>         for(i=0; i<JUMLAH; i++) {
>                 //free( abc[i] );
>         }
>         //free(abc);
> 
>         printf("done free\n");
>         scanf("%s",a);
> 
>         return 0;
> }
> 
> 

-- 
Regards,
Anitha B
@S A N K H Y A


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux