初学新手菜鸡 遇到一个问题,统计一个2g大小文件中每个单词的出现频率,修改memory limit后还是总报错 allowed memory size of xxxx bytes exhausted ,光测总行数或者字符数可以出结果,怎样优化呢
ini_set("memory_limit", "-1");function calcwordfrequence($sfilepatch){ $awordsinfile = array(); $aonelinewords = array(); $sonelinewords = ""; $fp = fopen($sfilepatch,"r"); while(!feof($fp)){ $sonelinewords = fgets($fp); $aonelinewords = str_word_count($sonelinewords,1); foreach($aonelinewords as $v){ array_push($awordsinfile, $v); } } fclose($fp); $ares = array_count_values($awordsinfile); arsort($ares); return $ares;}echo calcwordfrequence("2013.mp4");
这个问题没法解决,2G大小的文件硬件差点的电脑打开就耗光内存了。在存储上做分布式设计。
这个问题没法解决,2G大小的文件硬件差点的电脑打开就耗光内存了。在存储上做分布式设计。
使用split命令把文件切割成小文件再统计吧
只有文本文件才有行的概念
你测试的 2013.mp4 显然不是文本文件
如果文件中没有出现 \n,或出现的靠后,你的 $sOneLineWords = fgets($fp); 就会把内存消耗光了
如果你是日志之类的文本文件,可以用php的SplFileObject()类,专门用于操作大文件,以前用过这个分析nginx的访问日志,5个多G。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号