Memory leak even after deleting memory pointers from vector

Discussion in 'C++' started by cham, Sep 23, 2008.

  1. cham

    cham Guest

    Hi,

    I am working on c++ in a linux system ( Fedora core 4 ),
    kernel version - 2.6.11-1.1369_FC4
    gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )

    In my code i am creating a vector to store pointers of type structure
    "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create
    an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new"
    and push back into the vector,this is done inside a for loop for
    204800 times. After i come out of for loop i observe the memory
    consumed by the process using the command "pmap -d pid". The memory
    consumption increases by approximately 8 MB. After this i delete all
    the contents of vector using "delete" operator. Now if i observe the
    memory consumed by the process ( using "pmap -d pid" command ) it
    shows no reduction in the memory even after deallocating the memory in
    the code.

    It shows memory reduction after deleting vector contents if i store
    the "char *" elements into the vector instead of "SAMPLE_TABLE_STRUCT
    *" elements.

    Am not able to figure it out why even after deleting the vector ( of
    type "SAMPLE_TABLE_STRUCT *" )contents the memory reduction is not
    seen...?

    Can anyone please help me out here...?

    Here is the piece of code where am facing the problem -

    Code:
    
    #define ALTERNATE
    
    #define MAX_STRING_VEC_SIZE 134
    #define MAX_STRUCT_VEC_SIZE 1024*200//134
    #define MAX_MEM_SIZE 1024*50
    
    
    /*vector of char * type*/
    void Function()
    {
    	std::vector< char * > v_pData;
    
    #ifdef ALTERNATE
    	v_pData.resize( MAX_STRING_VEC_SIZE, NULL );
    #endif //ALTERNATE
    
    	bool bFlag = true;
    	while( bFlag );
    
    	//Allocate Memory
    	for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    	{
    		char * pData = new char [MAX_MEM_SIZE];
    		memset( pData, 0, MAX_MEM_SIZE );
    
    #ifdef ALTERNATE
    		v_pData[nInd] = pData;
    #else  //ALTERNATE
    		v_pData.push_back( pData );
    #endif //#endif //ALTERNATE
    
    	}
    
    	bFlag = true;
    	while( bFlag );
    
    	//Release all the Memory
    	for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    	{
    		delete [] v_pData[nInd];
    	}
    
    	v_pData.clear();
    }
    
    /*vector of SAMPLE_TABLE_STRUCT * type*/
    void Function1()
    {
    	std::vector< SAMPLE_TABLE_STRUCT * > v_pData;
    
    #ifdef ALTERNATE
    	v_pData.resize( MAX_STRUCT_VEC_SIZE, NULL );
    #endif //ALTERNATE
    
    	//Allocate Memory
    	for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    	{
    		SAMPLE_TABLE_STRUCT * pData = new SAMPLE_TABLE_STRUCT;
    
    #ifdef ALTERNATE
    		v_pData[nInd] = pData;
    #else  //ALTERNATE
    		v_pData.push_back( pData );
    #endif //#endif //ALTERNATE
    
    	}
    
    	//Release all the Memory
    	for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    	{
    		delete v_pData[nInd];
    		v_pData[nInd] = NULL;
    	}
    
    	v_pData.clear();
    }
    
    
    cham, Sep 23, 2008
    #1
    1. Advertising

  2. cham

    Sumanth Guest

    On Sep 23, 11:31 am, cham <> wrote:
    > Hi,
    >
    > I am working on c++ in a linux system ( Fedora core 4 ),
    > kernel version - 2.6.11-1.1369_FC4
    > gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )
    >
    > In my code i am creating a vector to store pointers of type structure
    > "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create
    > an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new"
    > and push back into the vector,this is done inside a for loop for
    > 204800 times. After i come out of for loop i observe the memory
    > consumed by the process using the command "pmap -d pid". The memory
    > consumption increases by approximately 8 MB. After this i delete all
    > the contents of vector using "delete" operator. Now if i observe the
    > memory consumed by the process ( using "pmap -d pid" command ) it
    > shows no reduction in the memory even after deallocating the memory in
    > the code.
    >
    > It shows memory reduction after deleting vector contents if i store
    > the "char *" elements into the vector instead of "SAMPLE_TABLE_STRUCT
    > *" elements.
    >
    > Am not able to figure it out why even after deleting the vector ( of
    > type "SAMPLE_TABLE_STRUCT *" )contents the memory reduction is not
    > seen...?
    >
    > Can anyone please help me out here...?
    >
    > Here is the piece of code where am facing the problem -
    >
    >
    Code:
    >
    > #define ALTERNATE
    >
    > #define MAX_STRING_VEC_SIZE 134
    > #define MAX_STRUCT_VEC_SIZE 1024*200//134
    > #define MAX_MEM_SIZE 1024*50
    >
    > /*vector of char * type*/
    > void Function()
    > {
    >         std::vector< char * > v_pData;
    >
    > #ifdef ALTERNATE
    >         v_pData.resize( MAX_STRING_VEC_SIZE, NULL );
    > #endif //ALTERNATE
    >
    >         bool bFlag = true;
    >         while( bFlag );
    >
    >         //Allocate Memory
    >         for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    >         {
    >                 char * pData = new char [MAX_MEM_SIZE];
    >                 memset( pData, 0, MAX_MEM_SIZE );
    >
    > #ifdef ALTERNATE
    >                 v_pData[nInd] = pData;
    > #else  //ALTERNATE
    >                 v_pData.push_back( pData );
    > #endif //#endif //ALTERNATE
    >
    >         }
    >
    >         bFlag = true;
    >         while( bFlag );
    >
    >         //Release all the Memory
    >         for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    >         {
    >                 delete [] v_pData[nInd];
    >         }
    >
    >         v_pData.clear();
    >
    > }
    >
    > /*vector of SAMPLE_TABLE_STRUCT * type*/
    > void Function1()
    > {
    >         std::vector< SAMPLE_TABLE_STRUCT * > v_pData;
    >
    > #ifdef ALTERNATE
    >         v_pData.resize( MAX_STRUCT_VEC_SIZE, NULL );
    > #endif //ALTERNATE
    >
    >         //Allocate Memory
    >         for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    >         {
    >                 SAMPLE_TABLE_STRUCT * pData = new SAMPLE_TABLE_STRUCT;
    >
    > #ifdef ALTERNATE
    >                 v_pData[nInd] = pData;
    > #else  //ALTERNATE
    >                 v_pData.push_back( pData );
    > #endif //#endif //ALTERNATE
    >
    >         }
    >
    >         //Release all the Memory
    >         for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    >         {
    >                 delete v_pData[nInd];
    >                 v_pData[nInd] = NULL;
    >         }
    >
    >         v_pData.clear();
    >
    > }
    >
    > 


    Hi,

    It is not guaranteed that the memory usage will "show" decrease in
    memory if you delete any objects. And its not a memory leak.

    When you delete objects, the memory management module of the OS does
    not immediately reclaimed that much memory. Optimization is done by
    retaining the "freed" memory within the process and when you request
    for additional memory using "new", this memory pool is used to return
    memory.

    Similarly during allocation,you might request 500 bytes of memory, but
    the memory allocation from the OS will be in multiples of an OS
    dependant memory chunk. The memory increase/decrease of a process will
    not follow the exact sequence of new/delete calls.

    In your example, after the deletion, try allocating memory again for
    the same set of objects. There should not be any increase in memory as
    the deleted memory should be reused.

    Rgds
    Sumanth, Sep 23, 2008
    #2
    1. Advertising

  3. cham

    Sumanth Guest

    On Sep 23, 11:55 am, Sumanth <> wrote:
    > On Sep 23, 11:31 am, cham <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > I am working on c++ in a linux system ( Fedora core 4 ),
    > > kernel version - 2.6.11-1.1369_FC4
    > > gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )

    >
    > > In my code i am creating a vector to store pointers of type structure
    > > "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create
    > > an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new"
    > > and push back into the vector,this is done inside a for loop for
    > > 204800 times. After i come out of for loop i observe the memory
    > > consumed by the process using the command "pmap -d pid". The memory
    > > consumption increases by approximately 8 MB. After this i delete all
    > > the contents of vector using "delete" operator. Now if i observe the
    > > memory consumed by the process ( using "pmap -d pid" command ) it
    > > shows no reduction in the memory even after deallocating the memory in
    > > the code.

    >
    > > It shows memory reduction after deleting vector contents if i store
    > > the "char *" elements into the vector instead of "SAMPLE_TABLE_STRUCT
    > > *" elements.

    >
    > > Am not able to figure it out why even after deleting the vector ( of
    > > type "SAMPLE_TABLE_STRUCT *" )contents the memory reduction is not
    > > seen...?

    >
    > > Can anyone please help me out here...?

    >
    > > Here is the piece of code where am facing the problem -

    >
    > >
    Code:
    [/color]
    >[color=green]
    > > #define ALTERNATE[/color]
    >[color=green]
    > > #define MAX_STRING_VEC_SIZE 134
    > > #define MAX_STRUCT_VEC_SIZE 1024*200//134
    > > #define MAX_MEM_SIZE 1024*50[/color]
    >[color=green]
    > > /*vector of char * type*/
    > > void Function()
    > > {
    > >         std::vector< char * > v_pData;[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >         v_pData.resize( MAX_STRING_VEC_SIZE, NULL );
    > > #endif //ALTERNATE[/color]
    >[color=green]
    > >         bool bFlag = true;
    > >         while( bFlag );[/color]
    >[color=green]
    > >         //Allocate Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    > >         {
    > >                 char * pData = new char [MAX_MEM_SIZE];
    > >                 memset( pData, 0, MAX_MEM_SIZE );[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >                 v_pData[nInd] = pData;
    > > #else  //ALTERNATE
    > >                 v_pData.push_back( pData );
    > > #endif //#endif //ALTERNATE[/color]
    >[color=green]
    > >         }[/color]
    >[color=green]
    > >         bFlag = true;
    > >         while( bFlag );[/color]
    >[color=green]
    > >         //Release all the Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    > >         {
    > >                 delete [] v_pData[nInd];
    > >         }[/color]
    >[color=green]
    > >         v_pData.clear();[/color]
    >[color=green]
    > > }[/color]
    >[color=green]
    > > /*vector of SAMPLE_TABLE_STRUCT * type*/
    > > void Function1()
    > > {
    > >         std::vector< SAMPLE_TABLE_STRUCT * > v_pData;[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >         v_pData.resize( MAX_STRUCT_VEC_SIZE, NULL );
    > > #endif //ALTERNATE[/color]
    >[color=green]
    > >         //Allocate Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    > >         {
    > >                 SAMPLE_TABLE_STRUCT * pData = new SAMPLE_TABLE_STRUCT;[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >                 v_pData[nInd] = pData;
    > > #else  //ALTERNATE
    > >                 v_pData.push_back( pData );
    > > #endif //#endif //ALTERNATE[/color]
    >[color=green]
    > >         }[/color]
    >[color=green]
    > >         //Release all the Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    > >         {
    > >                 delete v_pData[nInd];
    > >                 v_pData[nInd] = NULL;
    > >         }[/color]
    >[color=green]
    > >         v_pData.clear();[/color]
    >[color=green]
    > > }[/color]
    >[color=green]
    > > 

    >
    > Hi,
    >
    > It is not guaranteed that the memory usage will "show" decrease in
    > memory if you delete any objects. And its not a memory leak.
    >
    > When you delete objects, the memory management module of the OS does
    > not immediately reclaimed that much memory. Optimization is done by
    > retaining the "freed" memory within the process and when you request
    > for additional memory using "new", this memory pool is used to return
    > memory.
    >
    > Similarly during allocation,you might request 500 bytes of memory, but
    > the memory allocation from the OS will be in multiples of an OS
    > dependant memory chunk. The memory increase/decrease of a process will
    > not follow the exact sequence of new/delete calls.
    >
    > In your example, after the deletion, try allocating memory again for
    > the same set of objects. There should not be any increase in memory as
    > the deleted memory should be reused.
    >
    > Rgds


    Just to add, there are low level memory allocation routines such as
    sbrk, that control the size of a program's data space.
    Sumanth, Sep 23, 2008
    #3
  4. cham

    cham Guest

    On Sep 23, 3:55 pm, Sumanth <> wrote:
    > On Sep 23, 11:31 am, cham <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > I am working on c++ in a linux system ( Fedora core 4 ),
    > > kernel version - 2.6.11-1.1369_FC4
    > > gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )

    >
    > > In my code i am creating a vector to store pointers of type structure
    > > "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create
    > > an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new"
    > > and push back into the vector,this is done inside a for loop for
    > > 204800 times. After i come out of for loop i observe the memory
    > > consumed by the process using the command "pmap -d pid". The memory
    > > consumption increases by approximately 8 MB. After this i delete all
    > > the contents of vector using "delete" operator. Now if i observe the
    > > memory consumed by the process ( using "pmap -d pid" command ) it
    > > shows no reduction in the memory even after deallocating the memory in
    > > the code.

    >
    > > It shows memory reduction after deleting vector contents if i store
    > > the "char *" elements into the vector instead of "SAMPLE_TABLE_STRUCT
    > > *" elements.

    >
    > > Am not able to figure it out why even after deleting the vector ( of
    > > type "SAMPLE_TABLE_STRUCT *" )contents the memory reduction is not
    > > seen...?

    >
    > > Can anyone please help me out here...?

    >
    > > Here is the piece of code where am facing the problem -

    >
    > >
    Code:
    [/color]
    >[color=green]
    > > #define ALTERNATE[/color]
    >[color=green]
    > > #define MAX_STRING_VEC_SIZE 134
    > > #define MAX_STRUCT_VEC_SIZE 1024*200//134
    > > #define MAX_MEM_SIZE 1024*50[/color]
    >[color=green]
    > > /*vector of char * type*/
    > > void Function()
    > > {
    > >         std::vector< char * > v_pData;[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >         v_pData.resize( MAX_STRING_VEC_SIZE, NULL );
    > > #endif //ALTERNATE[/color]
    >[color=green]
    > >         bool bFlag = true;
    > >         while( bFlag );[/color]
    >[color=green]
    > >         //Allocate Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    > >         {
    > >                 char * pData = new char [MAX_MEM_SIZE];
    > >                 memset( pData, 0, MAX_MEM_SIZE );[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >                 v_pData[nInd] = pData;
    > > #else  //ALTERNATE
    > >                 v_pData.push_back( pData );
    > > #endif //#endif //ALTERNATE[/color]
    >[color=green]
    > >         }[/color]
    >[color=green]
    > >         bFlag = true;
    > >         while( bFlag );[/color]
    >[color=green]
    > >         //Release all the Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
    > >         {
    > >                 delete [] v_pData[nInd];
    > >         }[/color]
    >[color=green]
    > >         v_pData.clear();[/color]
    >[color=green]
    > > }[/color]
    >[color=green]
    > > /*vector of SAMPLE_TABLE_STRUCT * type*/
    > > void Function1()
    > > {
    > >         std::vector< SAMPLE_TABLE_STRUCT * > v_pData;[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >         v_pData.resize( MAX_STRUCT_VEC_SIZE, NULL );
    > > #endif //ALTERNATE[/color]
    >[color=green]
    > >         //Allocate Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    > >         {
    > >                 SAMPLE_TABLE_STRUCT * pData = new SAMPLE_TABLE_STRUCT;[/color]
    >[color=green]
    > > #ifdef ALTERNATE
    > >                 v_pData[nInd] = pData;
    > > #else  //ALTERNATE
    > >                 v_pData.push_back( pData );
    > > #endif //#endif //ALTERNATE[/color]
    >[color=green]
    > >         }[/color]
    >[color=green]
    > >         //Release all the Memory
    > >         for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
    > >         {
    > >                 delete v_pData[nInd];
    > >                 v_pData[nInd] = NULL;
    > >         }[/color]
    >[color=green]
    > >         v_pData.clear();[/color]
    >[color=green]
    > > }[/color]
    >[color=green]
    > > 

    >
    > Hi,
    >
    > It is not guaranteed that the memory usage will "show" decrease in
    > memory if you delete any objects. And its not a memory leak.
    >
    > When you delete objects, the memory management module of the OS does
    > not immediately reclaimed that much memory. Optimization is done by
    > retaining the "freed" memory within the process and when you request
    > for additional memory using "new", this memory pool is used to return
    > memory.
    >
    > Similarly during allocation,you might request 500 bytes of memory, but
    > the memory allocation from the OS will be in multiples of an OS
    > dependant memory chunk. The memory increase/decrease of a process will
    > not follow the exact sequence of new/delete calls.
    >
    > In your example, after the deletion, try allocating memory again for
    > the same set of objects. There should not be any increase in memory as
    > the deleted memory should be reused.
    >
    > Rgds


    Sumanth,

    Thank you for your valuable reply...

    But we observed reduction in memory when we use vector of type "char
    *" and not in case of user defined structure "SAMPLE_TABLE_STRUCT".
    Why there is a difference in the two cases...?

    Deepak
    cham, Sep 23, 2008
    #4
  5. cham

    rogo Guest

    > But we observed reduction in memory when we use vector of type "char
    > *" and not in case of user defined structure "SAMPLE_TABLE_STRUCT".
    > Why there is a difference in the two cases...?


    Use valgrind to test for memory leaks.

    Rogo
    rogo, Sep 24, 2008
    #5
  6. cham

    cham Guest

    On Sep 24, 5:07 pm, rogo <> wrote:
    > > But we observed reduction inmemorywhen we use vector of type "char
    > > *" and not in case of user defined structure "SAMPLE_TABLE_STRUCT".
    > > Why there is a difference in the two cases...?

    >
    > Use valgrind to test formemoryleaks.
    >
    > Rogo


    Hi Rogo,

    We checked with valgrind tool but it tells there are no memory
    leaks.....

    Thanks
    Cham ( Deepak )
    cham, Sep 25, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stan Goodman

    Even older fart, even newer newbie

    Stan Goodman, Jul 3, 2003, in forum: Java
    Replies:
    11
    Views:
    678
    Stan Goodman
    Jul 4, 2003
  2. Hanzo
    Replies:
    20
    Views:
    1,246
    Alexei Polkhanov
    Nov 18, 2003
  3. Xamalek
    Replies:
    7
    Views:
    680
  4. Replies:
    8
    Views:
    1,894
    Csaba
    Feb 18, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    656
Loading...

Share This Page