large array of objects isnt freed

Hello,
strange behaviour have seen with large array of objects in the following code.
If class internal array has length 100 memory is freed after delete [].
But if the length is smaller, e.g 20, memory isnt freed.
What is the reason?

Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

class BJ {
private:
   int      fMastersNum;
   int*     fIndexes;
	
public:
   BJ();
   virtual ~BJ();
};

BJ::BJ()
{
   fMastersNum = 100; 
   //fMastersNum = 20; 
   fIndexes = new int[fMastersNum];	  
}

BJ::~BJ()
{ 
   if(fIndexes) {
      delete [] fIndexes; 
   }
}
   
int main() 
{
   BJ *arr = new BJ[10000000];

   int kkk;
   cin >> kkk;

   delete [] arr;

   cin >> kkk;
}
Last edited on
As it stands, this program will request about 3900 megabytes of ram . It may be pushing beyond the capabilities of the system this is run on.

Ram required = 10000000 * ( sizeof(BJ) + 100 * sizeof(int) ) bytes.
Thanks, Chervil.
But It is ok with length of 100. Memory is freed with delete [].
Problem is with length of 20 !
What are the actual symptoms (e.g. error messages etc.) which you are having?
There is no errors messages. Problem is memory consuming after delete [].

When length of class internal array fMasterNum is 100, memory consumption is 4.1 Gb.
And after delete [] (and before the second std::cin) memory is freed completely.

If length of class internal array fMasterNum is 20, memory consumption is 1.2 Gb.
And after delete [] (and before the second std::cin) memory is freed on 0.3 Gb only.
Last edited on
Thanks for the clarification. I'm not able to test the 4.1Gb version as my system doesn't have that much RAM available. With the values changed to a smaller size, I tested the program, but again, my system has insufficient ram, so virtual memory is required. What this means is there is prolonged hard drive activity as the required memory is paged to/from the hard drive. This did slow down the delete [] phase for me, but the memory was always released properly. (I used Sysinternals Process Explorer on XP to examine the performance).

Actual behaviour I presume will depend upon operating system, compiler and available physical resources.
Topic archived. No new replies allowed.