简体   繁体   English

调试 LAMP/CakePHP 站点内存泄漏?

[英]Debugging LAMP/CakePHP site memory leak?

I run a site on AWS, built on CakePHP 2.2.3, PHP 5.6.32, and a MySQL database.我在 AWS 上运行一个站点,该站点基于 CakePHP 2.2.3、PHP 5.6.32 和 MySQL 数据库构建。 Recently, I've had problems with server crashes that seem to start when the server runs out of memory.最近,我遇到了服务器崩溃的问题,这些问题似乎是在服务器内存不足时开始的。

I've set up CloudWatch to monitor memory usage, and see the following over the last few days (server was reset ~5 days ago):我已经设置了 CloudWatch 来监控内存使用情况,并在过去几天看到了以下内容(服务器大约 5 天前重置):

CloudWatch 内存使用日志

Running 'top' on the server produces the same result for memory usage.在服务器上运行“top”会产生相同的内存使用结果。

This looks like a memory leak to me (is that correct?), but my understanding is that PHP 5.6 is supposed to do garbage collection.这对我来说看起来像是内存泄漏(对吗?),但我的理解是 PHP 5.6 应该进行垃圾收集。 I don't use explicit memory allocation in my code at all, but can't rule out the possibility that CakePHP does.我根本没有在我的代码中使用显式内存分配,但不能排除 CakePHP 这样做的可能性。 And maybe it's no a problem with PHP at all.也许这对 PHP 来说根本不是问题。

I'm new to web development, and am at a bit of a loss on where to start.我是 Web 开发的新手,不知道从哪里开始。 Google searches come back with solutions to OOM errors due to large allocations, but that's not the issue here. Google 搜索返回了由于大量分配导致的 OOM 错误的解决方案,但这不是这里的问题。 What could cause a site to show an almost monotonically increasing memory usage like this?是什么导致站点显示出像这样几乎单调增加的内存使用量? How should I go about finding the culprit?我应该如何去寻找罪魁祸首?

Edit : Output of 'top' sorted by memory usage below, in case it's helpful.编辑:'top' 的输出按下面的内存使用情况排序,以防有帮助。

顶部的输出

Edit 2 : Output of 'atop -m' below.编辑 2 :下面“atop -m”的输出。

顶层的输出

My advice would be to connect to a profiling service https://blackfire.io or https://newrelic.com .我的建议是连接到分析服务https://blackfire.iohttps://newrelic.com They have in-depth profiling and logging and can be pretty precise when it comes to resource allocation and which script/module is the culprit.他们有深入的分析和日志记录,在资源分配和哪个脚本/模块是罪魁祸首方面可以非常精确。

Short of that, if you have some idea where in your application could be having an issue, you can use strace ( https://linux.die.net/man/1/strace ) to attach to your PHP process(es) to view the stack output and execute those scripts/modules.... strace is very handy when debugging a php process.除此之外,如果您知道您的应用程序中可能存在问题的地方,您可以使用 strace ( https://linux.die.net/man/1/strace ) 附加到您的 PHP 进程以查看堆栈输出并执行那些脚本/模块.... strace 在调试 php 进程时非常方便。

Good luck!祝你好运!

I went into the Mysql.php code of Cakephp, and set every variable to null after a closeCursor, like:我进入了 Cakephp 的 Mysql.php 代码,并在 closeCursor 之后将每个变量设置为 null,例如:

$cols->closeCursor(); $cols = null;

This stopped our memory leak and our crashes.这阻止了我们的内存泄漏和崩溃。 It was normally topping at max memory and crashed after a while.它通常在最大内存时达到顶峰,并在一段时间后崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vtune Memory 泄漏助手 - Vtune Memory Leak helper 无法再访问 Amazon Lightsail LAMP 应用程序站点 - Amazon Lightsail LAMP app site can no longer be reached 更新到 Firebase 版本 9 onValue 后 React Native memory 泄漏错误 - React Native memory leak error after updating to Firebase version 9 onValue 为什么我的 Flutter 应用程序随机崩溃以及如何找到潜在的 memory 泄漏 - EXC_RESOURCE RESOURCE_TYPE_MEMORY - Why is my Flutter App Crashing Randomly and how to find potential memory leak - EXC_RESOURCE RESOURCE_TYPE_MEMORY 带灯 php8 的 AWS Lightsail - AWS Lightsail with Lamp php8 尝试使用 React 抓取网站时,出现 CORS 错误和 GCP 函数错误“函数调用被中断。错误:超出 memory 限制” - Getting a CORS err and GCP functions err "Function invocation was interrupted. Error: memory limit exceeded" while trying to scrape a site using React Go插件调试 - Go plugin debugging 调试 Cloud Firestore 的安全规则 - Debugging security rules for Cloud Firestore AppEngine:逐步调试 - AppEngine: Step-by-Step Debugging 在VSCode中调试Python Lambda函数 - Debugging Python Lambda Functions in VSCode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM