Configuración / proc / sys / vm / drop_caches para borrar el caché


80

Como parte de hacer algunos tiempos de caché en frío, estoy tratando de liberar el caché del sistema operativo. La documentación del kernel (recuperada en enero de 2019) dice:

drop_caches

Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes.  Once dropped, their
memory becomes free.

To free pagecache:
    echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
    echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
    echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync' prior to writing to /proc/sys/vm/drop_caches.  This will minimize the
number of dirty objects on the system and create more candidates to be
dropped.

This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...)  These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems.  Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use.  Because of this,
use outside of a testing or debugging environment is not recommended.

You may see informational messages in your kernel log when this file is
used:

    cat (1234): drop_caches: 3

These are informational only.  They do not mean that anything is wrong
with your system.  To disable them, echo 4 (bit 3) into drop_caches.

Soy un poco vago sobre los detalles. Corriendo

echo 3 > /proc/sys/vm/drop_caches

libera cache de página, dentries e inodes. Okay.

Entonces, si quiero que el sistema comience a almacenar en caché normalmente de nuevo, ¿debo restablecerlo a 0 primero? Mi sistema tiene el valor actualmente establecido en 0, que supongo es el predeterminado. ¿O se restablecerá solo? Veo al menos dos posibilidades aquí, y no estoy seguro de cuál es la verdadera:

  1. echo 3 > /proc/sys/vm/drop_cacheslibera cache de página, dentries e inodes. El sistema comienza inmediatamente a almacenar en caché nuevamente. No estoy seguro de qué esperaría que hiciera el valor /proc/sys/vm/drop_cachessi este es el caso. ¿Volver a 0 casi de inmediato?

  2. Si /proc/sys/vm/drop_cachesse establece en 3, el sistema no realiza ningún almacenamiento en memoria caché hasta que se restablece a 0.

¿Qué caso es verdad?


22
Para usarlo con sudo:echo 3 | sudo tee /proc/sys/vm/drop_caches
Volker Siegel

Vaca santa! @VolkerSiegel He estado fregando las Interwebs durante horas y encontré tu comentario al azar. Lo probé y funcionó a la perfección! Gracias por agregar este pequeño truco. No quiero secuestrar esta pregunta, pero si no es inapropiada, me gustaría pedir una explicación. Cuando uso sshpass con sudo -i && echo 3 > /proc/sys/vm/drop_caches, me sale un error: stdin: is not a tty. Usar su método "sudo" funciona. ¿Por qué?
Harperville

2
@harperville Haz una pregunta; esto es casi seguro un problema no relacionado.
Faheem Mitha

1
@harperville Sí, esa es una muy buena pregunta, con una respuesta interesante, ¡pregunte por separado! Entonces házmelo saber aquí. ¿O quieres que yo mismo haga la pregunta y luego la responda?
Volker Siegel

Respuestas:


107

No es pegajoso: simplemente escribe en el archivo para que suelte los cachés e inmediatamente comienza a almacenar en caché nuevamente.

Básicamente, cuando escribe en ese archivo, realmente no está cambiando una configuración, está emitiendo un comando al núcleo. El kernel actúa sobre ese comando (soltando los cachés) y luego continúa como antes.


2
OK gracias. ¿El valor vuelve a cambiar inmediatamente a 0?
Faheem Mitha

33
@FaheemMitha No, el valor que puedes leer es lo que pones al final, pero no se usa en ningún lado, solo importa la acción de escribir. El código fuente está adentro fs/drop_caches.c.
Gilles

¿Hay un beneficio específico para la JVM al hacer esto?
JE Carter II

3
No hay beneficios para las aplicaciones, por el contrario: los archivos se cargarán más lentamente porque no hay inodos en caché. Además, no tiene poca memoria: si una aplicación necesita memoria, la tomará del caché del sistema operativo. Ver linuxatemyram.com .
dr0i

@TomH, en "solo la acción de escribir importa" ¿es este el synccomando?
kaizenCoder
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.