Redis cannot automatically release memory after deleting data

Redis does system caching. Historical caching strategies and cache contents are not well adapted to the increased business volume. After a variety of dissN with dba on his face, he decided to clean up a wave of zombies.

But after deleting the useless key, there is no real memory release. Check the Redis related documents, and there is no command to release memory. Looking at the percentage of the dashboard is scratching my head. . .

Checked some information to understand the next day, and then consult dba big guys. . .

The official has stated the specific reasons
https://redis.io/topics/memory-optimization
summarized a few points:

This is not a problem with Redis itself. Redis itself has indeed called free to release these memories. This should be a problem with the underlying C runtime used.

When the key is deleted, Redis does not always release (return) memory to the operating system. This is not special to Redis, but most malloc () implementations work this way.

For example, an instance has 5GB of data, and then delete the equivalent of 2GB of data, used_memory_rss may still be about 5GB. This is because the underlying allocator cannot easily free memory. For example, most of the deleted keys are usually assigned to the same page as other keys that still exist.

However, the allocator is smart and able to reuse the memory of free blocks, so after you release 2 gb of 5 gb of data set, when you start to increase the key again, you will see that used_memory_rss remains stable without increasing much. The allocator is basically trying to reuse the 2GB of memory previously (logically) freed.

As far as glibc is concerned, mmap is used when allocating more than 128k of memory, and brk / sbrk is used to allocate small memory in the heap. The memory applied through mmap can be returned to the system immediately after calling free, but the memory in the heap is not necessarily unless the released memory is a continuous large block in the heap.

Redis itself has no memory management mechanism, only a statistical function of usage. Every time you need to create an object, you call malloc directly, and the objects in Redis are basically small, so they are basically in the memory of the heap.

mark a link, all in English, study in another day http://goog-perftools.sourceforge.net/doc/tcmalloc.html
 
 

The solution
can be known by the reason, we don't actually need to pay attention.
Especially after we have defined maxmemory and defined maxmemory_policy, even if the memory is full, redis will clear some unnecessary keys according to the elimination mechanism scheme to store new keys.

However, if the system memory usage has been affected, there are three options:

  • You can use the MEMORY PURGE command to organize the memory. (Instantly, it can free up rss memory space slightly)
  • Open activedefrag, hot defragmentation. (It will occupy the CPU, execute it on the main thread, and you can set the CPU occupancy rate)
  • Restart.

Read the solution, and then consult dba. . . .

 

 

 

Guess you like

Origin www.cnblogs.com/steve-jiang/p/12748210.html