Memory management method and device, equipment and storage medium
By dynamically managing memory allocators and memory blocks according to the life cycle and data volume, it solves the problems of low memory management efficiency and waste of storage space in the existing technology, and achieves efficient management of key-value objects and timely processing of expired data.
Patent Information
- Application Number
- CN202310368801.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-31
- Publication Date
- 2024-10-01
AI Technical Summary
In the existing distributed cache memory management solution, when the memory allocator manages key-value objects of different sizes, the memory management efficiency is low, and key-value objects with different life cycles cannot be effectively processed, resulting in a waste of storage space and expired data processing. Very difficult.
By determining the target memory allocator according to the life cycle of the key-value object, and storing it in the target memory block according to the amount of data, using different memory allocators to manage key-value objects with different life cycles, the key values stored in the memory block are realized. The expiration time of objects is handled in a timely manner.
It improves the efficiency of memory management, reduces the waste of storage space, can eliminate expired data in time, and improves the effective usage of memory.
Smart Images

Figure CN118733480A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a memory management method, apparatus, device and storage medium. Background Art
[0002] Key-Value storage is a NoSQL (Not only SQL) storage method, in which data is organized, indexed, and stored in the form of key-value pairs.
[0003] In the related art, a memory management solution for a distributed cache (Memcached) is provided, in which memory allocators (slab classes) are divided based on the data volume of key-value (KV) objects, so that key-value objects of different sizes are stored and managed through different memory allocators, that is, each memory allocator is only responsible for managing key-value objects of one memory size, and the memory blocks (slabs) managed by each memory allocator are fixed, and the size of each memory block is also fixed, which causes a waste of memory management space and increases the difficulty of managing key-value objects of different life cycles (Time to Live, TTL). Summary of the invention
[0004] The embodiments of the present application provide a memory management method, apparatus, device and storage medium, which can manage key-value objects according to different life cycles and improve the efficiency of memory management. The technical solution is as follows:
[0005] On the one hand, an embodiment of the present application provides a memory management method, the method comprising:
[0006] Receive a first data write request, where the first data write request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object;
[0007] Based on the first life cycle, determining a target memory allocator corresponding to the first key-value object, wherein different memory allocators are used to manage key-value objects with different life cycles;
[0008] Based on the first data volume, the first key-value object is stored in a target memory block corresponding to the target memory allocator.
[0009] On the other hand, an embodiment of the present application provides a memory management device, the device comprising:
[0010] A first receiving module is used to receive a first data writing request, where the first data writing request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object;
[0011] a first determining module, configured to determine a target memory allocator corresponding to the first key-value object based on the first life cycle, wherein different memory allocators are used to manage key-value objects of different life cycles;
[0012] A first storage module is used to store the first key-value object in a target memory block corresponding to the target memory allocator based on the first data volume.
[0013] On the other hand, an embodiment of the present application provides a computer device, which includes a processor and a memory, wherein the memory stores at least one instruction, and the at least one instruction is loaded and executed by the processor to implement the memory management method as described in the above aspects.
[0014] On the other hand, an embodiment of the present application provides a computer-readable storage medium, in which at least one instruction is stored. The at least one instruction is loaded and executed by a processor to implement the memory management method as described in the above aspects.
[0015] On the other hand, an embodiment of the present application provides a computer program product, which includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the memory management method provided by the above aspect.
[0016] In an embodiment of the present application, key-value objects with different life cycles are managed through different memory allocators. When a first data write request corresponding to a first key-value object is received, the target memory allocator corresponding to the first key-value object can be determined according to the first life cycle of the first key-value object in the first data write request, and the first key-value object can be stored in the target memory block corresponding to the target memory allocator according to the first data volume of the first key-value object. By adopting the scheme provided in the embodiment of the present application, key-value objects can be managed according to different life cycles, thereby improving the efficiency of memory management. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 A schematic diagram showing the structure of a memory management method provided in the related art is shown;
[0018] Figure 2 A schematic diagram showing an implementation environment provided by an exemplary embodiment of the present application is shown;
[0019] Figure 3 A flowchart of a memory management method provided by an exemplary embodiment of the present application is shown;
[0020] Figure 4 A flowchart of a memory management method provided by another exemplary embodiment of the present application is shown;
[0021] Figure 5 A schematic diagram of the structure of a memory allocator and a memory manager provided by an exemplary embodiment of the present application is shown;
[0022] Figure 6 A schematic diagram of allocating storage space to a memory block provided by an exemplary embodiment of the present application is shown;
[0023] Figure 7 A flowchart of writing and reading data from a hash table provided by an exemplary embodiment of the present application is shown;
[0024] Figure 8 A schematic diagram of a memory management architecture provided by an exemplary embodiment of the present application is shown;
[0025] Fig. 9 A schematic diagram showing the latest storage address information maintained by a hash table provided by an exemplary embodiment of the present application is shown;
[0026] Fig.10 A flowchart of writing a first key-value object provided by an exemplary embodiment of the present application is shown;
[0027] Fig.11 A flowchart of reading a first data value provided by an exemplary embodiment of the present application is shown;
[0028] Fig.12 A flowchart of eliminating expired data provided by an exemplary embodiment of the present application is shown;
[0029] Fig.13 A schematic diagram of traversing a memory block linked list provided by an exemplary embodiment of the present application is shown;
[0030] Fig.14 A schematic diagram of the internal structure of a memory manager provided by an exemplary embodiment of the present application is shown;
[0031] Fig.15 A structural block diagram of a memory management device provided by an exemplary embodiment of the present application is shown;
[0032] Fig.16 A schematic diagram of the structure of a computer device provided by an exemplary embodiment of the present application is shown. DETAILED DESCRIPTION
[0033] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0034] In the related art, a memory management solution for a distributed cache (Memcached) is provided, which uses a management mechanism based on a memory allocator (Slab Class), wherein each memory allocator is only responsible for managing key-value (KV) objects of a class of memory size, allocating storage space for key-value objects of a class of memory size, and the number of memory blocks (Slabs) managed by each memory allocator is the same and fixed, and the size of each memory block is also fixed.
[0035] In the process of allocating space for key-value objects, the computer device will determine the corresponding target memory allocator based on the data size of the key-value object, so that the memory allocator will search for allocable space from free memory blocks to store the key-value object.
[0036] Indicatively, Figure 1 As shown, the first memory allocator 101 is responsible for managing key-value objects with a memory size of 48 bytes, the second memory allocator 102 is responsible for managing key-value objects with a memory size of 96 bytes, and the third memory allocator 103 is responsible for managing key-value objects with a memory size of 192 bytes, and each memory allocator is responsible for managing N memory blocks, and the size of each memory block is 1M, so that each memory block in each memory allocator can be divided into multiple continuous key-value objects according to the space size of the key-value objects managed by the memory allocator.
[0037] Furthermore, when receiving a key-value object to be stored, the computer device first determines the data size of the key-value object. For example, when the data size of the key-value object is 56 bytes, the computer device determines that the second memory allocator 102 searches for allocatable storage space from the free memory blocks to store the key-value object; when the data size of the key-value object is 97 bytes, the computer device determines that the third memory allocator 103 searches for allocatable storage space from the free memory blocks to store the key-value object.
[0038] It can be seen that in the memory management scheme of distributed cache, each memory block belongs to a unique memory allocator and cannot be allocated and transferred between multiple memory allocators. In addition, the storage space of the key-value objects divided in the memory block is fixed. Therefore, when there is a large gap between the actual storage space consumed by the key-value objects and the pre-allocated storage space, it is easy to cause a waste of storage space.
[0039] In addition, memory allocation is performed only based on the storage capacity of key-value objects, resulting in the key-value objects stored in each memory block being out of order in the life cycle dimension, making it impossible to directly process expired data, resulting in too low effective memory utilization and reduced memory management efficiency.
[0040] In an embodiment of the present application, different memory allocators are used to manage key-value objects with different life cycles. Therefore, when a data write request is received, the computer device first determines the corresponding target memory allocator based on the life cycle of the key-value object, and then stores the key-value object in the target memory block corresponding to the target memory allocator based on the data volume of the key-value object. By dividing the memory allocators according to the life cycle, the expiration time of the key-value object stored in the memory block can be determined in time, so that the expired data can be processed and the memory management efficiency can be improved.
[0041] Optionally, the memory management method provided by the embodiment of the present application can be applied to a game application. For example, in the process of establishing a virtual object database, the computer device first determines the key-value object, the life cycle of the key-value object, and the data volume of the key-value object in the virtual object data write request according to the virtual object data write request, wherein the index value of the key-value object can be the account information corresponding to the virtual object, and the data value can include the object data of the virtual object, such as the life value, force value, virtual props owned by the virtual object, etc., and then the computer device determines the target memory allocator corresponding to the key-value object according to the life cycle, and stores the key-value object in the corresponding target memory block according to the data volume. By applying the memory management method provided by the embodiment of the present application to the game application, it is possible to manage the virtual object data corresponding to each account according to the life cycle of the virtual object data, eliminate expired virtual object data in time, and improve the management efficiency of the virtual object data.
[0042] Please refer to Figure 2 , which shows a schematic diagram of an implementation environment provided by an exemplary embodiment of the present application. The implementation environment may include: a terminal 210 and a server 220.
[0043] Terminal 210 is an electronic device with data writing and storage functions, wherein the data writing and storage functions can be functions of native applications in the terminal, or functions of third-party applications; the electronic device can be a smart phone, tablet computer, personal computer, wearable device or vehicle-mounted terminal, etc.
[0044] Figure 2 In the description, the terminal 210 is taken as a smart phone and an application program supporting data writing and storage functions is installed as an example, but this is not a limitation.
[0045] Figure 2 Only one terminal is shown in the figure, but in different embodiments, there are multiple other terminals that can access the server 220. The terminal 210 and other terminals are connected to the server 220 via a wireless network or a wired network.
[0046] The server 220 includes at least one of a single server, a server cluster consisting of multiple servers, a cloud computing platform, and a virtualization center. The server 220 is used to provide background services for applications that support data writing and storage functions. Optionally, the server 220 undertakes the main computing work and the terminal undertakes the secondary computing work; or, the server 220 undertakes the secondary computing work and the terminal undertakes the main computing work; or, the server 220 and the terminal adopt a distributed computing architecture for collaborative computing.
[0047] Server 220 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers. It can also be a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (CDN), as well as big data and artificial intelligence platforms.
[0048] In a possible implementation, the corresponding implementation environment is described by taking the memory management method provided in the embodiment of the present application as an example of being applied to a game application. The server 220 includes a memory 221, a processor 222, a user account database 223, a virtual object database 224, and a user-oriented input / output interface (I / O interface) 225. Among them, the memory 221 is a memory storage, and the processor 222 is used to load instructions stored in the server 220, process the user account database 223 and the virtual object database 224; the user account database 223 is used to store the data of the user account used by the terminal 210 and other terminals, such as the user account avatar, the user account nickname, etc.; the virtual object database 224 is used to store the object data of the virtual objects created by each user account; the user-oriented I / O interface 225 is used to establish communication and exchange data with the terminal 210 through a wireless network or a wired network, so as to realize the sending and receiving of messages between accounts.
[0049] In a possible implementation, when the terminal 210 receives a user control operation on a virtual object, that is, based on the control operation, a virtual object data write request is determined, thereby determining a target memory allocator in the terminal 210 corresponding to the virtual object data according to the life cycle of the key-value object in the virtual object data write request, and storing the virtual object data in the corresponding target memory block according to the data volume.
[0050] In a possible implementation, in order to synchronize the virtual object data generated according to the user's control operation on the virtual object to the server 220 in real time, the terminal 210 sends a virtual object data write request to the server 220, so that the server 220 receives the virtual object data write request, and determines the target memory allocator in the server 220 corresponding to the virtual object data according to the life cycle of the key-value object in the virtual object data write request, and stores the virtual object data in the corresponding target memory block according to the data volume.
[0051] Please refer to Figure 3 , which shows a flowchart of a memory management method provided by an exemplary embodiment of the present application. In this embodiment, the method is used for a computer device ( Figure 2 The method is described as an example using the terminal 210 or the server 220 in FIG. 1 , and includes the following steps:
[0052] Step 301: Receive a first data write request, where the first data write request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object.
[0053] In one possible implementation, a computer device receives a first data write request, and optionally, the first data write request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object, wherein the first life cycle is used to determine a valid period of data of the first key-value object, and the first data volume is used to determine a storage space required to be occupied by the first key-value object.
[0054] Optionally, the first key value object includes a first index value (Key value) and a first data value (Value value), and different key value objects may correspond to the same index value or the same data value.
[0055] Step 302: Determine a target memory allocator corresponding to the first key-value object based on the first life cycle, wherein different memory allocators are used to manage key-value objects with different life cycles.
[0056] Different from the related art, in which different memory allocators are used to manage key-value objects with different data amounts, in the embodiments of the present application, in order to improve the management efficiency of key-value objects with different life cycles, different memory allocators are used to manage key-value objects with different life cycles. For example, the first memory allocator is used to manage key-value objects with a life cycle of less than 2 seconds, the second memory allocator is used to manage key-value objects with a life cycle of less than 4 seconds, and the third memory allocator is used to manage key-value objects with a life cycle of less than 8 seconds.
[0057] In a possible implementation, when receiving the first data write request, the computer device first determines the target memory allocator corresponding to the first key-value object according to the first life cycle of the first key-value object. In an exemplary embodiment, when the first life cycle of the first key-value object is 3 seconds, the computer device determines the second memory allocator as the target memory allocator.
[0058] Step 303: based on the first data volume, store the first key-value object into a target memory block corresponding to the target memory allocator.
[0059] Different from the related art, each memory allocator divides the space of each memory block in advance according to the data volume of the key-value objects it is responsible for managing, so that the number of key-value objects that can be stored in each memory block is fixed, resulting in a waste of memory space. In the embodiment of the present application, each memory allocator does not divide the space in advance, but determines the target memory block for storing the first key-value object based on the storage space actually consumed by the key-value object.
[0060] In a possible implementation, the computer device determines a target memory block that can meet the storage requirements of the first key-value object based on the first data volume of the first key-value object, thereby storing the first key-value object in the target memory block corresponding to the target memory allocator.
[0061] To sum up, in the embodiments of the present application, key-value objects with different life cycles are managed through different memory allocators. When a first data write request corresponding to a first key-value object is received, the target memory allocator corresponding to the first key-value object can be determined according to the first life cycle of the first key-value object in the first data write request, and the first key-value object can be stored in the target memory block corresponding to the target memory allocator according to the first data volume of the first key-value object. By adopting the scheme provided in the embodiments of the present application, key-value objects can be managed according to different life cycles, thereby improving the efficiency of memory management.
[0062] In a possible implementation, in order to further improve memory management efficiency, the computer device may uniformly manage the memory blocks corresponding to each memory allocator through a memory manager (Slab Manager), thereby improving the effective utilization rate of each memory block.
[0063] Please refer to Figure 4 , which shows a flowchart of a memory management method provided by another exemplary embodiment of the present application. In this embodiment, the method is used for a computer device ( Figure 2 The method is described as an example using the terminal 210 or the server 220 in FIG. 1 , and includes the following steps:
[0064] Step 401: Receive a first data write request, where the first data write request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object.
[0065] Step 402: Determine a target memory allocator corresponding to the first key-value object based on the first life cycle, wherein different memory allocators are used to manage key-value objects with different life cycles.
[0066] The specific implementation of steps 401 to 402 may refer to steps 301 to 302, which will not be described in detail in this embodiment.
[0067] Step 403 ( Figure 4 (not shown), based on the first data volume, the first key-value object is stored in a first memory block corresponding to the target memory allocator, and the first memory block is located at the tail position of the memory block linked list corresponding to the target memory allocator.
[0068] Among them, each memory allocator manages the key-value objects stored in each memory block in the memory block linked list by managing the memory block linked list, and the memory blocks in the memory block linked list are located in the memory manager. The memory manager is used to create and manage the memory blocks corresponding to each memory allocator, and each memory allocator is used to maintain the head pointer and tail pointer of the memory block linked list.
[0069] Optionally, the computer device manages the memory blocks corresponding to each memory allocator in a unified manner through a memory manager (Slab Manager), that is, each memory block is uniformly stored in the memory manager. Each memory allocator manages the key-value objects stored in each memory block in the memory block linked list by managing the memory block linked list, wherein each memory allocator is responsible for maintaining the head pointer and tail pointer of the corresponding memory block linked list, and each memory block in the memory block linked list is created and managed by the memory manager.
[0070] Indicatively, Figure 5 As shown, the memory manager 504 is responsible for managing and allocating each memory block in the memory block linked list corresponding to each memory allocator, such as the first memory allocator 501, the second memory allocator 502, and the third memory allocator 503. Taking the first memory allocator 501 as an example, the first memory allocator 501 is responsible for maintaining the head pointer and the tail pointer of the first memory block linked list. Each memory block in the first memory block linked list is bound to an address pointer, and each memory block is connected in sequence based on the address pointer to form the first memory block linked list.
[0071] In one possible implementation, in order to improve the management efficiency of each memory block corresponding to the memory allocator and the processing efficiency of expired data in the memory block linked list, the computer device can store the first key-value object in the first memory block corresponding to the target allocator, wherein the first memory block is located at the tail position of the memory block linked list corresponding to the target memory allocator, so that the memory block linked list shows a trend of memory block life cycle expiring in sequence from the head to the tail, and the memory block at the head will expire first, followed by the memory blocks at the back of the linked list, and the last to expire is the memory block at the tail.
[0072] Indicatively, Figure 5 As shown, when the first memory allocator 501 is the target memory allocator, the computer device stores the first key-value object in the tail memory block, so that the life cycle (Time to Live, TTL) of each memory block in the first memory block linked list corresponding to the first memory allocator 501 shows an increasing trend, that is, TTL(S1)<TTL(S4)<TTL(S5)<···<TTL(Sn), and the memory block S1 at the head position will expire first, followed by the subsequent memory blocks, and the last to expire is the memory block Sn.
[0073] Step 403A: When the remaining storage space of the first memory block is greater than or equal to the first data amount, the first key-value object is stored in the first memory block.
[0074] In one possible implementation, since each memory block corresponding to the target memory allocator stores the written key-value objects according to the storage space actually consumed by the key-value objects, before writing the first key-value object into the first memory block corresponding to the target memory allocator, the computer device first needs to determine the remaining storage space of the first memory block, and then determine whether the first key-value object can be written into the first memory block based on the remaining storage space.
[0075] Indicatively, Figure 6 As shown, the computer device stores each key-value object in the first memory block according to the storage space actually consumed by each key-value object.
[0076] In a possible implementation, when the remaining storage space of the first memory block is greater than or equal to the first data amount, the computer device determines that the first memory block can store the first key-value object, and thus stores the first key-value object in the first memory block.
[0077] Step 404A: When the remaining storage space of the first memory block is less than the first data amount, a memory block allocation request is sent to the memory manager, where the memory block allocation request is used to request the memory manager to allocate a memory block to the target memory allocator.
[0078] In one possible implementation, when the remaining storage space of the first memory block is less than the first data amount, the computer device determines that the first memory block cannot store the first key-value object. Since the memory block linked list only allows data to be appended at the tail and does not allow data to be written to the middle memory block, when the first memory block at the tail of the linked list cannot store the first key-value object, the computer device can request the memory manager to allocate a memory block to the target memory allocator by sending a memory block allocation request to the memory manager.
[0079] Step 404B: when the memory manager allocates the second memory block to the target memory allocator, the second memory block is placed at the tail position of the memory block linked list.
[0080] In one possible implementation, when the memory manager receives a memory block allocation request, the memory manager allocates a second memory block to the target memory allocator, and in order to keep the data of each memory block in the memory block linked list expired in sequence, the computer device places the second memory block at the tail position of the memory block linked list, that is, after the first memory block.
[0081] Step 404C: store the first key-value object in the second memory block.
[0082] In a possible implementation, after placing the second memory block at the tail position of the memory block linked list, the computer device stores the first key-value object in the second memory block.
[0083] In one possible implementation, when the next data write request is received, the computer device determines whether to store the key-value object in the second memory block based on the data value of the key-value object in the data write request and the remaining storage space of the second memory block, without considering the remaining storage space of the first memory block.
[0084] In the above embodiment, each memory allocator manages the key-value objects stored in each memory block by means of a memory block linked list, and the memory blocks corresponding to each memory allocator are uniformly managed by the memory manager. Only when the target memory allocator has data storage requirements will the memory manager allocate memory blocks to the target memory allocator, thereby realizing the allocation and transfer of memory blocks between multiple memory allocators and improving the efficiency of memory management.
[0085] In addition, according to the storage space actually consumed by the first key-value object, the first key-value object is stored in the corresponding target memory block, thereby avoiding the waste of storage space in the memory block and improving the effective utilization rate of the memory block.
[0086] In one possible implementation, in order to improve the reading speed of key-value objects stored in each memory block in the memory manager, after storing the first key-value object in the target memory block, the computer device can store the storage address information corresponding to each key-value object by establishing a hash table, thereby reading the stored key-value objects from the memory manager based on the hash table.
[0087] Please refer to Figure 7 , which shows a flowchart of writing and reading data from a hash table provided by an exemplary embodiment of the present application, the process includes the following steps:
[0088] Step 701, determine first storage address information of a first key-value object, where the first storage address information includes an allocator number of a target memory allocator and address information of a target memory block.
[0089] In one possible implementation, after writing the first key-value object to the target memory block, the computer device determines first storage address information of the first key-value object, wherein the first storage address information includes the allocator number of the target memory allocator and the address information of the target memory block, and the address information may be the address pointer information corresponding to the target memory block in the memory block linked list.
[0090] Step 702: Write the first hash value and first storage address information corresponding to the first key-value object into a hash table, where the hash table is used to store and manage storage address information of each key-value object in the memory manager.
[0091] In one possible implementation, a computer device determines a corresponding first hash value based on a first index value corresponding to a first key value object, and writes the first hash value and first storage address information into a hash table, which is used to store and manage storage address information of each key value object in a memory manager.
[0092] Indicatively, Figure 8 As shown, after determining the target memory allocator from multiple memory allocators such as the first memory allocator 801, the second memory allocator 802, and the third memory allocator 803, the computer device stores the first key-value object in the target memory block corresponding to the target memory allocator, and the target memory block is located in the memory manager 804. The computer device then determines the first storage address information corresponding to the first key-value object based on the address information of the target memory block in the memory manager 804, and writes the first hash value and the first storage address information corresponding to the first key-value object into the hash table 805.
[0093] In one possible implementation, when the same index value corresponds to multiple key-value objects and the multiple key-value objects have not reached their life cycle, the computer device does not directly update the storage address of the original key-value object when storing a new key-value object, but re-determines the target memory allocator corresponding to the new key-value object and stores the new key-value object in the corresponding target memory block. However, in order to improve the efficiency and effectiveness of reading data from the hash table, the computer device only maintains the storage address information of the latest key-value object in the hash table.
[0094] Indicatively, Fig. 9 As shown, when the same index value corresponds to multiple key value objects and the multiple key value objects have not reached their life cycle, in the process of determining the storage address information from the memory manager 901 and writing the storage address information into the hash table 902, the computer device overwrites and updates the previous storage address information with the latest storage address information, that is, only the latest storage address information is maintained in the hash table 902.
[0095] In one possible implementation, a computer device receives a second data write request, which includes a second key-value object, a second life cycle of the second key-value object, and a second data volume of the second key-value object, so that the computer device stores the second key-value object in a corresponding memory block based on the second life cycle and the second data volume, and determines the second storage address information of the second key-value object. Before storing the second storage address information corresponding to the second key-value object in a hash table, the computer device first determines whether storage address information of other key-value objects having the same index value as the second key-value object already exists in the hash table.
[0096] In a possible implementation, when the second index value of the second key value object is different from the first index value of the first key value object, the computer device directly writes the second hash value and the second storage address information corresponding to the second index value into the hash table; when the second index value of the second key value object is the same as the first index value of the first key value object, the computer device updates the first hash value and the first storage address information stored in the hash table to the second hash value and the second storage address information.
[0097] Step 703: Receive a data read request, where the data read request includes a first index value of a first key-value object.
[0098] In a possible implementation, a computer device receives a data read request, wherein the data read request includes at least a first index value of a first key-value object.
[0099] Step 704: based on the first hash value corresponding to the first index value, search the hash table for first storage address information of the first key value object.
[0100] In a possible implementation, after receiving a data read request, the computer device determines a corresponding first hash value based on a first index value of the first key value object, and searches a hash table for first storage address information of the first key value object based on the first hash value.
[0101] Step 705: read the first data value of the first key-value object from the target memory block based on the first storage address information.
[0102] In one possible implementation, after determining the first storage address information of the first key-value object, the computer device determines the target memory block storing the first key-value object from the memory manager based on the first storage address information, and reads the first data value of the first key-value object from the target memory block.
[0103] In the above embodiment, by establishing a hash table, after the first key-value object is stored in the target memory block, the first storage address information corresponding to the first key-value object is further written into the hash table, so that the first key-value object can be read from the memory manager based on the address information stored in the hash table, thereby improving the reading speed of the key-value objects stored in each memory block in the memory manager and further improving the efficiency of memory management.
[0104] Please refer to Fig.10 , which shows a flowchart of writing a first key-value object provided by an exemplary embodiment of the present application.
[0105] like Fig.10 As shown, the computer device receives a first data write request, determines a first key-value object, a first life cycle and a first data volume of the first key-value object from the first data write request, thereby determining a target memory allocator according to the first life cycle, and determining a target memory block corresponding to the target memory allocator according to the first data volume, and then writing the first key-value object into the target memory block, determining first storage address information of the first key-value object in the memory manager, and writing the first storage address information into a hash table.
[0106] Please refer to Fig.11 , which shows a flowchart of reading a first data value provided by an exemplary embodiment of the present application.
[0107] like Fig.11As shown, the computer device receives a data read request, determines a first index value corresponding to a first key value object from the data read request, and determines a first hash value based on the first index value, thereby determining first storage address information of the first key value object from a hash table according to the first hash value, and based on the first storage address information, finds a target memory block corresponding to the first key value object from a memory manager, and reads the first data value of the first key value object from the target memory block.
[0108] In one possible implementation, in order to improve the effective utilization rate of each memory block in the memory manager, the computer device adopts a method of actively eliminating expired data, and regularly scans each memory block in the memory block linked list corresponding to each memory allocator through a data elimination thread.
[0109] Please refer to Fig.12 , which shows a flowchart of eliminating expired data provided by an exemplary embodiment of the present application, the process includes the following steps:
[0110] Step 1201, based on the data elimination thread, traverse the memory block linked list corresponding to each memory allocator in order from low to high life cycle. The data elimination thread is used to periodically scan each memory allocator to clear expired data.
[0111] Optionally, the data elimination thread is used to periodically scan each memory allocator to clear expired data stored in the memory block linked list corresponding to each memory allocator.
[0112] In a possible implementation, the computer device, based on the data elimination thread, sequentially traverses the memory block linked lists corresponding to the various memory allocators in the order of the life cycle of the key value objects managed by the various memory allocators from low to high. For example, the first memory allocator is used to manage key value objects with a life cycle of less than 2 seconds, the second memory allocator is used to manage key value objects with a life cycle of less than 4 seconds, and the third memory allocator is used to manage key value objects with a life cycle of less than 8 seconds. The computer device traverses the various memory allocators in the order from the first memory allocator to the second memory allocator and then to the third memory allocator.
[0113] In a possible implementation, for the memory block linked list corresponding to each memory allocator, the computer device traverses the memory block linked list in sequence from the head pointer to the tail pointer according to the linked list pointer sequence.
[0114] Step 1202: if there is a third memory block that meets the elimination criteria in the memory block linked list, recycle the third memory block, where the elimination criteria refers to that each third key-value object stored in the third memory block meets the elimination time requirement.
[0115] In one possible implementation, during the traversal of the memory block linked list, the computer device recycles the memory block that meets the elimination criteria in the memory block linked list, wherein the elimination criteria refers to that each key-value object stored in the memory block meets the elimination time requirement.
[0116] In a possible implementation, when there is a third memory block in the memory block linked list that meets the elimination criteria, the computer device recycles the third memory block, traverses and clears the third key value object stored in the third memory block, and deletes the third hash value and third storage address information corresponding to the third key value object from the hash table based on the index value of the third key value object.
[0117] In one possible implementation, during traversal of a memory block linked list, if there is a fourth memory block in the memory block linked list that does not meet the elimination criteria, the computer device stops traversing the current memory block linked list and traverses the next memory block linked list, wherein there is at least one key value object in the fourth memory block that does not meet the elimination time requirement.
[0118] Indicatively, Fig.13 As shown, after starting the data elimination thread, the computer device starts to traverse the memory block linked list corresponding to the first memory allocator 1301, first traverses the memory block S1 at the head position, and when the memory block S1 meets the elimination criteria, continues to traverse the memory block S4, and when the memory block S4 meets the elimination criteria, continues to traverse the memory block S5, and when the memory block S5 does not meet the elimination criteria, the computer device ends the traversal of the memory block linked list corresponding to the first memory allocator 1301, and starts to traverse the memory block linked list corresponding to the second memory allocator 1302, first traversing the memory block S3 at the head position, and when the memory block S3 does not meet the elimination criteria Under the standard condition, the computer device ends the traversal of the memory block linked list corresponding to the second memory allocator 1302, and starts to traverse the memory block linked list corresponding to the third memory allocator 1303, first traversing the memory block S2 at the head position, and when the memory block S2 meets the elimination criteria, continuing to traverse the memory block S6, when the memory block S6 does not meet the elimination criteria, the computer device ends the traversal of the memory block linked list corresponding to the third memory allocator 1303, and starts to traverse the memory block linked list corresponding to the next memory allocator, and when the traversal of the memory block linked list corresponding to the Nth memory allocator 1304 is completed, the single traversal ends.
[0119] Step 1203: Place the recycled third memory block in a free memory block linked list of the memory manager.
[0120] In a possible implementation, in order to improve the utilization rate of each memory block in the memory manager, the computer device is provided with a free memory block linked list in the memory manager. After recycling the third memory block, the computer device places the recycled third memory block in the free memory block linked list.
[0121] Optionally, the computer device may place the third memory block at the head position of the free memory block linked list, or may place the third memory block at the tail position of the free memory block linked list, which is not limited in the embodiment of the present application.
[0122] Step 1204: When the memory manager has no unallocated memory blocks and receives a memory block allocation request, determine the fifth memory block from the free memory block linked list.
[0123] In one possible implementation, in order to facilitate effective management of memory blocks in the memory manager, after the system starts, the computer device will create a memory block array in the memory manager and divide the memory block array into an allocated part and an unallocated part, wherein the allocated part includes memory blocks that have been allocated to each memory allocator, and the unallocated part includes memory blocks that have not yet been allocated to the memory allocator. For allocated and recycled memory blocks, the computer device will place them in a free memory block linked list.
[0124] In one possible implementation, when a memory block allocation request is received, the computer device can directly determine a memory block from the unallocated part and allocate the memory block to the corresponding target memory allocator; when there is no unallocated memory block and a memory block allocation request is received, the computer device directly determines the fifth memory block from the free memory block linked list, wherein the fifth memory block can be located at the head position of the free memory block linked list or at the tail position of the free memory block linked list, and this embodiment of the present application is not limited to this.
[0125] In a possible implementation, the computer device may place the recycled third memory block at the tail position of the free memory block linked list, and determine the fifth memory block from the head position of the free memory block linked list.
[0126] Indicatively, Fig.14As shown, the memory manager 1401 has a memory block array 1402 and a free memory block linked list 1403, wherein each memory block in the memory block array 1402 can belong to an allocated part and an unallocated part. When a memory block allocation request is received, the computer device determines a memory block from the unallocated part and allocates it to the target memory allocator for storing key-value objects. After starting the data elimination thread and traversing and recycling the memory blocks in sequence, the computer device places the recycled memory blocks in sequence at the tail position of the free memory block linked list 1403. When there is no unallocated part and a memory block allocation request is received, the computer device determines a memory block from the head position of the free memory block linked list 1403 and allocates it to the target memory allocator for storing key-value objects.
[0127] Step 1205: allocate the fifth memory block to the target memory allocator, and store the first key-value object in the fifth memory block.
[0128] Further, the computer device allocates the fifth memory block to the target memory allocator, and stores the first key-value object in the fifth memory block.
[0129] In the above embodiment, based on the data elimination thread, the memory block linked list corresponding to each memory allocator is traversed in turn, so as to recycle the memory blocks that meet the elimination criteria and place them in the free memory block linked list. Then, when there is no unallocated part and a memory block allocation request is received, the memory block can be directly determined from the free memory block linked list and allocated. By actively eliminating expired data, expired key-value objects stored in the memory block can be cleaned up in a timely and fast manner, thereby improving the effective utilization rate of the memory block.
[0130] Please refer to Fig.15 , which shows a structural block diagram of a memory management device provided by an exemplary embodiment of the present application, the device comprising:
[0131] The first receiving module 1501 is configured to receive a first data writing request, wherein the first data writing request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object;
[0132] A first determining module 1502 is used to determine a target memory allocator corresponding to the first key-value object based on the first life cycle, wherein different memory allocators are used to manage key-value objects of different life cycles;
[0133] The first storage module 1503 is used to store the first key-value object in a target memory block corresponding to the target memory allocator based on the first data volume.
[0134] Optionally, the first storage module 1503 includes:
[0135] A first storage unit, configured to store the first key-value object in a first memory block corresponding to the target memory allocator based on the first data volume, wherein the first memory block is located at the tail position of a memory block linked list corresponding to the target memory allocator;
[0136] Among them, each memory allocator manages the key-value objects stored in each memory block in the memory block linked list by managing the memory block linked list, and the memory blocks in the memory block linked list are located in the memory manager. The memory manager is used to create and manage the memory blocks corresponding to each memory allocator, and each memory allocator is used to maintain the head pointer and tail pointer of the memory block linked list.
[0137] Optionally, the first storage unit is used to:
[0138] When the remaining storage space of the first memory block is greater than or equal to the first data amount, the first key-value object is stored in the first memory block.
[0139] Optionally, the first storage module 1503 further includes:
[0140] A second storage unit is used to send a memory block allocation request to the memory manager when the remaining storage space of the first memory block is less than the first data amount, and the memory block allocation request is used to request the memory manager to allocate a memory block to the target memory allocator; when the memory manager allocates a second memory block to the target memory allocator, place the second memory block at the tail position of the memory block linked list; and store the first key-value object in the second memory block.
[0141] Optionally, after storing the first key-value object in a target memory block corresponding to the target memory allocator based on the first data volume, the device further includes:
[0142] A second determining module is used to determine first storage address information of the first key-value object, where the first storage address information includes an allocator number of the target memory allocator and address information of the target memory block;
[0143] The first writing module is used to write the first hash value corresponding to the first key-value object and the first storage address information into a hash table, where the hash table is used to store and manage storage address information of each key-value object in a memory manager.
[0144] Optionally, the device further comprises:
[0145] A second receiving module, configured to receive a data reading request, wherein the data reading request includes a first index value of the first key-value object;
[0146] A search module, configured to search the hash table for the first storage address information of the first key-value object based on a first hash value corresponding to the first index value;
[0147] A reading module is used to read the first data value of the first key-value object from the target memory block based on the first storage address information.
[0148] Optionally, the device further comprises:
[0149] A third receiving module is used to receive a second data writing request, where the second data writing request includes a second key-value object, a second life cycle of the second key-value object, and a second data volume of the second key-value object;
[0150] A second storage module, used for storing the second key-value object in a corresponding memory block based on the second life cycle and the second data volume;
[0151] A third determining module, used to determine second storage address information of the second key-value object;
[0152] A second writing module, configured to write a second hash value corresponding to the second index value and the second storage address information into the hash table when the second index value of the second key value object is different from the first index value of the first key value object;
[0153] An update module is used to update the first hash value and the first storage address information stored in the hash table to the second hash value and the second storage address information when the second index value of the second key value object is the same as the first index value of the first key value object.
[0154] Optionally, the device further comprises:
[0155] A traversal module is used to traverse the memory block linked list corresponding to each memory allocator in the order of life cycle from low to high based on the data elimination thread, and the data elimination thread is used to periodically scan each memory allocator to clear expired data;
[0156] a recycling processing module, configured to recycle the third memory block if there is a third memory block that meets the elimination criteria in the memory block linked list, wherein the elimination criteria refers to that each third key-value object stored in the third memory block meets the elimination time requirement;
[0157] The placement module is used to place the third memory block after the recycling process in the free memory block linked list of the memory manager.
[0158] Optionally, the recycling processing module is used to:
[0159] In the case where the third memory block that meets the elimination criterion exists in the memory block linked list, traversing and clearing the third key-value object stored in the third memory block;
[0160] The third hash value and the third storage address information corresponding to the third key value object are deleted from the hash table.
[0161] Optionally, the traversal module is used to:
[0162] Based on the data elimination thread, traverse the memory block linked list corresponding to each memory allocator and each memory block in the memory block linked list in the order of life cycle from low to high and the order of linked list pointers;
[0163] When there is a fourth memory block in the memory block linked list that does not meet the elimination criteria, the traversal of the current memory block linked list is stopped, and the traversal of the next memory block linked list is performed.
[0164] Optionally, the device further comprises:
[0165] a fourth determining module, configured to determine a fifth memory block from the free memory block linked list when the memory manager does not have any unallocated memory blocks and receives a memory block allocation request;
[0166] A third storage module is used to allocate the fifth memory block to the target memory allocator and store the first key-value object in the fifth memory block.
[0167] To sum up, in the embodiments of the present application, key-value objects with different life cycles are managed through different memory allocators. When a first data write request corresponding to a first key-value object is received, the target memory allocator corresponding to the first key-value object can be determined according to the first life cycle of the first key-value object in the first data write request, and the first key-value object can be stored in the target memory block corresponding to the target memory allocator according to the first data volume of the first key-value object. By adopting the scheme provided in the embodiments of the present application, key-value objects can be managed according to different life cycles, thereby improving the efficiency of memory management.
[0168] It should be noted that: the device provided in the above embodiment is only illustrated by the division of the above functional modules. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the device and method embodiments provided in the above embodiment belong to the same concept, and the implementation process thereof is detailed in the method embodiment, which will not be repeated here.
[0169] Please refer to Fig.16 , which shows a schematic diagram of the structure of a computer device provided by an exemplary embodiment of the present application. Specifically, the computer device 1600 includes a central processing unit (CPU) 1601, a system memory 1604 including a random access memory 1602 and a read-only memory 1603, and a system bus 1605 connecting the system memory 1604 and the central processing unit 1601. The computer device 1600 also includes a basic input / output system (I / O system) 1606 that helps transmit information between various devices in the computer, and a large-capacity storage device 1607 for storing an operating system 1613, an application program 1614, and other program modules 1615.
[0170] The basic input / output system 1606 includes a display 1608 for displaying information and an input device 1609 such as a mouse and a keyboard for user inputting information. The display 1608 and the input device 1609 are connected to the central processing unit 1601 through an input / output controller 1610 connected to the system bus 1605. The basic input / output system 1606 may also include an input / output controller 1610 for receiving and processing inputs from a plurality of other devices such as a keyboard, a mouse, or an electronic stylus. Similarly, the input / output controller 1610 also provides output to a display screen, a printer, or other types of output devices.
[0171] The mass storage device 1607 is connected to the central processing unit 1601 through a mass storage controller (not shown) connected to the system bus 1605. The mass storage device 1607 and its associated computer readable media provide non-volatile storage for the computer device 1600. That is, the mass storage device 1607 may include a computer readable medium (not shown) such as a hard disk or drive.
[0172] Without loss of generality, the computer-readable medium may include computer storage media and communication media. Computer storage media include volatile and non-volatile, removable and non-removable media implemented by any method or technology for storing information such as computer-readable instructions, data structures, program modules or other data. Computer storage media include random access memory (RAM), read-only memory (ROM), flash memory or other solid-state storage technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, cassette, tape, disk storage or other magnetic storage devices. Of course, those skilled in the art will know that the computer storage medium is not limited to the above. The above-mentioned system memory 1604 and mass storage device 1607 can be collectively referred to as memory.
[0173] The memory stores one or more programs, and the one or more programs are configured to be executed by one or more central processing units 1601. The one or more programs contain instructions for implementing the above-mentioned methods. The central processing unit 1601 executes the one or more programs to implement the methods provided by the above-mentioned various method embodiments.
[0174] According to various embodiments of the present application, the computer device 1600 can also be connected to a remote computer on the network through a network such as the Internet. That is, the computer device 1600 can be connected to the network 1611 through the network interface unit 1612 connected to the system bus 1605, or the network interface unit 1612 can be used to connect to other types of networks or remote computer systems (not shown).
[0175] An embodiment of the present application also provides a computer-readable storage medium, in which at least one instruction is stored. The at least one instruction is loaded and executed by a processor to implement the memory management method described in the above embodiment.
[0176] Optionally, the computer readable storage medium may include: ROM, RAM, solid state drives (SSD) or optical disks, etc. Among them, RAM may include resistance random access memory (ReRAM) and dynamic random access memory (DRAM).
[0177] The embodiment of the present application provides a computer program product, which includes computer instructions, which are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the memory management method described in the above embodiment.
[0178] A person skilled in the art will understand that all or part of the steps to implement the above embodiments may be accomplished by hardware or by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk or an optical disk, etc.
[0179] The above description is only an optional embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
Claims
1. A memory management method, characterized in that: The method comprises: Receive a first data write request, where the first data write request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object; Based on the first life cycle, determining a target memory allocator corresponding to the first key-value object, wherein different memory allocators are used to manage key-value objects with different life cycles; Based on the first data volume, the first key-value object is stored in a target memory block corresponding to the target memory allocator.
2. The method according to claim 1, characterized in that The storing, based on the first data volume, the first key-value object into a target memory block corresponding to the target memory allocator includes: Based on the first data volume, storing the first key-value object in a first memory block corresponding to the target memory allocator, the first memory block being located at the tail position of a memory block linked list corresponding to the target memory allocator; Among them, each memory allocator manages the key-value objects stored in each memory block in the memory block linked list by managing the memory block linked list, and the memory blocks in the memory block linked list are located in the memory manager. The memory manager is used to create and manage the memory blocks corresponding to each memory allocator, and each memory allocator is used to maintain the head pointer and tail pointer of the memory block linked list.
3. The method according to claim 2, characterized in that The storing, based on the first data amount, the first key-value object into a first memory block corresponding to the target memory allocator includes: When the remaining storage space of the first memory block is greater than or equal to the first data amount, the first key-value object is stored in the first memory block.
4. The method according to claim 3, characterized in that Based on the first data volume, storing the first key-value object in a target memory block corresponding to the target memory allocator further includes: In a case where the remaining storage space of the first memory block is less than the first data amount, sending a memory block allocation request to the memory manager, wherein the memory block allocation request is used to request the memory manager to allocate a memory block to the target memory allocator; When the memory manager allocates a second memory block to the target memory allocator, placing the second memory block at the tail position of the memory block linked list; Store the first key-value object in the second memory block.
5. The method according to claim 1, characterized in that After storing the first key-value object in the target memory block corresponding to the target memory allocator based on the first data volume, the method further includes: Determine first storage address information of the first key-value object, where the first storage address information includes an allocator number of the target memory allocator and address information of the target memory block; The first hash value corresponding to the first key-value object and the first storage address information are written into a hash table, where the hash table is used to store and manage storage address information of each key-value object in a memory manager.
6. The method according to claim 5, characterized in that The method further comprises: Receive a data read request, wherein the data read request includes a first index value of the first key-value object; Based on a first hash value corresponding to the first index value, searching the hash table for the first storage address information of the first key-value object; Based on the first storage address information, read the first data value of the first key-value object from the target memory block.
7. The method according to claim 5, characterized in that The method further comprises: receiving a second data write request, wherein the second data write request includes a second key-value object, a second life cycle of the second key-value object, and a second data volume of the second key-value object; Based on the second life cycle and the second data volume, storing the second key-value object in a corresponding memory block; Determine second storage address information of the second key-value object; When the second index value of the second key value object is different from the first index value of the first key value object, writing a second hash value corresponding to the second index value and the second storage address information into the hash table; When the second index value of the second key value object is the same as the first index value of the first key value object, the first hash value and the first storage address information stored in the hash table are updated to the second hash value and the second storage address information.
8. The method according to any one of claims 1 to 7, characterized in that: The method further comprises: Based on the data elimination thread, the memory block linked list corresponding to each memory allocator is traversed in the order of life cycle from low to high, and the data elimination thread is used to periodically scan each memory allocator to remove expired data; In the case where there is a third memory block in the memory block linked list that meets the elimination criteria, recycling the third memory block, wherein the elimination criteria refers to that each third key-value object stored in the third memory block meets the elimination time requirement; The third memory block after the recycling process is placed in the free memory block linked list of the memory manager.
9. The method according to claim 8, characterized in that When there is a third memory block that meets the elimination criteria in the memory block linked list, recycling the third memory block includes: In the case where the third memory block that meets the elimination criterion exists in the memory block linked list, traversing and clearing the third key-value object stored in the third memory block; The third hash value and the third storage address information corresponding to the third key value object are deleted from the hash table.
10. The method according to claim 8, characterized in that The data-based elimination thread traverses the memory block linked lists corresponding to each memory allocator in order of life cycle from low to high, including: Based on the data elimination thread, traverse the memory block linked list corresponding to each memory allocator and each memory block in the memory block linked list in the order of life cycle from low to high and the order of linked list pointers; When there is a fourth memory block in the memory block linked list that does not meet the elimination criteria, the traversal of the current memory block linked list is stopped, and the traversal of the next memory block linked list is performed.
11. The method according to claim 8, characterized in that The method further comprises: When the memory manager has no unallocated memory blocks and receives a memory block allocation request, determining a fifth memory block from the free memory block linked list; The fifth memory block is allocated to the target memory allocator, and the first key-value object is stored in the fifth memory block.
12. A memory management device, characterized in that: The device comprises: A first receiving module is used to receive a first data writing request, where the first data writing request includes a first key-value object, a first life cycle of the first key-value object, and a first data volume of the first key-value object; a first determining module, configured to determine a target memory allocator corresponding to the first key-value object based on the first life cycle, wherein different memory allocators are used to manage key-value objects of different life cycles; A first storage module is used to store the first key-value object in a target memory block corresponding to the target memory allocator based on the first data volume.
13. An electronic device, characterized in that: The electronic device includes a processor and a memory; the memory stores at least one instruction, and the at least one instruction is used to be executed by the processor to implement the memory management method as described in any one of claims 1 to 11.
14. A computer-readable storage medium, characterized in that: The storage medium stores at least one instruction, and the at least one instruction is used to be executed by a processor to implement the memory management method according to any one of claims 1 to 11.
15. A computer program product, characterized in that The computer program product includes computer instructions, which are stored in a computer-readable storage medium; a processor of an electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the electronic device implements the memory management method as described in any one of claims 1 to 11.
