A memory management method, device, apparatus and storage medium
Patent Information
- Application Number
- CN202211650812.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-21
- Publication Date
- 2026-10-30
- Estimated Expiration
- 2042-12-21
AI Technical Summary
[0005]而常用的内存垃圾回收(Garbage Collection,GC)策略都是设计成为分代GC,分代GC可以理解为是“大部分对象都在年轻代中被清理”的情形,即无法长时间存放活着的对象,而软件缓存一般都是需要存放长时间活着的对象的场景,导致软件缓存策略与GC的策略存在矛盾,因此,如果当缓存很大,则对GC就会造成更大的压力,从而导致应用程序性能下降或工作不稳定
[0053]通过将目标堆内存划分为年轻代区域、老年代区域以及包括子缓存区域的独立缓存区域,如果当待分配对象对应的存储空间值大于独立缓存区域的第一剩余空间值时,可以触发年轻代垃圾回收机制,并在回收后获取独立缓存区域的第二剩余空间值,进而,当第二剩余空间值小于第一空间阈值时,可以对独立缓存区域中进行扫描,来确定子缓存区域活对象小于存活阈值的第一子缓存区域,以及子缓存区域活对象大于存活阈值的第二子缓存区域,然后,将第一子缓存区域中的子缓存区域活对象,拷贝到第二子缓存区域,并清理第一子缓存区域,来将待分配对象分配至清理后的第一子缓存区域进行缓存。通过上述方式,能够通过在堆内存中增设独立缓存区域,来将缓存数据,特别是很大的缓存数据统一分配到缓存区域中统一管理,并在回收的时候根据不同的回收策略对于独立缓存区域的数据进行回收,其中,对于年轻代等普通的回收不回收独立缓存区域里的对象,只对独立缓存区域进行扫描,当独立缓存区域中非存活对象超过一定限制时,自动的进行独立缓存区域的回收,通过发现子缓存区域的子缓存区域活对象数目,会针对于子缓存区域活对象少的第一子缓存区域进行回收,来增加独立缓存区域的空间容量,来尽可能地存放缓存数据,且在回收的过程中能够减少缓存对象的拷贝,减少回收的暂停时间,从而能够提高应用的吞吐率和响应时间,使得应用更加稳定高效的工作。
Smart Images

Figure CN118227514B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of memory processing technology, and in particular to a memory management method, apparatus, device, and storage medium. Background Technology
[0002] As the program runs, the amount of memory occupied by instance objects, variables, and other information in memory increases. If garbage collection is not performed in time, it will inevitably lead to a decrease in program performance and may even cause some unnecessary system anomalies due to insufficient available memory.
[0003] Software caching is a software-implemented cache data structure used to store frequently accessed hot data. The data source is usually read from the network or files, and putting this data into memory can speed up data access.
[0004] Common software caching strategies typically involve evicting the least recently used cached data. Under this strategy, frequently used and popular cached data usually survives for a relatively long time without being evicted.
[0005] Commonly used garbage collection (GC) strategies are designed as generational GC. Generational GC can be understood as a situation where "most objects are cleaned up in the young generation," meaning that live objects cannot be stored for a long time. However, software caching generally needs to store long-lived objects, which leads to a conflict between software caching strategies and GC strategies. Therefore, if the cache is large, it will put more pressure on GC, resulting in decreased application performance or instability. Summary of the Invention
[0006] This application provides a memory management method, apparatus, device, and storage medium. It manages cached data by uniformly allocating it to a cache region and reclaiming data from independent cache regions according to different reclamation strategies. When the number of non-live objects in an independent cache region exceeds a certain limit, the independent cache region is automatically reclaimed. By detecting the number of live objects in a sub-cache region, the first sub-cache region with fewer live objects is reclaimed to increase the space capacity of the independent cache region, maximizing the storage of cached data. Furthermore, the reclamation process reduces the copying of cached objects and the pause time during reclamation, thereby improving application throughput and response time, and making the application work more stably and efficiently.
[0007] This application provides a memory management method, including:
[0008] The target heap memory is divided into a young generation region, an old generation region, and an independent cache region, where the independent cache region includes sub-cache regions.
[0009] If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, the young generation garbage collection mechanism is triggered, and the second remaining space value of the independent cache area is obtained after collection, wherein the second remaining space value is greater than or equal to the first remaining space value.
[0010] If the second remaining space value is less than the first space threshold, then the independent cache regions are scanned to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold.
[0011] Copy the live objects in the sub-cache area of the first sub-cache area to the second sub-cache area, and clean up the first sub-cache area;
[0012] The objects to be allocated are assigned to the first sub-cache area after cleanup for caching.
[0013] Another aspect of this application provides a memory management method apparatus, comprising:
[0014] The processing unit is used to divide the target heap memory into a young generation region, an old generation region, and an independent cache region, wherein the independent cache region includes a sub-cache region.
[0015] The acquisition unit is used to trigger the young generation garbage collection mechanism if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, and to acquire the second remaining space value of the independent cache area after collection, wherein the second remaining space value is greater than or equal to the first remaining space value.
[0016] The determining unit is used to scan the independent cache region if the second remaining space value is less than the first space threshold, and to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold.
[0017] The processing unit is also used to copy the sub-cache area or objects in the first sub-cache area to the second sub-cache area, and to clean up the first sub-cache area;
[0018] The processing unit is also used to allocate the objects to be assigned to the first sub-cache area after cleanup for caching.
[0019] In one possible design, in another implementation of the embodiments of this application,
[0020] The processing unit is also configured to compare the used space value of the old generation region with the second space threshold if the second remaining space value is greater than the first space threshold, and obtain the comparison result.
[0021] The determining unit is also used to scan the independent cache region if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, and to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold.
[0022] The processing unit is also used to copy the sub-cache area or objects in the first sub-cache area to the second sub-cache area, and to clean up the first sub-cache area;
[0023] The processing unit is also used to allocate the objects to be assigned to the first sub-cache area after cleanup for caching.
[0024] In one possible design, in another implementation of the embodiments of this application,
[0025] The acquisition unit is also used to acquire the third remaining space value of the independent cache region based on the cleaned first sub-cache region, wherein the third remaining space value is greater than the second remaining space value;
[0026] The processing unit is also used to trigger the overall garbage collection mechanism if the third remaining space value is less than the first space threshold, and to clean up each sub-cache area separately to obtain the fourth remaining space value of the independent cache area.
[0027] The processing unit is also used to allocate the object to be allocated to the cleaned independent cache area for caching if the fourth remaining space value is greater than the first space threshold.
[0028] In one possible design, in another implementation of the embodiments of this application,
[0029] The determination unit is also used to determine if the allocation of the object to be allocated has failed if the fourth remaining space value is less than the first space threshold, and to issue a prompt that there is a memory overflow exception in the independent cache area.
[0030] In one possible design, in another implementation of the embodiments of this application, the acquisition unit may specifically be used for:
[0031] If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, then the independent cache region is scanned to determine the cache region object in the independent cache region that references the young generation region object;
[0032] Copy the cached region object to the young generation region, execute the young generation garbage collection mechanism, and obtain the second remaining space value of the independent cache region after collection.
[0033] In one possible design, in another implementation of the embodiments of this application, the acquisition unit may specifically be used for:
[0034] Treat the cached area object as a live object and copy the live object to the survivor area;
[0035] Scan the young generation region to identify live objects in the generation region;
[0036] Based on the young generation garbage collection mechanism, live objects in the generation area are copied to the survivor area, and the generation area is cleaned up.
[0037] After the generation area is cleaned up, the second remaining space value of the independent cache area is calculated.
[0038] In one possible design, in another implementation of the embodiments of this application,
[0039] The processing unit is also used to copy the live objects in the generation area and the cache area objects to the old generation area when the survivor area is insufficient to store the live objects in the generation area and the cache area objects.
[0040] The processing unit is also used to trigger the recycling mechanism for older waste and to clean up older areas.
[0041] In one possible design, in another implementation of the embodiments of this application, the determining unit may specifically be used for:
[0042] If the second remaining space value is less than the first space threshold, then each sub-cache region is scanned, and the live objects in each sub-cache region are marked to obtain the number of live objects marked for each sub-cache region.
[0043] The number of live object tags corresponding to each sub-cache region is compared with the liveness threshold. Sub-cache regions with a number of live object tags less than the liveness threshold are identified as the first sub-cache region, and sub-cache regions with a number of live object tags greater than the liveness threshold are identified as the second sub-cache region.
[0044] In one possible design, in another implementation of the embodiments of this application, the determining unit may specifically be used for:
[0045] If the comparison result shows that the used space value of the old generation region is greater than the second space threshold, then each sub-cache region is scanned, and the live objects in the sub-cache regions scanned in each sub-cache region are marked to obtain the number of live objects marked for each sub-cache region.
[0046] The number of live object tags corresponding to each sub-cache region is compared with the liveness threshold. Sub-cache regions with a number of live object tags less than the liveness threshold are identified as the first sub-cache region, and sub-cache regions with a number of live object tags greater than the liveness threshold are identified as the second sub-cache region.
[0047] This application also provides a computer device, including: a memory, a processor, and a bus system;
[0048] The memory is used to store programs;
[0049] The processor implements the methods described above when executing a program in memory;
[0050] Bus systems are used to connect memory and processor to enable communication between them.
[0051] Another aspect of this application provides a computer-readable storage medium storing instructions that, when executed on a computer, cause the computer to perform the methods described above.
[0052] As can be seen from the above technical solutions, the embodiments of this application have the following beneficial effects:
[0053] By dividing the target heap memory into a young generation region, an old generation region, and an independent cache region including sub-cache regions, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, the young generation garbage collection mechanism can be triggered. After collection, the second remaining space value of the independent cache region is obtained. Then, when the second remaining space value is less than the first space threshold, the independent cache region can be scanned to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the survival threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the survival threshold. Then, the live objects in the sub-cache region of the first sub-cache region are copied to the second sub-cache region, and the first sub-cache region is cleaned up, so that the object to be allocated can be allocated to the cleaned first sub-cache region for caching. By adding independent cache regions in the heap memory as described above, cached data, especially large amounts of cached data, can be uniformly allocated and managed within these cache regions. During garbage collection, data in these independent cache regions is collected according to different collection strategies. For example, objects in the independent cache regions are not collected during normal garbage collection such as the young generation; only the independent cache regions are scanned. When the number of non-live objects in an independent cache region exceeds a certain limit, the independent cache region is automatically collected. By detecting the number of live objects in the sub-cache regions, the first sub-cache region with the fewest live objects is collected, thereby increasing the space capacity of the independent cache regions to store as much cached data as possible. Furthermore, the copying of cached objects during the collection process can be reduced, and the pause time during collection can be reduced, thereby improving the application's throughput and response time, making the application work more stably and efficiently. Attached Figure Description
[0054] Figure 1 This is a schematic diagram of the architecture of the image data control system in an embodiment of this application;
[0055] Figure 2 This is a flowchart of one embodiment of the memory management method in this application;
[0056] Figure 3 This is a flowchart of another embodiment of the memory management method in this application;
[0057] Figure 4 This is a flowchart of another embodiment of the memory management method in this application;
[0058] Figure 5 This is a flowchart of another embodiment of the memory management method in this application;
[0059] Figure 6 This is a flowchart of another embodiment of the memory management method in this application;
[0060] Figure 7 This is a flowchart of another embodiment of the memory management method in this application;
[0061] Figure 8 This is a flowchart of another embodiment of the memory management method in this application;
[0062] Figure 9 This is a flowchart of another embodiment of the memory management method in this application;
[0063] Figure 10 This is a flowchart of another embodiment of the memory management method in this application;
[0064] Figure 11 This is a schematic diagram illustrating the principle of an allocation object in the memory management method of this application embodiment;
[0065] Figure 12 This is a schematic diagram illustrating the principle of memory reclamation in the memory management method of this application embodiment;
[0066] Figure 13 This is a schematic diagram of one embodiment of the memory management device in this application;
[0067] Figure 14 This is a schematic diagram of one embodiment of the computer device described in this application. Detailed Implementation
[0068] This application provides a memory management method, apparatus, device, and storage medium. It manages cached data by uniformly allocating it to a cache region and reclaiming data from independent cache regions according to different reclamation strategies. When the number of non-live objects in an independent cache region exceeds a certain limit, the independent cache region is automatically reclaimed. By detecting the number of live objects in a sub-cache region, the first sub-cache region with fewer live objects is reclaimed to increase the space capacity of the independent cache region, maximizing the storage of cached data. Furthermore, the reclamation process reduces the copying of cached objects and the pause time during reclamation, thereby improving application throughput and response time, and making the application work more stably and efficiently.
[0069] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification, claims, and drawings of this application are used to distinguish similar objects and are not necessarily used to describe a particular order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented, for example, in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “corresponding to,” and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0070] To facilitate understanding, some terms or concepts involved in the embodiments of this application will be explained first.
[0071] 1. Java Virtual Machine (JVM): A virtual machine capable of running Java bytecode. Java applications run on the JVM. Currently, the most widely used JVM in the industry is the garbage collector (Hotspot JVM).
[0072] 2. Garbage Collection (GC): An automatic memory management mechanism that eliminates the need for programmers to manually reclaim memory, as GC automatically cleans up the memory.
[0073] 3. GC Types: In Hotspot JVM, commonly used GC algorithms can be categorized into several types based on their GC pause times:
[0074] For example, garbage collection algorithms like G1GC or CMS, which are mostly concurrent GCs, use the mark-copy algorithm for their young generation collection mechanism. The GC pause time is related to the number of live objects that need to be copied.
[0075] Parallel garbage collection algorithms like Shenandoah G or ZGC implement a concurrent copy algorithm. The required pause time is not related to the number of live objects copied, but only to the GC root node itself. While GC pause times are short, they can impact application throughput.
[0076] 4. Independent Heap Region: This is a way for the Java Virtual Machine to manage memory. For example, Hotspot JVM (the most commonly used type of Java Virtual Machine) G1GC and Shenandoah GC both manage memory based on Region, which divides large memory into small spaces with contiguous virtual addresses for management.
[0077] 5. Software Cache: A software-implemented cache data structure used to store frequently accessed hot data. The data source is usually read from the network or files. Putting this data into memory can speed up data access.
[0078] 6. Heap memory: For managed languages, heap memory stores newly created data and is a contiguous area of virtual address space. Managed languages typically require garbage collection (GC) to manage memory allocation and reclamation.
[0079] 7. Generational Garbage Collection Algorithm (GC): Generally, most GC on the JVM is divided into two regions: the Young generation and the Old generation. The Young generation stores newly allocated objects, while the Old generation stores objects that have survived several GC cycles.
[0080] 8. Young Generation Garbage Collection Mechanism: Young GC: A GC type that only collects garbage from the Young generation.
[0081] 9. Old Generation Garbage Collection Mechanism: Old GC: A GC type that reclaims the Old generation.
[0082] 10. Concurrent GC: A garbage collection mechanism that can be executed in parallel with business logic.
[0083] 11. Full GC: A GC type that reclaims the entire heap memory.
[0084] 12. Scanning algorithm mark: Generally, it starts from the root node GC root and marks the liveness of a certain object. Those objects that have not been marked will be collected. Therefore, the main purpose of scanning is to find live objects for collection.
[0085] 13. GC root node: Common root nodes include Java thread objects, etc. The marking starts from the root node to find all live objects.
[0086] 14. Garbage Collection Algorithm: Based on G1GC, it adopts the copy garbage collection algorithm of G1GC. After finding live objects in the marking phase, the live objects will be copied to a designated location. For example, objects in the young area will be copied to the Survivor area, and objects that have exceeded a certain age (marked as live objects by a certain number of GC cycles) will be copied to the old area. Copying is the most time-consuming phase in the GC process.
[0087] It is understood that in the specific implementation of this application, data related to the object to be allocated and the first remaining space value are involved. When the above embodiments of this application are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of related data must comply with the relevant laws, regulations and standards of the relevant countries and regions.
[0088] It is understandable that the memory management method disclosed in this application involves cloud technology, which will be further introduced below. Cloud technology refers to a hosting technology that unifies hardware, software, network, and other resources within a wide area network (WAN) or local area network (LAN) to achieve data computation, storage, processing, and sharing. Cloud technology is a general term for network technology, information technology, integration technology, management platform technology, and application technology applied in the cloud computing business model. It can form resource pools, be used on demand, and is flexible and convenient. Cloud computing technology will become an important support. The backend services of technical network systems require a large amount of computing and storage resources, such as video websites, image websites, and many portal websites. With the rapid development and application of the Internet industry, every item may have its own identification mark in the future, all of which need to be transmitted to the backend system for logical processing. Data of different levels will be processed separately, and various industry data will all require strong system support, which can only be achieved through cloud computing.
[0089] Cloud computing is a computing model that distributes computing tasks across a resource pool composed of a large number of computers, enabling various application systems to access computing power, storage space, and information services as needed. The network providing these resources is called the "cloud." From the user's perspective, the resources in the "cloud" are infinitely scalable, readily available, on-demand, expandable, and pay-as-you-go.
[0090] As a provider of fundamental cloud computing capabilities, a cloud resource pool (referred to as a cloud platform, generally called an IaaS (Infrastructure as a Service) platform) is established. Various types of virtual resources are deployed in the resource pool for external customers to choose from. The cloud resource pool mainly includes: computing devices (virtualized machines containing operating systems), storage devices, and network devices.
[0091] Based on logical function, a PaaS (Platform as a Service) layer can be deployed on top of the IaaS (Infrastructure as a Service) layer, and a SaaS (Software as a Service) layer can be deployed on top of the PaaS layer. Alternatively, SaaS can be deployed directly on top of IaaS. PaaS is a platform for running software, such as databases and web containers. SaaS refers to various types of transaction software, such as web portals and bulk SMS senders. Generally speaking, SaaS and PaaS are upper layers compared to IaaS.
[0092] Secondly, cloud security refers to the collective term for security software, hardware, users, organizations, and security cloud platforms based on cloud computing business models. Cloud security integrates emerging technologies and concepts such as parallel processing, grid computing, and unknown virus behavior detection. Through a large network of clients, it monitors abnormal software behavior on the network, obtains the latest information on Trojans and malware on the Internet, sends it to the server for automatic analysis and processing, and then distributes solutions for viruses and Trojans to each client.
[0093] Secondly, cloud storage is a new concept that extends and develops from the concept of cloud computing. A distributed cloud storage system (hereinafter referred to as a storage system) refers to a storage system that uses cluster applications, grid technology, and distributed storage file systems to bring together a large number of storage devices of various types (storage devices are also called storage nodes) in the network to work together and jointly provide data storage and transaction access functions to the outside world.
[0094] Currently, the storage method of storage systems is as follows: Logical volumes are created. During the creation of a logical volume, physical storage space is allocated to each logical volume. This physical storage space may consist of a single storage device or the disks of several storage devices. Clients store data on a logical volume, which means storing the data on the file system. The file system divides the data into many parts, each part being an object. Each object contains not only the data but also additional information such as a data identifier (ID, ID entity). The file system writes each object to the physical storage space of that logical volume and records the storage location information of each object. Therefore, when a client requests access to data, the file system can allow the client to access the data based on the storage location information of each object.
[0095] The process by which a storage system allocates physical storage space to a logical volume is as follows: the physical storage space is pre-divided into strips according to the capacity estimate of the objects stored in the logical volume (this estimate often has a large margin relative to the actual capacity of the objects to be stored) and the grouping of Redundant Array of Independent Disks (RAID). A logical volume can be understood as a strip, thus allocating physical storage space to the logical volume.
[0096] It should be understood that the memory management method provided in this application can be applied to various scenarios, including but not limited to artificial intelligence, cloud technology, maps, and intelligent transportation. It is used to allocate cached data to the cache area for storage and unified management by adding an independent cache area and reclaiming the data in the independent cache area according to different reclamation strategies, so as to optimize heap memory management and apply it to scenarios such as caching of e-commerce applications, caching of chat applications, caching of short video applications, and caching of hot news and information applications.
[0097] To address the aforementioned problems, this application proposes a memory management method, which is applied to... Figure 1 For the data object control system shown, please refer to [link / reference]. Figure 1 , Figure 1 This is a schematic diagram of the architecture of a data object control system in an embodiment of this application, such as... Figure 1As shown, the server divides the target heap memory into a young generation region, an old generation region, and an independent cache region including sub-cache regions. If the storage space value corresponding to the object to be allocated provided by the terminal device is greater than the first remaining space value of the independent cache region, the young generation garbage collection mechanism can be triggered. After collection, the second remaining space value of the independent cache region is obtained. Then, when the second remaining space value is less than the first space threshold, the independent cache region can be scanned to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the survival threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the survival threshold. Then, the live objects in the sub-cache region of the first sub-cache region are copied to the second sub-cache region, and the first sub-cache region is cleaned up so that the object to be allocated can be allocated to the cleaned first sub-cache region for caching. By adding independent cache regions in the heap memory as described above, cached data, especially large amounts of cached data, can be uniformly allocated and managed within these cache regions. During garbage collection, data in these independent cache regions is collected according to different collection strategies. For example, objects in the independent cache regions are not collected during normal garbage collection such as the young generation; only the independent cache regions are scanned. When the number of non-live objects in an independent cache region exceeds a certain limit, the independent cache region is automatically collected. By detecting the number of live objects in the sub-cache regions, the first sub-cache region with the fewest live objects is collected, thereby increasing the space capacity of the independent cache regions to store as much cached data as possible. Furthermore, the copying of cached objects during the collection process can be reduced, and the pause time during collection can be reduced, thereby improving the application's throughput and response time, making the application work more stably and efficiently.
[0098] Understandable Figure 1 Only one type of terminal device is shown in the diagram. In real-world scenarios, many more types of terminal devices can participate in the data processing. These include, but are not limited to, mobile phones, computers, smart voice interaction devices, smart home appliances, and in-vehicle terminals. The specific number and types depend on the actual scenario and are not limited here. Furthermore, Figure 1 The diagram shows one server, but in real-world scenarios, multiple servers can be involved, especially in scenarios involving multi-model training and interaction. The number of servers depends on the specific scenario and is not limited here.
[0099] It should be noted that in this embodiment, the server can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms. Terminal devices and servers can be directly or indirectly connected via wired or wireless communication, and terminal devices and servers can be connected to form a blockchain network; this application does not impose any limitations on this.
[0100] Based on the above introduction, the memory management method in this application will be described below. Please refer to [link / reference]. Figure 2 One embodiment of the memory management method in this application includes:
[0101] In step S101, the target heap memory is divided into a young generation region, an old generation region, and an independent cache region, wherein the independent cache region includes a sub-cache region.
[0102] It's understandable that software caches typically store data read from the network or disk, which needs to be stored in memory for subsequent processing. Alternatively, the cache may contain a large amount of frequently accessed data: for example, popular products in e-commerce applications are stored in the application's software cache, significantly reducing the time users spend accessing these items. However, if a user accesses a less popular product, the software caching strategy usually evicts the least recently used cached data and retains the frequently used, popular data. This can lead to less popular products not being cached and instead being retrieved from other sources (such as the database), resulting in longer access times for these less popular products.
[0103] Therefore, in order to store as much hot data as possible and reduce the eviction of the least recently used cached data, thereby reducing the access latency of infrequently used cached data such as unpopular products, this embodiment divides the target heap memory into a young generation region, an old generation region, and an independent cache region containing sub-cache regions, so as to uniformly allocate cached data to an independent cache region for management, so as to better store these cached data.
[0104] An independent heap region comprises one or more sub-heap regions. Understandably, the size of an independent heap region is typically estimated based on business characteristics and data from past execution processes. This allows for setting an upper limit on the size of the independent heap region for better management. The size of the independent heap region is usually larger than the size of the cache data structure itself. This allows for some memory wastage but significantly reduces garbage collection (GC) pause times.
[0105] The young generation is used to store newly created objects; all newly created objects are typically placed in the young generation. The goal of the young generation is to collect short-lived objects as quickly as possible. The young generation is usually divided into three areas: one generation area (Eden) and two survivor areas (Survivor0 and Survivor0). Most objects are created in the Eden area.
[0106] The old generation is used to store objects with long lifecycles. Objects that survive N garbage collections in the young generation are placed in the old generation. Therefore, it can be assumed that the old generation stores objects with long lifecycles.
[0107] The heap memory is a separate memory region from the stack, global data area, and code area. The heap allows a program to dynamically request a memory space of a certain size at runtime. The target heap memory refers to the heap memory corresponding to the application currently being managed and optimized.
[0108] Specifically, for the target heap memory that will be used to store some large amounts of cached data or data that requires special processing (such as frequent access), in order to better store this cached data, this embodiment adds an independent cache area in the target heap memory, so that these cached data are uniformly allocated to the independent cache area for unified management. Therefore, the target heap memory can be divided into young generation area, old generation area and independent cache area, etc.
[0109] In step S102, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, the young generation garbage collection mechanism is triggered, and the second remaining space value of the independent cache area is obtained after collection, wherein the second remaining space value is greater than or equal to the first remaining space value.
[0110] In this embodiment of the application, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, that is, when the space of the independent cache area is insufficient, the young generation garbage collection mechanism can be triggered to clean up memory and release a certain amount of cache space. Then, after the young generation garbage collection is completed, the second remaining space value of the independent cache area can be obtained so that the second remaining space value of the independent cache area can be used to better determine whether the used space of the independent cache area exceeds a certain upper limit or whether the remaining space is sufficient.
[0111] In this context, "objects to be allocated" refers to cached data that requires special handling in the business code. Typically, the system marks this cached data, either using Java annotations at the point of cache creation or other methods (no specific restrictions here). This ensures that marked cached data is processed separately during allocation, and the cached object is allocated to a fixed, independent heap region. The storage space value corresponding to the object to be allocated refers to the memory space required to store it, such as 7MB. The first remaining space value of the independent heap region refers to the size of the remaining space in the heap region when the object to be allocated is initially allocated. The second remaining space value of the independent heap region refers to the size of the remaining space in the heap region after the young generation garbage collection mechanism has been executed. The second remaining space value is greater than or equal to the first remaining space value.
[0112] Specifically, such as Figure 11 As shown, when starting the allocation of an object, it can be determined whether the remaining space in the independent cache area (i.e., the first remaining space value) is sufficient to store the cached data, i.e., the object to be allocated. Specifically, the storage space value corresponding to the object to be allocated can be compared with the first remaining space value of the independent cache area. If the storage space value corresponding to the object to be allocated is less than the first remaining space value of the independent cache area, i.e., there is enough space, the object to be allocated will be directly allocated to that independent cache area (e.g., ...). Figure 11 (As indicated by the allocation to the cache area).
[0113] Conversely, if the storage space corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, i.e., there is not enough space, since the general condition for Java to trigger GC is Young GC due to insufficient space, similarly, when the space of the independent cache region is insufficient, the garbage collection mechanism is triggered, i.e., the young generation garbage collection mechanism is triggered, such as... Figure 12As shown, when the young generation garbage collection mechanism is triggered, the independent cache area can be scanned first to determine whether there is a cache area object that references the young generation area object. If it exists, the cache area object can be copied as a live object to the young generation area. Then, the young generation garbage collection mechanism is executed to clean up the non-live objects in the young generation area, and the remaining space value of the current independent cache area is obtained after the collection, which is the second remaining space value.
[0114] In step S103, if the second remaining space value is less than the first space threshold, the independent cache region is scanned to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the survival threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the survival threshold.
[0115] In this embodiment of the application, if the second remaining space value is less than the first space threshold, that is, when the used space value of the independent cache region exceeds a certain upper limit, the reclamation mechanism for the independent cache region is triggered. That is, the independent cache region is first scanned to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the survival threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the survival threshold, so that the sub-cache region can be better reclaimed in the future.
[0116] The first space threshold is set based on actual application needs and is not specifically limited here. It is used to determine whether the remaining space in an independent cache region is sufficient, or whether the used space value of an independent cache region exceeds a certain upper limit. The survival threshold is also set based on actual application needs and is not specifically limited here. It is used to determine the number of live objects in a sub-cache region. The first sub-cache region refers to the region with a large number of garbage objects (non-live objects), i.e., the region where the number of live objects in the cache region is less than the survival threshold. The second sub-cache region refers to the region with fewer garbage objects (non-live objects), i.e., the region where the number of live objects in the cache region is greater than the survival threshold.
[0117] Specifically, if the second remaining space value is less than the first space threshold, meaning the current remaining space value of the independent cache region is still insufficient to store the objects to be allocated (for example, if the second remaining space value is 3M, which is less than the first space threshold of 8M, and the storage space value of the objects to be allocated is 7M, then the second remaining space value is insufficient to store the objects to be allocated), or, it can also be understood as when the used space value of the independent cache region exceeds a certain upper limit (for example, if the size of the independent cache region is 10M, and the used space value of the independent cache region exceeds a certain upper limit, such as 75%, i.e., 7.5M, then it is insufficient to store the objects to be allocated), then the reclamation mechanism of the independent cache region is triggered. That is, the independent cache region is first scanned. Specifically, it can be a reachability analysis algorithm to determine the live objects in each sub-cache region, i.e., live objects in the sub-cache region, and garbage objects that are not live and can be reclaimed. It can also be determined by other algorithms, without specific restrictions here.
[0118] Furthermore, the independent cache regions are scanned using a reachability analysis algorithm. Specifically, the program treats all reference relationships (such as strong references, soft references, weak references, and phantom references, whose strength gradually decreases) as a graph. For cached objects in each sub-cache region, starting from a root node (GC Root), the corresponding reference node is searched. After finding this node, the search continues for the reference nodes of this node. When all reference nodes have been searched, the remaining nodes are considered to be unreferenced nodes, i.e., useless nodes (unreachable). Useless nodes will be judged as reclaimable objects, i.e., non-live objects. Nodes with reference relationships are live objects, i.e., live objects in the sub-cache region.
[0119] Furthermore, after obtaining the live objects of each sub-cache region, the live objects of the sub-cache region can be compared with the liveness threshold. If the number of live objects in the sub-cache region is less than the liveness threshold, it can be determined as the first sub-cache region (e.g., ...). Figure 12 The region with the fewest live objects in the cache region is identified as the second sub-cache region. If the number of live objects in a sub-cache region exceeds the liveness threshold, it can be identified as the second sub-cache region.
[0120] In step S104, the sub-cache area live objects in the first sub-cache area are copied to the second sub-cache area, and the first sub-cache area is cleaned up.
[0121] In this embodiment, after determining a first sub-cached region where the number of live objects in the sub-cached region is less than the survival threshold, and a second sub-cached region where the number of live objects in the sub-cached region is greater than the survival threshold, based on the independent cache region reclamation mechanism, the live objects in the first sub-cached region can be copied to the second sub-cached region, and the first sub-cached region can be cleaned up. This reduces the copying of cached objects and the pause time of reclamation, thereby freeing up some space in the independent cache region and better storing objects to be allocated. This can improve the throughput and response time of the application to a certain extent, making the application work more stably and efficiently.
[0122] Specifically, such as Figure 12 As shown, after identifying the first sub-cache region where the number of live objects is less than the survival threshold, and the second sub-cache region where the number of live objects is greater than the survival threshold, if a typical garbage collection strategy, such as the copying algorithm, is directly applied to each sub-cache region, the available memory is divided into two equal-sized blocks, and only one block is used at a time. When the memory in one block is exhausted, the surviving objects are copied to the other block, and then the used memory space is cleared all at once. However, the efficiency of the copying algorithm is highly dependent on the number of live objects. If there are many live objects, i.e., too many cached objects need to be copied, the efficiency of the copying algorithm will be greatly reduced, increasing GC pause time.
[0123] Therefore, in this embodiment, live objects from the sub-cache areas in the first sub-cache area are copied to the second sub-cache area. Specifically, for the first sub-cache area with fewer live objects, live objects from its sub-cache areas are copied to the second sub-cache area with more live objects. Then, the second sub-cache area with more live objects is not cleaned; only the first sub-cache area, which has no live objects, is cleaned. At this point, the cleanup of the first sub-cache area without live objects can be performed using a conventional garbage collection algorithm (e.g., ...). Figure 12 (This refers to clearing the cache area of regions with fewer objects).
[0124] In step S105, the objects to be allocated are assigned to the first sub-cache area after cleanup for caching.
[0125] In this embodiment of the application, after clearing the first sub-cache area, it can be used to store objects to be allocated. That is, the objects to be allocated can be allocated to the cleared first sub-cache area for caching, so that when the user accesses the objects to be allocated, i.e. the cached data, the cache can be hit, thereby reducing the access latency of the cached data.
[0126] Specifically, after clearing the first sub-cache area, if the memory space value of the cleared first sub-cache area is greater than the storage space value of the object to be allocated, that is, the space of the current independent cache area is sufficient, it can be used to store the object to be allocated, that is, the object to be allocated can be allocated to the cleared first sub-cache area for caching.
[0127] In this embodiment, a memory management method is provided. This method adds an independent cache region to the heap memory, allowing for unified allocation and management of cached data, especially large amounts of cached data. During garbage collection, different collection strategies are applied to the data in the independent cache region. For regular garbage collection such as the young generation, objects in the independent cache region are not collected; only the independent cache region is scanned. When the number of non-live objects in the independent cache region exceeds a certain limit, automatic garbage collection is performed. By identifying the number of live objects in the sub-cache regions, the first sub-cache region with the fewest live objects is collected, thereby increasing the space capacity of the independent cache region to store as much cached data as possible. Furthermore, the method reduces the copying of cached objects and the pause time during garbage collection, thereby improving application throughput and response time, and making the application work more stably and efficiently.
[0128] Optionally, in the above Figure 2 Based on the corresponding embodiments, in another optional embodiment of the memory management method provided in this application, such as... Figure 3 As shown, in step S102, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, the young generation garbage collection mechanism is triggered. After obtaining the second remaining space value of the independent cache region after collection, the method further includes:
[0129] In step S301, if the second remaining space value is greater than the first space threshold, the used space value of the old generation region is compared with the second space threshold to obtain the comparison result.
[0130] In this embodiment of the application, if the second remaining space value is greater than the first space threshold, that is, the used space value of the independent cache region does not exceed a certain upper limit, in order to ensure that the objects to be allocated can be safely and stably allocated to the independent cache region, it can be determined whether the old generation region is currently full, that is, whether the used space value of the old generation region exceeds a certain upper limit. Then, the used space value of the old generation region is compared with the second space threshold to obtain the comparison result.
[0131] The second space threshold is set according to actual application needs and is not specifically limited here. The second space threshold is used to determine whether the remaining space in the old generation area is sufficient, or to determine whether the used space value of the old generation area exceeds a certain upper limit.
[0132] Specifically, such as Figure 12 As shown, if the second remaining space value is greater than the first space threshold, that is, the used space value of the independent cache region has not exceeded a certain upper limit, in order to ensure that the objects to be allocated can be safely and stably allocated to the independent cache region, it can be determined whether the old generation region is currently full, that is, whether the used space value of the old generation region exceeds a certain upper limit, and trigger a Full GC. This is because, under normal circumstances, the program does not want to cause a Full GC due to insufficient space during allocation, so as to clean up the entire heap memory. Since frequent triggering of Full GC will increase the number of copied cache objects, and copying is the most time-consuming stage in the GC process, in order to avoid the situation where the used space value of the old generation region reaches a certain upper limit and a Full GC is immediately triggered, this embodiment can compare the used space value of the old generation region with the second space threshold to obtain the comparison result, so that subsequent processing can be carried out based on the comparison result.
[0133] In step S302, if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, then the independent cache region is scanned to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the survival threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the survival threshold.
[0134] In this embodiment of the application, if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, that is, when the used space value of the old generation region exceeds a certain upper limit, the reclamation mechanism for the independent cache region is triggered. That is, the independent cache region is first scanned to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the survival threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the survival threshold, so that the sub-cache region can be better reclaimed in the future.
[0135] Specifically, if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, that is, the used space value of the old generation region exceeds a certain upper limit, a cleanup phase can be added to release some space for data storage, thereby avoiding frequent triggering of the overall Full GC. This triggers a reclamation mechanism for independent cache regions, which means that the target heap memory can be globally marked (e.g., ...). Figure 12 The global marking (as shown) means that the current young generation region, old generation region and independent cache region are scanned respectively, and the live objects in the young generation region, old generation region and independent cache region are marked respectively.
[0136] Furthermore, for scanning independent cache regions, a reachability analysis algorithm can be used to determine the live objects in each sub-cache region, i.e., live objects in the sub-cache region, as well as non-live objects that can be reclaimed garbage objects. Alternatively, other algorithms can be used to determine them, without specific restrictions here.
[0137] Furthermore, the independent cache regions are scanned using a reachability analysis algorithm. Specifically, the program treats all reference relationships (such as strong references, soft references, weak references, and phantom references, whose strength gradually decreases) as a graph. For cached objects in each sub-cache region, starting from a root node (GC Root), the corresponding reference node is searched. After finding this node, the search continues for the reference nodes of this node. When all reference nodes have been searched, the remaining nodes are considered to be unreferenced nodes, i.e., useless nodes (unreachable). Useless nodes will be determined as reclaimable objects, i.e., non-live objects. Nodes with reference relationships are live objects, i.e., live objects in the sub-cache region.
[0138] Furthermore, after obtaining the live objects of each sub-cache region, the live objects of the sub-cache region can be compared with the liveness threshold. If the number of live objects in the sub-cache region is less than the liveness threshold, it can be determined as the first sub-cache region (e.g., ...). Figure 12 The region with the fewest live objects in the cache region is identified as the second sub-cache region. If the number of live objects in a sub-cache region exceeds the liveness threshold, it can be identified as the second sub-cache region.
[0139] In step S303, the sub-cache area live objects in the first sub-cache area are copied to the second sub-cache area, and the first sub-cache area is cleaned up.
[0140] In this embodiment, after determining a first sub-cached region where the number of live objects in the sub-cached region is less than the survival threshold, and a second sub-cached region where the number of live objects in the sub-cached region is greater than the survival threshold, based on the independent cache region reclamation mechanism, the live objects in the first sub-cached region can be copied to the second sub-cached region, and the first sub-cached region can be cleaned up. This reduces the copying of cached objects and the pause time of reclamation, thereby freeing up some space in the independent cache region and better storing objects to be allocated. This can improve the throughput and response time of the application to a certain extent, making the application work more stably and efficiently.
[0141] Specifically, such as Figure 12As shown, after identifying the first sub-cache region where the number of live objects is less than the survival threshold, and the second sub-cache region where the number of live objects is greater than the survival threshold, if a typical garbage collection strategy, such as the copying algorithm, is directly applied to each sub-cache region, the available memory is divided into two equal-sized blocks, and only one block is used at a time. When the memory in one block is exhausted, the surviving objects are copied to the other block, and then the used memory space is cleared all at once. However, the efficiency of the copying algorithm is highly dependent on the number of live objects. If there are many live objects, i.e., too many cached objects need to be copied, the efficiency of the copying algorithm will be greatly reduced, increasing GC pause time.
[0142] Therefore, in this embodiment, live objects from the sub-cache areas in the first sub-cache area are copied to the second sub-cache area. Specifically, for the first sub-cache area with fewer live objects, live objects from its sub-cache areas are copied to the second sub-cache area with more live objects. Then, the second sub-cache area with more live objects is not cleaned; only the first sub-cache area, which has no live objects, is cleaned. At this point, the cleanup of the first sub-cache area without live objects can be performed using a conventional garbage collection algorithm (e.g., ...). Figure 12 (This indicates the region with fewer cached objects).
[0143] In step S304, the object to be allocated is assigned to the first sub-cache area after cleanup for caching.
[0144] In this embodiment of the application, after clearing the first sub-cache area, it can be used to store objects to be allocated. That is, the objects to be allocated can be allocated to the cleared first sub-cache area for caching, so that when the user accesses the objects to be allocated, i.e. the cached data, the cache can be hit, thereby reducing the access latency of the cached data.
[0145] Specifically, after clearing the first sub-cache area, if the memory space value of the cleared first sub-cache area is greater than the storage space value of the object to be allocated, that is, the space of the current independent cache area is sufficient, it can be used to store the object to be allocated, that is, the object to be allocated can be allocated to the cleared first sub-cache area for caching.
[0146] Optionally, in the above Figure 2 Based on any corresponding embodiment, in another optional embodiment of the memory management method provided in this application, such as... Figure 4 As shown, after step 104 copies the sub-cache area or objects in the first sub-cache area to the second sub-cache area and cleans up the first sub-cache area, the method further includes:
[0147] In step 401, based on the cleaned first sub-cache region, the third remaining space value of the independent cache region is obtained, wherein the third remaining space value is greater than the second remaining space value;
[0148] In step 402, if the third remaining space value is less than the first space threshold, the overall garbage collection mechanism is triggered to clean up each sub-cache area separately, and a fourth remaining space value of the independent cache area is obtained.
[0149] In step 403, if the fourth remaining space value is greater than the first space threshold, the object to be allocated will be allocated to the cleaned independent cache area for caching.
[0150] In this embodiment, after clearing the first sub-cache area, the third remaining space value of the independent cache area can be obtained. If the third remaining space value is less than the first space threshold, that is, the space of the current independent cache area is insufficient, the overall garbage collection mechanism is triggered to clean each sub-cache area separately. Then, the fourth remaining space value of the cleaned independent cache area is obtained. If the fourth remaining space value is greater than the first space threshold, that is, the space of the current independent cache area is sufficient, the object to be allocated can be allocated to the cleaned independent cache area for caching so that when the user accesses the object to be allocated, that is, the cached data, the cache can be hit, thereby reducing the access latency of the cached data.
[0151] The third remaining space value of the independent cache region refers to the size of the remaining space in the independent cache region after the first sub-cache region is cleared. The fourth remaining space value refers to the size of the remaining space in the independent cache region after the full garbage collection mechanism is executed.
[0152] Specifically, after clearing the first sub-cache region, it can be determined whether the current independent cache region has enough space. This can be done by obtaining the current third remaining space value of the independent cache region and comparing it with the first space threshold. If the third remaining space value is less than the first space threshold, it can be understood that the used space value of the independent cache region after reclamation still exceeds a certain limit, and the overall garbage collection mechanism (Full GC) can be triggered to clean up the entire heap memory, that is, to clean up the young generation region, the old generation region, and the independent cache region. However, since the independent cache region is composed of individual sub-cache regions, when cleaning up the independent cache region, each sub-cache region can be cleaned up separately. This is because the Full GC algorithm itself is a compression algorithm, which will compress live objects in the space of the independent cache region in sequence. Therefore, in the Full GC phase, each sub-cache region of the independent cache region needs to be compressed separately to achieve the goal of overall compression.
[0153] Furthermore, after cleaning up each sub-cache area individually, the fourth remaining space value of the cleaned independent cache area can be obtained to determine whether the space of the current independent cache area is sufficient. Specifically, the fourth remaining space value can be compared with the first space threshold. If the fourth remaining space value is greater than the first space threshold, that is, the space of the current independent cache area is sufficient, it can be used to store objects to be allocated. In other words, the objects to be allocated can be allocated to the cleaned first sub-cache area for caching.
[0154] Optionally, in the above Figure 4 Based on any corresponding embodiment, in another optional embodiment of the memory management method provided in this application, such as... Figure 5 As shown, in step S402, if the third remaining space value is less than the first space threshold, the overall garbage collection mechanism is triggered to clean each sub-cache area separately. After obtaining the fourth remaining space value of the independent cache area, the method further includes:
[0155] In step S501, if the fourth remaining space value is less than the first space threshold, it is determined that the allocation of the object to be allocated has failed, and a prompt is issued indicating that there is a memory overflow exception in the independent cache area.
[0156] In this embodiment of the application, after each sub-cache area is cleaned up individually, the fourth remaining space value of the cleaned independent cache area can be obtained. If the fourth remaining space value is less than the first space threshold, that is, the space of the current independent cache area is insufficient and cannot store the object to be allocated, it can be determined that the allocation of the object to be allocated has failed, and a prompt that there is a memory overflow exception in the independent cache area is issued, so that the application can be aware of the memory overflow exception in a timely manner and maintain the memory of the application in a timely manner.
[0157] Furthermore, after cleaning each sub-cache region individually, the fourth remaining space value of the cleaned independent cache region can be obtained to determine whether the current independent cache region has sufficient space. Specifically, this can be done by comparing the fourth remaining space value with the first space threshold. If the fourth remaining space value is less than the first space threshold, meaning the current independent cache region has insufficient space and cannot store the object to be allocated, then it can be determined that the allocation of the object to be allocated has failed, and a message indicating that there is a memory overflow exception in the independent cache region (e.g., ...) can be issued. Figure 11 The program intentionally throws an OutOfMemory exception so that the user's program can be aware of the memory overflow exception and take certain exception handling actions, such as increasing the size of the cache area.
[0158] Optionally, in the above Figure 2 Based on any corresponding embodiment, in another optional embodiment of the memory management method provided in this application, such as... Figure 6 As shown, in step S102, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, the young generation garbage collection mechanism is triggered, and after collection, the second remaining space value of the independent cache region is obtained, including:
[0159] In step S601, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, the independent cache region is scanned to determine the cache region object in the independent cache region that references the young generation region object.
[0160] In step S602, the cache region object is copied to the young generation region, the young generation garbage collection mechanism is executed, and the second remaining space value of the independent cache region is obtained after the collection.
[0161] In this embodiment of the application, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, that is, when the space of the independent cache area is insufficient, the young generation garbage collection mechanism can be triggered to clean up memory and release a certain amount of cache space. Then, after the young generation garbage collection is completed, the second remaining space value of the independent cache area can be obtained so that the second remaining space value of the independent cache area can be used to better determine whether the used space of the independent cache area exceeds a certain upper limit or whether the remaining space is sufficient.
[0162] In this context, "objects to be allocated" refers to cached data that requires special handling in the business code. Typically, the system marks this cached data, either using Java annotations at the point of cache creation or other methods (no specific restrictions here). This ensures that marked cached data is processed separately during allocation, and the cached object is allocated to a fixed, independent heap region. The storage space value corresponding to the object to be allocated refers to the memory space required to store it, such as 7MB. The first remaining space value of the independent heap region refers to the size of the remaining space in the heap region when the object to be allocated is initially allocated. The second remaining space value of the independent heap region refers to the size of the remaining space in the heap region after the young generation garbage collection mechanism has been executed. The second remaining space value is greater than or equal to the first remaining space value.
[0163] Specifically, such as Figure 11As shown, when starting the allocation of an object, it can be determined whether the remaining space in the independent cache area (i.e., the first remaining space value) is sufficient to store the cached data, i.e., the object to be allocated. Specifically, the storage space value corresponding to the object to be allocated can be compared with the first remaining space value of the independent cache area. If the storage space value corresponding to the object to be allocated is less than the first remaining space value of the independent cache area, i.e., there is enough space, the object to be allocated will be directly allocated to that independent cache area (e.g., ...). Figure 11 (As indicated by the allocation to the cache area).
[0164] Conversely, if the storage space corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, i.e., there is not enough space, since the general condition for Java to trigger GC is Young GC due to insufficient space, similarly, when the space of the independent cache region is insufficient, the garbage collection mechanism is triggered, i.e., the young generation garbage collection mechanism is triggered, such as... Figure 12 As shown, when the young generation garbage collection mechanism is triggered, the independent cache area can be scanned first to determine whether there is a cache area object that references the young generation area object. If it exists, the cache area object can be copied as a live object to the young generation area. Then, the young generation garbage collection mechanism is executed to clean up the non-live objects in the young generation area, and the remaining space value of the current independent cache area is obtained after the collection, which is the second remaining space value.
[0165] Optionally, in the above Figure 6 Based on any corresponding embodiment, in another optional embodiment of the memory management method provided in this application, such as... Figure 7 As shown, the young generation region includes a generation region and a survivor region; step S602 copies the cache region object to the young generation region, executes the young generation garbage collection mechanism, and obtains the second remaining space value of the independent cache region after collection, including:
[0166] In step S701, the cache region object is treated as a live object and copied to the survivor region;
[0167] In step S702, the young generation region is scanned to identify live objects in the generation region;
[0168] In step S703, based on the young generation garbage collection mechanism, live objects in the generation area are copied to the survivor area, and the generation area is cleaned up;
[0169] In step S704, after the generation area is cleaned up, the second remaining space value of the independent cache area is calculated.
[0170] In this embodiment, if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, i.e., the space of the independent cache region is insufficient, the young generation garbage collection mechanism can be triggered to clean up memory and release a certain amount of cache space. This can be done by treating the cache region object as a live object and copying the live object to the survivor area. At the same time, the young generation region is scanned to determine the live objects in the generation area. Then, based on the young generation garbage collection mechanism, the live objects in the generation area can be copied to the survivor area, and the generation area can be cleaned up. After the generation area is cleaned up, the second remaining space value of the independent cache region is calculated so that the second remaining space value of the independent cache region can be used to better determine whether the used space of the independent cache region exceeds a certain upper limit or whether the remaining space is sufficient.
[0171] Understandably, during a normal Young GC phase, cached objects within the spaces of each sub-cache region of the independent cache region are not reclaimed; only objects in the Young region are reclaimed. The cached objects within each sub-cache region, i.e., cache region objects, are treated as GC roots and scanned. In other words, the Young GC phase still only reclaims objects in the Young region. If a cache region object references an object in the Young region, it will be treated as a live object and copied to the Survivor space.
[0172] Specifically, such as Figure 11 As shown, when starting the allocation of an object, it can be determined whether the remaining space in the independent cache area (i.e., the first remaining space value) is sufficient to store the cached data, i.e., the object to be allocated. Specifically, the storage space value corresponding to the object to be allocated can be compared with the first remaining space value of the independent cache area. If the storage space value corresponding to the object to be allocated is less than the first remaining space value of the independent cache area, i.e., there is enough space, the object to be allocated will be directly allocated to that independent cache area (e.g., ...). Figure 11 (As indicated by the allocation to the cache area).
[0173] Conversely, if the storage space corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, i.e., there is not enough space, since the general condition for Java to trigger GC is Young GC due to insufficient space, similarly, when the space of the independent cache region is insufficient, the garbage collection mechanism is triggered, i.e., the young generation garbage collection mechanism is triggered, such as... Figure 12 As shown, when the young generation garbage collection mechanism is triggered, the independent cache area can be scanned first (e.g., Figure 12 The cache GCroot scan (as shown) is used to determine whether there is a cache region object in the independent cache region that references the young generation region object. If it exists, the cache region object can be copied as a live object to the survivor area of the young generation region.
[0174] Furthermore, a young generation garbage collection mechanism is implemented to clean up non-surviving objects in the young generation region. Specifically, this can be achieved by dividing the young generation region into one generation region (Eden) and two survivor regions (such as Survivor0 and Survivor1) according to a conventional 8:1:1 ratio. During collection, the young generation region is scanned first (e.g., ...). Figure 12 The system performs other GC root scans (as indicated) to identify live objects in the generated region. These live objects can then be copied to a survivor region (such as survivor0). After that, the generated region (eden) is cleared, and the remaining space value of the current independent cache region is obtained after the garbage collection, which is the second remaining space value.
[0175] Understandably, when the survivor region (e.g., survivor0) is also full of objects, all live objects in the spawn region (eden) and the survivor region (e.g., survivor0) are copied to another survivor region (e.g., survivor1). Then, the spawn region (eden) and the survivor region (e.g., survivor0) are cleared. At this point, the survivor region (e.g., survivor0) is empty, so the survivor0 region and the survivor1 region can be swapped, that is, the survivor1 region is kept empty. This process is repeated.
[0176] Optionally, in the above Figure 7 Based on any corresponding embodiment, in another optional embodiment of the memory management method provided in this application, such as... Figure 8 As shown, step S703, based on the young generation garbage collection mechanism, copies live objects from the generation area to the survivor area and cleans up the generation area. The method further includes:
[0177] In step S801, when the surviving area is insufficient to store the live objects in the generation area and the cache area objects, the live objects in the generation area and the cache area objects are copied to the old generation area.
[0178] In step S802, the old-age waste recycling mechanism is triggered to clean up the old-age area.
[0179] In this embodiment, based on the young generation garbage collection mechanism, live objects in the generation area are copied to the survivor area, and the generation area is cleaned up. If the survivor area is insufficient to store live objects in the generation area and cache area objects, the live objects in the generation area and cache area objects are copied to the old generation area, and the old generation garbage collection mechanism is triggered to clean up the old generation area in order to release a certain amount of storage space.
[0180] Specifically, based on the young generation garbage collection mechanism, after copying live objects from the generation area to the survivor area and cleaning up the generation area, if the survivor area is insufficient to store live objects from the generation area and cached objects, in order to avoid memory overflow, live objects from the generation area and cached objects can be copied to the old generation area, triggering the old generation garbage collection mechanism to clean up the old generation area. Specifically, the mark-compact algorithm can be used, which is divided into two phases: the marking phase and the compaction phase. The marking phase can scan the old generation area to determine the live objects and non-live objects in the old generation area and mark all objects that need to be reclaimed, i.e., non-live objects. After the marking is completed, the compaction phase does not directly clean up the reclaimable objects, but moves all live objects to one end and then cleans up the memory outside the boundary.
[0181] It is understandable that when objects in an independent cache region exceed a certain age (i.e., how many times they have been marked as live by GC), they can also be copied to the old generation region (Old area).
[0182] Optionally, in the above Figure 2 Based on any corresponding embodiment, in another optional embodiment of the memory management method provided in this application, such as... Figure 9 As shown, in step S103, if the second remaining space value is less than the first space threshold, then a scan is performed on the independent cache regions to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold, including:
[0183] In step S901, if the second remaining space value is less than the first space threshold, then each sub-cache region is scanned, and the live objects in each sub-cache region are marked to obtain the number of live objects marked for each sub-cache region.
[0184] In step S902, the number of live object tags corresponding to each sub-cache region is compared with the survival threshold, and the sub-cache region with the number of live object tags less than the survival threshold is determined as the first sub-cache region, and the sub-cache region with the number of live object tags greater than the survival threshold is determined as the second sub-cache region.
[0185] In this embodiment, if the second remaining space value is less than the first space threshold, that is, when the used space value of the independent cache region exceeds a certain upper limit, the reclamation mechanism for the independent cache region is triggered. That is, each sub-cache region is first scanned, and the live objects in each sub-cache region are marked to obtain the number of live object marks corresponding to each sub-cache region. Then, the number of live object marks corresponding to each sub-cache region can be compared with the survival threshold. The sub-cache regions with the number of live object marks less than the survival threshold are determined as the first sub-cache region, and the sub-cache regions with the number of live object marks greater than the survival threshold are determined as the second sub-cache region, so that the subsequent reclamation of the sub-cache regions can be better carried out.
[0186] Specifically, if the second remaining space value is less than the first space threshold, meaning the current remaining space value of the independent cache region is still insufficient to store the objects to be allocated (for example, if the second remaining space value is 3M, which is less than the first space threshold of 8M, and the storage space value of the objects to be allocated is 7M, then the second remaining space value is insufficient to store the objects to be allocated), or, in other words, when the used space value of the independent cache region exceeds a certain upper limit (for example, if the size of the independent cache region is 10M, and the used space value of the independent cache region exceeds a certain upper limit, such as 75%, i.e., 7.5M), then it is insufficient to store the objects to be allocated, and the reclamation mechanism of the independent cache region is triggered. This means that the target heap memory can be globally marked (e.g., ...). Figure 12 The global marking (as shown) means that the current young generation region, old generation region and independent cache region are scanned respectively, and the live objects in the young generation region, old generation region and independent cache region are marked respectively.
[0187] Furthermore, for scanning independent cache regions, a reachability analysis algorithm can be used to determine the live objects in each sub-cache region, i.e., live objects in the sub-cache region, as well as non-live objects that can be reclaimed garbage objects. Alternatively, other algorithms can be used to determine them, without specific restrictions here.
[0188] Furthermore, the independent cache regions are scanned using a reachability analysis algorithm. Specifically, the program treats all reference relationships (such as strong references, soft references, weak references, and phantom references, whose strength gradually decreases) as a graph. For cached objects in each sub-cache region, starting from a root node (GC Root), the corresponding reference node is searched. After finding this node, the search continues for the reference nodes of this node. When all reference nodes have been searched, the remaining nodes are considered to be unreferenced nodes, i.e., useless nodes (unreachable). Useless nodes will be judged as reclaimable objects, i.e., non-live objects. Nodes with reference relationships are live objects, i.e., live objects in the sub-cache region.
[0189] Furthermore, after obtaining the live objects of each sub-cache region, the live objects of each sub-cache region obtained by scanning are marked (e.g., ...). Figure 12 The system identifies the live objects in the designated cache region and obtains the number of live object tags for each sub-cache region. Then, it compares the number of live object tags for each sub-cache region with a liveness threshold. If the number of live object tags for a sub-cache region is less than the liveness threshold, then that sub-cache region is designated as the first sub-cache region (e.g., [example of sub-cache region]). Figure 12 The region with the fewest live objects in the cache region is identified as the second sub-cache region. If the number of live objects in a sub-cache region exceeds the liveness threshold, then that sub-cache region can be identified as the second sub-cache region.
[0190] Optionally, in the above Figure 3 Based on any corresponding embodiment, in another optional embodiment of the memory management method provided in this application, such as... Figure 10 As shown, in step S302, if the comparison result indicates that the used space value of the old generation region is greater than the second space threshold, then a scan is performed on the independent cache regions to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold, including:
[0191] In step S1001, if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, then each sub-cache region is scanned, and the live objects in each sub-cache region are marked to obtain the number of live object markings corresponding to each sub-cache region.
[0192] In step S1002, the number of live object tags corresponding to each sub-cache region is compared with the survival threshold, and the sub-cache region with the number of live object tags less than the survival threshold is determined as the first sub-cache region, and the sub-cache region with the number of live object tags greater than the survival threshold is determined as the second sub-cache region.
[0193] In this embodiment, if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, that is, when the used space value of the old generation region exceeds a certain upper limit, the reclamation mechanism for the independent cache region is triggered. That is, each sub-cache region is scanned separately, and the live objects in each sub-cache region are marked to obtain the number of live object marks corresponding to each sub-cache region. Then, the number of live object marks corresponding to each sub-cache region can be compared with the survival threshold, and the sub-cache region with the number of live object marks less than the survival threshold is determined as the first sub-cache region, and the sub-cache region with the number of live object marks greater than the survival threshold is determined as the second sub-cache region, so that the subsequent reclamation of the sub-cache regions can be better.
[0194] Specifically, if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, that is, the used space value of the old generation region exceeds a certain upper limit, a cleanup phase can be added to release some space for data storage, thereby avoiding frequent triggering of the overall Full GC. This triggers a reclamation mechanism for independent cache regions, which means that the target heap memory can be globally marked (e.g., ...). Figure 12 The global marking (as shown) means that the current young generation region, old generation region and independent cache region are scanned respectively, and the live objects in the young generation region, old generation region and independent cache region are marked respectively.
[0195] Furthermore, for scanning independent cache regions, a reachability analysis algorithm can be used to determine the live objects in each sub-cache region, i.e., live objects in the sub-cache region, as well as non-live objects that can be reclaimed garbage objects. Alternatively, other algorithms can be used to determine them, without specific restrictions here.
[0196] Furthermore, the independent cache regions are scanned using a reachability analysis algorithm. Specifically, the program treats all reference relationships (such as strong references, soft references, weak references, and phantom references, whose strength gradually decreases) as a graph. For cached objects in each sub-cache region, starting from a root node (GC Root), the corresponding reference node is searched. After finding this node, the search continues for the reference nodes of this node. When all reference nodes have been searched, the remaining nodes are considered to be unreferenced nodes, i.e., useless nodes (unreachable). Useless nodes will be judged as reclaimable objects, i.e., non-live objects. Nodes with reference relationships are live objects, i.e., live objects in the sub-cache region.
[0197] Furthermore, after obtaining the live objects of each sub-cache region, the live objects of each sub-cache region obtained by scanning are marked (e.g., ...). Figure 12The system identifies the live objects in the designated cache region and obtains the number of live object tags for each sub-cache region. Then, it compares the number of live object tags for each sub-cache region with a liveness threshold. If the number of live object tags for a sub-cache region is less than the liveness threshold, then that sub-cache region is designated as the first sub-cache region (e.g., [example of sub-cache region]). Figure 12 The region with the fewest live objects in the cache region is identified as the second sub-cache region. If the number of live objects in a sub-cache region exceeds the liveness threshold, then that sub-cache region can be identified as the second sub-cache region.
[0198] The memory management method apparatus in this application is described in detail below. Please refer to [link / reference]. Figure 13 , Figure 13 This is a schematic diagram of one embodiment of the memory management method apparatus in this application. The memory management method apparatus 20 includes:
[0199] Processing unit 201 is used to divide the target heap memory into a young generation region, an old generation region, and an independent cache region, wherein the independent cache region includes a sub-cache region.
[0200] The acquisition unit 202 is used to trigger the young generation garbage collection mechanism if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, and to acquire the second remaining space value of the independent cache area after collection, wherein the second remaining space value is greater than or equal to the first remaining space value.
[0201] The determining unit 203 is used to scan the independent cache region if the second remaining space value is less than the first space threshold, and to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold.
[0202] The processing unit 201 is also used to copy the sub-cache area or object in the first sub-cache area to the second sub-cache area and clean up the first sub-cache area;
[0203] The processing unit 201 is also used to allocate the object to be allocated to the first sub-cache area after cleanup for caching.
[0204] Optionally, in the above Figure 13 Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application,
[0205] The processing unit 201 is further configured to compare the used space value of the old generation region with the second space threshold if the second remaining space value is greater than the first space threshold, and obtain the comparison result.
[0206] The determining unit 203 is also used to scan the independent cache region if the comparison result shows that the used space value of the old generation region is greater than the second space threshold, and to determine the first sub-cache region where the number of live objects in the sub-cache region is less than the survival threshold, and the second sub-cache region where the number of live objects in the sub-cache region is greater than the survival threshold.
[0207] The processing unit 201 is also used to copy the sub-cache area or object in the first sub-cache area to the second sub-cache area and clean up the first sub-cache area;
[0208] The processing unit 201 is also used to allocate the object to be allocated to the first sub-cache area after cleanup for caching.
[0209] Optionally, in the above Figure 13 Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application,
[0210] The acquisition unit 202 is also used to acquire the third remaining space value of the independent cache region based on the cleaned first sub-cache region, wherein the third remaining space value is greater than the second remaining space value;
[0211] The processing unit 201 is also used to trigger the overall garbage collection mechanism if the third remaining space value is less than the first space threshold, and to clean up each sub-cache area separately to obtain the fourth remaining space value of the independent cache area.
[0212] The processing unit 201 is also used to allocate the object to be allocated to the cleaned independent cache area for caching if the fourth remaining space value is greater than the first space threshold.
[0213] Optionally, in the above Figure 13 Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application,
[0214] The determination unit 203 is also used to determine that the allocation of the object to be allocated has failed if the fourth remaining space value is less than the first space threshold, and to issue a prompt that there is a memory overflow exception in the independent cache area.
[0215] Optionally, in the above Figure 13 Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application, the acquisition unit 202 can specifically be used for:
[0216] If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, then the independent cache region is scanned to determine the cache region object in the independent cache region that references the young generation region object;
[0217] Copy the cached region object to the young generation region, execute the young generation garbage collection mechanism, and obtain the second remaining space value of the independent cache region after collection.
[0218] Optionally, in the above Figure 13 Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application, the acquisition unit 202 can specifically be used for:
[0219] Treat the cached area object as a live object and copy the live object to the survivor area;
[0220] Scan the young generation region to identify live objects in the generation region;
[0221] Based on the young generation garbage collection mechanism, live objects in the generation area are copied to the survivor area, and the generation area is cleaned up.
[0222] After the generation area is cleaned up, the second remaining space value of the independent cache area is calculated.
[0223] Optionally, in the above Figure 13 Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application,
[0224] The processing unit 201 is also used to copy the live objects in the generation area and the cache area objects to the old generation area when the survivor area is insufficient to store the live objects in the generation area and the cache area objects.
[0225] The processing unit 201 is also used to trigger the recycling mechanism for old-age waste and to clean up the old-age areas.
[0226] Optionally, in the above Figure 13 Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application, the determining unit 203 may specifically be used for:
[0227] If the second remaining space value is less than the first space threshold, then each sub-cache region is scanned, and the live objects in each sub-cache region are marked to obtain the number of live objects marked for each sub-cache region.
[0228] The number of live object tags corresponding to each sub-cache region is compared with the liveness threshold. Sub-cache regions with a number of live object tags less than the liveness threshold are identified as the first sub-cache region, and sub-cache regions with a number of live object tags greater than the liveness threshold are identified as the second sub-cache region.
[0229] Optionally, in the above Figure 13Based on the corresponding embodiments, in another embodiment of the memory management method apparatus provided in this application, the determining unit 203 may specifically be used for:
[0230] If the comparison result shows that the used space value of the old generation region is greater than the second space threshold, then each sub-cache region is scanned, and the live objects in the sub-cache regions scanned in each sub-cache region are marked to obtain the number of live objects marked for each sub-cache region.
[0231] The number of live object tags corresponding to each sub-cache region is compared with the liveness threshold. Sub-cache regions with a number of live object tags less than the liveness threshold are identified as the first sub-cache region, and sub-cache regions with a number of live object tags greater than the liveness threshold are identified as the second sub-cache region.
[0232] This application also provides a schematic diagram of another computer device, such as... Figure 14 As shown, Figure 14 This is a schematic diagram of a computer device structure provided in an embodiment of this application. The computer device 300 can vary significantly due to different configurations or performance. It may include one or more central processing units (CPUs) 310 (e.g., one or more processors) and a memory 320, and one or more storage media 330 (e.g., one or more mass storage devices) for storing application programs 331 or data 332. The memory 320 and storage media 330 can be temporary or persistent storage. The program stored in the storage media 330 may include one or more modules (not shown in the diagram), each module including a series of instruction operations on the computer device 300. Furthermore, the CPU 310 may be configured to communicate with the storage media 330 and execute the series of instruction operations in the storage media 330 on the computer device 300.
[0233] Computer device 300 may also include one or more power supplies 340, one or more wired or wireless network interfaces 350, one or more input / output interfaces 360, and / or one or more operating systems 333, such as Windows Server. TM Mac OS X TM Unix TM Linux TM FreeBSD TM etc.
[0234] The aforementioned computer device 300 is also used to perform, for example Figures 2 to 10 The steps in the corresponding embodiments.
[0235] Another aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements... Figures 2 to 10 The steps in the method described in the illustrated embodiment.
[0236] Another aspect of this application provides a computer program product comprising a computer program, which, when executed by a processor, implements as follows: Figures 2 to 10 The steps in the method described in the illustrated embodiment.
[0237] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0238] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection between apparatuses or units through some interfaces, and may be electrical, mechanical, or other forms.
[0239] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0240] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0241] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
Claims
1. A memory management method, characterized in that, include: The target heap memory is divided into a young generation region, an old generation region, and an independent cache region, wherein the independent cache region includes a sub-cache region; If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, the young generation garbage collection mechanism is triggered, and the second remaining space value of the independent cache area is obtained after collection, wherein the second remaining space value is greater than or equal to the first remaining space value. If the second remaining space value is less than the first space threshold, then the independent cache region is scanned to determine the first sub-cache region where the number of live object marks in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live object marks in the sub-cache region is greater than the liveness threshold. Copy the sub-cache area or objects in the first sub-cache area to the second sub-cache area, and clean up the first sub-cache area; If the remaining space value of the first sub-cache area after cleaning is greater than or equal to the storage space value corresponding to the object to be allocated, then the object to be allocated will be allocated to the first sub-cache area after cleaning for caching. If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, the young generation garbage collection mechanism is triggered, and after obtaining the second remaining space value of the independent cache region after collection, the method further includes: If the second remaining space value is greater than the first space threshold, then the used space value of the old generation region is compared with the second space threshold to obtain the comparison result; If the comparison result is that the used space value of the old generation region is greater than the second space threshold, then the independent cache region is scanned to determine the first sub-cache region where the number of live object marks in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live object marks in the sub-cache region is greater than the liveness threshold. Copy the sub-cache area or objects in the first sub-cache area to the second sub-cache area, and clean up the first sub-cache area; If the remaining space value of the first sub-cache area after cleanup is greater than or equal to the storage space value corresponding to the object to be allocated, then the object to be allocated will be allocated to the first sub-cache area after cleanup for caching.
2. The method according to claim 1, characterized in that, After copying the live objects from the sub-cache area in the first sub-cache area to the second sub-cache area and clearing the first sub-cache area, if the cleared first sub-cache area is insufficient to store the objects to be allocated, the method further includes: Based on the cleaned first sub-cache region, a third remaining space value of the independent cache region is obtained, wherein the third remaining space value is greater than the second remaining space value; If the third remaining space value is less than the first space threshold, the overall garbage collection mechanism is triggered to clean up each of the sub-cache areas separately, and a fourth remaining space value of the independent cache area is obtained. If the fourth remaining space value is greater than the first space threshold, then the object to be allocated will be allocated to the cleaned independent cache area for caching.
3. The method according to claim 2, characterized in that, If the third remaining space value is less than the first space threshold, the overall garbage collection mechanism is triggered to clean up each of the sub-cache regions individually. After obtaining the fourth remaining space value of the independent cache region, the method further includes: If the fourth remaining space value is less than the first space threshold, it is determined that the allocation of the object to be allocated has failed, and a prompt is issued indicating that there is a memory overflow exception in the independent cache area.
4. The method according to claim 1, characterized in that, If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, then the young generation garbage collection mechanism is triggered, and after collection, the second remaining space value of the independent cache region is obtained, including: If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, then the independent cache region is scanned to determine the cache region object in the independent cache region that references the young generation region object; The cache region object is copied to the young generation region, the young generation garbage collection mechanism is executed, and the second remaining space value of the independent cache region is obtained after the collection.
5. The method according to claim 4, characterized in that, The young generation region includes a generation region and a survival region; the step of copying the cache region object to the young generation region, executing the young generation garbage collection mechanism, and obtaining the second remaining space value of the independent cache region after collection includes: The cached area object is treated as a live object, and the live object is copied to the survivor area; The young generation region is scanned to identify live objects in the generation region; Based on the young generation garbage collection mechanism, live objects in the generation area are copied to the survivor area, and the generation area is cleaned up. After the generation area is cleaned up, the second remaining space value of the independent cache area is calculated.
6. The method according to claim 5, characterized in that, After copying live objects from the generation area to the survivor area based on the young generation garbage collection mechanism, and cleaning up the generation area, the method further includes: When the survivor area is insufficient to store the live objects in the generation area and the cache area objects, the live objects in the generation area and the cache area objects are copied to the old generation area. Trigger the recycling mechanism for old-age waste to clean up the old-age area.
7. The method according to claim 1, characterized in that, If the second remaining space value is less than the first space threshold, then the independent cache region is scanned to determine a first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and a second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold, including: If the second remaining space value is less than the first space threshold, then each of the sub-cache regions is scanned, and the live objects in each sub-cache region are marked to obtain the number of live object marks corresponding to each sub-cache region. The number of live object tags corresponding to each sub-cache region is compared with the survival threshold, and the sub-cache region with the number of live object tags less than the survival threshold is determined as the first sub-cache region, and the sub-cache region with the number of live object tags greater than the survival threshold is determined as the second sub-cache region.
8. The method according to claim 1, characterized in that, If the comparison result indicates that the used space value of the old generation region is greater than the second space threshold, then the independent cache region is scanned to determine a first sub-cache region where the number of live objects in the sub-cache region is less than the liveness threshold, and a second sub-cache region where the number of live objects in the sub-cache region is greater than the liveness threshold, including: If the comparison result is that the used space value of the old generation region is greater than the second space threshold, then each of the sub-cache regions is scanned, and the live objects in the sub-cache regions scanned in each of the sub-cache regions are marked to obtain the number of live object marks corresponding to each sub-cache region; The number of live object tags corresponding to each sub-cache region is compared with the survival threshold, and the sub-cache region with the number of live object tags less than the survival threshold is determined as the first sub-cache region, and the sub-cache region with the number of live object tags greater than the survival threshold is determined as the second sub-cache region.
9. A memory management device, characterized in that, include: The processing unit is used to divide the target heap memory into a young generation region, an old generation region, and an independent cache region, wherein the independent cache region includes a sub-cache region; The acquisition unit is used to trigger the young generation garbage collection mechanism if the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache area, and to acquire the second remaining space value of the independent cache area after collection, wherein the second remaining space value is greater than or equal to the first remaining space value. The determining unit is configured to scan the independent cache region if the second remaining space value is less than the first space threshold, and determine a first sub-cache region where the number of live object tags in the sub-cache region is less than the liveness threshold, and a second sub-cache region where the number of live object tags in the sub-cache region is greater than the liveness threshold. The processing unit is used to copy the sub-cache area or object in the first sub-cache area to the second sub-cache area, and to clean up the first sub-cache area; The processing unit is further configured to allocate the object to be allocated to the cleaned first sub-cache area for caching; If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, the young generation garbage collection mechanism is triggered, and after obtaining the second remaining space value of the independent cache region after collection, the device further includes: The processing unit is further configured to compare the used space value of the old generation region with the second space threshold if the second remaining space value is greater than the first space threshold, and obtain a comparison result. The determining unit is further configured to scan the independent cache region if the comparison result is that the used space value of the old generation region is greater than the second space threshold, and determine the first sub-cache region where the number of live object tags in the sub-cache region is less than the liveness threshold, and the second sub-cache region where the number of live object tags in the sub-cache region is greater than the liveness threshold. The processing unit is further configured to copy the sub-cached area or object in the first sub-cached area to the second sub-cached area, and clean up the first sub-cached area; The processing unit is further configured to allocate the object to be allocated to the cleaned first sub-cached area for caching if the remaining space value of the cleaned first sub-cached area is greater than or equal to the storage space value corresponding to the object to be allocated.
10. The apparatus according to claim 9, characterized in that, After copying the live objects from the sub-cache area in the first sub-cache area to the second sub-cache area and clearing the first sub-cache area, if the cleared first sub-cache area is insufficient to store the objects to be allocated, the device further includes: The acquisition unit is further configured to acquire a third remaining space value of the independent cache region based on the cleaned first sub-cache region, wherein the third remaining space value is greater than the second remaining space value; The processing unit is further configured to trigger an overall garbage collection mechanism if the third remaining space value is less than the first space threshold, and to clean each of the sub-cache areas separately to obtain the fourth remaining space value of the independent cache area. If the fourth remaining space value is greater than the first space threshold, then the object to be allocated will be allocated to the cleaned independent cache area for caching.
11. The apparatus according to claim 10, characterized in that, The determining unit is further configured to determine that the allocation of the object to be allocated has failed if the fourth remaining space value is less than the first space threshold, and to issue a prompt that there is a memory overflow exception in the independent cache area.
12. The apparatus according to claim 9, characterized in that, The acquisition unit is specifically used for: If the storage space value corresponding to the object to be allocated is greater than the first remaining space value of the independent cache region, then the independent cache region is scanned to determine the cache region object in the independent cache region that references the young generation region object; The cache region object is copied to the young generation region, the young generation garbage collection mechanism is executed, and the second remaining space value of the independent cache region is obtained after the collection.
13. A computer device comprising a memory, a processor, and a bus system, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 8; The bus system is used to connect the memory and the processor to enable communication between the memory and the processor.
14. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 8.
15. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Generational garbage collector with persistent object cache
US20020099765A1
Prompt large object reclamation
US20110208792A1
