例如,我有一个功能,女巫从数据库中获取产品,我按类别进行了部分调用,因为没有它,它将消耗所有内存。因此,当我调用零件内存时,不会清除内存,它会启动另一个占用更多内存的进程。我可以按文件分开,但是我需要更方便的方法。
function getproducts($cat){
error_log("START | ".$cat." | MEMORY: ".formatBytes(memory_get_usage(true)), 0);
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat
)
),
'post_type' => 'product',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
error_log("QTY: ".$the_query->found_posts." | Mem: ".formatBytes(memory_get_usage(true)), 0);
error_log("END | MEMORY: ".formatBytes(memory_get_usage(true)), 0);
}
getproducts('part1');
getproducts('part2');
getproducts('part3');
日志信息:
[17-Jan-2020 09:33:07 UTC] START | part1 | MEMORY: 2 MB
[17-Jan-2020 09:33:14 UTC] QTY: 3151 | Mem: 198 MB
[17-Jan-2020 09:33:14 UTC] END | MEMORY: 198 MB
[17-Jan-2020 09:33:14 UTC] START | part2 | MEMORY: 198 MB
[17-Jan-2020 09:33:15 UTC] QTY: 533 | Mem: 204 MB
[17-Jan-2020 09:33:15 UTC] END | MEMORY: 204 MB
如您所见,内存正在累加。