简体   繁体   English

PHP内存管理

[英]PHP Memory Management

I have a few time-consuming and (potentially) memory-intensive functions in my LAMP web application. 我的LAMP Web应用程序中有一些耗时且(可能)内存密集的函数。 Most of these functions will be executed every minute via cron (in some cases, the cron job will execute multiple instances of these functions). 大多数这些函数将通过cron每分钟执行一次(在某些情况下,cron作业将执行这些函数的多个实例)。

Since memory is finite, I don't want to run into issues where I am trying to execute a function the environment can no longer handle. 由于内存是有限的,我不想遇到我试图执行环境无法再处理的函数的问题。 What is a good approach at dealing with potential memory problems? 处理潜在的内存问题有什么好方法?

I'm guessing that I need to determine how much memory is available to me, how much memory each function requires before executing it, determine what other functions are being executed by the cron AND their memory usage, etc. 我猜我需要确定可用的内存量,执行每个函数需要多少内存,确定cron正在执行的其他函数及其内存使用情况等。

Also, I don't want to run into the issue where a certain function somehow gets execution priority over other functions. 此外,我不想遇到某个函数以某种方式获得执行优先于其他函数的问题。 If any priority is given, I'd like to have control over that somehow. 如果给予任何优先权,我想以某种方式控制它。

you could look into caching technologies like APC which lets you write stuff right into the RAM so that you can access it fast which of use if you dont want to do expensive tasks like mysql queries repeatedly. 你可以看看像APC这样的缓存技术,它可以让你把东西写进RAM中,这样你就可以快速访问它,如果你不想重复执行昂贵的任务,比如mysql查询。

an example for caching i could think of would be that you could cache emails rather than retreiving them again and again from the email server. 一个缓存的例子,我可以想到你可以缓存电子邮件,而不是一次又一次地从电子邮件服务器中重新搜索它们。 basicaly ram caching is a very useful technique if you have things in your script that you want to preserve for the next time of script execution but if your script does unique things every time it is executed it would be useless. basicaly ram caching是一种非常有用的技术,如果你的脚本中有一些东西要保留下次执行脚本,但是如果你的脚本每次执行它都会做一些独特的事情就没用了。 also as for contoll you could call memory_get_usage() on each script execution and write that value into the apc cache so that every cron could retreive that value and look whether enough memory is free for it to complete. 对于contoll,你可以在每个脚本执行时调用memory_get_usage()并将该值写入apc缓存,这样每个cron都可以检索该值并查看是否有足够的内存可供它完成。

as for average usage you could write an array with the last lets say 100 function executions and when you call that function again it could apc_fetch that from the ram and calculate the average memory usage for that function then compare it to how much ram is being used right now and then decide wheter to start. 至于平均使用情况,你可以编写一个数组,最后一个让我们说100个函数执行,当你再次调用该函数时,它可以从ram中获取apc_fetch并计算该函数的平均内存使用量,然后将其与ram的使用量进行比较现在,然后决定开始。 furthermore it could write that estimate into the current memory usage variable to prevent other scripts from being run. 此外,它可以将该估计值写入当前内存使用量变量,以防止其他脚本运行。 at the end of that function you subtract that amount from the variable again. 在该函数的末尾,您再次从变量中减去该数量。 tl;dr: look into the apc_fetch, apc_store and memory_get_usage functions tl; dr:查看apc_fetch,apc_store和memory_get_usage函数

You can find out how much memory is currently in use by your script using memory_get_usage But you can not determine how much your next function will need, before executing it. 您可以使用memory_get_usage找出脚本当前正在使用多少内存但是在执行之前您无法确定下一个函数需要多少内存。 You can only see after execuiting, using memory_get_usage . 您只能execuiting后看到,使用memory_get_usage You can however store the memory your function used the last times in a database and calculate with the average memory amount. 但是,您可以将函数最后一次使用的内存存储在数据库中,并使用平均内存量进行计算。

Regarding the eecution priority, I don't think it is posible to determine with PHP. 关于执行优先级,我认为用PHP确定它是不可行的。 Apache (or whatever webserver you are using) spawns multiple processes and the operating system schedules which one will be executed in which order. Apache(或您正在使用的任何Web服务器)会生成多个进程,操作系统会调度哪个进程将以哪个顺序执行。

Part of your problem may be the fact you are doing a cron every minute? 您的部分问题可能是您每分钟都在做一次cron吗? Why not set some flags so only one instance of that cron is running before another executes the full logic? 为什么不设置一些标志,这样只有一个cron实例在另一个执行完整逻辑之前运行? ie create a flat file thats deleted at the end of the cron to act as a 'lock'. 即创建一个在cron末尾删除的平面文件,作为“锁定”。 This will make sure one cron process fully completes before any others go forward. 这将确保一个cron进程在任何其他进程前完全完成。 However, I urge you to refer to my comment on your post so that I and others can give you more solid advice. 但是,我建议您参考我对您帖子的评论,以便我和其他人可以给您更多可靠的建议。

Try optimizing your algorithms. 尝试优化算法。 Like... 喜欢...

  • Once you're finished with a variable you should destroy it if you no longer need it. 完成变量后,如果不再需要变量,则应将其销毁。
  • Close MySQL connections after you've finished with them. 完成后关闭MySQL连接。
  • Use recursion. 使用递归。

Also as Jauzsika said change your memory limit in your php.ini, although don't make it too high. 另外,正如Jauzsika所说,改变你的php.ini内存限制,虽然不要太高。 If you need more than 256MB RAM then I would suggest changing to a different language instead of PHP. 如果你需要超过256MB的RAM,那么我建议改用不同的语言而不是PHP。

In your position, I'd consider writing a daemon instead of relying on cron. 在你的位置,我会考虑编写一个守护进程而不是依赖于cron。 The daemon could monitor a queue and be aware of the number of child processes it has running. 守护程序可以监视队列并了解它运行的子进程数。 Managing multiple processes definitely isn't php's biggest strength, but you can do it . 管理多个进程肯定不是php的最大优势,但你可以做到 Pear even includes a System_Daemon package. Pear甚至包括System_Daemon包。

Your daemon could use memory_get_usage and call out out free , uptime , and friends to throttle the number of workers to match system conditions. 您的守护程序可以使用memory_get_usage并呼出freeuptime和朋友来限制工作人员数量以匹配系统条件。

I don't have any direct experience with this, and I wouldn't be too too surprised if a daemon written in PHP gradually leaked memory. 我对此没有任何直接的经验,如果用PHP编写的守护程序逐渐泄露内存,我也不会太惊讶。 But if it's acceptably slow, cron could cycle the daemon every so often... 但如果速度慢得令人满意,cron可以经常循环守护进程......

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM