QNetworkAccessManager increases CPU and Memory Usage
-
Hai All,
I am running Qt QML application on embedded device (imx6ull). This device is running with Yocto dunfell version and Qt 15.14.2 version.
I Just declared the QNetworkAccessManger class in my QML application. This is causing the CPU usage to go high upto 90% as shown in the below Image. When I run it for long duration, gradually the memory is also increasing. Finally the OOM killer is killing the application
Note: Development OS : Ubuntu 20.04
Process containing ID 598 : ACC1120_RAVEN_A is running with 98.8 of CPU usage. when it runs for log duration the memory usage also increases.
Below is the Code which I am using
main.cpp#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QNetworkAccessManager> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QNetworkAccessManager manager; QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
main.qml
import QtQuick 2.12 import QtQuick.Window 2.12 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") }
pro file
QT += quick QT += network TEMPLATE = app TARGET = ACC1120_RAVEN_APPL # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp RESOURCES += qml.qrc # Default rules for deployment. target.path = /home/root INSTALLS += target
Looks like there is some bug in the QNetworkAccessManager.
How can I fix this issue ?Regards,
James A -
Do you have try to run with gprof? for watch what are using CPU
And you have try to build a console app with QNetworkAccessManager.
-
I tried with console app by running it for 30 seconds. Still I see there is increase in the CPU usage , which went to 98%. From the gprof logs I cloud not make out what is wrong. I updated the code and gprog logs below
#QT += quick QT += network #QT += gui CONFIG += c++11 console CONFIG -= app_bundle #TEMPLATE = app TARGET = ACC1120_RAVEN_APPL CONFIG += debug # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp #RESOURCES += qml.qrc QMAKE_CXXFLAGS_DEBUG *= -pg QMAKE_LFLAGS_DEBUG *= -pg # Default rules for deployment. target.path = /home/root INSTALLS += target
main.cpp
//#include <QGuiApplication> #include <QNetworkAccessManager> #include <QTimer> #include <QCoreApplication> int main(int argc, char *argv[]) { //#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); //#endif // QGuiApplication a(argc, argv); QCoreApplication a(argc, argv); QNetworkAccessManager manager; QTimer::singleShot(30000,[=](){ QCoreApplication::quit(); }); return a.exec(); }
gprof logs
Flat profile: Each sample counts as 0.01 seconds. no time accumulated % cumulative self self total time seconds seconds calls Ts/call Ts/call name 0.00 0.00 0.00 3 0.00 0.00 std::remove_reference<main::{lambda()#1}&>::type&& std::move<main::{lambda()#1}&>(std::remove_reference&&) 0.00 0.00 0.00 1 0.00 0.00 QAtomicInt::QAtomicInt(int) 0.00 0.00 0.00 1 0.00 0.00 QAtomicInteger<int>::QAtomicInteger(int) 0.00 0.00 0.00 1 0.00 0.00 QBasicAtomicInteger<int>::QBasicAtomicInteger(int) 0.00 0.00 0.00 1 0.00 0.00 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, Qt::TimerType, QObject const*, QtPrivate::FunctionPointer) 0.00 0.00 0.00 1 0.00 0.00 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, QtPrivate::FunctionPointer) 0.00 0.00 0.00 1 0.00 0.00 QTimer::defaultTypeFor(int) 0.00 0.00 0.00 1 0.00 0.00 QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, main::{lambda()#1}>::call({lambda()#1}&, void**) 0.00 0.00 0.00 1 0.00 0.00 QtPrivate::QSlotObjectBase::QSlotObjectBase(void (*)(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*)) 0.00 0.00 0.00 1 0.00 0.00 QtPrivate::QSlotObjectBase::~QSlotObjectBase() 0.00 0.00 0.00 1 0.00 0.00 QtPrivate::ApplyReturnValue<void>::ApplyReturnValue(void*) 0.00 0.00 0.00 1 0.00 0.00 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::QFunctorSlotObject({lambda()#1}) 0.00 0.00 0.00 1 0.00 0.00 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::~QFunctorSlotObject() 0.00 0.00 0.00 1 0.00 0.00 void QtPrivate::Functor<main::{lambda()#1}, 0>::call<QtPrivate::List<>, void>({lambda()#1}&, void*, {lambda()#1}&*) 0.00 0.00 0.00 1 0.00 0.00 std::__atomic_base<int>::__atomic_base(int) 0.00 0.00 0.00 1 0.00 0.00 std::atomic<int>::atomic(int) 0.00 0.00 0.00 1 0.00 0.00 main::{lambda()#1}::operator()() const % the percentage of the total running time of the time program used by this function. cumulative a running sum of the number of seconds accounted seconds for by this function and those listed above it. self the number of seconds accounted for by this seconds function alone. This is the major sort for this listing. calls the number of times this function was invoked, if this function is profiled, else blank. self the average number of milliseconds spent in this ms/call function per call, if this function is profiled, else blank. total the average number of milliseconds spent in this ms/call function and its descendents per call, if this function is profiled, else blank. name the name of the function. This is the minor sort for this listing. The index shows the location of the function in the gprof listing. If the index is in parenthesis it shows where it would appear in the gprof listing if it were to be printed. Copyright (C) 2012-2020 Free Software Foundation, Inc. Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. Call graph (explanation follows) granularity: each sample hit covers 4 byte(s) no time propagated index % time self children called name 0.00 0.00 1/3 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, QtPrivate::FunctionPointer) [14] 0.00 0.00 1/3 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, Qt::TimerType, QObject const*, QtPrivate::FunctionPointer) [13] 0.00 0.00 1/3 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::QFunctorSlotObject({lambda()#1}) [20] [9] 0.0 0.00 0.00 3 std::remove_reference<main::{lambda()#1}&>::type&& std::move<main::{lambda()#1}&>(std::remove_reference&&) [9] ----------------------------------------------- 0.00 0.00 1/1 QtPrivate::QSlotObjectBase::QSlotObjectBase(void (*)(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*)) [17] [10] 0.0 0.00 0.00 1 QAtomicInt::QAtomicInt(int) [10] 0.00 0.00 1/1 QAtomicInteger<int>::QAtomicInteger(int) [11] ----------------------------------------------- 0.00 0.00 1/1 QAtomicInt::QAtomicInt(int) [10] [11] 0.0 0.00 0.00 1 QAtomicInteger<int>::QAtomicInteger(int) [11] 0.00 0.00 1/1 QBasicAtomicInteger<int>::QBasicAtomicInteger(int) [12] ----------------------------------------------- 0.00 0.00 1/1 QAtomicInteger<int>::QAtomicInteger(int) [11] [12] 0.0 0.00 0.00 1 QBasicAtomicInteger<int>::QBasicAtomicInteger(int) [12] 0.00 0.00 1/1 std::atomic<int>::atomic(int) [24] ----------------------------------------------- 0.00 0.00 1/1 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, QtPrivate::FunctionPointer) [14] [13] 0.0 0.00 0.00 1 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, Qt::TimerType, QObject const*, QtPrivate::FunctionPointer) [13] 0.00 0.00 1/3 std::remove_reference<main::{lambda()#1}&>::type&& std::move<main::{lambda()#1}&>(std::remove_reference&&) [9] 0.00 0.00 1/1 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::QFunctorSlotObject({lambda()#1}) [20] ----------------------------------------------- 0.00 0.00 1/1 main [7] [14] 0.0 0.00 0.00 1 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, QtPrivate::FunctionPointer) [14] 0.00 0.00 1/1 QTimer::defaultTypeFor(int) [15] 0.00 0.00 1/3 std::remove_reference<main::{lambda()#1}&>::type&& std::move<main::{lambda()#1}&>(std::remove_reference&&) [9] 0.00 0.00 1/1 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, Qt::TimerType, QObject const*, QtPrivate::FunctionPointer) [13] ----------------------------------------------- 0.00 0.00 1/1 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, QtPrivate::FunctionPointer) [14] [15] 0.0 0.00 0.00 1 QTimer::defaultTypeFor(int) [15] ----------------------------------------------- 0.00 0.00 1/1 void QtPrivate::Functor<main::{lambda()#1}, 0>::call<QtPrivate::List<>, void>({lambda()#1}&, void*, {lambda()#1}&*) [22] [16] 0.0 0.00 0.00 1 QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, main::{lambda()#1}>::call({lambda()#1}&, void**) [16] 0.00 0.00 1/1 main::{lambda()#1}::operator()() const [25] 0.00 0.00 1/1 QtPrivate::ApplyReturnValue<void>::ApplyReturnValue(void*) [19] ----------------------------------------------- 0.00 0.00 1/1 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::QFunctorSlotObject({lambda()#1}) [20] [17] 0.0 0.00 0.00 1 QtPrivate::QSlotObjectBase::QSlotObjectBase(void (*)(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*)) [17] 0.00 0.00 1/1 QAtomicInt::QAtomicInt(int) [10] ----------------------------------------------- 0.00 0.00 1/1 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::~QFunctorSlotObject() [21] [18] 0.0 0.00 0.00 1 QtPrivate::QSlotObjectBase::~QSlotObjectBase() [18] ----------------------------------------------- 0.00 0.00 1/1 QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, main::{lambda()#1}>::call({lambda()#1}&, void**) [16] [19] 0.0 0.00 0.00 1 QtPrivate::ApplyReturnValue<void>::ApplyReturnValue(void*) [19] ----------------------------------------------- 0.00 0.00 1/1 std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, Qt::TimerType, QObject const*, QtPrivate::FunctionPointer) [13] [20] 0.0 0.00 0.00 1 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::QFunctorSlotObject({lambda()#1}) [20] 0.00 0.00 1/1 QtPrivate::QSlotObjectBase::QSlotObjectBase(void (*)(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*)) [17] 0.00 0.00 1/3 std::remove_reference<main::{lambda()#1}&>::type&& std::move<main::{lambda()#1}&>(std::remove_reference&&) [9] ----------------------------------------------- 0.00 0.00 1/1 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) [26] [21] 0.0 0.00 0.00 1 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::~QFunctorSlotObject() [21] 0.00 0.00 1/1 QtPrivate::QSlotObjectBase::~QSlotObjectBase() [18] ----------------------------------------------- 0.00 0.00 1/1 QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) [26] [22] 0.0 0.00 0.00 1 void QtPrivate::Functor<main::{lambda()#1}, 0>::call<QtPrivate::List<>, void>({lambda()#1}&, void*, {lambda()#1}&*) [22] 0.00 0.00 1/1 QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, main::{lambda()#1}>::call({lambda()#1}&, void**) [16] ----------------------------------------------- 0.00 0.00 1/1 std::atomic<int>::atomic(int) [24] [23] 0.0 0.00 0.00 1 std::__atomic_base<int>::__atomic_base(int) [23] ----------------------------------------------- 0.00 0.00 1/1 QBasicAtomicInteger<int>::QBasicAtomicInteger(int) [12] [24] 0.0 0.00 0.00 1 std::atomic<int>::atomic(int) [24] 0.00 0.00 1/1 std::__atomic_base<int>::__atomic_base(int) [23] ----------------------------------------------- 0.00 0.00 1/1 QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, main::{lambda()#1}>::call({lambda()#1}&, void**) [16] [25] 0.0 0.00 0.00 1 main::{lambda()#1}::operator()() const [25] ----------------------------------------------- This table describes the call tree of the program, and was sorted by the total amount of time spent in each function and its children. Each entry in this table consists of several lines. The line with the index number at the left hand margin lists the current function. The lines above it list the functions that called this function, and the lines below it list the functions this one called. This line lists: index A unique number given to each element of the table. Index numbers are sorted numerically. The index number is printed next to every function name so it is easier to look up where the function is in the table. % time This is the percentage of the `total' time that was spent in this function and its children. Note that due to different viewpoints, functions excluded by options, etc, these numbers will NOT add up to 100%. self This is the total amount of time spent in this function. children This is the total amount of time propagated into this function by its children. called This is the number of times the function was called. If the function called itself recursively, the number only includes non-recursive calls, and is followed by a `+' and the number of recursive calls. name The name of the current function. The index number is printed after it. If the function is a member of a cycle, the cycle number is printed between the function's name and the index number. For the function's parents, the fields have the following meanings: self This is the amount of time that was propagated directly from the function into this parent. children This is the amount of time that was propagated from the function's children into this parent. called This is the number of times this parent called the function `/' the total number of times the function was called. Recursive calls to the function are not included in the number after the `/'. name This is the name of the parent. The parent's index number is printed after it. If the parent is a member of a cycle, the cycle number is printed between the name and the index number. If the parents of the function cannot be determined, the word `<spontaneous>' is printed in the `name' field, and all the other fields are blank. For the function's children, the fields have the following meanings: self This is the amount of time that was propagated directly from the child into the function. children This is the amount of time that was propagated from the child's children to the function. called This is the number of times the function called this child `/' the total number of times the child was called. Recursive calls by the child are not listed in the number after the `/'. name This is the name of the child. The child's index number is printed after it. If the child is a member of a cycle, the cycle number is printed between the name and the index number. If there are any cycles (circles) in the call graph, there is an entry for the cycle-as-a-whole. This entry shows who called the cycle (as parents) and the members of the cycle (as children.) The `+' recursive calls entry shows the number of function calls that were internal to the cycle, and the calls entry for each member shows, for that member, how many times it was called from other members of the cycle. Copyright (C) 2012-2020 Free Software Foundation, Inc. Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. Index by function name [10] QAtomicInt::QAtomicInt(int) [16] QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, main::{lambda()#1}>::call({lambda()#1}&, void**) (qobjectdefs_impl.h) [22] void QtPrivate::Functor<main::{lambda()#1}, 0>::call<QtPrivate::List<>, void>({lambda()#1}&, void*, {lambda()#1}&*) (qobjectdefs_impl.h) [11] QAtomicInteger<int>::QAtomicInteger(int) [17] QtPrivate::QSlotObjectBase::QSlotObjectBase(void (*)(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*)) [23] std::__atomic_base<int>::__atomic_base(int) [12] QBasicAtomicInteger<int>::QBasicAtomicInteger(int) [18] QtPrivate::QSlotObjectBase::~QSlotObjectBase() [24] std::atomic<int>::atomic(int) [13] std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, Qt::TimerType, QObject const*, QtPrivate::FunctionPointer) (qtimer.h) [19] QtPrivate::ApplyReturnValue<void>::ApplyReturnValue(void*) [9] std::remove_reference<main::{lambda()#1}&>::type&& std::move<main::{lambda()#1}&>(std::remove_reference&&) (move.h) [14] std::enable_if<(!QtPrivate::FunctionPointer<main::{lambda()#1}>::IsPointerToMemberFunction)&&(!std::is_same<char const*, QtPrivate::FunctionPointer>::value), void>::type QTimer::singleShot<int, main::{lambda()#1}>(int, QtPrivate::FunctionPointer) (qtimer.h) [20] QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::QFunctorSlotObject({lambda()#1}) (qobjectdefs_impl.h) [25] main::{lambda()#1}::operator()() const (main.cpp) [15] QTimer::defaultTypeFor(int) [21] QtPrivate::QFunctorSlotObject<main::{lambda()#1}, 0, QtPrivate::List<>, void>::~QFunctorSlotObject() (qobjectdefs_impl.h)
-
I have test this code on a Colibri iMX6ULL
No cpu usage (<0.3%)QT5.12.3 (build my self)
top | grep test 11433 root 20 0 67588 8564 7892 S 0.3 1.7 0:00.21 test
cat /rpoc/cpuinfo processor : 0 model name : ARMv7 Processor rev 5 (v7l) BogoMIPS : 12.00 Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc07 CPU revision : 5 Hardware : Freescale i.MX6 UltraLite (Device Tree) Revision : 0000 Serial : 06398965
uname -a Linux colibri-imx6ull 4.9.166-2.8.6+gd899927728be #1 SMP Mon Apr 1 14:10:54 UTC 2019 armv7l GNU/Linux
Sorry maybe try with old version of qt for check if is not a new bug
-
I switched to Yocto thud version which had Qt 15.11.3. I observed the same problem , the CPU usage is going high.
Actually there should not be any Ethernet or WiFi connection to the Board when the application starts up , only then this is reproducible.
And When it is running with 98% percent of CPU usage , if I connect my ethernet cable to the board ( no internet connection) , the CPU usage comes down. Again if I remove the cable , the CPU usage is normal.So it seems like , only at the application start up , QNetworkAccessManager is looking for some IP address to be assigned from the router either to eth0 or wlan0. It works properly else it keeps increasing the CPU and memory usage.