PDA

View Full Version : SQLQuery usage of RAM



Momergil
20th April 2012, 15:25
Hello!

I'm developing an application that 'do queries to a' MySQL.

As far as I know, all times that I instantiate a new query and do a research in the MySQL server, the data returns and is stored in the query object, and from that it is read. No problems in that; the software runes fine.

The problem is about the memory that is occupied with the data received fromt he MySQL. If I declare the query as a pointer or an object in a function, as far as I know (by reading the documentation), when the function stop (i.e. go to its end), the query is destroyed and [I image and hope] the memory occupied is released. But what if I declare the query as a global variable (which is the case in my app), so the query is never destroyed? In this case, does all search and data reception is stored in the RAM memory and never goes out till the software is closed (so the query used memory never stop growing)?


-
I'm doing this question because my software is time-by-time growing the used RAM memory in the computer, so after 2 days or more the free memory goes out and the computer "crashes". And now I have to find in a 16 million lines of code software what is causing this RAM problem. What is most interesting, however, is that even if the software is closed, the increased used RAM memory is not released!
-


Thanks,


Momergil

kito
20th April 2012, 16:20
instead why don't you declare your connecion as glabal and create query object only when needed?

Spitfire
23rd April 2012, 13:45
If you're talking about QSqlQuery and QSqlDatabase then you should have no problems and you should not be using pointers or global objects.

If you're talking about sql in general, then it's wrong place to ask the question as answer depends on what you're using :)

In general, query should hold only results of last executed statement.
Unless there's leak in the query object itself, you should not see memory gorowing infinitely.

Momergil
23rd April 2012, 22:19
kito, to be sincere the software was developed by a former job colegue and I simply don't know why he decided to create global queries >.< In my softwares I always use as you sad.


If you're talking about QSqlQuery and QSqlDatabase then you should have no problems and you should not be using pointers or global objects.

If you're talking about sql in general, then it's wrong place to ask the question as answer depends on what you're using :)

In general, query should hold only results of last executed statement.
Unless there's leak in the query object itself, you should not see memory gorowing infinitely.

Well, actually I'm [unfortunately] using Borland C++ Builder 6 with ZEOS library, but the documentation is so terrible that I guessed maybe things would be equal in Qt and Builder in this point.

In any case, it's nice to hear about Qt since I'm developing another software in Qt that uses MySQL as well.


Anyway, no problems. I was already figuring out that this wasn't my software's problem =]


Thanks, God bless,


Momergil

ChrisW67
24th April 2012, 01:38
If I declare the query as a pointer or an object in a function, as far as I know (by reading the documentation), when the function stop (i.e. go to its end), the query is destroyed and [I image and hope] the memory occupied is released.
No. If you allocate something on the heap, i.e. with
qry = new QSqlQuery("SELECT ...", db); then you are responsible for ensuring that it is deleted at the corrected time. If you do not ensure deletion you have the classic memory leak you are describing. You can ensure deletion of QObjects by giving them a suitable parent at creation. QSqlQuery is not a QObject so you have to ensure deletion yourself (in a destructor, explicitly at the end of the function etc.).

There is generally no reason to allocate a QSqlQuery on the heap though.

Momergil
21st May 2012, 13:45
Ok Chris,

thanks!


Momergil