PHP 如何处理 RAM 中的变量?

发布于 2024-08-18 11:53:42 字数 186 浏览 4 评论 0原文

我很好奇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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春庭雪 2024-08-25 11:53:42

如果变量只是一个 $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.

护你周全 2024-08-25 11:53:42

您可以尝试 memory_get_usage() 监视如何处理内存以响应某些声明。例如,我做了以下工作:

echo memory_get_usage(); // 84944
$var = "foo";
echo memory_get_usage(); // 85072
unset($var);
echo memory_get_usage(); // 85096

与存储在 $_SESSION 中相比:

echo memory_get_usage(); // 85416
$_SESSION['var'] = "foo";
echo memory_get_usage(); // 85568
unset($_SESSION['var']);
echo memory_get_usage(); // 85584

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:

echo memory_get_usage(); // 84944
$var = "foo";
echo memory_get_usage(); // 85072
unset($var);
echo memory_get_usage(); // 85096

Comparing to storing in $_SESSION:

echo memory_get_usage(); // 85416
$_SESSION['var'] = "foo";
echo memory_get_usage(); // 85568
unset($_SESSION['var']);
echo memory_get_usage(); // 85584
放飞的风筝 2024-08-25 11:53:42

只有代码页在进程之间隐式共享。进程的数据是独立的,线程的数据是组合的,除非通过例如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文