3

I noticed that whenever I use create_function a name is assigned to the function that looks like:

lambda_N

What's weird is that if I refresh the page that N increases, like lambda_2, lambda_3 etc.

Does that mean that these functions stay in memory across page requests? Does the memory explode if I create like 20.000 functions like these?

1

2 Answers 2

4

Why does the number increase if you hit refresh

With mod_php the lifetime of the executor (which representes the state of the php interpreter) is longer than the lifetime of a request since the executor is stored in the memory of the apache process. The apache process isn't terminated by default so it can handle new requests after the old request is finished. Basically each apache process has its own executor.

The number N in \0lambda_N must be unique for each executor since this is the name of the function in the function table (which is also stored per executor). The number N is generated from a counter named lambda_count stored in the _zend_executor_globals structure. It gets incremented on each call to create_function.

So if you refresh the page your request gets handled by the same process and the lambda_count seems to increment each time (while experimenting I found out that with refreshing ctrl-f5 or by executing other requests, the number is more random so I assume the process is switches more often).

Does the function stay in memory

The short answer is no. Apparently the function table (among other things like the op arrays) is cleaned after each request in the php_request_shutdown callback. As far as I can see the function entry is still in the hash but the functions opcodes are freed (I could've missed the part where the hash-entries are deleted).

So certain members of the executor are shared across multiple requests, but the sensible ones are cleared out.

I'm not sure how the lifetime of the functions are handled if you use fcgi, where the PHP process also serves more than one request.

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

Comments

2

Basically yes, if you'll create a lot of lambda function you can run out of memory. The allocated memory is never released. Hovever there's a workaround for that:

http://www.php.net/manual/en/function.create-function.php#98939

2 Comments

Can I use these functions like a persistent caching mechanism?
It's really better to use real cache mechanism if you need cache.

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.