By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

Memory leak even after deleting memory pointers from vector

P: 2
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 -

Expand|Select|Wrap|Line Numbers
  1.  
  2. #define ALTERNATE
  3.  
  4. #define MAX_STRING_VEC_SIZE 134
  5. #define MAX_STRUCT_VEC_SIZE 1024*200//134
  6. #define MAX_MEM_SIZE 1024*50
  7.  
  8.  
  9. /*vector of char * type*/
  10. void Function()
  11. {
  12.     std::vector< char * > v_pData;
  13.  
  14. #ifdef ALTERNATE
  15.     v_pData.resize( MAX_STRING_VEC_SIZE, NULL );
  16. #endif //ALTERNATE
  17.  
  18.     bool bFlag = true;
  19.     while( bFlag );
  20.  
  21.     //Allocate Memory    
  22.     for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
  23.     {
  24.         char * pData = new char [MAX_MEM_SIZE];
  25.         memset( pData, 0, MAX_MEM_SIZE );
  26.  
  27. #ifdef ALTERNATE
  28.         v_pData[nInd] = pData;
  29. #else  //ALTERNATE
  30.         v_pData.push_back( pData );
  31. #endif //#endif //ALTERNATE
  32.  
  33.     }
  34.  
  35.     bFlag = true;
  36.     while( bFlag );
  37.  
  38.     //Release all the Memory
  39.     for( int nInd = 0 ; nInd < MAX_STRING_VEC_SIZE; nInd++ )
  40.     {
  41.         delete [] v_pData[nInd];
  42.     }
  43.  
  44.     v_pData.clear();
  45. }
  46.  
  47. /*vector of SAMPLE_TABLE_STRUCT * type*/
  48. void Function1()
  49. {
  50.     std::vector< SAMPLE_TABLE_STRUCT * > v_pData;
  51.  
  52. #ifdef ALTERNATE
  53.     v_pData.resize( MAX_STRUCT_VEC_SIZE, NULL );
  54. #endif //ALTERNATE
  55.  
  56.     //Allocate Memory    
  57.     for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
  58.     {
  59.         SAMPLE_TABLE_STRUCT * pData = new SAMPLE_TABLE_STRUCT;
  60.  
  61. #ifdef ALTERNATE
  62.         v_pData[nInd] = pData;
  63. #else  //ALTERNATE
  64.         v_pData.push_back( pData );
  65. #endif //#endif //ALTERNATE
  66.  
  67.     }
  68.  
  69.     //Release all the Memory
  70.     for( int nInd = 0 ; nInd < MAX_STRUCT_VEC_SIZE; nInd++ )
  71.     {
  72.         delete v_pData[nInd];
  73.         v_pData[nInd] = NULL;
  74.     }
  75.  
  76.     v_pData.clear();
  77. }
  78.  
  79.  
Sep 23 '08 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
The vector is a vector of char* and from your post you added 204800 char* to the vector. After you delete the char* from the vector I would not expect the vector to get smaller. That's because vector is optimized for speed and once it has got to a size it wants to stay there.

You shrink your vector by swapping it with an empy one.
Sep 23 '08 #2

Expert 10K+
P: 11,448
You shrink your vector by swapping it with an empy one.
And even then the memory previously allocated to the vector will not be returned
to the OS but stays part of the 'heap' owned by the process.

kind regards,

Jos
Sep 23 '08 #3

P: 2
And even then the memory previously allocated to the vector will not be returned
to the OS but stays part of the 'heap' owned by the process.

kind regards,

Jos

Thank you for your valuable reply...

But we observed reduction in memory (after deletion of entries from vector) when we use vector of type "char *" (in Function()) and not in case of user defined structure "SAMPLE_TABLE_STRUCT" (in Function1()).
Why there is a difference in the two cases...?

Deepak
Sep 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.