Let me explain what happens using top:
First, I run my program and this is the memory usage:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11180 rafael 15 0 45424 26m 8372 S 0.0 5.3 0:00.56 AVP
After execute this code:
FrameRGB
* Loaders
::qt_loader(QString str
) { if(img.isNull()) {
printf("Failed to open image\n");
return NULL;
}
int w = img.width();
int h = img.height();
FrameRGB *res = new FrameRGB(w, h);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int color = img.pixel(x,y);
int R = qRed(color);
int G = qGreen(color);
int B = qBlue(color);
res->simpleSetPixel(y, x, PixelRGB(R, G, B));
}
}
return res;
}
FrameRGB * Loaders::qt_loader(QString str) {
QImage img(str);
if(img.isNull()) {
printf("Failed to open image\n");
return NULL;
}
int w = img.width();
int h = img.height();
FrameRGB *res = new FrameRGB(w, h);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int color = img.pixel(x,y);
int R = qRed(color);
int G = qGreen(color);
int B = qBlue(color);
res->simpleSetPixel(y, x, PixelRGB(R, G, B));
}
}
return res;
}
To copy to clipboard, switch view to plain text mode
This function loads an image using QImage... and the result is:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11180 rafael 18 0 153m 121m 9928 S 0.0 24.1 0:04.78 AVP
In theory, after done some operations, the memory would back to ~45m... but it doesn't
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11180 rafael 15 0 151m 119m 9928 S 0.0 23.7 0:05.23 AVP
But, if I change this code to:
FrameRGB
* Loaders
::qt_loader(QString str
) { int w = 320;
int h = 240;
FrameRGB *res = new FrameRGB(w, h);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
res->simpleSetPixel(y, x, PixelRGB(0, 0, 0));
}
}
return res;
}
FrameRGB * Loaders::qt_loader(QString str) {
int w = 320;
int h = 240;
FrameRGB *res = new FrameRGB(w, h);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
res->simpleSetPixel(y, x, PixelRGB(0, 0, 0));
}
}
return res;
}
To copy to clipboard, switch view to plain text mode
In other words, I discarded the QImage loading method and replaced by a constant flat color. In this case, the memory comes back to ~45m! So, I deduced that something is wrong with the QImage.
Bookmarks