memory_get_peak_memory()와 "실제 사용"
경우,real_usage
가 「인수」로 되어 있습니다.true
PHP DOCS는 시스템에서 할당된 실제 메모리 크기를 가져올 것이라고 말합니다.의 false
됩니다.emalloc()
다음 2가지 옵션 중 어떤 것이 php.ini의 메모리 제한 값에 대해 할당된 최대 메모리를 반환합니까?
그 한계에 도달하기 위해 대본이 얼마나 가까웠는지 알고 싶다.
그럼 간단한 스크립트를 사용하여 테스트해 보겠습니다.
ini_set('memory_limit', '1M');
$x = '';
while(true) {
echo "not real: ".(memory_get_peak_usage(false)/1024/1024)." MiB\n";
echo "real: ".(memory_get_peak_usage(true)/1024/1024)." MiB\n\n";
$x .= str_repeat(' ', 1024*25); //store 25kb more to string
}
출력:
not real: 0.73469543457031 MiB
real: 0.75 MiB
not real: 0.75910949707031 MiB
real: 1 MiB
...
not real: 0.95442199707031 MiB
real: 1 MiB
not real: 0.97883605957031 MiB
real: 1 MiB
PHP Fatal error: Allowed memory size of 1048576 bytes exhausted (tried to allocate 793601 bytes) in /home/niko/test.php on line 7
실제 사용량은 시스템에서 할당된 메모리인 것 같습니다.이 메모리는 스크립트에서 현재 필요한 것보다 더 큰 버킷에 할당되어 있는 것 같습니다.(퍼포먼스상이라고 생각합니다).이것은 php 프로세스가 사용하는 메모리이기도 합니다.
$real_usage = false
에 의해 .
제한에 했는지 확인하려면 , 「 제한」을 사용합니다.$real_usage = true
서론
하면 됩니다.memory_get_usage(false)
왜냐하면 당신이 원하는 것은 할당된 메모리가 아니라 사용된 메모리이기 때문입니다.
차이점
의 ★★★★★★★★★★★★★★★★★.Google Mail
되어 있을지도 25MB
현재 사용하고 있는 것은 아닙니다.
이것이 바로 PHP 문서가 말한 것입니다.
시스템에서 할당된 메모리의 실제 크기를 가져오려면 이 설정을 TRUE로 설정합니다.설정되지 않거나 FALSE일 경우 emalloc()에서 사용되는 메모리만 보고됩니다.
두 인수 모두 메모리 제한에 따라 할당된 메모리를 반환하지만 주요 차이점은 다음과 같습니다.
memory_get_usage(false)
을 emalloc()
동시에memory_get_usage(true)
Memory Mile Store에서 시연할 수 있는 마일스톤을 반환한다.
그 한계에 도달하기 위해 대본이 얼마나 가까웠는지 알고 싶다.
이 방법에는 몇 가지 계산이 필요하며 루프 또는 특정 사용 사례에서만 사용할 수 있습니다.내가 왜 그런 말을 하지?
상상하다
ini_set('memory_limit', '1M');
$data = str_repeat(' ', 1024 * 1024);
The above script would fail before you even get the chance to start start checking memory
.
PHP의 변수 또는 특정 섹션에 사용되는 메모리를 확인하는 방법은 다음과 같습니다.
$start_memory = memory_get_usage();
$foo = "Some variable";
echo memory_get_usage() - $start_memory;
설명을 참조하십시오. 그러나 루프 또는 재귀 함수에 있는 경우 최대 메모리 사용량을 사용하여 메모리 피크에 도달하는 시기를 안전하게 예측할 수 있습니다.
예
ini_set('memory_limit', '1M');
$memoryAvailable = filter_var(ini_get("memory_limit"), FILTER_SANITIZE_NUMBER_INT);
$memoryAvailable = $memoryAvailable * 1024 * 1024;
$peakPoint = 90; // 90%
$memoryStart = memory_get_peak_usage(false);
$memoryDiff = 0;
// Some stats
$stat = array(
"HIGHEST_MEMORY" => 0,
"HIGHEST_DIFF" => 0,
"PERCENTAGE_BREAK" => 0,
"AVERAGE" => array(),
"LOOPS" => 0
);
$data = "";
$i = 0;
while ( true ) {
$i ++;
// Get used memory
$memoryUsed = memory_get_peak_usage(false);
// Get Difference
$memoryDiff = $memoryUsed - $memoryStart;
// Start memory Usage again
$memoryStart = memory_get_peak_usage(false);
// Gather some stats
$stat['HIGHEST_MEMORY'] = $memoryUsed > $stat['HIGHEST_MEMORY'] ? $memoryUsed : $stat['HIGHEST_MEMORY'];
$stat['HIGHEST_DIFF'] = $memoryDiff > $stat['HIGHEST_DIFF'] ? $memoryDiff : $stat['HIGHEST_DIFF'];
$stat['AVERAGE'][] = $memoryDiff;
$stat['LOOPS'] ++;
$percentage = (($memoryUsed + $stat['HIGHEST_DIFF']) / $memoryAvailable) * 100;
// var_dump($percentage, $memoryDiff);
// Stop your script
if ($percentage > $peakPoint) {
print(sprintf("Stoped at: %0.2f", $percentage) . "%\n");
$stat['AVERAGE'] = array_sum($stat['AVERAGE']) / count($stat['AVERAGE']);
$stat = array_map(function ($v) {
return sprintf("%0.2f", $v / (1024 * 1024));
}, $stat);
$stat['LOOPS'] = $i;
$stat['PERCENTAGE_BREAK'] = sprintf("%0.2f", $percentage) . "%";
echo json_encode($stat, 128);
break;
}
$data .= str_repeat(' ', 1024 * 25); // 1kb every time
}
Stoped at: 95.86%
{
"HIGHEST_MEMORY": "0.71",
"HIGHEST_DIFF": "0.24",
"PERCENTAGE_BREAK": "95.86%",
"AVERAGE": "0.04",
"LOOPS": 11
}
그래도 실패할 수 있습니다.
후에 도 있습니다.if ($percentage > $peakPoint) {
이 증가하여 추가 됩니다.
print(sprintf("Stoped at: %0.2f", $percentage) . "%\n");
$stat['AVERAGE'] = array_sum($stat['AVERAGE']) / count($stat['AVERAGE']);
$stat = array_map(function ($v) {
return sprintf("%0.2f", $v / (1024 * 1024));
}, $stat);
$stat['LOOPS'] = $i;
$stat['PERCENTAGE_BREAK'] = sprintf("%0.2f", $percentage) . "%";
echo json_encode($stat, 128);
break;
If the memory to process this request is greater than the memory available the script would fail.
결론
이 솔루션은 완벽한 솔루션은 아니지만 메모리를 정기적으로 체크하고 피크(90%)를 넘으면exit
즉석에서 화려한 것을 버리다
real_usage
false는 스크립트가 사용한 사용량을 보고합니다.이것이 둘 중 더 정확할 것이다.
real_usage
true는 스크립트에 할당된 메모리를 보고합니다.이게 둘 중에 더 높은 거예요.
저는 아마true
비교하려고 하면 스크립트는 메모리 제한을 초과하여 할당되지 않으며 스크립트가 (및 다른 모든 스크립트가) 그 사용량을 초과하지 않는 한 계속 실행됩니다.
PHP memory_get_usage에 따라
실제_실제
사용하지 않는 페이지를 포함하여 시스템에서 할당된 총 메모리를 가져오려면 이 설정을 TRUE로 설정합니다.설정되어 있지 않거나 FALSE일 경우 사용된 메모리만 보고됩니다.
따라서 스크립트에서 사용되는 메모리를 가져오려면 memory_get_memory()를 사용해야 합니다.기본값 real_memory는 false입니다.
시스템에 의해 할당된 메모리를 가져오고 싶지만 실제 사용된 메모리량은 신경 쓰지 않는 경우 memory_get_memory(true)를 사용합니다.
<!-- Print CPU memory and load -->
<?php
$output = shell_exec('free');
$data = substr($output,111,19);
echo $data;
echo file_get_contents('/proc/loadavg');
$load = sys_getloadavg();
$res = implode("",$load);
echo $res;
?>
언급URL : https://stackoverflow.com/questions/15745385/memory-get-peak-usage-with-real-usage
'programing' 카테고리의 다른 글
자바의 정수 분할 (0) | 2022.09.21 |
---|---|
유형 ORM 대량 삽입? (0) | 2022.09.21 |
2+40은 왜 42일까요? (0) | 2022.09.21 |
MySQL 스토어드 프로시저에서 사용 여부 (0) | 2022.09.21 |
그림.js v2 - 격자선 숨기기 (0) | 2022.09.21 |