Memory management method, memory management device, electronic device and storage medium
By dividing the sub-regions of heap memory and stack memory in the Java virtual machine, and selecting some areas for concurrent recycling during global memory garbage collection, the full-region scanning problem in the initial marking stage of the Java virtual machine is solved, and the operation of the application is improved efficiency.
Patent Information
- Application Number
- CN202310167271.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-17
- Publication Date
- 2025-01-10
- Estimated Expiration
- 2043-02-17
AI Technical Summary
When Java virtual machines are performing memory management, the initial marking phase of recycling space will cause full-area scanning, causing application threads to be suspended, affecting the normal operation of the business.
Divide multiple first sub-regions in heap memory, divide multiple second sub-regions in stack memory, each second sub-regions map one by one with a first sub-regions, and select part when global memory garbage collection The second sub-region performs initial marking and concurrent recycling to avoid full-domain scanning.
Through concurrent recycling and concurrent reset, the problem of application threads waiting for global memory garbage collection in Java virtual machines is avoided, and the normal operation efficiency of the business is improved.
Smart Images

Figure CN116107702B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a memory management method, a memory management device, an electronic device and a storage medium. Background Art
[0002] In the Java virtual machine, the program continuously generates and recycles objects while running, and some objects become useless after use, so the program will recycle these objects to free up memory space.
[0003] Currently, when the Java virtual machine performs memory management, its object recycling mechanism causes the application thread to be suspended to make way for the thread that recycles the object, which affects the normal operation of the business. Specifically, when the Java virtual machine uses the G1 garbage collector to trigger global memory garbage collection, the heap memory will be scanned in the initial marking phase of garbage collection, which requires the application thread to be suspended, which seriously affects the normal operation of the business. Summary of the invention
[0004] The present application provides a memory management method, a memory management device, an electronic device and a storage medium, aiming to solve the problem in the prior art that when a Java virtual machine performs memory management, the initial marking phase of the reclaimed space will perform a full area scan on the heap memory, resulting in the need to suspend the application thread.
[0005] In a first aspect of the present application, a memory management method for a Java virtual machine is proposed, the memory management method comprising the following steps:
[0006] Divide a plurality of first sub-regions in the heap memory, divide a plurality of second sub-regions in the stack memory, and each of the second sub-regions is mapped one-to-one with one of the first sub-regions;
[0007] When global memory garbage collection is triggered, a portion of the second sub-region is selected in the stack memory for initial marking to determine a target second sub-region, and concurrent collection and concurrent reset are performed on the first sub-region corresponding to the target second sub-region.
[0008] Furthermore, the first sub-area includes a first regular area and a first large area, the first regular area is used to store regular objects, the second large area is used to store large objects, and the second sub-area includes a second regular area and a second large area that are mapped one-to-one with the first regular area and the first large area; wherein the heap memory space occupied by the regular objects is within a threshold, and the heap memory space occupied by the large objects exceeds the threshold.
[0009] Furthermore, the step of selecting a portion of the second sub-region in the stack memory for initial marking to determine a target second sub-region, and concurrently recovering and concurrently resetting the first sub-region corresponding to the target second sub-region includes:
[0010] Selecting the second largest area in the stack memory for initial marking to determine the target second largest area;
[0011] Concurrently marking the first largest area corresponding to the target second largest area to determine the target first largest area;
[0012] Concurrently recovering and concurrently resetting the target first large area;
[0013] It is determined whether the space recovery of the heap memory reaches a predetermined ratio, and when the predetermined ratio is reached, it is determined that the global memory garbage recovery is completed.
[0014] Further, when it is determined that the space recovery of the heap memory does not reach the predetermined ratio, the step of selecting a portion of the second sub-region in the stack memory for initial marking to determine a target second sub-region, and concurrently recovering and concurrently resetting the first sub-region corresponding to the target second sub-region also includes:
[0015] Selecting a preset number of the second regular areas in the stack memory for initial marking to determine a target second regular area;
[0016] Concurrently marking the first conventional area corresponding to the target second conventional area to determine the target first conventional area;
[0017] The target first regular area is concurrently reclaimed and concurrently reset.
[0018] Furthermore, the step of concurrently marking the first largest area corresponding to the target second largest area to determine the target first largest area includes:
[0019] Setting the target first largest area to a read-only state;
[0020] After the steps of concurrently reclaiming and concurrently resetting the target first large area, the memory management method further includes:
[0021] The target first large area is set to a readable and writable state.
[0022] Furthermore, the step of concurrently reclaiming and concurrently resetting the first sub-region corresponding to the target second sub-region includes:
[0023] The first sub-area corresponding to the target second sub-area is sorted by recycling value, and concurrent recycling is performed in descending order of recycling value.
[0024] In a second aspect of the present application, a memory management device for a Java virtual machine is provided, the memory management device comprising:
[0025] A partitioning module, used for partitioning a plurality of first sub-regions in the heap memory and a plurality of second sub-regions in the stack memory, wherein each of the second sub-regions is mapped one-to-one with one of the first sub-regions;
[0026] A processing module is used to select a part of the second sub-area in the stack memory for initial marking to determine the target second sub-area when triggering global memory garbage collection, and concurrently recycle and concurrently reset the first sub-area corresponding to the target second sub-area.
[0027] Furthermore, the first sub-area includes a first regular area and a first large area, the first regular area is used to store regular objects, the second large area is used to store large objects, and the second sub-area includes a second regular area and a second large area that are mapped one-to-one with the first regular area and the first large area; wherein the heap memory space occupied by the regular objects is within a threshold, and the heap memory space occupied by the large objects exceeds the threshold.
[0028] In a third aspect of the present application, an electronic device is proposed, comprising a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the memory management method proposed in the first aspect of the present application.
[0029] In a fourth aspect of the present application, a storage medium is further proposed, on which program instructions are stored, and the program instructions are used to execute the memory management method proposed in the first aspect of the present application.
[0030] The present application has the following beneficial effects: since the heap memory is divided into multiple first sub-areas, and the stack memory is divided into multiple second sub-areas, and each second sub-area corresponds to a first sub-area, then in the case of triggering global memory garbage collection, the target second sub-area can be determined by selecting part of the second sub-areas for initial marking, and concurrent recovery and concurrent reset can be performed on the first sub-area corresponding to the target second sub-area to complete the global memory garbage collection process.
[0031] At this time, at the beginning of the initial marking phase, the first sub-area corresponding to the second sub-area outside the target second sub-area can normally apply for new objects and other tasks, thereby avoiding the traditional solution of performing a full-domain scan of the heap memory for initial marking, that is, avoiding the problem that the application thread in the Java virtual machine needs to wait for the global memory garbage collection to complete before continuing execution. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 A flowchart of a memory management method according to an embodiment of the present application;
[0033] Figure 2 A schematic diagram of a module of a memory management device according to an embodiment of the present application;
[0034] Figure 3 A schematic diagram of a module of an electronic device according to an embodiment of the present application;
[0035] Figure 4 This is a schematic diagram of the division of heap memory and stack memory in an embodiment of the present application;
[0036] Figure 5 A schematic diagram of a process for triggering global memory garbage collection according to an embodiment of the present application;
[0037] Figure 6 This is another schematic diagram of the division of heap memory and stack memory in an embodiment of the present application;
[0038] Figure 7 A flowchart of a memory management method according to an embodiment of the present application;
[0039] Figure 8 A flowchart of a memory management method according to an embodiment of the present application;
[0040] Fig. 9 A flowchart of the memory management method of the present application embodiment
[0041] Fig.10 A flowchart of the memory management method of the present application embodiment
[0042] Fig.11 A flowchart of a memory management method according to an embodiment of the present application.
[0043] Common components and symbols:
[0044] Electronic device 100, memory 11, processor 12;
[0045] Memory management device 200 , partition module 21 , and processing module 22 . DETAILED DESCRIPTION
[0046] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present application, and cannot be understood as limiting the present application.
[0047] In the description of this application, unless otherwise clearly defined, words such as setting and connecting should be understood in a broad sense, and technicians in the relevant technical field can reasonably determine the specific meanings of the above words in this application based on the specific content of the technical solution.
[0048] See also Figure 1 The first aspect of the present application provides a memory management method for a Java virtual machine, the memory management method comprising the following steps:
[0049] S10: dividing a plurality of first sub-regions in the heap memory and a plurality of second sub-regions in the stack memory, wherein each second sub-region is mapped one-to-one with a first sub-region;
[0050] S20: When global memory garbage collection is triggered, a portion of the second sub-region is selected in the stack memory for initial marking to determine a target second sub-region, and concurrent collection and concurrent reset are performed on the first sub-region corresponding to the target second sub-region.
[0051] See also Figure 2 In the second aspect of the present application, a memory management device 200 for a Java virtual machine is proposed, comprising a partitioning module 21 and a processing module 22. The memory management method provided by the present application can be implemented by the memory management device 200. Specifically, the partitioning module 21 is used to partition a plurality of first sub-regions in the heap memory and a plurality of second sub-regions in the stack memory, and each second sub-region is mapped one-to-one with a first sub-region; the processing module 22 is used to select a portion of the second sub-regions in the stack memory for initial marking to determine the target second sub-region when triggering global memory garbage collection, and concurrently recycle and concurrently reset the first sub-region corresponding to the target second sub-region.
[0052] See also Figure 3 The third aspect of the present application proposes an electronic device 100, which includes a memory 11 and a processor 12, wherein the memory 11 stores a computer program, and the processor 12 executes the computer program to divide the heap memory into multiple first sub-areas, divide the stack memory into multiple second sub-areas, each second sub-area is mapped one-to-one with a first sub-area, and is used to select part of the second sub-areas in the stack memory for initial marking to determine the target second sub-area when triggering global memory garbage collection, and concurrently recycle and concurrently reset the first sub-area corresponding to the target second sub-area.
[0053] In this way, since the heap memory is divided into multiple first sub-areas, and the stack memory is divided into multiple second sub-areas, and each second sub-area corresponds to a first sub-area, then when the global memory garbage collection is triggered, the target second sub-area can be determined by selecting part of the second sub-areas for initial marking, and concurrent recovery and concurrent reset can be performed on the first sub-area corresponding to the target second sub-area to complete the global memory garbage collection process.
[0054] At this time, at the beginning of the initial marking phase, the first sub-area corresponding to the second sub-area outside the target second sub-area can normally apply for new objects and other tasks, thereby avoiding the traditional solution of performing a full-domain scan of the heap memory for initial marking, that is, avoiding the problem that the application thread in the Java virtual machine needs to wait for the global memory garbage collection to complete before continuing execution.
[0055] Specifically, it should be noted that the Java Virtual Machine (JVM), hereinafter referred to as JVM, has instructions for allocating memory but not for releasing memory. The JVM itself is responsible for deciding how and when to release memory occupied by objects that are no longer referenced by the application system.
[0056] Among them, memory management in JVM is essentially implemented through different garbage collectors. Garbage collectors are system threads that run according to garbage collection algorithms. At present, the main memory garbage collection (GC) algorithms generally include mark-sweep algorithm, copy algorithm, mark-compact algorithm, etc.
[0057] However, no matter which algorithm is used, when the JVM is doing global memory garbage collection, there is a problem that only the memory garbage collection thread is working, and all other threads in the application will be suspended. In other words, the system will temporarily stop responding to external requests, causing the system to "pseudo-death".
[0058] JVM memory is divided into method area, stack, heap, register, etc., among which the heap occupies the largest memory space, which can be as high as 70%. When JVM performs memory garbage collection, it mainly targets the "heap" part of the memory. The heap can be divided into "new generation", "old generation" and "metaspace". Most objects are recycled in the "new generation", while some objects that have not been recycled for many times in the "new generation" will be transferred to the "old generation". When the memory usage of the "old generation" reaches a certain proportion (the proportion can be configured, for example, configured at 80%), then JVM will trigger FULL GC, that is, global memory garbage collection. When FULL GC is performed, all current garbage collectors need to suspend application threads more or less. During this period, all threads are suspended waiting for garbage collection, and the program is stuck, which seriously affects the user thread business.
[0059] For example, in G1, a widely used garbage collector for JVM, G1 divides the entire heap memory into several areas, each of which can be designated as "new generation" or "old generation" according to the nature of the memory. When recycling objects, the recycling of objects is usually divided into multiple stages, such as initial marking, concurrent marking, re-marking, filter recycling, and concurrent re-making.
[0060] G1 has the problem of suspending application threads during the initial marking and re-marking stages of object recycling. This is because when a region is recycled, it is necessary to globally scan all applications that can serve as GC roots, causing user threads to suspend and affect user thread business.
[0061] In view of this, the present application proposes a memory management method to solve the problem that the garbage collection thread and the application thread cannot be completely parallel when the JVM triggers global memory garbage collection.
[0062] Specifically, the electronic device 100 in the present application can be a desktop computer, a laptop computer, a tablet computer, a mobile terminal, etc.
[0063] In step S10, dividing the heap memory into a plurality of first sub-regions can be understood as logically splitting the entire heap into a plurality of regions, wherein the first sub-regions can be used to store objects to facilitate block recovery of objects.
[0064] In addition, multiple second sub-areas are divided in the stack memory, and each second sub-area has a one-to-one mapping relationship with a first sub-area. In this way, the complete stack is split into multiple areas corresponding to the heap, which facilitates the block recovery of objects.
[0065] It should be noted that the first sub-area and the second sub-area are both logically independent spaces and can be recycled separately.
[0066] like Figure 4As shown, Figure 4 Region1...RegionN in the stack are second sub-regions 1...second sub-regions N, and Region1...N and BigRegion are different types of second sub-regions; Region1...RegionN in the heap are first sub-regions 1...first sub-regions N, and Region1...N and BigRegion are different types of first sub-regions. The dotted line indicates that each second sub-region has a one-to-one mapping relationship with a first sub-region.
[0067] In step S20, in one embodiment, the portion may be 1 / 3 of the total quantity, and may be another quantity, depending on the actual needs of the program design.
[0068] When the JVM triggers global memory garbage collection, the processor 12 may select 1 / 3 of the second sub-region in the stack memory for initial marking to determine a target second sub-region, where the target second sub-region is the second sub-region of the initially marked portion.
[0069] Since the second sub-region divided in the stack memory is mapped one-to-one with the first sub-region divided in the heap memory, the processor 12 can find the first sub-region corresponding to the target second sub-region for concurrent recycling and concurrent reset.
[0070] It can be understood that in order to perform concurrent recycling and concurrent reset, the processor 12 concurrently marks the first sub-region corresponding to the target second sub-region. The concurrently marked first sub-region contains marked recyclable objects. Concurrent recycling is to perform garbage collection on the above recyclable objects.
[0071] In addition, the initial marking, also known as the GC Root marking, marks the GC Root in the target second sub-area; after concurrent marking, the recyclable objects can be marked in the corresponding first sub-area according to the marked GC Root. In the initial marking stage, since only part of the second sub-area in the stack memory is selected for initial marking, rather than scanning all the second sub-areas in the stack memory to mark the GC Root, there is no need to suspend the user thread at this time, and the program can apply for space normally in the unselected second sub-area and the first sub-area corresponding to the unselected second sub-area.
[0072] It can be understood that before the concurrent reset, the first sub-area that is concurrently recovered terminates the application of the new object due to the concurrent mark; therefore, the processor 12 can perform a concurrent reset on the first sub-area that is concurrently recovered, and after the reset, the first sub-area corresponding to the target second sub-area is reopened and can be used normally by the program.
[0073] So, please combine Figure 5By using a smaller granularity when searching for GC Root, that is, when performing initial marking, that is, using the second sub-area of the selected part to replace the global search stack memory, it is ensured that the application thread and the global memory garbage collection thread are always executed in parallel, and the user thread will not be blocked in any step of the garbage collection.
[0074] See also Figure 4 and Figure 6 In some embodiments, the first sub-area includes a first regular area and a first large area, the first regular area is used to store regular objects, the second large area is used to store large objects, and the second sub-area includes a second regular area and a second large area that are mapped one-to-one with the first regular area and the first large area; wherein the heap memory space occupied by regular objects is within a threshold, and the heap memory space occupied by large objects exceeds the threshold.
[0075] In this way, by further subdividing the first sub-area and the second sub-area, regular objects that occupy a small space and large objects that occupy a large space are stored separately, thereby facilitating improving the efficiency of garbage collection.
[0076] Specifically, Figure 4 As shown, Figure 4 Region1...RegionN in the stack are the second regular region 1...second regular region N, BigRegion is the second largest region, and there may be multiple BigRegions in the stack; Region1...RegionN in the heap are the first regular region 1...first regular region N, BigRegion is the first largest region, and there may be multiple BigRegions in the heap.
[0077] Figure 6 In a first sub-area, multiple objects can be stored, for example, R0...Rn can be object 0, R10...R1n can be object 1, and so on; a second sub-area can store stack needles corresponding to these objects, such as R0...Rn can be stack needle 0 of object 0, R10...R1n can be stack needle 1 of object 1.
[0078] Corresponding to the division of the large area and the regular area, the second largest area and the second regular area store the stack needles of the large object and the stack needles of the regular object respectively. The heap memory space occupied by the regular object is within the threshold, which means that the heap memory space occupied by the regular object is less than or equal to the threshold; the heap memory space occupied by the large object exceeds the threshold, which means that the heap memory space occupied by the large object is greater than the threshold.
[0079] In one embodiment, when a program creates an object, it first creates a stack pointer in the second sub-area of the stack memory, and then applies for a space in the corresponding first sub-area of the heap memory for data storage. At this time, if the object is too large, it is directly included in the large object and stored in the first large area, and its stack pointer is also stored in the second large area of the stack. In this way, separating large objects is mainly to facilitate object recycling, so that more space can be recycled under the same time complexity.
[0080] Furthermore, in the case of memory partitioning provided in this application, the detailed steps of allocating space for an object when a thread in a program creates an object are as follows:
[0081] First, determine whether the thread already exists. If so, directly index the corresponding second sub-area of the stack memory. If not, determine whether it is necessary to apply for a new second sub-area. Then, create a stack needle for the thread in the corresponding second sub-area, and find the reference of the corresponding first sub-area in the heap memory of the corresponding second sub-area according to the refSet in the stack memory.
[0082] Then determine whether the object is a large object. If it is a large object, store it in the first large area. Otherwise, directly open up a new space in the corresponding first sub-area, create an object and write data to the first sub-area of the corresponding heap memory. At the same time, the object reference is pushed into the second sub-area of the stack memory. If the object is a regular object, it is pushed into the second regular area. If it is a large object, it is pushed into the second large area.
[0083] See also Figure 7 and Figure 8 , a step (S20) of selecting a portion of the second sub-region in the stack memory for initial marking to determine the target second sub-region, and concurrently recovering and concurrently resetting the first sub-region corresponding to the target second sub-region, including:
[0084] S21: Select the second largest area in the stack memory for initial marking to determine the target second largest area;
[0085] S22: Concurrently mark the first largest area corresponding to the second largest target area to determine the first largest target area;
[0086] S23: concurrently reclaim and concurrently reset the first largest target area;
[0087] S24: Determine whether the space recovery of the heap memory reaches a predetermined ratio, and determine that the global memory garbage collection is completed if the predetermined ratio is reached.
[0088] In some embodiments, the processing module 22 is used to select the second largest area in the stack memory for initial marking to determine the target second largest area; and to concurrently mark the first largest area corresponding to the target second largest area to determine the target first largest area; and to concurrently recycle and concurrently reset the target first largest area; and to determine whether the space recovery of the heap memory reaches a predetermined proportion, and determine that the global memory garbage collection is completed when the predetermined proportion is reached.
[0089] In some embodiments, the processor 12 is used to select the second largest area in the stack memory for initial marking to determine the target second largest area; and to concurrently mark the first largest area corresponding to the target second largest area to determine the target first largest area; and to concurrently recycle and concurrently reset the target first largest area; and to determine whether the space recovery of the heap memory reaches a predetermined proportion, and determine that the global memory garbage collection is completed when the predetermined proportion is reached.
[0090] In this way, by implementing priority marking and recycling reset for large objects, more space can be reclaimed within the effective time period, thereby improving recycling efficiency.
[0091] Specifically, in one embodiment, in steps S21-S24, the target second largest area may be the entire second largest area in the stack memory, and the second largest area is initially marked to determine the target second largest area. Since the second largest area divided in the stack memory is mapped one-to-one with the first largest area divided in the heap memory, the processor 12 can concurrently mark the first largest area corresponding to the target second largest area to determine the target first largest area, and then concurrently recycle and concurrently reset the target first largest area.
[0092] It can be understood that in order to perform concurrent recycling and concurrent reset, the processor 12 concurrently marks the first largest area corresponding to the target second largest area to obtain the target first largest area, and the target first largest area contains marked recyclable large objects. Concurrent recycling is to perform garbage collection on the above-mentioned recyclable large objects, and concurrent reset is to reopen the target first largest area that has been recycled for use.
[0093] Then, it is determined whether the space recovery of the heap memory reaches a predetermined ratio, and the global memory garbage collection is completed when the predetermined ratio is reached. The predetermined ratio can be 50%, 60%, 70%, 80%, etc., which can be determined according to actual usage requirements.
[0094] In this way, large objects are marked first to achieve recycling of large objects, reclaim more space within the effective time period, and improve recycling efficiency.
[0095] See also Figure 7 and Fig. 9, when it is determined that the space recovery of the heap memory does not reach the predetermined ratio, a step (S20) of selecting a portion of the second sub-region in the stack memory for initial marking to determine the target second sub-region, and concurrently recovering and concurrently resetting the first sub-region corresponding to the target second sub-region, further includes:
[0096] S25: selecting a preset number of second regular areas in the stack memory for initial marking to determine a target second regular area;
[0097] S26: Concurrently marking the first regular area corresponding to the target second regular area to determine the target first regular area;
[0098] S27: Concurrently reclaim and concurrently reset the target first regular area.
[0099] In some embodiments, the processing module 22 is used to select a preset number of second conventional areas in the stack memory for initial marking to determine the target second conventional area; and to concurrently mark the first conventional area corresponding to the target second conventional area to determine the target first conventional area; and to concurrently recycle and concurrently reset the target first conventional area.
[0100] In some embodiments, the processor 12 is used to select a preset number of second conventional areas in the stack memory for initial marking to determine the target second conventional area; and to concurrently mark the first conventional area corresponding to the target second conventional area to determine the target first conventional area; and to concurrently recycle and concurrently reset the target first conventional area.
[0101] In this way, when recycling only large objects does not meet the heap memory space recycling requirements, regular objects are recycled to improve recycling efficiency.
[0102] Specifically, in one embodiment, in steps S25-S27, the preset number may be 1 / 3 of the second regular areas, and the second regular areas are initially marked to determine the target second regular areas. Since the second regular areas divided in the stack memory are mapped one-to-one to the first regular areas divided in the heap memory, the processor 12 may concurrently mark the first regular areas corresponding to the target second regular areas to determine the target first regular areas, and then concurrently recycle and concurrently reset the target first regular areas.
[0103] It can be understood that in order to perform concurrent recycling and concurrent reset, the processor 12 concurrently marks the first regular area corresponding to the target second regular area to obtain the target first regular area, and the target first regular area contains marked recyclable regular objects. Concurrent recycling is to perform garbage collection on the above-mentioned recyclable regular objects, and concurrent reset is to reopen the target first regular area after recycling.
[0104] See also Fig.10 In some implementations, the step of concurrently marking the first largest area corresponding to the target second largest area to determine the target first largest area (step S22) includes:
[0105] S220: setting the target first largest area to a read-only state;
[0106] After the step (S23) of concurrently reclaiming and concurrently resetting the target first largest area, the memory management method further includes:
[0107] S230: Setting the target first largest area to a readable and writable state.
[0108] In some implementations, the processing module 22 is used to set the target first large area to a read-only state, and is used to set the target first large area to a readable and writable state.
[0109] In some implementations, the processor 12 is configured to set the target first large area to a read-only state, and to set the target first large area to a readable and writable state.
[0110] Specifically, in one scenario, when concurrent marking determines the target largest area, the target largest area is set to read-only state and does not provide external write operations. If the program needs to create a large object at this time, it will apply for it in another first largest area. The target largest area will not provide external services again until the target largest area is concurrently recycled and reset, and the target largest area is set to read-write state.
[0111] See also Fig.11 In some embodiments, the step of concurrently reclaiming and concurrently resetting the first sub-region corresponding to the target second sub-region (step S20) includes:
[0112] S28: sorting the first sub-regions corresponding to the target second sub-region by recycling value, and performing concurrent recycling in descending order of recycling value.
[0113] In some embodiments, the processing module 22 may be used to sort the first sub-regions corresponding to the target second sub-regions by recycling value, and perform concurrent recycling in descending order of recycling value.
[0114] In some embodiments, the processor 12 may be configured to sort the first sub-regions corresponding to the target second sub-region by recycling value, and perform concurrent recycling in descending order of recycling value.
[0115] In this way, the efficiency of memory garbage collection can be optimized.
[0116] Specifically, it can be seen from the above that there are multiple first sub-regions corresponding to the target second sub-region. In order to optimize the efficiency of memory garbage collection, the first sub-region with high value can be preferentially recycled in step S28. The recycling value can be determined according to the number of objects to be recycled in the first sub-region, the importance of the threads corresponding to the objects to be recycled, etc.
[0117] In a fourth aspect of the present application, a storage medium is further proposed, on which program instructions are stored, and the program instructions are used to execute the memory management method proposed in the first aspect of the present application.
[0118] The above is a description of a specific embodiment of the present specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0119] Program instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate form. Storage media include: any entity or device capable of carrying computer program codes, recording media, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signals, telecommunication signals, and software distribution media. It should be noted that the content contained in the storage medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, storage media do not include electric carrier signals and telecommunication signals.
[0120] It should be noted that, for the convenience of description, the aforementioned method embodiments are all described as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described action sequence, because according to the present application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0121] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0122] The embodiments of the present application are described in detail above in conjunction with the accompanying drawings, but the present application is not limited to the above embodiments, and various changes can be made within the knowledge scope of ordinary technicians in the technical field without departing from the purpose of the present application.
Claims
1. A memory management method for a Java virtual machine, characterized in that: The memory management method comprises the following steps: Divide a plurality of first sub-regions in the heap memory, divide a plurality of second sub-regions in the stack memory, and each of the second sub-regions is mapped one-to-one with one of the first sub-regions; When global memory garbage collection is triggered, a portion of the second sub-region is selected in the stack memory for initial marking to determine a target second sub-region, and concurrent collection and concurrent reset are performed on the first sub-region corresponding to the target second sub-region.
2. The memory management method according to claim 1, characterized in that: The first sub-area includes a first regular area and a first large area, the first regular area is used to store regular objects, the first large area is used to store large objects, and the second sub-area includes a second regular area and a second large area that are mapped one-to-one with the first regular area and the first large area; wherein the heap memory space occupied by the regular objects is within a threshold, and the heap memory space occupied by the large objects exceeds the threshold.
3. The memory management method according to claim 2, characterized in that: The step of selecting a portion of the second sub-region in the stack memory to perform initial marking to determine a target second sub-region, and concurrently recovering and concurrently resetting the first sub-region corresponding to the target second sub-region includes: Selecting the second largest area in the stack memory for initial marking to determine the target second largest area; Concurrently marking the first largest area corresponding to the target second largest area to determine the target first largest area; Concurrently recovering and concurrently resetting the target first large area; It is determined whether the space recovery of the heap memory reaches a predetermined ratio, and when the predetermined ratio is reached, it is determined that the global memory garbage recovery is completed.
4. The memory management method according to claim 3, characterized in that: When it is determined that the space recovery of the heap memory does not reach the predetermined ratio, the step of selecting a portion of the second sub-region in the stack memory for initial marking to determine a target second sub-region, and concurrently recovering and concurrently resetting the first sub-region corresponding to the target second sub-region also includes: Selecting a preset number of the second regular areas in the stack memory for initial marking to determine a target second regular area; Concurrently marking the first conventional area corresponding to the target second conventional area to determine the target first conventional area; The target first regular area is concurrently reclaimed and concurrently reset.
5. The memory management method according to claim 3, characterized in that: The step of concurrently marking the first large area corresponding to the target second large area to determine the target first large area includes: Setting the target first largest area to a read-only state; After the steps of concurrently reclaiming and concurrently resetting the target first large area, the memory management method further includes: The target first large area is set to a readable and writable state.
6. The memory management method according to claim 1, characterized in that: The step of concurrently reclaiming and concurrently resetting the first sub-area corresponding to the target second sub-area includes: The first sub-area corresponding to the target second sub-area is sorted by recycling value, and concurrent recycling is performed in descending order of recycling value.
7. A memory management device for a Java virtual machine, characterized in that: The memory management device comprises: A partitioning module, used for partitioning a plurality of first sub-regions in the heap memory and a plurality of second sub-regions in the stack memory, wherein each of the second sub-regions is mapped one-to-one with one of the first sub-regions; A processing module is used to select a part of the second sub-area in the stack memory for initial marking to determine the target second sub-area when triggering global memory garbage collection, and concurrently recycle and concurrently reset the first sub-area corresponding to the target second sub-area.
8. The memory management device according to claim 7, characterized in that: The first sub-area includes a first regular area and a first large area, the first regular area is used to store regular objects, the first large area is used to store large objects, and the second sub-area includes a second regular area and a second large area that are mapped one-to-one with the first regular area and the first large area; wherein the heap memory space occupied by the regular objects is within a threshold, and the heap memory space occupied by the large objects exceeds the threshold.
9. An electronic device, characterized in that: The electronic device comprises a memory and a processor, the memory stores a computer program, and the processor executes the computer program to implement the memory management method according to any one of claims 1 to 6.
10. A storage medium, characterized in that: The storage medium stores program instructions, and the program instructions are used to execute the memory management method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Resource recovery method and apparatus
CN108459898A