Redis deep memory recycling and object sharing

Source: Internet
Author: User

Memory Recycling

C language does not have automatic memory recovery function, Redis in its own object system to build a reference counting technology implementation of memory recycling mechanism , through this mechanism, the program can track the object's reference count information, the appropriate time to automatically release objects and memory collection. The reference count information for each object is recorded by the RefCount property of the Redis object structure, and when a new object is created, the reference count value is initialized to 1, and when the object is used by a new program, its reference count value is increased by 1, when it is no longer used by a program, minus 1, and the reference count value becomes 0. The memory occupied by the object is freed.

Object sharing

The reference count property of an object also has the effect of object sharing. Redis will share string objects with values from 0 to 9999. If key a creates a string object that contains an integer value of 100 as the value object, the key B also creates a string object that also holds the integer value 100 as the value object, and the server can have key A and key B share the same string object, which requires two steps in Redis. 1) Point the value pointer of the database key to an existing value object, 2) increase the reference count of the shared value object by one. The shared object mechanism is very helpful for saving memory, and the more the same value objects are stored in the database, the more memory the object sharing mechanism can save.

Q. Why does Redis share only String objects that contain integer worth?

When the server considers a value object that sets a shared object to a key, the program needs to first check whether the given shared object and the target object the key is trying to create are exactly the same. The more complex values a shared object holds, the higher the complexity required to verify that the shared and target objects are the same, and the more CPU time it consumes.

If the shared object is a string object that holds an integer value, verify the complexity of the Operation O (1)

If the shared object is a string object that holds a string value, verify the complexity of the Operation O (N)

If the shared object is an object that contains multiple values (such as a list object or a hash object), verify the complexity of the Operation O (N2)

So while sharing more complex objects can save more memory, it is limited by CPU time, and Redis only shares the string objects that contain integers.

Redis deep memory recycling and object sharing

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.