473,545 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory leak even after deleting memory pointers from vector

2 New Member
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_S TRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_S TRUCT" 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_S TRUCT *" elements.

Am not able to figure it out why even after deleting the vector ( of type "SAMPLE_TABLE_S TRUCT *" )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
3 2653
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
JosAH
11,448 Recognized Expert MVP
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
deepak1905
2 New Member
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_S TRUCT" (in Function1()).
Why there is a difference in the two cases...?

Deepak
Sep 24 '08 #4

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

Similar topics

4
3840
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online contests (http://online-judge.uva.es/p/v1/127.html). The program I wrote seems to work fine, but it takes way too much memory to run. I am not that...
10
1922
by: Jonathan Ames | last post by:
Moving to C++ from Java, I'm still confused by some aspects of memory cleanup operations. For example, let's say I have a class MovingObject which maintains a pointer to another class MovementAlgorithm. MovingObject has a method SetMovementAlgorithm(MovementAlgorithm* movementAlgorithm) if the body of this method reads
2
2641
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
4
1387
by: deancoo | last post by:
I'm pretty sure that the program bellow contains a memory leak. After deleting the "hand_vector" instance, not all memory is released (only about 30%). It appears to me as though the instances of "hand" are still hanging around. Are they not on the heap? Shouldn't they be released along with the "hand_vector" instance? Thanks for any help....
3
2020
by: wolverine | last post by:
Hi Let me first of all tell that this problem is not specific to a compiler like gcc. It even comes in windows. So please dont regard the question as off topic. I am posting a code using stl. I viewed the memory for program by giving top command eg: top -d 0.2 -p 'pid' The pid will be printed out by the program itself.
18
9053
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here?
17
3052
by: christophe.chazeau | last post by:
Hi, I have a problem with a really simple chunk of code which should work but does obviously does not. This chunk of code is just a POC aimed at finding a bug in a larger project in which the same problem seems to occur. Here the deal : when I run this piece of code, I expect all the memory allocated by the "Test" object to be freed but what...
9
4200
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It starts off at a nice 4% of memory, then slowly grows up to 50% and beyond. This translates to around 2 gigs of physical memory, and that's really way...
27
2928
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a. try { a = new int ;
5
505
by: cham | last post by:
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...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.