简体   繁体   中英

Qt Application Memory Usage

I'm working on an application that involves using real time, continuous, audio input, for which I'm using portaudio. I created simple cli version first in order to set things up and the memory usage in the cli version seemed fine. In the CLI version, while the recording was on, no extra memory was being allocated aside from the initial buffer.

However, in the process of applying the same code from the CLI to a GUI (for which, I'm using Qt), I notice that as the recording proceeds, the memory being used by the application constantly increases (by about 3 mb/sec). Since my application involves audio input for an undefined amount of time, such behaviour can be extremely fatal and thus,I started stripping off segments from the code inorder to check which area was responsible for this memory being used. (note that this behavior was NOT being observed from the cli version, and that nearly the same code was used in qt, aside from the addition of a Q_OBJECT macro)

I tried this for a while but could not find anything.

I am relatively new to using Qt and would greatly appreciate some help with this

My widget apps header file

#ifndef DETECTORMAIN_H
#define DETECTORMAIN_H

#include <QMainWindow>
#include <QThread>
#include <QPointer>
#include "utils/rectools.h"   /*Custom header file, the implementation of 
                                which has been carried over from the cli version
                                which worked with constant memory usage. 
                                Contains a class Recorder with the Q_OBJECT macro*/

QT_BEGIN_NAMESPACE
namespace Ui { class detectorMain; }
QT_END_NAMESPACE

class detectorMain : public QMainWindow
{
    Q_OBJECT

public:
    detectorMain(QWidget *parent = nullptr);
    ~detectorMain();


private slots:
    void on_beginRecordingButton_clicked();

    void on_stopRecordingButton_clicked();

    void updateVolLabel(std::string);

private:
    Ui::detectorMain *ui;
    QPointer<Recorder> recorder;
    QThread* thread;

};
#endif // DETECTORMAIN_H

Source file

#include "detectormain.h"
#include "ui_detectormain.h"


detectorMain::detectorMain(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::detectorMain)
{
    ui->setupUi(this);
    recorder = new Recorder;
    thread = new QThread;
    recorder->moveToThread(thread);
    thread->start();

    ui->beginRecordingButton->setEnabled(true);
    ui->stopRecordingButton->setEnabled(false);

    connect(recorder, SIGNAL(UpdateVols(std::string)), this, SLOT(updateVolLabel(std::string)));
}

detectorMain::~detectorMain()
{
    delete ui;
}


void detectorMain::on_beginRecordingButton_clicked()
{
    ui->beginRecordingButton->setEnabled(false);
    ui->stopRecordingButton->setEnabled(true);
    recorder->begin_recording();

}

void detectorMain::on_stopRecordingButton_clicked()
{
    recorder->stop_recording();

    ui->beginRecordingButton->setEnabled(true);
    ui->stopRecordingButton->setEnabled(false);
}

void detectorMain::updateVolLabel(std::string vol)
{
    ui->VolLabel->setText(QString::fromStdString(vol));
    QCoreApplication::processEvents();
}

The function Recorder::begin_recording() emits UpdateVols(std::string) where UpdateVols is a signal in the Recorder class. It emits at a pretty fast rate (around 1 every ms). Does this have something to with the memory usage increasing? In the CLI version I could just cout whatever I needed to and this is the most major difference in code between the CLI version and the Qt version.

The memory usage does not reduce after the begin_recording() function has finished execution though which confuses me.

For example, the app starts at 9 mb memory usage, increases to about 39 when begin_recording() runs for 10 seconds, and stays at 39mb when not doing anything afterwards.

If it was the fast emit rate, I assume that the memory usage will return to normal when it is no longer emitting.

Additionally, in the above code, when the beginRecordingButton is clicked, the app becomes unresponsive, but resumes once the function recorder->begin_recording() has finished execution. I guess this has something to do with the memory increasing constantly but I'm not too sure.

I believe the error to lie somewhere in how I'm using the QThread object and moving the recorder object to it, but I'm not sure how to fix it.

In the process of finding out where the memory usage was being increased, I tried running a bare app with just the buttons and labels, not involving the recorder class at all. On simply opening this app with zero functionality, the memory usage kept increasing by about 0.5mb whenever it showed a non zero CPU utilization. After increasing, it does not fall down even if the cpu utilisation was 0.

Overall, I would like to ask,

1)How can I fix the issue of the memory usage constantly increasing? (I don't think the error lies in the Recorder class as the same code runs without qt at constant memory)

2)Is the behavior mentioned in the final paragraph normal?

What @GM said in the comments worked. Thank You

The problem was the emit rate. 1khz is too fast and a lot gets build up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM