我如何知道我的 PHP 应用程序是否使用了过多内存?

发布于 2024-08-25 12:55:02 字数 357 浏览 5 评论 0原文

我正在开发一个 PHP Web 应用程序,让用户可以相互联网、预订活动以及相互发送消息。只有大约 100 个用户。

我在使用 Ubuntu 9.10、apache2、mysql 5 和 php 5 的 VPS 上设置了该应用程序。我有 360 Mb RAM,但升级到 720 MB。

最近,我的网络应用程序由于内存使用过多而出现中断。从我在错误日志中可以看出,服务器似乎会自动终止消耗过多内存的 apache 进程。因此,我将内存从 360 MB 升级到 720 MB,作为权宜之计。

所以我的问题是,如何解决这些中断问题?我如何知道我的网站需要更多内存是由于代码质量不佳还是网站自然增长的一部分?确定哪些 PHP 脚本消耗最多内存的最有效方法是什么?

I'm working on a PHP web application that let's users network with each other, book events, and message eachother. There's only about 100 users.

I set up the application on a VPS with Ubuntu 9.10, apache2, mysql 5 and php 5. I had 360 Mb of RAM, but upgraded to 720 MB.

Lately, my web application has been experiencing outages due to excessive memory usage. From what I can tell in error logs, it seems the server automatically kills apache processes that consume too much memory. As a result, I upgraded memory from 360 MB to 720 MB as a stop-gap measure.

So my question is, how do I go about resolving these outage issues? How do I know if my website's need for more memory is due to poor code or if it's part of the website's natural growth? What's the most efficient way to determine which PHP scripts consume the most memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

瑾夏年华 2024-09-01 12:55:03

如果您的进程规模较小,您可以做的另一件事是调低您的 httpd 设置,将最小备用服务器设置为 1,最大备用服务器设置为 3。如果您只有 100 个用户,这应该没问题,因为您不会在意关于仅为少数用户启动进程的开销。

Another thing you can do if your process size is small is turn down your httpd settings to have min spare servers of 1, and max spare servers of 3. If you only have 100 users, this should be fine, as you won't care about the overhead of starting processes for only a few users.

银河中√捞星星 2024-09-01 12:55:03

这是一个支持分析 PHP 的工具: http://xdebug.org/docs/profiler

Here is a tool that supports profiling PHP: http://xdebug.org/docs/profiler

日暮斜阳 2024-09-01 12:55:03

取决于您的 httpd 版本 & php 中,httpd 可能会保留不需要的内存段,并不必要地增加正在运行的进程大小。我有一个执行此操作的盒子,并且我通过每晚重新启动 httpd 解决了这个问题,如下所示:

30 00 * * *     /httpd/sbin/apachectl restart
31 00 * * *     /httpd/sbin/apachectl start
35 00 * * *     /httpd/sbin/apachectl start
40 00 * * *     /httpd/sbin/apachectl start

如您所见,我用 3 个尾随启动来跟踪我的重新启动,以防万一 apache 无法返回重启后恢复正常。 3 可能有点过分了,但另一方面,它不会造成任何伤害,所以为什么不呢。

Depending on your version of httpd & php, httpd could be holding on to segments of memory that it doesn't need, and growing the running process size unnecessarily. I have a box that does this, and I've solved the issue by doing an httpd restart on a nightly basis like so:

30 00 * * *     /httpd/sbin/apachectl restart
31 00 * * *     /httpd/sbin/apachectl start
35 00 * * *     /httpd/sbin/apachectl start
40 00 * * *     /httpd/sbin/apachectl start

As you can see, I follow up my restart with 3 trailing starts, just in case apache fails to come back to life after the restart. 3 is probably overkill, but on the other hand, it doesn't hurt anything, so why not.

秋意浓 2024-09-01 12:55:02

对此没有简单的答案,尽管我怀疑这可能是您的代码中的问题。

php.ini 文件中的 memory_limit 设置是什么?通常我建议至少 4Mb,通常为 16。您要处理多少并发点击?该网站是否进行了大量的统计数据报告?或者通过 PHP 渲染图像?你在任何地方使用 file_get_contents() 吗?

您确实需要设置一些自定义日志记录来报告每个 URL、退出时日志文件的大小。例如,您可以自动添加:(

<?php
register_shutdown_function('log_mem');

function log_mem()
{
   fputs(STDERR, '[' . date('c') . '] ' . memory_get_usage() . ' ' 
     . $_SERVER["REQUEST_URI"] . "\n");
}

注意 - 没有结束标记)
这会将每个 PHP 页面使用的内存写入 error_log,以便您可以更轻松地隔离问题。

HTH

C.

There's no simple answer to this, although I would suspect that it may a problem in your code.

What is the memory_limit setting in your php.ini file? Typically I'd recommend at least 4Mb, and usually 16. How many concurrent hits are you fielding? Is the site doing a lot of heavy reporting of stats? Or rendering of images via PHP? Do you use file_get_contents() anywhere?

You really need to set up some custom logging to report for each URL, the size of the log file at exit. e.g. you could auto-prepend:

<?php
register_shutdown_function('log_mem');

function log_mem()
{
   fputs(STDERR, '[' . date('c') . '] ' . memory_get_usage() . ' ' 
     . $_SERVER["REQUEST_URI"] . "\n");
}

(Note - no closing tag)
This will write out the memory used by each PHP page to the error_log so you can isolate the problem more easily.

HTH

C.

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