Redis goes deep into memory recovery and object sharing, and redis goes deep into object recycling

Source: Internet
Author: User

Redis goes deep into memory recovery and object sharing, and redis goes deep into object recycling

Memory reclaim

The C language does not have the automatic memory recovery function. Redis builds a memory recovery mechanism based on the reference counting technology in its own object system. Through this mechanism, the program can automatically release the object and recycle the memory when appropriate by tracking the reference counting information of the object. The reference count information of each object is recorded by the refcount attribute of the redis object structure. When a new object is created, the reference count value is initialized to 1. When the object is used by a new program, its reference count value will be increased by 1; when it is no longer used by a program, it will be reduced by 1; the reference count value will be changed to 0, and the memory occupied by the object will be released.

Object sharing

The reference count attribute of an object also shares objects. Redis will share string objects with values ranging from 0 to 9999. If key A creates A String object that contains an integer of 100 as the value object, key B also creates A String object that also saves the integer of 100 as the value object, the server allows keys A and B to share the same string object. Redis requires two steps. 1) direct the database key value pointer to an existing value object. 2) add one reference count for the shared value object. The shared object mechanism is very helpful for saving memory. The more objects with the same value saved in the database, the more memory the object sharing mechanism can save.

Q: Why does Redis share only the string objects that contain integers?

When the server is considering setting a shared object as a key value object, the program needs to first check whether the given shared object and the target object to be created are identical. The more complex the value of a shared object is, the higher the complexity required to verify whether the shared object and the target object are the same, and the more CPU time consumed.

If the shared object is a string object that saves the integer value, verify the complexity of the operation O (1)

If the shared object is a string object that saves string values, verify the complexity of the O (N) operation)

If the shared object contains multiple values (such as List objects or hash objects), verify the complexity of the operation O (N2)

Therefore, although sharing more complex objects can save more memory, Redis only shares the string objects that contain integers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.