Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2007
qvector - freeing memory for pointers stored in it


Message 1 in thread

Hi,

I have a bunch of elements of type T.

I have a vector V such that V = QVector<T *>

And I have a higher level of vector D such that D = QVector<V *>

In my program, I create a lot of V's and stick it in D.

At some point of time, I compute if I the V's that I have are valid or 
not (according to some of my criteria), and for those that are invalid, 
I want to remove the memory used up by those V's to store the pointer to 
T's.

I dont want any element of type T to disappear (cause the valid V's 
still need to keep pointing to the T's).

In my code I am doing

for (int i=0; i<D.size(); i++)
{
  if (false == D.at(i)->isValid())
  {
    D.at(i).clear();
    D.at(i).resize(0);
    D.at(i).squeeze();
  }
}


After this operation, I do not see any increase in the free memory in my 
system.  What am I doing wrong?

-- 
 [ signature omitted ] 

Message 2 in thread

On 09.04.07 18:06:14, Ashutosh Chakraborty wrote:
> In my code I am doing
> 
> for (int i=0; i<D.size(); i++)
> {
>  if (false == D.at(i)->isValid())
>  {
>    D.at(i).clear();
>    D.at(i).resize(0);
>    D.at(i).squeeze();
>  }
> }
> 
> 
> After this operation, I do not see any increase in the free memory in my 
> system.  What am I doing wrong?

You don't delete the element at position i. You need to add something
like:

V* tmp = D.at(i);
D.remove(i);
delete tmp;

Also you can remove the 3 lines you have there. The Overhead of QVector
is really small, so removing its elements doesn't free that much memory
(unless you have a few thousand items in it).

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

Thanks Andreas!  Unfortunately, I could not make it work for me.  I 
simplified my problem
earlier and might have caused confusion.  I will re-state it.

There are a bunch of objects (30000 in number), each of them is of type 
T. Defined as
Object *T;

V is a class, which has a vector of  pointer to T as its data member.  
Defined as
class V
{
  int x;
  Qstring y;
  QVector<T *> vectorOfT;
  bool isValid;
  ~V() { vectorOfT.clear(); }
}

D is a data collection of Vs.  Defined as
QVector <V *> D;

In my problem, D.size() is a large number (10 million).  However, only 
0.1% of these
are good (has its isValid set to true).  To remove the bad items, I do

for (int i=0; i<D.size(); i++)
{
  if (false == D.at(i)->isValid)
  {
    V* tmp = D.at(i);
    D.remove(i);            // Thanks Andreas for this suggestion
    delete tmp;
  }
}

The memory footprint before and after doing the above is same = 800mb.

Where am I missing something?

regards,
Ashu

Andreas Pakulat wrote:
> On 09.04.07 18:06:14, Ashutosh Chakraborty wrote:
>   
>> In my code I am doing
>>
>> for (int i=0; i<D.size(); i++)
>> {
>>  if (false == D.at(i)->isValid())
>>  {
>>    D.at(i).clear();
>>    D.at(i).resize(0);
>>    D.at(i).squeeze();
>>  }
>> }
>>
>>
>> After this operation, I do not see any increase in the free memory in my 
>> system.  What am I doing wrong?
>>     
>
> You don't delete the element at position i. You need to add something
> like:
>
> V* tmp = D.at(i);
> D.remove(i);
> delete tmp;
>
> Also you can remove the 3 lines you have there. The Overhead of QVector
> is really small, so removing its elements doesn't free that much memory
> (unless you have a few thousand items in it).
>
> Andreas
>
>   

-- 
 [ signature omitted ] 

Message 4 in thread

Hi,

> The memory footprint before and after doing the above is same = 800mb.
> 
> Where am I missing something?

I didn't not have a look at the code, but note that the memory freed by 
a program is not immediately given back to the system.

This is not related to Qt, rather it's an internal issue of the C run-time.

--
 [ signature omitted ] 

Message 5 in thread

Are all the T objects 'valid'?

Have you considered deleting them (instead of just removing the pointer out
of the deleted V vector) but while leaving those referred to untouched
(reference counting? garbage collection? or something).

Or are the V vectors just permutations of the same objects?

 - Paul

--
 [ signature omitted ] 

Message 6 in thread

All the T's are valid.  Thus, all V's are just permutations of a small 
number of Ts.

For example, there are 10 million V's and just 20 thousand Ts

For this reason I am not deleting any Ts.

regards,
Ashu

Paul Findlay wrote:
> Are all the T objects 'valid'?
>
> Have you considered deleting them (instead of just removing the pointer out
> of the deleted V vector) but while leaving those referred to untouched
> (reference counting? garbage collection? or something).
>
> Or are the V vectors just permutations of the same objects?
>
>  - Paul
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>   

-- 
 [ signature omitted ]