PHP 如何处理 RAM 中的变量?
我很好奇PHP如何处理内存中的变量?如果我有 100 个常量或变量集,它们保存与我的应用程序相关的值,而不是基于每个用户,例如站点名称、版本号等,所有用户都具有相同的值。
如果 100 个用户同时访问该页面,PHP 会将这 100 个变量放入 ram 100 次吗?或者它是否以某种方式只将值存储在 RAM 中 1 次并且所有用户都从中获取?
I am curious how PHP handles variables in memory? If I have 100 constants or variables set that hold values related to my application and not on a per user basis, like site name, version number, things like that, which all users have the same value.
Will PHP put these 100 variables into ram 100 times if 100 users are hitting the page at the same time? Or does it somehow only store the value in RAM 1 time and all users feed off of that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果变量只是一个 $variable,那么是的,100 个变量将乘以 100 个用户。即使我们计算会话存储,在请求运行期间,这些变量也存储在内存中的 $_SESSION 中。
然而,我怀疑你真的需要担心,几个变量占用的空间量很少是一个问题;许多大型 PHP 应用程序将为每个请求加载数千个变量,然后在请求结束时清除它们。 PHP 占用空间并不是很大,内存控制更多地取决于您的 PHP 部署方法(mod_php 与 CGI/FastCGI),而不是与您运行的任何应用程序有关。
更具体地说,您的机器能否同时处理 100 个请求与您的 PHP 脚本无关,因为 PHP 解释器通常比它运行的脚本占用更多的内存。但是,如果每个脚本都将一个非常大的文件加载到字符串(或大型数据库结果集等)中,那么脚本的内存使用可能是一个问题。然而,对于一般情况,这取决于网络服务器的设置。
If the variable just a $variable, then yes, the 100 variables will be multiplied by 100 users. Even when we're counting session storage, during the time the request is running, these variables are also stored in memory, in $_SESSION.
However, I doubt you really need to be concerned, the amount of space taken up by a few variables is rarely an issue; many large PHP applications will load thousands of variables for each request, and then clean them out at the end of the request. The PHP footprint is not terribly large, and memory control is more up to your deployment method of PHP (mod_php vs CGI/FastCGI) than anything to do with any applications you run.
To be more specific, whether your machine can handle 100 simultaneous requests is mostly unrelated to your PHP script, as the PHP interpreter generally takes up a lot more memory than the scripts it runs. If, however, each of those scripts is loading a very large file into a string (or a large database result set, or the like) then it is possible that your script's memory use is a concern. For the general case however, it's something that comes down to the webserver's setup.
您可以尝试
memory_get_usage()
监视如何处理内存以响应某些声明。例如,我做了以下工作:与存储在
$_SESSION
中相比:You could experiment with
memory_get_usage()
to monitor how memory is being handled in response to certain declarations. For instance, I worked up the following:Comparing to storing in
$_SESSION
:只有代码页在进程之间隐式共享。进程的数据是独立的,线程的数据是组合的,除非通过例如 SysV 共享内存显式覆盖。
Only code pages are implicitly shared between processes. Data is separate for processes and combined for threads unless this is explicitly overridden via e.g. SysV shared memory.