Tabla de Contenidos
Curiosidades: Consumos
Para ver cuanto consume nuestros scripts y poder optimizarlos podemos realizar lo siguiente:
$inicial = memory_get_usage(); /* Aqui meteremos el código que queramos comprobar */ $final = memory_get_usage(); $totaUsado = $final - $inicial; echo 'Total memoria gastada ($Variable): '.$totaUsado;
Recomendación: Para liberar recursos se puede usar la función <html><a href=“http://php.net/manual/es/function.unset.php” target=“_blank”>unset()</a></html>.
Ejemplos
Variable: (180 bytes)
$inicial = memory_get_usage(); $Variable = 'Valor'; $final = memory_get_usage(); $totaUsado = $final - $inicial; echo 'Total memoria gastada ($Variable): '.$totaUsado;
Constante: (16 bytes)
$inicial = memory_get_usage(); const constante = 'Valor'; $final = memory_get_usage(); $totaUsado = $final - $inicial; echo 'Total memoria gastada (Constante): '.$totaUsado;
Define: (16 bytes)
$inicial = memory_get_usage(); define ('Constante','Valor'); $final = memory_get_usage(); $totaUsado = $final - $inicial; echo 'Total memoria gastada (define): '.$totaUsado;
Array: (256 bytes)
$inicial = memory_get_usage(); $Array = array('Valor'); $final = memory_get_usage(); $totaUsado = $final - $inicial; echo 'Total memoria gastada (array): '.$totaUsado;
Array Asociativo: (268 bytes)
$inicial = memory_get_usage(); $Asociativo = array( 'Asocia'=>'Valor' ); $final = memory_get_usage(); $totaUsado = $final - $inicial; echo 'Total memoria gastada (array asociativo): '.$totaUsado;
Ejemplos en funcionamiento: <html><a href=“http://pruebas.lohacemosweb.net/MemoriaUsada/index.php” target=“_blank”>Ejemplos.</a></html>