ouch! [or how to save lots of memory with only a pair of ()]

nicholas on 2005-12-17T23:29:21

Ouch! I started with this:

$ perl -MDevel::Size=total_size -le 'use POSIX; print total_size (\%POSIX::)'
398550
$ perl -MDevel::Size=total_size -le 'use POSIX(); print total_size(\%POSIX::)'
70603

and then found that things got "better". (Although I think that I will be corrected somewhat on the closure comment)

One thought about what this means is that anything running with ithreads is likely to spawn threads faster if it avoids as much importing as it can. So go check your use statements in your mod_perl programs.


Oh, it's better than that....

Elian on 2005-12-18T18:20:07

Since POSIX throws things in other places too, you've got to look at the (very) big picture:


perl -MDevel::Size=total_size -le 'use POSIX; print total_size(\%::)'
728436
perl -MDevel::Size=total_size -le 'use POSIX(); print total_size(\%::)'
213560

You're not saving 300K, you're saving 500K...

Forking

Juerd on 2005-12-18T23:31:38

Although when *forking*, you're typically sharing all the memory that isn't touched by the child processes.

So if you run mod_perl the old fashioned (and IMO still: better) way, consider loading all of POSIX upfront. This will improve runtime of the first script (per Apache child!) that needs something out of POSIX, and memory use, as you now load everything only once instead of parts duplicately.

As always: the way you use code is important for the way you should write it. A good rule of thumb is to always explicitly import (never use the default imports), and if you can benefit from loading everything in the child process, do it explicitly there.

My Apache processes on one machine are 15 MB each of which 11 MB is shared. This used to be 15 MB of which only 3 MB was shared. With a few dozen Apache processes, this saves me a whole lot of memory!