3

I have a big php cronjob that run every 20minutes. After each execution the RAM memory used by the virtual server increase and not properly free once the script is terminated!

But why? The memory isn't cached

             total       used       free     shared    buffers     cached
Mem:          2048       1948         99          0          0         88
-/+ buffers/cache:       1859        188
Swap:            0          0          0

The script make some curl/multi_curl and update the Mysql DB.

There is a way to debug the script seeing all the variable and the memory usage over time? or simple the ones not released at the end...

1 Answer 1

1

The memory should be freed when a program exits (regardless if the program freed it or not). For example.

[kbrandt@ny-kbrandt01: ~] cat eat_mem.c
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
    #1 GByte
    const int m = 1024*1024*1024;
    #Allocate a gig
    void *p = (int*)malloc(m);
    #Write a Gigs worth of zeros to that gig
    memset(p,0,m);
    sleep(10);
    #In theory I should free(p) here (heh... heh... I said "free p", get it?)
    return 0;
}

[kbrandt@ny-kbrandt01: ~] gcc eat_mem.c; ./a.out &; sleep 1; free -m;sleep 10; free -m
[1] 10666
             total       used       free     shared    buffers     cached
Mem:          7872       1848       6023          0        143        431
-/+ buffers/cache:       1273       6599
Swap:        30467          0      30467
[1]  + done       ./a.out
             total       used       free     shared    buffers     cached
Mem:          7872        823       7049          0        143        431
-/+ buffers/cache:        247       7624
Swap:        30467          0      30467

So assuming your script run the by the cron job exits, that would not be the issue. So you need to see which program is growing in memory. You can just watch this using top (You can sort by resident memory size by pressing F (capital) and then q for RES in top) and see what program grows.

My guess is that it is going to be MySQL, because whatever your script does, it does stuff in SQL and SQL ends up using more memory. This probably actually isn't a problem if I had to guess, MySQL will take the memory it can get and use it wisely most of the time.

3
  • +1 for recommending top Jun 19, 2014 at 12:11
  • In the top there is php. And the memory still occupied by mysql should be "cache", not "used" right? I tried to restart mysqld and no memory changed. The point is that I don't do anything special in the script, I don't allocate memory like your example. I'm really stuck. Jun 19, 2014 at 12:45
  • @AndreaSica: Re MySQL. I'm not positive, but I don't think MySQL would be cache, I would think it memmaps the the database files so it has more control. Higher level languages like PHP will be allocating memory with things like malloc(), they just do it for you. How about a screenshot of top + free -m output for each the following: 1) After a reboot and before your script has run 2) While your script is running, and 3) After your script has completed Jun 19, 2014 at 12:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .