Re: Gio & memory management

On Sat, 11 Oct 2008 14:06:49 +0200, nico <nico yojik eu> wrote:


I wrote a program that list recursively files from a given directory.
I'm impressed by the memory usage of such a program (after one minutes,
it uses more than 100 BM on my /usr/ directory... after 5 minute I
have to kill the process : I've only 512 MB of RAM... so my system is 
becoming unstable).
(for comparison, the same program written in php uses less than 1 MB to
list all files of my /usr/).

Is something wrong in my code? Do I forgot to unref or free something?

Yes. It seems you should read again the documentation for GError :).
See the attached patch against your code for a better hadling of

You don't set a GError yourself, instead you initialise this pointer to
NULL and then pass a reference to a particular function. And this
function sets the pointer to a valid GError object which you can use.
And this should be freed when no longer needed.

Additionally, your fiddling with strcat to build the path is completely
unnecessary (and it was wrong), use g_build_filename() for that.

Regarding the memory usage:
maybe there is still something which should be freed/unref'd, but
generally I guess most of the usage comes from the fact that you run
the code recursively. That is, each function call of scan_dir() with
all its allocated data exists until the scan_dir() call from inside
this function is finished.
So, when you walk a tree like

and your scan_dir function is at the last level 'code', then all
scan_dir() calls from the previous levels are still running and waiting
for their child calls to finish and so they still use the memory.


Get my GPG key from

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]