Qt-interest Archive, March 2002
QListView - has anyone analyzed its (high?) memory usage?
Message 1 in thread
I've written a test program that puts a database of strings in a
QListView where I've added 5 columns (see below for actual code, but you
wont be able to build this because I use some custom classes 'DataBase'
and 'Song' (a database of song data). I am using Qt2.3.2 under Windows.
I find that, using the windows task manager to look at memory usage by
process, that when I comment out the listview creating/adding that the
test application takes 8,920K (this is the memory cost of the base app,
plus the data stored in the DataBase class. I found that the base app
took about 3,724K, therefore the database (of 3000songs) took 5196,
which I can justify).
My question is that adding the data to the QListView makes my process
memory usage go from 8,920K to 11,812K... 3MB just for the QListView.
The code below adds all the string lengths up and that comes out to
127,384bytes. I know that QStrings are arrays of QChars so I should
double this number and get 260K... still no-where near 3MB. I've looked
at the QListView and QListViewItem code and it seems to be very memory
efficient. It seems to me that I should just be using the memory from a
QString for every populated column. Has anyone instrumented this before
and could explain to me where all this memory is going?
Thanks,
Tim Harvey
Example code (can't compile as requires DataBase and Song class, but you
can at least see what I'm doing).
int main(int argc,char **argv)
{
QApplication qapp(argc, argv);
QDialog dialog(0,0,TRUE);
qapp.setMainWidget(&dialog);
QVBoxLayout layout(&dialog);
QListView lv(&dialog, "listview");
QLabel Label(&dialog);
layout.addWidget(&lv);
layout.addWidget(&Label);
QTime start = QTime::currentTime();
DataBase dbase;
dbase.Load("./songlist.txt");
lv.addColumn( "Trk" );
lv.addColumn( "Song" );
lv.addColumn( "Artist" );
lv.addColumn( "Album" );
lv.addColumn( "Track" );
QList<Song> list;
dbase.QuerySongs(list);
QListIterator<Song> it(list);
ulong nMemUsage = 0;
for (; it.current(); ++it)
{
Song* pSong = it.current();
QListViewItem* pItem = new QListViewItem(&lv,
QString::number(pSong->Track()), pSong->Title(), pSong->Artist(),
pSong->Album(), QString::number(pSong->Track()));
nMemUsage += ( 2*(QString::number(pSong->Track())).length() +
pSong->Title().length() + pSong->Artist().length() +
pSong->Album().length() );
}
QString text;
QString tm = dbase.time();
QString sz = dbase.bytes();
text.sprintf("%d songs, %d albums, %d artists, %s %s: loaded in %3.2f
seconds: %d bytes",
dbase.numSongs(), dbase.numAlbums(), dbase.numArtists(), tm.ascii(),
sz.ascii(),
(float) start.msecsTo(QTime::currentTime()) / 1000.0, nMemUsage);
Label.setText(text);
return dialog.exec();
// while(1);
}