Object memory layout method for separating object control information from data information
By separating the control information and data information of the object during the runtime of the hosting language and distributing the memory separately, the memory management efficiency efficiency and delay of the access of the object mixed memory layout in the existing technology have been solved. performance.
Patent Information
- Application Number
- CN202411312594.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-20
- Publication Date
- 2025-01-28
AI Technical Summary
Mixed memory layouts of objects in the runtime of existing managed languages result in inefficient memory management, increased access latency, and limited efficiency of garbage collection and memory swap mechanisms.
Separate the control information and data information of the object, allocate memory in the control of memory space and data memory space, prioritize or replace the data memory space, and try to keep the control memory space as much as possible.
It improves the efficiency of object access and garbage recycling, reduces the overhead of memory exchange and compression, and improves the overall performance and response speed of the system.
Smart Images

Figure CN119376913A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of computer memory management, and in particular relates to an object memory layout optimization method for a managed language virtual machine. Background Art
[0002] The managed language runtime provides a managed environment to manage memory management throughout the entire life cycle of objects from allocation to recycling. The memory allocator allocates space for objects and performs necessary initialization. The garbage collector automatically detects the survival of objects regularly, releases the memory space occupied by dead objects, and moves surviving objects to new spaces to reduce memory fragmentation, freeing up old spaces for objects allocated by the memory allocator. When the system is short of memory, the memory swap mechanism and memory compression mechanism in the system will select memory pages according to certain algorithms (such as the least recently used LRU or first-in-first-out FIFO, etc.) to swap out or compress them to relieve memory pressure.
[0003] When the memory allocator allocates memory space for an object, it allocates the object's metadata, reference type, and non-reference type member variables as a whole in a continuous memory space from parent class to child class. System services (such as garbage collection) or applications determine the location of reference fields or data fields based on metadata information and complete access to the object pointed to by the reference field or its own data field.
[0004] When the system memory is insufficient, the memory swap mechanism and memory compression mechanism in the system will be triggered. Both the memory swap mechanism and the memory compression mechanism release memory in units of memory pages. The memory swap mechanism and the memory compression mechanism use algorithms such as Least Recently Used (LRU) or First In First Out (FIFO) to select the target page. The memory swap mechanism releases the memory occupied by the page after swapping out the memory page to the hard disk, and the memory compression mechanism releases the memory occupied by the page after compressing the target page to the hard disk. When the data in the target page is accessed, the memory swap mechanism swaps the memory page where the data is located from the hard disk into the memory.
[0005] In the managed language runtime, the control information and data information of an object are used differently, and the existing mixed memory layout of objects reduces the efficiency of memory management. The memory allocator allocates the object as a whole. If swapping out or compression occurs, it is swapped out or compressed as a whole. However, the access methods of an object's control information and data information are completely different. First, the control information will be accessed more frequently when used by the application, such as when searching for the fields of a child object. Secondly, some system services will also access the control field separately. For example, after the garbage collector is triggered, the control field of the object needs to be traversed to determine the reachability of the object in memory, so as to complete garbage collection for unreachable objects. However, the existing memory management mechanism mixes the control information and data information of the object. When swapping out or compressing, the object's control information and data information are swapped out or compressed as a whole. When accessing the control information, the object's control information and data information need to be swapped in or decompressed as a whole. Mixing the object information increases the delay in the process of applications or system services accessing the object's control field.
[0006] In addition, the existing mixed memory layout of objects also affects the efficiency of application memory access. In the managed language runtime, object access forms an access chain according to the member variables of the reference type in the object. In the access chain, if any object is not in the memory, it will trigger the swapping in or decompression of the memory. Mixed storage of object information results in the object passing through the access chain is not the final access target of the application, and the access process only needs to access the control field of the object, but if the object is compressed or swapped out, it will still be swapped in and decompressed as a whole. Mixed storage of object information increases the latency of object access.
[0007] There are currently two types of inventions for optimizing memory management in managed language runtimes. One type is to optimize the garbage collection mechanism. Tian Lin invented a method for clearing objects according to clearing conditions to reduce the triggering frequency of garbage collection (CN112433809A), and Gao Jianwu invented a method for detecting whether the display interface of the terminal is in a sliding stop state and executing a memory garbage collection process when in the stop state to reduce the performance impact of garbage collection on program operation (CN106201358A). The other type is to optimize the exchange mechanism. Wang Siyu invented a method for expanding the scope of process detection by sensing the memory recovery situation to improve the efficiency of memory recovery (CN117130767A), and Chang Yan et al. invented a memory management method for configuring three asynchronous exchange threads to manage the memory of different types of pages respectively (CN116954876A).
[0008] Glossary
[0009] Managed language: refers to a programming language that entrusts the code to a hosting platform for execution and management in a specific runtime environment, usually providing memory management, security and other runtime functions.
[0010] Managed language runtime: The execution environment for managed languages, which provides the functions and services required to execute and manage managed language code on a specific platform, such as memory management, security, thread management, etc.
[0011] Memory management: used to allocate and release memory resources to ensure that the program can use memory efficiently and prevent problems such as memory leaks and memory overflows.
[0012] Object: Object is the basic unit of memory management in managed languages. In managed languages, the general representation of an object consists of metadata and member variables. Metadata includes the type of the object, multithreaded locks, etc. In complex objects, member variables are divided into reference and non-reference member variables. References are sometimes called pointers, which point to other objects. There will be many object instances in the memory space of the managed language runtime.
[0013] Object control information: object metadata, member variables of reference type, and references to data blocks.
[0014] Object data information: non-reference type member variables of the object.
[0015] Garbage Collector: An automatic memory management mechanism that detects and releases memory resources that are no longer in use to reduce the programmer's burden of memory management and prevent memory leaks.
[0016] Memory swapping: Swap out data in memory to the hard disk to free up memory resources. When data is accessed, it is swapped from the hard disk to the memory.
[0017] Memory compression: compresses the data in memory to reduce the memory space occupied. When the data is accessed, it is decompressed. Summary of the invention
[0018] The object of the present invention is to provide an object memory layout method for separating object control information and data information to improve the efficiency of object access or managed language runtime system services.
[0019] In the traditional object memory layout, when the memory allocator allocates memory for an object, it allocates a continuous memory block to store the control information and data information of the object. Figure 1 As shown in the figure, the order of the object's metadata and member variables in the memory block depends on the implementation of the managed language runtime.
[0020] Different from the traditional method, the present invention modifies the memory layout of objects in the managed language runtime. Specifically, the object information is divided into control information and data information, and the two parts of information are separated when the memory is allocated; the allocatable space in the memory is divided into control memory space and data memory space; when an object is allocated memory, space is allocated for control information in the control memory space, and space is allocated for data information in the data memory space. When the memory is insufficient, the data memory space is compressed first. The original conflict problem between the garbage collector and the exchange mechanism is solved from the perspective of memory organization.
[0021] The present invention divides object information into two categories: control information and data information. Figure 2 As shown. Among them, the control information of the object includes the metadata and reference type member variables of the object; the data information only contains non-reference type member variables. In order to ensure that the data information is correctly found from the control information, the control information part adds a data block reference pointing to the data information; in the new memory allocation scheme, the control information and data information are allocated to different memory areas to improve memory access efficiency and the convenience of object management. This scheme aims to solve the problem caused by the mixing of control information and data information in the existing memory allocation scheme. As shown Figure 3 As shown, the present invention divides the memory allocation space into control memory space and data memory space. When allocating memory for an object, data information is allocated to the data memory space, control information is allocated to the control memory space, and corresponding data block pointers are set.
[0022] The object memory layout method for separating object control information and data information proposed in the present invention includes object creation, object access, garbage collection, memory exchange and compression, etc. The specific steps are as follows:
[0023] (1) Object creation: When a new object needs to be created, first dynamically allocate the corresponding memory space in the control memory space and data memory space according to the size of the object's control information and data information; then fill the object's metadata and data block pointer information in the control information; then, initialize the member variables in the control information and data information according to the requirements of the new object;
[0024] (2) Object access: When an object needs to be accessed, the control information of the object is first quickly found by controlling the reference type member variables in the memory space. If the object is being accessed, the control information is accessed and operated directly. If the object is being accessed, the data block is located according to the data block information in the control information, and then the data is accessed and operated.
[0025] (3) Garbage collection: When an object is not referenced by other objects, it means that its life cycle ends and it can be recycled. The process recycles the data information first, and then the control information. For objects that are not recycled, the memory space storing the control information and the memory space storing the data are sorted separately to reduce fragmentation between objects (that is, the memory area that is not allocated and has discontinuous addresses due to frequent allocation and release of memory). When moving objects in the data memory space, update the data block pointer (pointing to the address of the data block memory) in the control information of the object;
[0026] (4) Memory swapping: When swapping memory, priority is given to memory pages in the data memory space, and control information in the control memory space is not swapped from the memory to the swap partition of the hard disk as much as possible. When accessing data information that has been swapped out, the memory page where the data information is located is swapped in first. When the data information that has been swapped out needs to be recycled along with the object, the corresponding space is directly released in the swap area.
[0027] (5) Memory compression: When compressing memory, priority is given to compressing memory pages in the data memory space, and control information in the control memory space is not compressed as much as possible. When accessing compressed data information, the memory page where the data information is located is decompressed and put into the memory. When the compressed data information needs to be recycled along with the object, the corresponding memory is directly released in the compressed space.
[0028] The present invention introduces a new optimization strategy, which is applicable to systems with memory swap mechanisms or memory compression mechanisms. In the present invention, the garbage collector only focuses on the control memory space and performs reachability analysis scanning only in the control memory space, which reduces the occupation of system resources and can more quickly identify and recycle garbage objects and quickly release memory resources.
[0029] In addition, when an application accesses an object, access to a reference type member variable is also limited to the control memory space. This means that when an application accesses a reference type member variable of an object, it only needs to operate in the control memory space without involving the memory area that has been swapped or compressed, which improves the performance and efficiency of the application.
[0030] Through the optimization strategy designed by the present invention, the efficiency of the garbage collector and the application access object is improved. The system can manage memory resources more efficiently, reduce the overhead of garbage collection, and provide faster and more responsive application execution.
[0031] The method of the present invention can be used in the operation of managed languages, such as Java Virtual Machine (JVM), Python Virtual Machine (PVM), etc. When only control information is needed to be accessed, memory access is performed only in the control memory space. When the memory needs to be swapped out or compressed, the data memory space is swapped out and compressed first, and the control information is retained in the memory as much as possible, so as to realize the separate management of control information and data information and improve the efficiency of garbage collection and object access. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 This is a schematic diagram of the original object memory layout.
[0033] Figure 2 It is a schematic diagram of object information separation of the present invention.
[0034] Figure 3 Schematic diagram of the object memory layout of the present invention. DETAILED DESCRIPTION
[0035] The present invention is a memory layout method designed for a managed language platform, which can be applied in managed language runtimes, including but not limited to Android, openJDK's Java virtual machine and Python virtual machine. Next, the specific implementation of the present invention will be described in detail using the Java language managed platform as an example.
[0036] According to the present invention, the memory layout of the memory management part of the Java language runtime is modified. First, the original memory space is divided into a control memory space and a data memory space. Since the memory layout is modified in the present invention, the memory management logic of the original solution needs to be adjusted to a certain extent, and the main modification is reflected in the modification of the logic of object creation, object access, garbage collection, and memory exchange and compression stages.
[0037] For the object creation phase, the original logic is to allocate a memory area to store the control information and data information of the object. Because of the new memory layout designed by the present invention, the control information and data information of the object need to be stored separately. Therefore, when implementing, the size of the control information and data information of the object must be determined first, and then the corresponding size of memory space must be applied for in the control memory space and the data memory space respectively. Finally, the metadata and data information memory addresses of the object are filled in the control information area, and the member variables of the control information and data information are initialized according to the requirements of the newly created object. The address of the control information is returned after the object is created.
[0038] For the object access stage, because we first obtain the control information of the object, we need to first determine the type of the member variable of the object to be accessed. If the type is a reference type, then directly access and operate. If the type is a basic data type, then you need to first find the corresponding data block according to the address referenced by the data block, and then access it.
[0039] For the garbage collection stage, there are mainly two cases of logical modification involved. The first case is when the object needs to be recycled. The memory of the data information needs to be recycled first, and then the memory of the control information needs to be recycled. The second case is that the object does not need to be recycled, but because the control memory space and the data memory space are frequently allocated and released, resulting in insufficient space to allocate to other objects, the control memory space and the data memory space need to be spatially organized (the objects are moved to the head of the memory space and placed continuously). At the same time, because the memory address of the data block changes due to data movement, the data block reference address of the object in the control information needs to be modified.
[0040] For the memory swap phase, the original logic is to swap the control information and data information of the infrequently used memory to the swap partition of the hard disk based on whether the data is the most frequently accessed recently. According to the observation of the present invention, the control information is more easily used than the data information, so the logic modification that needs to be made is: when memory swap is required, the memory of the data information is swapped out to the hard disk first, and the control information is retained. The advantage of this is that it reduces the swapping out of the memory and re-swapping in, and improves the efficiency of memory use.
[0041] For the memory compression stage, the original logic is the same as the original swap-out logic in the previous step, and the infrequently used memory pages are compressed. During the implementation of the present invention, the main logic modifications are: the memory pages in the data memory space are compressed first, and the control information in the control memory space is not compressed as much as possible; when accessing the compressed data information, the memory page where the data information is located is decompressed and put into the memory; when the compressed data information needs to be recycled with the object, the corresponding memory is directly released in the compression space.
[0042] By using the memory layout method of the present invention, the efficiency of garbage collector and application access to objects is improved. Because the garbage collector only needs to access the control information of the object to determine whether the object is alive, the access to the object data information is reduced. At the same time, because the data information that is not frequently accessed can be swapped out, the system as a whole occupies less memory resources. Therefore, using the design of the present invention, the system can manage memory resources more efficiently, reduce the overhead of garbage collection, and provide faster and more responsive application execution.
Claims
1. A method for object memory layout with separation of object control information and data information, characterized in that: Object information is divided into two categories: control information and data information; among which, the control information of the object includes the metadata and reference type member variables of the object; the data information is only the non-reference type member variables; the control memory space and the data memory space are divided in the memory allocation space; when allocating memory for the object, the data information is allocated to the data memory space, the control information is allocated to the control memory space, and the corresponding data block pointer is set; the specific steps are as follows: (1) Object creation: When a new object needs to be created, first dynamically allocate the corresponding memory space in the control memory space and data memory space according to the size of the object's control information and data information; then fill the object's metadata and data block pointer information in the control information; then, initialize the member variables in the control information and data information according to the requirements of the new object; (2) Object access: When you need to access an object, first find the control information of the object by controlling the reference type member variables in the memory space; if the object is being accessed, then directly access and operate; if the object is being accessed, then locate the data block according to the data block information in the control information, and then access and operate the data; (3) Garbage collection: When an object is not referenced by other objects, it means that its life cycle has ended and it can be recycled. The process is as follows: first recycle the data information, then recycle the control information. For objects that are not recycled, the memory space storing the control information and the memory space storing the data are sorted separately to reduce fragmentation between objects. When moving objects in the data memory space, the data block pointer in the object's control information is updated. (4) Memory swap: When swapping memory, give priority to memory pages in the data memory space, and try not to swap control information in the control memory space from the memory to the swap partition of the hard disk; when accessing the swapped-out data information, first swap in the memory page where the data information is located; when the swapped-out data information needs to be recycled along with the object, directly release the corresponding space in the swap area; (5) Memory compression: When compressing memory, priority is given to compressing memory pages in the data memory space, and control information in the control memory space is not compressed as much as possible; when accessing compressed data information, the memory page where the data information is located is decompressed and put into the memory; when the compressed data information needs to be recycled along with the object, the corresponding memory is directly released in the compressed space.
Citation Information
Patent Citations
Recovery method and device for memory garbage
CN106201358A
JVM memory management method, device and equipment and readable storage medium
CN112433809A
Memory management method and electronic equipment
CN116954876A
Memory recovery method, electronic equipment and storage medium
CN117130767A