Memory allocation method and device based on runtime system and storage medium

By configuring different types of memory blocks to store objects of different data sizes and pointer layout types, the problems of resource waste and CPU overhead in the existing technology are solved, and more efficient memory allocation and garbage collection are achieved.

CN119473578AActive Publication Date: 2025-02-18BEIJING VOLCANO ENGINE TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202411498417.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-24
Publication Date
2025-02-18
Estimated Expiration
2044-10-24

Smart Images

  • Figure CN119473578A_ABST
    Figure CN119473578A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a memory allocation method and device based on a runtime system and a storage medium. The method comprises the following steps: determining a first data size and a first pointer layout type of a to-be-allocated object; according to the first data size and the first pointer layout type, a target memory block used for storing the to-be-allocated object is determined from different types of pre-configured memory blocks, and any type of memory block is used for storing objects with the same data size and pointer layout type; and allocating an available memory space for the to-be-allocated object in the target memory block, and storing the to-be-allocated object in the available memory space. Objects with different data sizes and different pointer layout types are stored by configuring different types of memory blocks without using additional object heads or global bitmap to store pointer layout information of the objects, so that additional memory overhead and CPU overhead of encoding and decoding are reduced, the memory allocation efficiency is improved, and the user experience is improved. The efficiency of the process of obtaining the pointer in the object in subsequent garbage collection is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present disclosure relate to the field of computer and network communication technology, and in particular to a memory allocation method, device and storage medium based on a runtime system. Background Art

[0002] Memory management is usually responsible for memory allocation and garbage collection of the runtime system. The task of memory allocation is to return a block of available memory space of a specified size, and the task of garbage collection is to distinguish between live and dead objects as accurately as possible, and clean up dead objects for subsequent memory allocation. In order to distinguish between live and dead objects as accurately as possible, you need to know which positions in the object are pointers for recursive marking. Memory allocation and garbage collection are generally performed at intervals. Pointer layout information is provided during the memory allocation process, and pointer layout information is also needed during garbage collection. Therefore, the pointer layout information needs to be continued until garbage collection.

[0003] In the prior art, pointer layout information can usually be identified by allocating an object header to each object, or by encoding the pointer layout information of the object through a global bitmap, which can then be provided to the garbage collection process. However, the above methods will generate additional overhead and lead to resource waste. Summary of the invention

[0004] The embodiments of the present disclosure provide a memory allocation method, device and storage medium based on a runtime system to reduce the extra overhead of memory allocation and reduce resource waste.

[0005] In a first aspect, an embodiment of the present disclosure provides a memory allocation method based on a runtime system, comprising:

[0006] Determine a first data size and a first pointer layout type of the object to be allocated; wherein any pointer layout type is used to identify the layout mode of the pointer in the object; wherein any pointer layout type is used to identify the layout mode of the pointer in the object;

[0007] Determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type;

[0008] Allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

[0009] In a second aspect, an embodiment of the present disclosure provides a memory allocation device based on a runtime system, including:

[0010] A determination unit, used to determine a first data size and a first pointer layout type of the object to be allocated;

[0011] a memory block selection unit, configured to determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type;

[0012] A memory allocation unit is used to allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

[0013] In a third aspect, an embodiment of the present disclosure provides an electronic device, including: at least one processor and a memory;

[0014] The memory stores computer-executable instructions;

[0015] The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor executes the runtime system-based memory allocation method as described in the first aspect and various possible designs of the first aspect above.

[0016] In a fourth aspect, an embodiment of the present disclosure provides a computer-readable storage medium, in which computer execution instructions are stored. When a processor executes the computer execution instructions, a memory allocation method based on a runtime system as described in the first aspect and various possible designs of the first aspect is implemented.

[0017] In a fifth aspect, an embodiment of the present disclosure provides a computer program product, including computer execution instructions. When a processor executes the computer execution instructions, it implements the memory allocation method based on the runtime system as described in the first aspect and various possible designs of the first aspect.

[0018] The memory allocation method, device and storage medium based on the runtime system provided by the embodiments of the present disclosure determine the first data size and the first pointer layout type of the object to be allocated; wherein any pointer layout type is used to identify the layout mode of the pointer in the object; according to the first data size and the first pointer layout type, determine the target memory block for storing the object to be allocated from pre-configured memory blocks of different types, wherein any type of memory block is used to store a type of object, and the objects of the same type have the same data size and pointer layout type; allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space. By configuring different types of memory blocks to store objects of different data sizes and different pointer layout types, there is no need to use additional object headers or global bitmaps to store the pointer layout information of the object, which reduces additional memory overhead, reduces the CPU overhead of encoding and decoding, improves memory allocation efficiency, and can improve the efficiency of subsequent garbage collection and other processes that require obtaining pointers in objects. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0020] Figure 1a and Figure 1b A schematic diagram of memory allocation in the prior art;

[0021] Figure 2 A schematic diagram of a memory allocation method based on a runtime system according to an embodiment of the present disclosure;

[0022] Figure 3 A schematic diagram of memory block allocation provided by an embodiment of the present disclosure;

[0023] Figure 4 A schematic diagram of a memory recycling process flow provided by another embodiment of the present disclosure;

[0024] Figure 5 A structural block diagram of a memory allocation device based on a runtime system provided in an embodiment of the present disclosure;

[0025] Figure 6 A schematic diagram of the hardware structure of an electronic device provided in one embodiment of the present disclosure. DETAILED DESCRIPTION

[0026] In order to make the purpose, technical solution and advantages of the embodiments of the present disclosure clearer, the technical solution in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are part of the embodiments of the present disclosure, not all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.

[0027] The runtime system (Runtime System), also known as the runtime environment, is a software system that is responsible for processing and managing the underlying computer resources during the execution of an application. The runtime system can provide many support and services required by the application, such as memory management, thread management, etc. Among them, memory management is usually responsible for the memory allocation and garbage collection of the runtime system. The task of memory allocation is to return a piece of available memory space of a specified size, and the task of garbage collection is to distinguish between live and no longer live objects as accurately as possible, and clean up no longer live objects for subsequent memory allocation. If you want to distinguish between live and no longer live objects as accurately as possible, you need to know which positions in the object are pointers in order to perform recursive marking. Memory allocation and garbage collection are generally performed at intervals. Pointer layout information is provided during the memory allocation process, and pointer layout information is also needed during garbage collection. Therefore, the pointer layout information needs to be continued until garbage collection can still be used.

[0028] In the prior art, pointer layout information can usually be identified by allocating an object header to each object, or by encoding the pointer layout information of the object through a global bitmap, which can then be provided to the garbage collection process. The specific process is as follows:

[0029] 1) Object header-based method: Allocate additional object header space for each object to point to the pointer layout type bitmap stored in the metadata. The bitmap of the same pointer layout type is the same. The bitmap can be used to indicate whether each field in the pointer layout type is a pointer, for example Figure 1aFor each object, such as 8B (Byte) or 16B and larger objects, an object header space is allocated to record the pointer Type Pointer of the pointer layout type, pointing to the bitmap of the pointer layout type stored in the metadata, that is, TypeA, TypeB, etc. in the figure. When garbage collection is needed, the object header is found according to the object address and the specified offset offset. According to the object header, the corresponding pointer layout type bitmap can be obtained from the metadata, and then recursive marking is performed, that is, starting from the root object, the referenced objects are recursively traversed based on the pointer and marked as alive. After the traversal is completed, the objects that are not marked as alive can be recycled.

[0030] 2) Global bitmap-based method: Use an additional global bitmap, each object occupies a section of it, and calculate the corresponding address in the global bitmap based on the address of the object. During memory allocation, the pointer layout information is encoded into the specified bitmap in an agreed manner, and the pointer layout information is decoded from the bitmap according to the same rules when it is needed.

[0031] Specifically, in order to facilitate calculation, objects are generally classified according to data size, for example, objects are classified in the form of 8B, 16B, 24B..., and further classified according to whether they contain pointers, that is, objects of each data size are allocated two types of memory areas, including pointers and not including pointers. For objects of each data size that contain pointers, the bitmap can be configured according to its pointer layout information when they are stored in the memory area containing pointers. Specifically, in general, the pointer size is 8B under a 64-bit system, and each 8B in the object can be mapped to 1 bit in the bitmap in turn to indicate whether the corresponding 8B in the object is a pointer, where 0 in the bitmap indicates that it is not a pointer, and 1 indicates that it is a pointer, such as Figure 1b As shown; for objects of each data size that do not contain pointers, they are directly stored in the memory area that does not contain pointers.

[0032] During garbage collection, scan from the root object to determine the memory area where the currently traversed object is located. If it belongs to a memory area that does not contain a pointer, directly mark the currently traversed object as alive and return. If it belongs to a memory area that contains a pointer, index the global bitmap to obtain a bitmap corresponding to the currently traversed object, decode the bitmap to obtain whether every 8B of the object is a pointer, mark the currently traversed object as alive, and continue traversing according to the pointer.

[0033] However, both of the above methods have certain additional overheads:

[0034] Based on the object header method, at least an additional 4B of space is required for each object to store the object header, which is a large overhead for objects that occupy a small space.

[0035] The global bitmap-based approach requires additional space to store the global bitmap. The additional space required is less than that based on the object header. However, additional CPU resources are required to index the global bitmap and update the content of the bitmap during memory allocation and garbage collection.

[0036] In addition, the pointer layout information above is only needed when the object is alive during garbage collection. If it is a short-lived object, that is, it is no longer alive in the most recent round of garbage collection after allocation, this information is not needed, so it is unnecessary to store the pointer layout information additionally.

[0037] In order to solve the above technical problems, the disclosed embodiment provides a memory allocation method based on the runtime system, which configures different types of memory blocks to store objects of different data sizes and different pointer layout types. It only needs to determine the type of memory block to know the layout type of the object, without using an additional object header or global bitmap to store the pointer layout information of the object, thereby reducing the additional memory overhead and the CPU overhead of encoding and decoding, improving the memory allocation efficiency, and making the subsequent garbage collection and other processes that need to obtain pointers in the object more efficient. Short-lifecycle objects do not need pointer layout information, and can also be applied in the same way, without additional memory overhead, which can bring a significant performance improvement in scenarios with more short-lifecycle objects in microservices.

[0038] Specifically, the method can be implemented by determining a first data size and a first pointer layout type of the object to be allocated; wherein any pointer layout type is used to identify the layout method of the pointer in the object; according to the first data size and the first pointer layout type, determining a target memory block for storing the object to be allocated from pre-configured memory blocks of different types, wherein any type of memory block is used to store objects of one type, and objects of the same type have the same data size and pointer layout type; allocating available memory space for the object to be allocated in the target memory block, and storing the object to be allocated in the available memory space.

[0039] The memory allocation method based on the runtime system disclosed in the present invention will be described in detail below in conjunction with specific embodiments.

[0040] refer to Figure 2 , Figure 2A flowchart of a memory allocation method based on a runtime system provided in an embodiment of the present disclosure. The method of this embodiment can be applied in a terminal device or a server, and the memory allocation method based on a runtime system includes:

[0041] S201. Determine a first data size and a first pointer layout type of an object to be allocated; wherein any pointer layout type is used to identify a layout method of a pointer in an object.

[0042] In this embodiment, for any object to be allocated that needs to allocate memory for storage, the data size and pointer layout type of the object to be allocated can be determined first. For ease of description, in this embodiment, the data size of the object to be allocated is recorded as the first data size, and the pointer layout type of the object to be allocated is recorded as the first pointer layout type. The data size of the object is the size of the space occupied by the object in the memory, for example, it can be 8B (Byte), 16B, 24B, etc., and the object may include a pointer to reference other objects, or it may not include a pointer, and the position of the pointer in the object may also be different, and there are different pointer layout types.

[0043] For example, assuming that the pointer is 8B, there are two pointer layout types for the 8B object, namely: one is that the 8B object includes a pointer (that is, the 8B object is all pointers, for the convenience of description, the pointer layout type can be recorded as [pointer]), and the other is that the 8B object does not include a pointer (the pointer layout type can be recorded as [nopointer]);

[0044] For a 16B object, which includes two 8B, each 8B may be a pointer or may not be a pointer, so there are four pointer layout types, including:

[0045] [nopointer, nopointer]: Neither the two 8B of the 16B object are pointers;

[0046] [nopointer, pointer]: The first 8B of the 16B object is not a pointer, and the second 8B is a pointer;

[0047] [pointer,nopointer]: The first 8B of the 16B object is a pointer, and the second 8B is not a pointer;

[0048] [pointer, pointer]: The two 8B of the 16B object are pointers.

[0049] For a 24B object, which includes 3 8B, each 8B may be a pointer or may not be a pointer, so there are 8 types of pointer layouts, which will not be repeated here.

[0050] That is, in objects of different pointer layout types of the same data size, whether the fields included therein are pointers is different, and all possible pointer layout types can be determined through permutations and combinations. Of course, not all possible pointer layout types are available, and this can be determined based on actual conditions.

[0051] S202. Determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type.

[0052] In this embodiment, different types of memory blocks may be preconfigured to store objects of different data sizes and different pointer layout types respectively.

[0053] For example, for the above example, Figure 3 As shown, for an 8B object, two types of memory blocks can be configured (each type of memory block can include one or more memory blocks), one type of memory block is used to store objects with a data size of 8B and a pointer layout type of [pointer], and the other type of memory block is used to store objects with a data size of 8B and a pointer layout type of [nopointer];

[0054] For 16B objects, four types of memory blocks can be configured (each type of memory block may include one or more memory blocks), where the first type of memory block can be used to store objects with a data size of 16B and a pointer layout type of [nopointer, nopointer], the second type of memory block can be used to store objects with a data size of 16B and a pointer layout type of [nopointer, pointer], the third type of memory block can be used to store objects with a data size of 16B and a pointer layout type of [pointer, nopointer], and the fourth type of memory block can be used to store objects with a data size of 16B and a pointer layout type of [pointer, pointer].

[0055] By analogy, for any pointer layout type of an object of any data size, a memory block of one type can be allocated to store objects of that data size and pointer layout type. Of course, if there is no object of a certain data size and pointer layout type, there is no need to allocate the corresponding memory block.

[0056] Furthermore, after determining the first data size and the first pointer layout type of the object to be allocated, a target memory block for storing the object to be allocated can be determined from different types of memory blocks based on the first data size and the first pointer layout type, wherein the target memory block is used to store objects of the first data size and the first pointer layout type.

[0057] In a specific implementation, the first data size and the first pointer layout type can be passed as parameters to the memory allocation function malloc, and the memory allocation function malloc determines the target memory block for storing the object to be allocated. Specifically, if the first pointer layout type is a pointer layout type in which no pointer exists, the memory block for storing the object in which no pointer exists is directly determined as the target memory block; if the first pointer layout type is a pointer layout type in which a pointer exists, the corresponding memory block is selected as the target memory block.

[0058] S203: Allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

[0059] In this embodiment, in determining the target memory block, available memory space can be allocated from the target memory block for the object to be allocated. Optionally, the available memory space can be found through the memory allocation function malloc, and the address of the available memory space can be returned. Then, according to the address of the available memory space, the object to be allocated can be stored in the available memory space.

[0060] Through the above process, when it is necessary to obtain a pointer in any object, the pointer layout type of the object can be quickly determined according to the type of the memory block where the object is located, and then the pointer can be quickly obtained from the object according to the pointer layout type, without using an additional object header or global bitmap to store the pointer layout information of the object, thereby reducing additional memory overhead and the CPU overhead of encoding and decoding, improving memory allocation efficiency, and making subsequent garbage collection and other processes that require obtaining pointers in objects more efficient, which can bring significant performance improvements in scenarios with a large number of life cycle objects such as microservices.

[0061] The memory allocation method based on the runtime system provided in this embodiment determines the first data size and the first pointer layout type of the object to be allocated; wherein any pointer layout type is used to identify the layout mode of the pointer in the object; according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated is determined from pre-configured memory blocks of different types, wherein any type of memory block is used to store a type of object, and objects of the same type have the same data size and pointer layout type; available memory space is allocated for the object to be allocated in the target memory block, and the object to be allocated is stored in the available memory space. By configuring different types of memory blocks to store objects of different data sizes and different pointer layout types, there is no need to use additional object headers or global bitmaps to store the pointer layout information of the object, which reduces additional memory overhead, reduces the CPU overhead of encoding and decoding, improves memory allocation efficiency, and can improve the efficiency of subsequent garbage collection and other processes that require obtaining pointers in objects.

[0062] Based on any of the above embodiments, when pre-configuring different types of memory blocks, the following may be specifically included:

[0063] Determine all candidate data sizes and all candidate pointer layout types of objects of each candidate data size according to the set of objects to be allocated;

[0064] For any candidate pointer layout type of an object of any candidate data size, a memory block of one type is allocated for storing the object of any candidate pointer layout type of the candidate data size.

[0065] In this embodiment, a statistical analysis may be first performed on the set of objects to be allocated to determine all candidate data sizes and all candidate pointer layout types of objects of each candidate data size. In specific implementation, the set of objects to be allocated may be first classified according to data size, and then object subsets of different data sizes may be obtained, thus determining all candidate data sizes; then, each object subset of data size may be classified according to pointer layout type, and a grandchild set of objects of different pointer layout types of each object subset of data size may be obtained, thus determining all candidate pointer layout types of objects of each candidate data size; and then, for any candidate pointer layout type of objects of any candidate data size, a type of memory block (which may include one or more memory blocks) may be allocated for storing objects of that candidate data size and that candidate pointer layout type.

[0066] It should be noted that, although in the above example all possible pointer layout types can be determined through permutations and combinations for objects of the same data size, such as the four pointer layout types for 16B objects, through statistical analysis of the set of objects to be allocated, objects of a certain data size and a certain pointer layout type may not exist, in which case there is no need to create corresponding memory blocks, and only corresponding memory blocks need to be created for the candidate data sizes and candidate pointer layout types that have been counted. For example, through statistical analysis it is found that only two pointer layout types, [nopointer, nopointer] and [nopointer, pointer], exist in 16B objects, in which case only corresponding memory blocks need to be created for 16B[nopointer, nopointer] and 16B[nopointer, pointer] objects, and there is no need to create corresponding memory blocks for 16B[pointer, nopointer] and [pointer, pointer] objects.

[0067] In addition, optionally, when statistically analyzing the set of objects to be allocated, if it is found that the number of objects of a certain data size and a certain pointer layout type is relatively small, for example, less than a preset number threshold, it is also possible not to create a corresponding memory block for objects of this data size and this pointer layout type, but to use the existing technology to add an object header to record the pointer layout type information, or to record the pointer layout type information through a bitmap. Since the number of objects is relatively small, it will not increase too much additional memory overhead, nor will it increase too much CPU overhead for encoding and decoding.

[0068] Based on the above embodiment, when configuring different types of memory blocks, a mapping relationship between different memory block types and different pointer layout types may be created to facilitate subsequent rapid determination of the pointer layout type according to the memory block type.

[0069] As a simplification, different memory block types can be represented by memory block identifiers, that is, different types of memory blocks have different identifiers, and the same type of memory blocks have the same identifier, and then a mapping relationship between identifiers of different types of memory blocks and different pointer layout types can be created.

[0070] After any type of memory block is configured, a corresponding identifier may be configured in metadata of any type of memory block.

[0071] As an example, in this embodiment, the identifier of each memory block can be represented by a reference value plus an offset, wherein the same data size corresponds to the same reference value, and the offset corresponds to different pointer layout types of the same data size, that is, the identifier index of any memory block can be represented as:

[0072] index=baseindex+offset

[0073] Among them, baseindex indicates the base value, and offset indicates the offset.

[0074] For example, the reference value corresponding to 8B is 0, and the offsets of the memory blocks corresponding to the two different pointer layout types of 8B are 0 and 1 respectively, so the identification indexes of the memory blocks corresponding to the two different pointer layout types of 8B are 0 and 1 respectively; and the reference value corresponding to 16B is 2, and the offsets of the memory blocks corresponding to the four different pointer layout types of 16B are 0, 1, 2, and 3 respectively, so the identification indexes of the memory blocks corresponding to the four different pointer layout types of 16B are 2, 3, 4, and 5 respectively; and so on. Of course, the identifications of the memory blocks do not need to be continuous, that is, the reference values ​​do not need to be configured as in the above example, and can also be any other number, as long as there is no repeated identification.

[0075] On the basis of any of the above embodiments, after the storage of the object is realized based on the above runtime system-based memory allocation method, garbage collection can also be performed, that is, after the object to be allocated is stored in the available memory space in the above S203, as shown in FIG. Figure 4 As shown, it may also include:

[0076] S401, in response to a garbage collection instruction, traversing from a known root object;

[0077] S402, determining the second pointer layout type of the currently traversed object according to the type of the memory block where the currently traversed object is located, obtaining the pointer in the currently traversed object according to the second pointer layout type of the currently traversed object to continue traversing based on the pointer, and marking the traversed object as alive;

[0078] S403: Perform garbage collection on objects that are not marked as alive.

[0079] In this embodiment, the garbage collection process adopts a tracing garbage collection method, that is, starting from the root object, recursively traverses all referenced objects, and marks them as alive, and then objects that are not marked as alive can be recycled.

[0080] Specifically, the garbage collection instruction can be triggered by the user or automatically, and the traversal starts from the known root object. The pointer layout type of the root object can be determined according to the type of the memory block where the root object is located, and then the pointer of the object referenced by the root object is obtained from the root object according to the pointer layout type of the root object, and the root object is marked as alive, and the object referenced by the root object is continued to be traversed according to the pointer; for the object referenced by the root object, similarly, the pointer layout type of the object is determined according to the type of the memory block where the object is located, and then the pointer of the object referenced by the object is obtained from the root object according to the pointer layout type of the object, and the object is marked as alive, and the object referenced by the object is continued to be traversed according to the pointer; and so on. After the traversal is completed, the objects that are not marked as alive can be garbage collected.

[0081] It should be noted that marking the traversed object as alive can be done at any time, for example, it can be marked when the traversal of the current traversal object begins, or it can be marked when the pointer of the referenced object is obtained from the current traversal object and then the referenced object is read, etc. There is no limitation here.

[0082] Based on the above embodiment, when obtaining the pointer in the currently traversed object according to the second pointer layout type of the currently traversed object in S402 to continue traversing based on the pointer and marking the traversed object as alive, it may specifically include:

[0083] Obtaining a pointer to an object referenced by the current traversal object from the current traversal object according to the second pointer layout type;

[0084] If the pointer of the referenced object is obtained, the currently traversed object is marked as alive, and the referenced object is continued to be traversed according to the pointer of the referenced object; or

[0085] If the pointer of the referenced object is not obtained, the current traversal object is marked as alive, and the traversal process after the current traversal object is stopped.

[0086] In this embodiment, after determining the second pointer layout type of the current traversal object according to the type of the memory block where the current traversal object is located, the second pointer layout type may be a pointer layout type including a pointer or a pointer layout type not including a pointer. Therefore, when obtaining the pointer of the referenced object from the current traversal object according to the second pointer layout type, if the second pointer layout type is a pointer layout type including a pointer, the pointer of the referenced object can be obtained, and the current traversal object is marked as alive, and the referenced object is continued to be traversed according to the pointer of the referenced object; if the second pointer layout type is a pointer layout type not including a pointer, the pointer of the referenced object is not obtained, the current traversal object may be marked as alive, and the traversal process after the current traversal object is stopped, that is, the current traversal object does not reference other objects, and recursion cannot be continued from the current traversal object. However, if there are other traversal processes, for example, a certain object includes multiple pointers, then the traversal process will branch, and the branch where the current traversal object is located will stop traversing, which will not affect the continued traversal of other branches.

[0087] In addition, marking the current traversal object as alive is not necessarily performed after obtaining the pointer of the referenced object. The current traversal object can be marked as alive first, and then the pointer of the referenced object can be obtained, or the current traversal object can be marked as alive at other times.

[0088] Optionally, after creating a mapping relationship between identifiers of different types of memory blocks and different pointer layout types, and configuring corresponding identifiers in the metadata of each type of memory block, when it is necessary to determine the second pointer layout type of the currently traversed object based on the type of the memory block where the currently traversed object is located, the identifier of the memory block where the currently traversed object is located can be obtained from the metadata of the memory block where the currently traversed object is located; and then the second pointer layout type of the currently traversed object can be determined based on the identifier of the memory block where the currently traversed object is located and the mapping relationship.

[0089] Alternatively, based on the creation of a mapping relationship between different memory block types and different pointer layout types, the type of the memory block where the current traversal object is located can be first determined, and then the second pointer layout type of the current traversal object can be determined based on the type of the memory block where the current traversal object is located and the mapping relationship.

[0090] Corresponding to the memory allocation method based on the runtime system in the above embodiment, Figure 5 A structural block diagram of a memory allocation device based on a runtime system provided by an embodiment of the present disclosure. For ease of explanation, only the parts related to the embodiment of the present disclosure are shown. Figure 5The memory allocation device 500 based on the runtime system includes: a determination unit 501, a memory block selection unit 502, and a memory allocation unit 503.

[0091] The determining unit 501 is used to determine a first data size and a first pointer layout type of the object to be allocated; any pointer layout type is used to identify the layout mode of the pointer in the object;

[0092] A memory block selection unit 502 is used to determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type;

[0093] The memory allocation unit 503 is used to allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

[0094] In one or more embodiments of the present disclosure, the device further includes a garbage collection unit 504, which is used to:

[0095] In response to a garbage collection instruction, traversal starts from a known root object;

[0096] Determine the second pointer layout type of the current traversal object according to the type of the memory block where the current traversal object is located, obtain the pointer in the current traversal object according to the second pointer layout type of the current traversal object to continue traversing based on the pointer, and mark the traversed object as alive;

[0097] Garbage collection is performed on objects that are not marked as alive.

[0098] In one or more embodiments of the present disclosure, when the garbage collection unit 504 obtains the pointer in the currently traversed object according to the second pointer layout type of the currently traversed object to continue traversing based on the pointer, and marks the traversed object as alive, it is configured to:

[0099] Obtaining a pointer to an object referenced by the current traversal object from the current traversal object according to the second pointer layout type;

[0100] If the pointer of the referenced object is obtained, the currently traversed object is marked as alive, and the referenced object is continued to be traversed according to the pointer of the referenced object; or

[0101] If the pointer of the referenced object is not obtained, the current traversal object is marked as alive, and the traversal process after the current traversal object is stopped.

[0102] In one or more embodiments of the present disclosure, when determining the second pointer layout type of the currently traversed object according to the type of the memory block where the currently traversed object is located, the garbage collection unit 504 is used to:

[0103] Obtaining the identifier of the memory block where the currently traversed object is located from the metadata of the memory block where the currently traversed object is located;

[0104] The second pointer layout type of the currently traversed object is determined according to the identifier of the memory block where the currently traversed object is located and a preset mapping relationship, wherein the preset mapping relationship is a mapping relationship between identifiers of different types of memory blocks and different pointer layout types.

[0105] In one or more embodiments of the present disclosure, the memory block selection unit 502 is further configured to:

[0106] Determine all candidate data sizes and all candidate pointer layout types of objects of each candidate data size according to the set of objects to be allocated;

[0107] For any candidate pointer layout type of an object of any candidate data size, a memory block of one type is allocated for storing the object of any candidate pointer layout type of the candidate data size.

[0108] In one or more embodiments of the present disclosure, the memory block selection unit 502 is further configured to:

[0109] Creating a mapping relationship between identifiers of different types of memory blocks and different pointer layout types as the preset mapping relationship;

[0110] After any type of memory block is configured, a corresponding identifier is configured in the metadata of any type of memory block.

[0111] In one or more embodiments of the present disclosure, in objects of different pointer layout types with the same data size, whether the fields included therein are pointers is different.

[0112] The device provided in this embodiment can be used to execute the technical solution of the above method embodiment. Its implementation principle and technical effect are similar, and this embodiment will not be repeated here.

[0113] refer to Figure 6, which shows a schematic diagram of the structure of an electronic device 600 suitable for implementing the embodiment of the present disclosure, and the electronic device 600 may be a terminal device or a server. The terminal device may include but is not limited to mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, personal digital assistants (PDAs), tablet computers, portable multimedia players (PMPs), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 6 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.

[0114] like Figure 6 As shown, the electronic device 600 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 to a random access memory (RAM) 603. Various programs and data required for the operation of the electronic device 600 are also stored in the RAM 603. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0115] Typically, the following devices may be connected to the I / O interface 605: input devices 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; output devices 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; storage devices 608 including, for example, a magnetic tape, a hard disk, etc.; and communication devices 609. The communication device 609 may allow the electronic device 600 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 The electronic device 600 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.

[0116] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.

[0117] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.

[0118] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.

[0119] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device executes the method shown in the above embodiment.

[0120] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as an independent software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer via any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0121] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0122] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware. The name of a unit does not limit the unit itself in some cases. For example, the first acquisition unit may also be described as a "unit for acquiring at least two Internet Protocol addresses".

[0123] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.

[0124] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0125] In a first aspect, according to one or more embodiments of the present disclosure, a runtime system-based memory allocation method is provided, comprising:

[0126] Determine a first data size and a first pointer layout type of the object to be allocated; wherein any pointer layout type is used to identify the layout mode of the pointer in the object;

[0127] Determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type;

[0128] Allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

[0129] According to one or more embodiments of the present disclosure, after storing the object to be allocated in the available memory space, the method further includes:

[0130] In response to a garbage collection instruction, traversal starts from a known root object;

[0131] Determine the second pointer layout type of the current traversal object according to the type of the memory block where the current traversal object is located, obtain the pointer in the current traversal object according to the second pointer layout type of the current traversal object to continue traversing based on the pointer, and mark the traversed object as alive;

[0132] Garbage collection is performed on objects that are not marked as alive.

[0133] According to one or more embodiments of the present disclosure, the step of obtaining a pointer in the currently traversed object according to the second pointer layout type of the currently traversed object to continue traversing based on the pointer, and marking the traversed object as alive includes:

[0134] Obtaining a pointer to an object referenced by the current traversal object from the current traversal object according to the second pointer layout type;

[0135] If the pointer of the referenced object is obtained, the currently traversed object is marked as alive, and the referenced object is continued to be traversed according to the pointer of the referenced object; or

[0136] If the pointer of the referenced object is not obtained, the current traversal object is marked as alive, and the traversal process after the current traversal object is stopped.

[0137] According to one or more embodiments of the present disclosure, determining the second pointer layout type of the currently traversed object according to the type of the memory block where the currently traversed object is located includes:

[0138] Obtaining the identifier of the memory block where the currently traversed object is located from the metadata of the memory block where the currently traversed object is located;

[0139] The second pointer layout type of the currently traversed object is determined according to the identifier of the memory block where the currently traversed object is located and a preset mapping relationship, wherein the preset mapping relationship is a mapping relationship between identifiers of different types of memory blocks and different pointer layout types.

[0140] According to one or more embodiments of the present disclosure, the method further includes:

[0141] Determine all candidate data sizes and all candidate pointer layout types of objects of each candidate data size according to the set of objects to be allocated;

[0142] For any candidate pointer layout type of an object of any candidate data size, a memory block of one type is allocated for storing the object of any candidate pointer layout type of the candidate data size.

[0143] According to one or more embodiments of the present disclosure, the method includes:

[0144] Creating a mapping relationship between identifiers of different types of memory blocks and different pointer layout types as the preset mapping relationship;

[0145] After any type of memory block is configured, a corresponding identifier is configured in the metadata of any type of memory block.

[0146] According to one or more embodiments of the present disclosure, in objects of different pointer layout types with the same data size, whether each field included therein is a pointer is different.

[0147] In a second aspect, according to one or more embodiments of the present disclosure, a memory allocation device based on a runtime system is provided, comprising:

[0148] A determination unit, used to determine a first data size and a first pointer layout type of the object to be allocated; wherein any pointer layout type is used to identify a layout mode of the pointer in the object;

[0149] a memory block selection unit, configured to determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type;

[0150] A memory allocation unit is used to allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

[0151] According to one or more embodiments of the present disclosure, the device further includes a garbage collection unit, configured to:

[0152] In response to a garbage collection instruction, traversal starts from a known root object;

[0153] Determine the second pointer layout type of the current traversal object according to the type of the memory block where the current traversal object is located, obtain the pointer in the current traversal object according to the second pointer layout type of the current traversal object to continue traversing based on the pointer, and mark the traversed object as alive;

[0154] Garbage collection is performed on objects that are not marked as alive.

[0155] According to one or more embodiments of the present disclosure, when the garbage collection unit obtains the pointer in the currently traversed object according to the second pointer layout type of the currently traversed object to continue traversing based on the pointer, and marks the traversed object as alive, it is configured to:

[0156] Obtaining a pointer to an object referenced by the current traversal object from the current traversal object according to the second pointer layout type;

[0157] If the pointer of the referenced object is obtained, the currently traversed object is marked as alive, and the referenced object is continued to be traversed according to the pointer of the referenced object; or

[0158] If the pointer of the referenced object is not obtained, the current traversal object is marked as alive, and the traversal process after the current traversal object is stopped.

[0159] According to one or more embodiments of the present disclosure, when determining the second pointer layout type of the currently traversed object according to the type of the memory block where the currently traversed object is located, the garbage collection unit is used to:

[0160] Obtaining the identifier of the memory block where the currently traversed object is located from the metadata of the memory block where the currently traversed object is located;

[0161] The second pointer layout type of the currently traversed object is determined according to the identifier of the memory block where the currently traversed object is located and a preset mapping relationship, wherein the preset mapping relationship is a mapping relationship between identifiers of different types of memory blocks and different pointer layout types.

[0162] According to one or more embodiments of the present disclosure, the memory block selection unit is further configured to:

[0163] Determine all candidate data sizes and all candidate pointer layout types of objects of each candidate data size according to the set of objects to be allocated;

[0164] For any candidate pointer layout type of an object of any candidate data size, a memory block of one type is allocated for storing the object of any candidate pointer layout type of the candidate data size.

[0165] According to one or more embodiments of the present disclosure, the memory block selection unit is further configured to:

[0166] Creating a mapping relationship between identifiers of different types of memory blocks and different pointer layout types as the preset mapping relationship;

[0167] After any type of memory block is configured, a corresponding identifier is configured in the metadata of any type of memory block.

[0168] According to one or more embodiments of the present disclosure, in objects of different pointer layout types with the same data size, whether each field included therein is a pointer is different.

[0169] In a third aspect, according to one or more embodiments of the present disclosure, there is provided an electronic device, comprising: at least one processor and a memory;

[0170] The memory stores computer-executable instructions;

[0171] The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor executes the runtime system-based memory allocation method as described in the first aspect and various possible designs of the first aspect above.

[0172] In a fourth aspect, according to one or more embodiments of the present disclosure, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer execution instructions. When a processor executes the computer execution instructions, the memory allocation method based on the runtime system as described in the first aspect and various possible designs of the first aspect is implemented.

[0173] In a fifth aspect, according to one or more embodiments of the present disclosure, a computer program product is provided, comprising computer execution instructions. When a processor executes the computer execution instructions, the memory allocation method based on the runtime system as described in the first aspect and various possible designs of the first aspect is implemented.

[0174] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.

[0175] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.

[0176] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.

Claims

1. A memory allocation method based on a runtime system, characterized in that: include: Determine a first data size and a first pointer layout type of the object to be allocated; Any pointer layout type is used to identify how pointers are laid out in an object; Determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type; Allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

2. The method according to claim 1, characterized in that: After storing the object to be allocated in the available memory space, the method further includes: In response to a garbage collection instruction, traversal starts from a known root object; Determine the second pointer layout type of the current traversal object according to the type of the memory block where the current traversal object is located, obtain the pointer in the current traversal object according to the second pointer layout type of the current traversal object to continue traversing based on the pointer, and mark the traversed object as alive; Garbage collection is performed on objects that are not marked as alive.

3. The method according to claim 2, characterized in that The step of obtaining a pointer in the currently traversed object according to the second pointer layout type of the currently traversed object to continue traversing based on the pointer, and marking the traversed object as being in a live state, includes: Obtaining a pointer to an object referenced by the current traversal object from the current traversal object according to the second pointer layout type; If the pointer of the referenced object is obtained, the currently traversed object is marked as alive, and the referenced object is continued to be traversed according to the pointer of the referenced object; or If the pointer of the referenced object is not obtained, the current traversal object is marked as alive, and the traversal process after the current traversal object is stopped.

4. The method according to claim 2, characterized in that: The second pointer layout type of the currently traversed object is determined according to the type of the memory block where the currently traversed object is located, include, Obtaining an identifier of the memory block where the currently traversed object is located from metadata of the memory block where the currently traversed object is located; The second pointer layout type of the currently traversed object is determined according to the identifier of the memory block where the currently traversed object is located and a preset mapping relationship, wherein the preset mapping relationship is a mapping relationship between identifiers of different types of memory blocks and different pointer layout types.

5. The method according to claim 1, characterized in that The method further comprises: Determine all candidate data sizes and all candidate pointer layout types of objects of each candidate data size according to the set of objects to be allocated; For any candidate pointer layout type of an object of any candidate data size, a memory block of one type is allocated for storing the object of any candidate pointer layout type of the candidate data size.

6. The method according to claim 4, characterized in that The method comprises: Creating a mapping relationship between identifiers of different types of memory blocks and different pointer layout types as the preset mapping relationship; After any type of memory block is configured, a corresponding identifier is configured in the metadata of any type of memory block.

7. The method according to any one of claims 1 to 6, characterized in that: In objects of different pointer layout types with the same data size, whether the fields included therein are pointers may differ.

8. A memory allocation device based on a runtime system, characterized in that: include: A determination unit, used to determine a first data size and a first pointer layout type of the object to be allocated; Any pointer layout type is used to identify how pointers are laid out in an object; a memory block selection unit, configured to determine, according to the first data size and the first pointer layout type, a target memory block for storing the object to be allocated from pre-configured memory blocks of different types; wherein any type of memory block is used to store one type of object, and objects of the same type have the same data size and pointer layout type; A memory allocation unit is used to allocate available memory space for the object to be allocated in the target memory block, and store the object to be allocated in the available memory space.

9. An electronic device, characterized in that: include: at least one processor and memory; The memory stores computer-executable instructions; The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the processor executes the computer-executable instructions, the method according to any one of claims 1 to 7 is implemented.

11. A computer program product, characterized in that The method comprises computer-executable instructions, and when a processor executes the computer-executable instructions, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Memory allocation method and device, computer equipment and storage medium

    CN114490076A

  • Memory management method and device, electronic equipment and computer readable storage medium

    CN118113480A

  • Systems And Methods Of Memory And Access Management

    US20150227414A1