问题
I am developing a software which maps information in 3D space. I use a container to hold this information. The container which I use is
QList< QList< QMap<float, QList<quint16> > > > lidardata;
which basically is a 2D grid representing a rectangular area where each cell is 1 meter x 1 meter, and in each cell the QMap contains a key value representing height and a list of four related values at that height. This way I can store five values (height + other values). I insert values in a loop like this (rown and coln are row and column indexes respectively)
QList<quint16> NEWLIST;
NEWLIST << (width*100.0) << (1000.0+sens*100.0) << (quint16)(intensity*1000.0) ;
lidardata[ rown ][ coln ].insert( heightValue, NEWLIST);
Before this approach instead of using QMap<float, QList<quint16>
I used QList<quint16>
and just appending the 5 values.
Now the question is: running my program runs out of memory quite fast. It took up about 800Mb of memory to complete with the first solution (QList instead of QMap), now it runs out (at about 1.4 Gb) at 75% of the total data-storing process.
Can someone confirm that storing the same amount of information using QMap<float, QList<quint16>
instead of QList<quint16>
does require a lot more space in memory?
Does anyone have any hints to limit space? I will go back to the old solution if nothing comes up.
回答1:
As mentionned in comment:
your code may suffer from Primitive Obsession.
Try to solve your problem using the ValueObject fix stated in this tutorial : create a class with all needed attibutes, and work on instances of this class instead of maintaining nested Qlist
s and QMap
s.
来源:https://stackoverflow.com/questions/16528424/size-of-qt-containers-is-qmap-much-larger-than-qlist