I have a Drupal 7 installation that apparently leaks memory somewhere. The memory usage of nginx is going up in production until the server fails. This started basically when I added a new node with PHP content to it. My PHP code does basically this:


global $user;
$token = uuid();
$uid = user_load($user->uid);
$myprofile = 'main';
$profile = profile2_load_by_user($uid, $myprofile);
$birth_date= $profile->field_birth_date['und'][0]['value'];

$conn = mysql_connect ("my.sql.server:3306/somedb", "user", "pass");
mysql_select_db("somedb", $conn);
mysql_query("INSERT INTO special_table (TOKEN, USER) VALUES ('$token','$user->name')");
mysql_close ($conn);

echo 'output some html based on birthdate..';

In addition I use memcached module. That custom DB connection that I have to open and do a single insert is not the DB of the drupal installation, that is going into a separate database. Surely not the nicest solution, but I guess that should not leak anything.

Do you see anything in this code that can possibly lead to memory leak?

  • 1
    I dont see anything wrong. Dunno why you're using basic php Mysql functions, D7 supports multiple database connections ...
    – tenken
    Oct 13, 2013 at 18:04
  • thank you @tenken for taking a look. that basic mysql thing was just the quickest thing that I could come up with.
    – jabal
    Oct 13, 2013 at 18:21
  • 3
    No need for $uid = user_load($user->uid); just use $user->uid also no need for mysql_query use db_set_active and db_query instead Oct 13, 2013 at 20:51

2 Answers 2


It doesn't look like this is causing a memory leak as @tenken said in his comment. However, since this is going in a node, that whole PHP snippet is stored in the database which isn't recommended. It will take more memory to grab this code from the database, then run it.

Try sticking the code in a function in PHP file and then running that function somewhere else on the site, i.e. a custom module. This is a good tutorial on how to do that.


mysql_connect() is deprecated. See that big red box in the API docs? So yes, it may cause various unhealthy effects on your site. Use Drupal's built-in multiple database handling to be safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.