donald128 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, guys!

Problem: my mod_perl leaks and I cannot control it.

I run mod_perl script under Ubuntu (production code).
Usually there are 8-10 script instances running concurrently.
According to Unix "top" utilty each instance takes 55M of memory.
55M is a lot, but I was told here that most of this memory is shared.
http://stackoverflow.com/questions/1547590/how-to-minimize-memory-allocation-of-modperl-script
The memory is leaking.
There are 512M on the server.
There is a significant decrease of free memory in 24 hours after reboot.
Test: free memory on the system at the moment 10 scripts are running:
-after reboot: 270M
-in 24 hours since reboot: 50M

In 24 hours memory taken by each script is roughly the same - 55M (according to "top" utility).
I don't understand where the memory leakes out.
And don't know how can I find the leaks.
I share memory, I preload all the modules required by the script in startup.pl.

One more test.
A very simple mod_perl script ("Hello world!") takes 52M (according to "top")

According to "Practical mod_perl" I can use GTop utility to measure the real memory taken by mod_perl.
http://modperlbook.org/html/9-3-Process-Memory-Measurements.html
I have made a very simple script that measures the memory with GTop.
It shows there are 54M real memory taken by a very simple perl script!
54 Megabytes by "Hello world"?!!!

proc-mem-size: 59,707392
proc-mem-share: 52,59264
diff: 54,448128

There must be something wrong in the way I measure mod_perl memory.
Help please!
This problem is driving me mad for several days.

These are the snapshots of "top" output after reboot and in 24 hours after reboot.
The processes are sorted by Memory.

---- RIGHT AFTER REBOOT ---- top - 10:25:24 up 55 min, 2 users, load average: 0.10, 0.07, 0.07 Tasks: 59 total, 3 running, 56 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 97.3%id, 0.7%wa, 0.0%hi, 0.0%si, + 2.0%st Mem: 524456k total, 269300k used, 255156k free, 12024k buffe +rs Swap: 0k total, 0k used, 0k free, 71276k cache +d PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2307 www-data 15 0 58500 27m 5144 S 0.0 5.3 0:02.02 apache2 2301 www-data 15 0 58492 27m 4992 S 0.0 5.3 0:02.09 apache2 2302 www-data 15 0 57936 26m 4960 R 0.0 5.2 0:01.74 apache2 2895 www-data 15 0 57812 26m 5048 S 0.0 5.2 0:00.98 apache2 2903 www-data 15 0 56944 26m 4792 S 0.0 5.1 0:01.12 apache2 2886 www-data 15 0 56860 26m 4784 S 0.0 5.1 0:01.20 apache2 2896 www-data 15 0 56520 26m 4804 S 0.0 5.1 0:00.85 apache2 2911 www-data 15 0 56404 25m 4768 S 0.0 5.1 0:00.87 apache2 2901 www-data 15 0 56520 25m 4744 S 0.0 5.1 0:00.84 apache2 2893 www-data 15 0 56608 25m 4740 S 0.0 5.1 0:00.73 apache2 2277 root 15 0 51504 22m 6332 S 0.0 4.5 0:01.02 apache2 2056 mysql 18 0 98628 21m 5164 S 0.0 4.2 0:00.64 mysqld 3162 root 15 0 6356 3660 1276 S 0.0 0.7 0:00.00 vi 2622 root 15 0 8584 2980 2392 R 0.0 0.6 0:00.07 sshd 3083 root 15 0 8448 2968 2392 S 0.0 0.6 0:00.06 sshd 3164 par 15 0 5964 2828 1868 S 0.0 0.5 0:00.05 proftpd 1 root 18 0 3060 1900 576 S 0.0 0.4 0:00.00 init 2690 root 17 0 4272 1844 1416 S 0.0 0.4 0:00.00 bash 3151 root 15 0 4272 1844 1416 S 0.0 0.4 0:00.00 bash 2177 root 15 0 8772 1640 520 S 0.0 0.3 0:00.00 sendmail- +mta 2220 proftpd 15 0 5276 1448 628 S 0.0 0.3 0:00.00 proftpd 2701 root 15 0 2420 1120 876 R 0.0 0.2 0:00.09 top 1966 root 18 0 5396 1084 692 S 0.0 0.2 0:00.00 sshd ---- ROUGHLY IN 24 HOURS AFTER REBOOT top - 17:45:38 up 23:39, 1 user, load average: 0.02, 0.09, 0.11 Tasks: 55 total, 2 running, 53 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, + 0.0%st Mem: 524456k total, 457660k used, 66796k free, 127780k buffe +rs Swap: 0k total, 0k used, 0k free, 114620k cache +d PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 16248 www-data 15 0 63712 35m 6668 S 0.0 6.8 0:23.79 apache2 19417 www-data 15 0 60396 31m 6472 S 0.0 6.2 0:10.95 apache2 19419 www-data 15 0 60276 31m 6376 S 0.0 6.1 0:11.71 apache2 19321 www-data 15 0 60480 29m 4888 S 0.0 5.8 0:11.51 apache2 21241 www-data 15 0 58632 29m 6260 S 0.0 5.8 0:05.18 apache2 22063 www-data 15 0 57400 28m 6396 S 0.0 5.6 0:02.05 apache2 21240 www-data 15 0 58520 27m 4856 S 0.0 5.5 0:04.60 apache2 21236 www-data 15 0 58244 27m 4868 S 0.0 5.4 0:05.24 apache2 22499 www-data 15 0 56736 26m 4776 S 0.0 5.1 0:00.70 apache2 2055 mysql 15 0 100m 25m 5656 S 0.0 5.0 0:20.95 mysqld 2277 root 18 0 51500 22m 6332 S 0.0 4.5 0:01.07 apache2 22686 www-data 15 0 53004 21m 4092 S 0.0 4.3 0:00.21 apache2 22689 root 15 0 8584 2980 2392 R 0.0 0.6 0:00.06 sshd 2176 root 15 0 8768 1928 736 S 0.0 0.4 0:00.00 sendmail- +mta 1 root 18 0 3064 1900 576 S 0.0 0.4 0:00.02 init 22757 root 15 0 4268 1844 1416 S 0.0 0.4 0:00.00 bash 2220 proftpd 18 0 5276 1448 628 S 0.0 0.3 0:00.00 proftpd 22768 root 15 0 2424 1100 876 R 0.0 0.2 0:00.00 top 1965 root 15 0 5400 1088 692 S 0.0 0.2 0:00.00 sshd 2258 root 18 0 3416 1036 820 S 0.0 0.2 0:00.01 cron 1928 klog 25 0 2248 1008 420 S 0.0 0.2 0:00.04 klogd 1946 messageb 19 0 2648 804 596 S 0.0 0.2 0:01.63 dbus-daem +on 1908 syslog 18 0 2016 716 556 S 0.0 0.1 0:00.17 syslogd

Replies are listed 'Best First'.
Re: Can I measure memory taken by mod_perl?
by almut (Canon) on Oct 15, 2009 at 09:08 UTC
    I have not taken the snapshot of the header, but it displayed 50M of free memory ...

    Why not use the same tool (i.e. top instead of ps aux) after 24 hours? That would make it easier to assess what's going on (for example, the ps output does not show system buffer/cache usage).

    That said, I don't really see a significant change in either the resident or virtual memory usage of the apache processes after 24 hours (in the ps output), so I suspect there's some misinterpretation of the data going on... It's pretty normal for a Linux system to show only around 50M "free" after it has been running for a while — independently of how much RAM it has. That doesn't necessarily mean there's something wrong.  How does your memory "problem" actually manifest, i.e. how does Apache behavior change, and/or what doesn't work any longer?

    In other words, I don't think there are any leaks in your case. Linux is just using the memory for buffering purposes etc. to improve performance, instead of leaving it sit there unused...  It's maybe worth pointing out that those buffers will be discarded and the memory made available as soon as there's a "better" use for it, such as when an application needs memory.  So, don't worry, be happy! :)  And google for Linux memory management for some more background.

      Hi, almut!
      Thank you so much for the answer!

      I have updated the 24h top snapshot - I've taken it right now.
      Probably now you can tell more on the subject?

      Thanks, I wasn't aware that Linux may not release the memory (for example when the process dies). My assumption was misleading.

      At the moment the server is running fast and stable.
      Of course I'm happy, but I want to know the limits.

      I'm afraid of swapping effect. I don't have an idea how to calculate the maximum number of mod_perl instances that can run stable at the same time (without swapping). So I could set MaxClients effectively.

      Thanks again for the clarification on Linux memory management.

      Pavel