简体   繁体   English

Qt 应用 Memory 使用

[英]Qt Application Memory Usage

I'm working on an application that involves using real time, continuous, audio input, for which I'm using portaudio.我正在开发一个涉及使用实时、连续、音频输入的应用程序,为此我正在使用 portaudio。 I created simple cli version first in order to set things up and the memory usage in the cli version seemed fine.我首先创建了简单的 cli 版本以进行设置,cli 版本中的 memory 使用似乎很好。 In the CLI version, while the recording was on, no extra memory was being allocated aside from the initial buffer.在 CLI 版本中,当录制打开时,除了初始缓冲区外,没有分配额外的 memory。

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).但是,在将相同代码从 CLI 应用到 GUI(为此,我使用 Qt)的过程中,我注意到随着记录的进行,应用程序使用的 memory 不断增加(大约 3 mb/秒)。 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.由于我的应用程序在未定义的时间内涉及音频输入,因此这种行为可能非常致命,因此,我开始从代码中剥离段,以检查哪个区域负责使用这个 memory。 (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) (请注意,在 cli 版本中没有观察到这种行为,并且在 qt 中使用了几乎相同的代码,除了添加了 Q_OBJECT 宏)

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我对使用 Qt 比较陌生,非常感谢一些帮助

My widget apps header file我的小部件应用程序 header 文件

#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. function Recorder::begin_recording() 发出 UpdateVols(std::string) 其中 UpdateVols 是 Recorder class 中的一个信号。 It emits at a pretty fast rate (around 1 every ms).它以相当快的速度发射(每毫秒大约 1 次)。 Does this have something to with the memory usage increasing?这是否与 memory 使用量增加有关? 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.在 CLI 版本中,我可以计算出我需要的任何内容,这是 CLI 版本和 Qt 版本之间代码的最大区别。

The memory usage does not reduce after the begin_recording() function has finished execution though which confuses me.在 begin_recording() function 完成执行后,memory 的使用量并没有减少,但这让我感到困惑。

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.例如,应用程序以 9 mb 的 memory 使用量开始,当 begin_recording() 运行 10 秒时增加到大约 39,之后不做任何事情时保持在 39 mb。

If it was the fast emit rate, I assume that the memory usage will return to normal when it is no longer emitting.如果是快速发射率,我假设 memory 不再发射时使用会恢复正常。

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.此外,在上面的代码中,当点击 beginRecordingButton 时,应用程序变得无响应,但在 function recorder->begin_recording() 执行完成后恢复。 I guess this has something to do with the memory increasing constantly but I'm not too sure.我想这与 memory 不断增加有关,但我不太确定。

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.我相信错误在于我如何使用 QThread object 并将记录器 object 移动到它,但我不确定如何修复它。

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.在找出 memory 使用量增加的地方的过程中,我尝试运行一个只有按钮和标签的裸应用程序,根本不涉及记录器 class。 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.在简单地以零功能打开此应用程序时,只要显示非零 CPU 利用率,memory 的使用量就会持续增加约 0.5mb。 After increasing, it does not fall down even if the cpu utilisation was 0.增加后,即使cpu利用率为0也不会下降。

Overall, I would like to ask,总的来说,我想问一下,

1)How can I fix the issue of the memory usage constantly increasing? 1)如何解决memory使用量不断增加的问题? (I don't think the error lies in the Recorder class as the same code runs without qt at constant memory) (我认为错误不在记录器 class 中,因为相同的代码在没有 qt 的情况下以恒定内存运行)

2)Is the behavior mentioned in the final paragraph normal? 2)最后一段中提到的行为是否正常?

What @GM said in the comments worked. @GM 在评论中所说的有效。 Thank You谢谢你

The problem was the emit rate.问题是排放率。 1khz is too fast and a lot gets build up. 1khz 太快了,而且积累了很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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