How Redis uses different memory allocators to compare fragmentation rates
In the source code of zmalloc.c in Redis, we can see the following code:
/* Explicitly override malloc/free etc when using tcmalloc. */
#if defined(USE_TCMALLOC )
#define malloc(size) tc_malloc(size)
tc_calloc(count,size) is the definition of calloc function
#define realloc(ptr,size) tc_realloc( ptr,size)
#define free(ptr) tc_free(ptr)
#elif defined(USE_JEMALLOC)
#define malloc(size) je_malloc(size)
The following sentence is rewritten as requested: The statement "#define calloc(count,size) je_calloc(count,size)" is used to replace the standard defined function "calloc" with a custom function named "je_calloc".
#define realloc(ptr, size) je_realloc(ptr,size)
#define free(ptr) je_free(ptr)
#endif
From the above code we can see that Redis is When compiling, it will first determine whether to use tcmalloc. If so, the function implementation in the standard libc will be replaced with the function corresponding to tcmalloc. After this, it will check whether jemalloc takes effect. If not, the memory management function in standard libc will be used.
In the latest version 2.4.4, jemalloc has been included in the source code package as part of the source code package, so it can be used directly. And if you want to use tcmalloc, you need to install it yourself.
Let’s briefly talk about how to install the tcmalloc package. tcmalloc is part of google-proftools, so we actually need to install google-proftools. When installing on a 64-bit machine, the required libunwind library must first be installed.
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz
tar zxvf libunwind-0.99-alpha.tar.gz
cd libunwind-0.99-alpha/
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
Comparison of fragmentation rates of how Redis uses different memory allocators
Then install google-preftools:
wget http://google-perftools.googlecode.com/files/ google-perftools-1.8.1.tar.gz
tar zxvf google-perftools-1.8.1.tar.gz
cd google-perftools-1.8.1/
./configure --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --disable-debugalloc --enable-minimal
make && make install
Sudo echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf #If you don’t have this file, create one yourself
sudo /sbin/ldconfig
Then install Redis and specify the corresponding parameters when making to enable tcmalloc
$ curl -O http://redis.googlecode.com/files/redis-2.4.4.tar. gz
$ tar xzvf redis-2.4.4.tar.gz
$ cd redis-2.4.4
$ make USE_TCMALLOC=yes FORCE_LIBC_MALLOC=yes
$ sudo make install
After restarting Redis, you can see the memory allocator used through the info command.
Back to the topic of this article, this article discusses the three memory allocators corresponding to tcmalloc, jemalloc and libc. The following is a simple test result, taken from the Redis info information, designed to evaluate its performance and fragmentation rate. The tests used different allocators. We can see that the fragmentation rate is the lowest when using tcmalloc, which is 1.01, jemalloc is 1.02, and libc’s allocator fragmentation rate is 1.31, as shown below:
Used_memory:708391440
used_menory_human:675.57M
used_memory_rss:715169792
used_memory_peak:708814040
used_memory_peak_human:675.98M
mem_fragmentation_ratio:1.01
mem_ allocator:tcmalloc -1.7
used_memory:708381168
used_menory_human:675.56M
used_memory_rss:723587072
used_memory_peak:708803768
## used_memory_peak_human:675.97 M mem_fragmentation_ratio:1.02 mem_allocator:jemalloc-2.2.1 used_memory:869000400 used_menory_human:828.74M used_memory_rss:1136689152 Used_memory_peak:868992208 used_memory_peak_human:828.74M mem_fragmentation_ratio:1.31 mem_allocator:libcThe above is the detailed content of How Redis uses different memory allocators to compare fragmentation rates. For more information, please follow other related articles on the PHP Chinese website!

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment