redis Series ------ objects Redis (eight) understand memory

Foreword

Redis did not directly use the data structure to achieve the key-value pair database , but based on these data structures to create an object system that contains the string, lists, hash object, a collection of objects and ordered collection of five objects object type, each object uses at least one data structure we described earlier. By five different types of objects, the Redis can before executing the command, depending on the type of the object to determine whether the object is given a command can be executed. Another advantage of using objects is that we can use for different scenarios, set for the object to achieve a variety of different data structures, so as to optimize the efficiency of the use of objects in different scenes.

In addition, the Redis system object is also achieved based on reference counting garbage collection mechanism technique: when the program is no longer using an object, the memory occupied by the object is automatically released; Further, through reference in the Redis counting technology object sharing mechanism, this mechanism can under appropriate conditions, by having a plurality of keys to save memory databases share the same object.

The type of encoding target

Redis use objects to represent keys and values ​​in the database, every time we create a new key-value pair in the Redis database, we will create at least two objects, an object used as key value pairs (key object) another object is used as key-value pairs (target value).

Redis Each object is represented by a Redis Object structure, said structure and properties of the three stored data relating to the type attribute respectively, encoding ptr properties and properties:
. 1 typedef struct redisObject {
 2  
. 3      // type 
. 4      unsigned type: . 4 ;
 . 5  
. 6      // encoding 
. 7      unsigned encoding: . 4 ;
 . 8  
. 9      // points to a data structure of the underlying implementation pointer 
10      void * PTR;
 . 11  
12 is      // ... 
13 is  
14 } robj;

 

We can see an object contains three main fields.

type: indicates the type of object. Such as String, List, Hash, etc.

encoding: the object represented by what the underlying data structure. Such as INT (integer), EMBSTR (concise version sds), RAW (sds), HT (map) etc.

ptr: ptr is a pointer to an object data structure used.

As shown below: 

set  k v

k is of type String, embstr data structure, which is concise version of the sds, the follow-up talk.

        www.wityx.com

embstr and sds difference

Before we talk about data structures, have not seen embStr, yes, I also see this section only know this thing.

Redis To optimize, engage in a embStr, he is dedicated to the memory encoding short string optimizations.

  • embstr The number of memory allocated code will create a string object you want from the  raw lower two encoded once. raw Coding will be called twice the memory allocation functions to create, respectively,  redisObject structure and  sdshdr architecture , and  embstr coding is to be called once by the memory allocation function allocates a block of contiguous space , which in turn contains space  redisObject and  sdshdr two structures. Because a continuous, a discontinuous.
  • Release  embstr -encoded string object only needs to be called once the memory release function to release  raw the encoding of the string to call the two memory deallocation functions. Ditto
  • Because  embstr all of the data encoded string objects are stored in a contiguous memory inside, so this encoded string object than  raw encoded string objects are better able to take advantage of the cache brings.

In general, because embstr is a contiguous allocation of memory, making it allocated memory is released again, so the efficiency will be increased. Meanwhile embste <==> sds is 44 bytes.

From the figure, we can clearly see. len <= 44 are data structures embster if len> 44 transits to raw. As for why 44.

You can go to count. Reference article:

https://zhuanlan.zhihu.com/p/67876900    

https://xiaoyue26.github.io/2019/01/19/2019-01/redis%E7%9A%84embstr%E4%B8%BA%E4%BB%80%E4%B9%88%E6%98%AF39B/

www.wityx.com

 

RAM

Redis To save memory, really a lot of trouble.

c language like Java, Go and other languages, itself does not have an automatic mechanism for recovery of memory. Java's garbage collection leads to STW has been criticized, ZGC recently read data, the rise of Java really is.

Thus Redis constructed in their own objects in the system a reference count ( Reference Counting ) garbage collection mechanism implemented technology, through this mechanism, the program can reference count information by tracking an object, the object is automatically released at the appropriate time and memory recall .

 But people familiar with the JVM knows that he has a reference count is defective, not solve the problem of circular references.

The A <==> B but has no references to any other node AB, but due to the mutual reference AB count of 1 will never be recovered. So Java with the GC ROOT.

But Redis do not know why there is no problem, looking for information, did not find out why. Most say that Redis is not a complicated structure, so? There are big brothers can answer without?

We can reference count   refcount token OBJECT command, query the token is referenced several times, if 0, then it can be recovered.

And most important point is that, Redis integer 0-9999 (a total of 1W integer) do caching. -128-127 similar to Java to do the same cache.

However, no string of values, such as cache aaaaa, after a string is determined whether the library which need to sweep the entire library, time consuming, and very large pressure cpu.

In optimization, consider a compromise, it caches 0-9999 bar. In fact, look at the price Taobao goods, enough cache 0-100, 0-100, after all, accounted for 99% of the merchandise.

Specifically to see: http: //redisbook.com/preview/object/share_object.html

 

After words

  • Redis database key for each key and value pairs of an object.
  • There Redis strings, lists, hash, collection, ordered collection of five types of objects, each type of object has at least two or more of the encoding, encoding different objects can be optimized in different usage scenarios efficiency.
  • Server before executing certain commands, you will first check to see whether the type given key executes the specified commands, type a value object of type checking key is to check keys.
  • Redis object system with a reference count memory recovery mechanisms implemented when an object is no longer used, the memory occupied by the object is automatically released.
  • Redis share values ​​are integers from 0 to 9999 of the object.
  • Object will record their time was last accessed, this time can be used to calculate an object of idle time.

 

reference: 

 

Guess you like

Origin www.cnblogs.com/wuliaojava/p/11783005.html