Introduzione
si dovrebbe usare memory_get_usage(false)
perché ciò che si vuole è la memoria utilizzata non è la memoria allocata.
cosa è la differenza
tuo Google Mail
potrebbe aver assegnato 25MB
di archiviazione per voi, ma non significa che è ciò che avete utilizzato al momento.
Questo è esattamente ciò che il doc PHP stava dicendo
Set this to TRUE to get the real size of memory allocated from system. If not set or FALSE only the memory used by emalloc() is reported.
Sia argomento sarebbe tornato di memoria relativa assegnata al limite di memoria, ma la differenza principale è:
memory_get_usage(false)
dare la memoria utilizzata da emalloc()
mentre memory_get_usage(true)
ritorna pietra miliare che può essere dimostrata qui Memory Mile Store
Voglio sapere quanto era vicina la sceneggiatura per raggiungere quel limite.
Ciò richiederebbe alcuni calcoli e potrebbe funzionare solo in loop o casi d'uso specifici. Perché ho detto tale?
Immaginate
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
.
quanto ne so l'unico modo per controllare la memoria utilizzata per una sezione variabile o specifico di PHP è:
$start_memory = memory_get_usage();
$foo = "Some variable";
echo memory_get_usage() - $start_memory;
See Explanation, ma se si è in una funzione loop o ricorsiva si può usare massima l'utilizzo della memoria per stimare in modo sicuro quando verrà raggiunto il picco di memoria.
Esempio
ini_set('memory_limit', '1M');
$memoryAvailable = filter_var(ini_get("memory_limit"), FILTER_SANITIZE_NUMBER_INT);
$memoryAvailable = $memoryAvailable * 1024 * 1024;
$peekPoint = 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 Diffrence
$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 scipt
if ($percentage > $peekPoint) {
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
}
Output
Stoped at: 95.86%
{
"HIGHEST_MEMORY": "0.71",
"HIGHEST_DIFF": "0.24",
"PERCENTAGE_BREAK": "95.86%",
"AVERAGE": "0.04",
"LOOPS": 11
}
Live Demo
Questo può ancora sicuro
Può fallire perché dopo if ($percentage > $peekPoint) {
questo ancora ancora aggiungere a che fare compito supplementare con consuma anche la memoria
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 grater than the memory available the script would fail.
Conclusione
La sua non è una soluzione perfetta, ma verificare la presenza di memoria a intervalli e se il suo superiore sbirciare (es. 90%) exit
all'istante e lasciare le cose fantastiche
Mi piacerebbe indicarti una presentazione di Julien Pauli http://www.youtube.com/watch?v=sm1HUrnsxLI per la conferenza php uk 2013, in cui parla di come la memoria funzioni all'interno di PHP. – mpratt
Vedere anche http://stackoverflow.com/a/7234026/632951 – Pacerier