10

I have a script, that gets data send (about 16MB in size), which I read using php://input.

$in = file_get_contents("php://input");

But I'm running into memory limit issues, so I traced it down using memory_get_usage() and realized, that the script is already consuming 47MB memory on startup (before issuing any command).

My guess is, this is due to PHP pre-filing global variables. So I'm searching for a way to unset those, since I'm only reading the php://input stream.

I tried:

unset($GLOBALS);
unset($_SERVER);
unset($_GET);
unset($_POST);
unset($_FILES);
unset($_REQUEST);
unset($_ENV);
unset($_COOKIE);
unset($HTTP_RAW_POST_DATA);
unset($http_response_header);
unset($argc);
unset($argv);
gc_collect_cycles();

And it reduces the memory usage from 47MB to 31MB.

Is there anything else I can do, to have PHP consume less memory on startup?

It would give me more memory to work with the data I received.

10
  • My PHP Version is 5.4.9 Commented Oct 19, 2013 at 8:42
  • you could increase the memory_limit in your php ini file. Interesting question though! Commented Oct 19, 2013 at 8:46
  • My provider won't increase it. And anyhow, I would prefer using less memory, than just increasing limits. Commented Oct 19, 2013 at 8:47
  • can you provide any details of what was the gain after each change?. I mean something like "after doing an unset($variable); I got 2 kb, after unset($other_variable); I got 2MB". Do you have a relation of the loaded modules? The 47 MB is after you have set some variables or is the same even with an empty page with just a memory_get_usage()? Commented Mar 15, 2014 at 5:50
  • Further to PatomaS' comment, are you sure your memory footprint is not 40 k rather than 40 M? On my box I am reading <100kB. Just to rule this out: memory_get_usage() returns a value in bytes. Commented Mar 15, 2014 at 13:38

4 Answers 4

5

It is interesting that at startup your script is taking so much memory. But I'd suggest you to modify your program in such a way that it can work with lesser amount of memory.

$in = file_get_contents("php://input");

will load all the input data in memory, which is bad. Instead try to read a few KB's of data in memory, process it and the proceed with the rest until you have consumed all the data.

You can use http://php.net/manual/en/function.stream-get-line.php

Sign up to request clarification or add additional context in comments.

3 Comments

The data I get, is JSON encoded. Afaik there is no serial json decode function. See stackoverflow.com/questions/15077870/… So unless, I would build one my own, I need to fetch all data.
If that is the case, then may be you can try this library to parse JSON github.com/kuma-giyomu/JSONParser. See stackoverflow.com/questions/4049428/…
That could help. But I think its a good idea to minimize the memory usage at startup as well. For it is taking 47, of 100 MB memory limit, which is already half the memory the script has got.
3
+50

Do you have xDebug enabled on your server? It could another extension like that. phpinfo() will tell you what extensions are installed.

What is the memory usage for a simple script like

<?php 
echo memory_get_usage(false) . "\n";
echo memory_get_usage(true) . "\n";
echo "test\n";
?>

Also try

var_dump(get_defined_vars()); 

to see all the variable loaded in your script.

Comments

0

Send the data using Content-Type: application/json

PHP will not set $_POST variable then.

Comments

0

If it is allowed by your configuration and hardware you can increase the memory limit at runtime:

ini_set('memory_limit', '128M');

or increase it through .htaccess or apache configuration, as follows:

php_value memory_limit 128M

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.