-1

I have following code which I created to check how memory can allocate and free using using qt and c++

Platform: Linux, Ubunut 16.04

testmemeory.h

#include <QObject>

class testMemeory : public QObject
{
    Q_OBJECT
public:
    explicit testMemeory(QObject *parent = nullptr);
    void freeMEm();
    char* str;

};

testmemeory.cpp

testMemeory::testMemeory(QObject *parent) : QObject(parent)
{
        str = new char [30000];
}

void testMemeory::freeMEm(){
    delete [] str;
}

This is the array I used to store the object.

std::vector<testMemeory*> testList;

And I am allocating the memory using

void MainWindow::allocateMemory()
{
    for(int i=0;i<50000;i++){
        testMemeory *t = new testMemeory();
        testList.push_back(t);
    }

    qDebug()<<"Memory allocated..";
}

And releasing memory using

void MainWindow::relaseMemory()
{
    for(int i=0;i<testList.size();i++)
        testList.at(i)->freeMEm();

    qDeleteAll(testList);
    testList.clear();

    qDebug()<<"Memory freed..";

}

When I excute allocate memory the RAM used for the application increased from 150MB to about 350MB where as if call relaseMemory() function the RAM is still 350 MB and it not reducing to 150 MB

What could be the reason.

CodeDezk
  • 1,230
  • 1
  • 12
  • 40
  • 5
    Some operating systems don't automatically unmap memory pages from your process. They are still marked as free, so the OS might unmap (or otherwise reuse) them if needed, but they are still counted for your process. It's a false positive. – Some programmer dude Feb 11 '19 at 10:10
  • I have tested this in Ubuntu 16.04. – CodeDezk Feb 11 '19 at 10:11
  • Duplicate: https://stackoverflow.com/questions/29529135/why-does-my-programs-memory-not-release – Yksisarvinen Feb 11 '19 at 10:11
  • 1
    You have released memory as far as your program is concerned i.e. the memory released can no longer be used without causing undefined behaviour. However, there is no guarantee that the memory will be returned to the host environment - which means it can remain allocated to your program, even if your program cannot use it. The reason for that behaviour is to allow your program to dynamically allocate memory again, without the performance hit of requesting it anew from the host system. – Peter Feb 11 '19 at 10:13

1 Answers1

2

Typically, when an application needs memory, it requests it from an in-process memory arena and, if there's not enough, the arena will get more from the host environment. Then it's handed out from the arena as needed by individual allocations.

When the application is finished with a bit of memory, it hands it back to the arena for later allocations. It typically doesn't get handed back from the arena to the host.

So, if you're measuring how much memory is allocated to the application from the point of view of the host, it will tend to go up but not down, at least until the application exits, at which point all process memory will be given back.

In other words, something like this:

+-------------+               +-------+
|             | <- allocate - |       |             +------+
| application |               | arena | <- obtain - | host |
|             | --- free ---> |       |             +------+
+-------------+               +-------+                 ^
\_____________________________________/                 |
                   |                                    |
                   +------ all handed back on exit -----+
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953