Memory Management while Inserting and Removing values from QTableView
up vote
0
down vote
favorite
In my Application I am required to insert and remove data from tableView multiple times while application is running.After a complete cycle of Inserting and removing data from tableView memory(RAM) consumption of my application increases as checked in task Manager. After each successive cycle of insertion and deletion memory consumption keeps on Increasing.
Below is my code:
MemoryLeak.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-11-19T16:00:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MemoryLeak
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES +=
main.cpp
mainwindow.cpp
HEADERS +=
mainwindow.h
FORMS +=
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QStandardItemModel *model;
public slots:
void createModel();
private slots:
void on_clearModel_clicked();
void on_displayData_clicked();
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
createModel();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createModel(){
model=new QStandardItemModel(this);
model->setColumnCount(3);
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Age"));
model->setHeaderData(2, Qt::Horizontal, tr("Class"));
ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->tableView->horizontalHeader()->resizeSection(0,580);
ui->tableView->horizontalHeader()->resizeSection(1,280);
ui->tableView->horizontalHeader()->resizeSection(2,280);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setStyleSheet("alternate-background-color:#00FF00;border: 1px solid red;");
}
void MainWindow::on_clearModel_clicked()
{
model->setRowCount(0);
}
void MainWindow::on_displayData_clicked()
{
int rowCount = 0;
QStandardItem *item;
QString value;
for(rowCount = 0; rowCount < 5000; rowCount ++){
value = QString("Person_%1").arg(rowCount);
item = new QStandardItem(QString("%0").arg(value));
model->setItem(rowCount,0,item);
// delete item;// No value is inserted in column
item = new QStandardItem(QString(" %0").arg(rowCount));
model->setItem(rowCount,1,item);
item = new QStandardItem(QString("%0").arg(2));
model->setItem(rowCount,2,item);
}
}
Memory consumption From app start:
11020 KB when app started
16144 KB when display data function Called
12812 KB when clear model called
16356 KB when display data function Called
13304 KB when clear Model Called
So Ram consumption increased by 2 MB in 2 Insert/Remove cycles.
To me it seems to be memory leak issue because memory is allocated to QStandardItem object( item = new QStandardItem(QString("%0").arg(value))
) but is never freed.I tried to free the memory by calling delete item
but after it a row is inserted with blank column.
What might be the possible reason for increase in memory consumption.
c++ qt memory-management qtableview qstandarditemmodel
|
show 7 more comments
up vote
0
down vote
favorite
In my Application I am required to insert and remove data from tableView multiple times while application is running.After a complete cycle of Inserting and removing data from tableView memory(RAM) consumption of my application increases as checked in task Manager. After each successive cycle of insertion and deletion memory consumption keeps on Increasing.
Below is my code:
MemoryLeak.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-11-19T16:00:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MemoryLeak
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES +=
main.cpp
mainwindow.cpp
HEADERS +=
mainwindow.h
FORMS +=
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QStandardItemModel *model;
public slots:
void createModel();
private slots:
void on_clearModel_clicked();
void on_displayData_clicked();
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
createModel();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createModel(){
model=new QStandardItemModel(this);
model->setColumnCount(3);
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Age"));
model->setHeaderData(2, Qt::Horizontal, tr("Class"));
ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->tableView->horizontalHeader()->resizeSection(0,580);
ui->tableView->horizontalHeader()->resizeSection(1,280);
ui->tableView->horizontalHeader()->resizeSection(2,280);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setStyleSheet("alternate-background-color:#00FF00;border: 1px solid red;");
}
void MainWindow::on_clearModel_clicked()
{
model->setRowCount(0);
}
void MainWindow::on_displayData_clicked()
{
int rowCount = 0;
QStandardItem *item;
QString value;
for(rowCount = 0; rowCount < 5000; rowCount ++){
value = QString("Person_%1").arg(rowCount);
item = new QStandardItem(QString("%0").arg(value));
model->setItem(rowCount,0,item);
// delete item;// No value is inserted in column
item = new QStandardItem(QString(" %0").arg(rowCount));
model->setItem(rowCount,1,item);
item = new QStandardItem(QString("%0").arg(2));
model->setItem(rowCount,2,item);
}
}
Memory consumption From app start:
11020 KB when app started
16144 KB when display data function Called
12812 KB when clear model called
16356 KB when display data function Called
13304 KB when clear Model Called
So Ram consumption increased by 2 MB in 2 Insert/Remove cycles.
To me it seems to be memory leak issue because memory is allocated to QStandardItem object( item = new QStandardItem(QString("%0").arg(value))
) but is never freed.I tried to free the memory by calling delete item
but after it a row is inserted with blank column.
What might be the possible reason for increase in memory consumption.
c++ qt memory-management qtableview qstandarditemmodel
Task Manager is not the correct tool to analyze memory consumption. read stackoverflow.com/questions/4690800/how-to-profile-memory-usage
– eyllanesc
Nov 19 at 11:43
@eyllanesc which tool do you suggest. for precise result
– Asus gates
Nov 19 at 11:44
read the link...
– eyllanesc
Nov 19 at 11:44
1
Why do you callmodel->setRowCount(0);
function instead of callingmodel::clear()
function?
– vahancho
Nov 19 at 11:44
@vahancho model::clear also clear my header data
– Asus gates
Nov 19 at 11:46
|
show 7 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In my Application I am required to insert and remove data from tableView multiple times while application is running.After a complete cycle of Inserting and removing data from tableView memory(RAM) consumption of my application increases as checked in task Manager. After each successive cycle of insertion and deletion memory consumption keeps on Increasing.
Below is my code:
MemoryLeak.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-11-19T16:00:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MemoryLeak
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES +=
main.cpp
mainwindow.cpp
HEADERS +=
mainwindow.h
FORMS +=
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QStandardItemModel *model;
public slots:
void createModel();
private slots:
void on_clearModel_clicked();
void on_displayData_clicked();
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
createModel();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createModel(){
model=new QStandardItemModel(this);
model->setColumnCount(3);
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Age"));
model->setHeaderData(2, Qt::Horizontal, tr("Class"));
ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->tableView->horizontalHeader()->resizeSection(0,580);
ui->tableView->horizontalHeader()->resizeSection(1,280);
ui->tableView->horizontalHeader()->resizeSection(2,280);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setStyleSheet("alternate-background-color:#00FF00;border: 1px solid red;");
}
void MainWindow::on_clearModel_clicked()
{
model->setRowCount(0);
}
void MainWindow::on_displayData_clicked()
{
int rowCount = 0;
QStandardItem *item;
QString value;
for(rowCount = 0; rowCount < 5000; rowCount ++){
value = QString("Person_%1").arg(rowCount);
item = new QStandardItem(QString("%0").arg(value));
model->setItem(rowCount,0,item);
// delete item;// No value is inserted in column
item = new QStandardItem(QString(" %0").arg(rowCount));
model->setItem(rowCount,1,item);
item = new QStandardItem(QString("%0").arg(2));
model->setItem(rowCount,2,item);
}
}
Memory consumption From app start:
11020 KB when app started
16144 KB when display data function Called
12812 KB when clear model called
16356 KB when display data function Called
13304 KB when clear Model Called
So Ram consumption increased by 2 MB in 2 Insert/Remove cycles.
To me it seems to be memory leak issue because memory is allocated to QStandardItem object( item = new QStandardItem(QString("%0").arg(value))
) but is never freed.I tried to free the memory by calling delete item
but after it a row is inserted with blank column.
What might be the possible reason for increase in memory consumption.
c++ qt memory-management qtableview qstandarditemmodel
In my Application I am required to insert and remove data from tableView multiple times while application is running.After a complete cycle of Inserting and removing data from tableView memory(RAM) consumption of my application increases as checked in task Manager. After each successive cycle of insertion and deletion memory consumption keeps on Increasing.
Below is my code:
MemoryLeak.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-11-19T16:00:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MemoryLeak
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES +=
main.cpp
mainwindow.cpp
HEADERS +=
mainwindow.h
FORMS +=
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QStandardItemModel *model;
public slots:
void createModel();
private slots:
void on_clearModel_clicked();
void on_displayData_clicked();
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
createModel();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createModel(){
model=new QStandardItemModel(this);
model->setColumnCount(3);
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Age"));
model->setHeaderData(2, Qt::Horizontal, tr("Class"));
ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->tableView->horizontalHeader()->resizeSection(0,580);
ui->tableView->horizontalHeader()->resizeSection(1,280);
ui->tableView->horizontalHeader()->resizeSection(2,280);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setStyleSheet("alternate-background-color:#00FF00;border: 1px solid red;");
}
void MainWindow::on_clearModel_clicked()
{
model->setRowCount(0);
}
void MainWindow::on_displayData_clicked()
{
int rowCount = 0;
QStandardItem *item;
QString value;
for(rowCount = 0; rowCount < 5000; rowCount ++){
value = QString("Person_%1").arg(rowCount);
item = new QStandardItem(QString("%0").arg(value));
model->setItem(rowCount,0,item);
// delete item;// No value is inserted in column
item = new QStandardItem(QString(" %0").arg(rowCount));
model->setItem(rowCount,1,item);
item = new QStandardItem(QString("%0").arg(2));
model->setItem(rowCount,2,item);
}
}
Memory consumption From app start:
11020 KB when app started
16144 KB when display data function Called
12812 KB when clear model called
16356 KB when display data function Called
13304 KB when clear Model Called
So Ram consumption increased by 2 MB in 2 Insert/Remove cycles.
To me it seems to be memory leak issue because memory is allocated to QStandardItem object( item = new QStandardItem(QString("%0").arg(value))
) but is never freed.I tried to free the memory by calling delete item
but after it a row is inserted with blank column.
What might be the possible reason for increase in memory consumption.
c++ qt memory-management qtableview qstandarditemmodel
c++ qt memory-management qtableview qstandarditemmodel
asked Nov 19 at 11:40
Asus gates
63213
63213
Task Manager is not the correct tool to analyze memory consumption. read stackoverflow.com/questions/4690800/how-to-profile-memory-usage
– eyllanesc
Nov 19 at 11:43
@eyllanesc which tool do you suggest. for precise result
– Asus gates
Nov 19 at 11:44
read the link...
– eyllanesc
Nov 19 at 11:44
1
Why do you callmodel->setRowCount(0);
function instead of callingmodel::clear()
function?
– vahancho
Nov 19 at 11:44
@vahancho model::clear also clear my header data
– Asus gates
Nov 19 at 11:46
|
show 7 more comments
Task Manager is not the correct tool to analyze memory consumption. read stackoverflow.com/questions/4690800/how-to-profile-memory-usage
– eyllanesc
Nov 19 at 11:43
@eyllanesc which tool do you suggest. for precise result
– Asus gates
Nov 19 at 11:44
read the link...
– eyllanesc
Nov 19 at 11:44
1
Why do you callmodel->setRowCount(0);
function instead of callingmodel::clear()
function?
– vahancho
Nov 19 at 11:44
@vahancho model::clear also clear my header data
– Asus gates
Nov 19 at 11:46
Task Manager is not the correct tool to analyze memory consumption. read stackoverflow.com/questions/4690800/how-to-profile-memory-usage
– eyllanesc
Nov 19 at 11:43
Task Manager is not the correct tool to analyze memory consumption. read stackoverflow.com/questions/4690800/how-to-profile-memory-usage
– eyllanesc
Nov 19 at 11:43
@eyllanesc which tool do you suggest. for precise result
– Asus gates
Nov 19 at 11:44
@eyllanesc which tool do you suggest. for precise result
– Asus gates
Nov 19 at 11:44
read the link...
– eyllanesc
Nov 19 at 11:44
read the link...
– eyllanesc
Nov 19 at 11:44
1
1
Why do you call
model->setRowCount(0);
function instead of calling model::clear()
function?– vahancho
Nov 19 at 11:44
Why do you call
model->setRowCount(0);
function instead of calling model::clear()
function?– vahancho
Nov 19 at 11:44
@vahancho model::clear also clear my header data
– Asus gates
Nov 19 at 11:46
@vahancho model::clear also clear my header data
– Asus gates
Nov 19 at 11:46
|
show 7 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373866%2fmemory-management-while-inserting-and-removing-values-from-qtableview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Task Manager is not the correct tool to analyze memory consumption. read stackoverflow.com/questions/4690800/how-to-profile-memory-usage
– eyllanesc
Nov 19 at 11:43
@eyllanesc which tool do you suggest. for precise result
– Asus gates
Nov 19 at 11:44
read the link...
– eyllanesc
Nov 19 at 11:44
1
Why do you call
model->setRowCount(0);
function instead of callingmodel::clear()
function?– vahancho
Nov 19 at 11:44
@vahancho model::clear also clear my header data
– Asus gates
Nov 19 at 11:46