Efficient garbage collection method based on Java virtual machine
By optimizing the heap space of Java virtual machines in generational garbage collection, the problems of low garbage collection efficiency and memory fragmentation in the existing technology are solved, and more efficient memory management and resource utilization are achieved.
Patent Information
- Application Number
- CN202510189889.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-20
- Publication Date
- 2025-06-13
AI Technical Summary
Existing garbage collection algorithms are inefficient in embedded devices, have long system pauses, and are prone to memory fragmentation, affecting the efficiency of memory management.
By performing generational garbage collection optimization on the heap space of Java virtual machines, the permanent zone, the old generation and the new generation are divided, the old generation is dynamically adjusted, and the incremental collection method and the full collection strategy are adopted to avoid the occurrence of memory fragmentation.
It improves the efficiency of garbage collection, reduces system pause time, maximizes the use of memory space, avoids the generation of memory fragmentation, and improves overall performance and resource utilization.
Smart Images

Figure CN120144485A_ABST
Abstract
Description
Technical Field
[0001] The present invention discloses an efficient garbage collection method based on the Java virtual machine, which relates to the technical field of memory management. Background Art
[0002] The basic principle of garbage collection (GC) is to collect the memory space occupied by useless objects for redistribution. The three most basic GC algorithms are: reference counting method, node copying method, and mark and sweep method. They each have their deficiencies: the reference counting method cannot detect circular references, which may lead to memory leaks, and the change of object references requires an operation of incrementing or decrementing by 1, bringing additional overhead to the system; the node copying algorithm wastes half of the heap space; the mark and sweep algorithm starts from the root object and marks the objects in the heap space. All reachable objects are marked as live objects, and the rest are garbage objects. This algorithm has a relatively high efficiency for garbage collection, but it will generate memory fragmentation, which is not conducive to management. Summary of the Invention
[0003] In view of the problems of the prior art, the present invention provides an efficient garbage collection method based on the Java virtual machine, which optimizes the existing generational garbage collection algorithm, improves the garbage collection efficiency in embedded devices, reduces the system pause time, and makes full use of memory resources.
[0004] The specific solution proposed by the present invention is as follows:
[0005] The present invention provides an efficient garbage collection method based on the Java virtual machine, including:
[0006] Step 1: Based on the Java virtual machine, the heap space is sequentially divided into a permanent generation area, an old generation area, and a young generation area. No free space in the old generation is reserved for the permanent generation area. Each time a young generation garbage collection is performed, the length of the old generation is dynamically increased, and the remaining space in the young generation is used as the young generation.
[0007] Step 2: Starting from the root object set, the heap space is scanned, the reachable objects are marked as live objects, and all the live objects in the young generation are copied to the old generation and tightly connected to the end of the old generation. When the space in the old generation is insufficient, the garbage collection of the old generation is started, and all the live objects in the old generation are compressed to the head of the old generation.
[0008] Step 3: When the length of the young generation is less than a preset threshold, a full collection of the entire heap space is performed. During the full collection, the live objects in the heap space are copied and compressed to avoid the generation of memory fragmentation.
[0009] Further, in step 2 of the efficient garbage collection method based on the Java virtual machine, starting the garbage collection of the old generation includes: adopting an incremental collection method to collect the old generation in blocks to avoid the long system pause caused by full collection.
[0010] Further, in step 3 of the efficient garbage collection method based on the Java virtual machine, the threshold for determining the length of the young generation is 1 / 6 of the heap space. When the length of the young generation is less than 1 / 6 of the heap space, a full collection of the entire heap space is performed.
[0011] The present invention also provides an efficient garbage collection device based on the Java virtual machine, including a partitioning module and a garbage collection module.
[0012] Based on the Java virtual machine, the partitioning module sequentially partitions the heap space into a permanent generation, an old generation, and a young generation, without reserving any free space for the old generation in the permanent generation. Each time the garbage collection module performs garbage collection on the young generation, it dynamically increases the length of the old generation and uses the remaining space in the young generation as the young generation.
[0013] Starting from the root object set, the garbage collection module scans the heap space, marks reachable objects as live objects, copies all live objects in the young generation to the old generation, and tightly connects them to the tail of the old generation. When the space in the old generation is insufficient, it starts the garbage collection of the old generation and compresses all live objects in the old generation to the head of the old generation.
[0014] When the length of the young generation is less than the preset threshold, the garbage collection module performs a full collection on the entire heap space. During the full collection, it copies and compresses the live objects in the heap space to avoid the generation of memory fragmentation.
[0015] Further, when the garbage collection module of the efficient garbage collection device based on the Java virtual machine starts the garbage collection of the old generation, it includes: adopting an incremental collection method to collect the old generation in blocks to avoid the long system pause caused by full collection.
[0016] Further, the threshold for the garbage collection module of the efficient garbage collection device based on the Java virtual machine to determine the length of the young generation is 1 / 6 of the heap space. When the length of the young generation is less than 1 / 6 of the heap space, a full collection of the entire heap space is performed.
[0017] The present invention also provides an efficient garbage collection device based on the Java virtual machine, including: at least one memory and at least one processor;
[0018] The at least one memory is used to store machine-readable programs.
[0019] The at least one processor is configured to call the machine-readable program and execute any one of the efficient garbage collection methods based on the Java virtual machine.
[0020] The present invention also provides a computer-readable medium having computer instructions stored thereon, which, when executed by a processor, cause the processor to execute the efficient garbage collection method based on the Java virtual machine.
[0021] The advantages of the present invention are as follows:
[0022] The efficiency of garbage collection is improved, and the system pause time is reduced.
[0023] Through dynamic generational and incremental collection, the memory space is utilized to the maximum extent, the number of collections in the old generation is reduced, and the overall performance is improved.
[0024] The generation of memory fragmentation is avoided, and the system runs more stably.
[0025] The resource utilization rate of embedded devices is improved, and it is applicable to resource-constrained embedded Java environments. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 It is a schematic diagram of the process of partitioning the heap space in the present invention.
[0027] Figure 2 It is a schematic diagram of dynamic generational garbage collection within the partition in the present invention.
[0028] Figure 3 It is a schematic diagram of full collection within the partition in the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0029] The present invention will be further described below with reference to the accompanying drawings and specific embodiments, so that those skilled in the art can better understand the present invention and implement it, but the embodiments given are not intended to limit the present invention.
[0030] Embodiment 1
[0031] The present invention provides an efficient garbage collection method based on the Java virtual machine, including:
[0032] Step 1: Based on the Java virtual machine, the heap space is sequentially divided into a permanent generation area, an old generation area, and a young generation area. No free space in the old generation is reserved for the permanent generation area. Each time a young generation garbage collection is performed, the length of the old generation is dynamically increased, and the remaining space in the young generation is used as the young generation.
[0033] Step 2: Starting from the root object set, scan the heap space, mark reachable objects as live objects, copy all live objects in the young generation to the old generation, and tightly connect them to the end of the old generation. When the space in the old generation is insufficient, initiate garbage collection in the old generation and compress all live objects in the old generation to the head of the old generation.
[0034] Among them, initiating garbage collection in the old generation in Step 2 includes: adopting an incremental collection method to collect the old generation in blocks to avoid long system pauses caused by full collection.
[0035] Step 3: When the length of the young generation is less than the preset threshold, perform a full collection on the entire heap space. During the full collection, copy and compress the live objects in the heap space to avoid the generation of memory fragmentation.
[0036] Among them, the threshold for determining the length of the young generation in Step 3 is 1 / 6 of the heap space. When the length of the young generation is less than 1 / 6 of the heap space, perform a full collection on the entire heap space.
[0037] Embodiment 2
[0038] The present invention also provides an efficient garbage collection device based on a Java virtual machine, including a partitioning module and a garbage collection module.
[0039] Based on the Java virtual machine, the partitioning module sequentially partitions the heap space into a permanent area, an old generation, and a young generation, without reserving any free space in the old generation for the permanent area. Each time the garbage collection module performs garbage collection in the young generation, it dynamically increases the length of the old generation and uses the remaining space in the young generation as the young generation.
[0040] Starting from the root object set, the garbage collection module scans the heap space, marks reachable objects as live objects, copies all live objects in the young generation to the old generation, and tightly connects them to the end of the old generation. When the space in the old generation is insufficient, initiate garbage collection in the old generation and compress all live objects in the old generation to the head of the old generation.
[0041] When the length of the young generation is less than the preset threshold, the garbage collection module performs a full collection on the entire heap space. During the full collection, copy and compress the live objects in the heap space to avoid the generation of memory fragmentation.
[0042] Regarding the information interaction and execution process among the above-mentioned modules in the device, since they are based on the same concept as the method embodiment of the present invention, the specific content can be referred to the description in the method embodiment of the present invention and will not be elaborated here.
[0043] Similarly, the advantages of the device of the present invention are:
[0044] It improves the efficiency of garbage collection and reduces the system pause time.
[0045] By means of dynamic generational and incremental collection, the memory space is maximally utilized, the number of collections in the old generation is reduced, and the overall performance is improved.
[0046] The generation of memory fragmentation is avoided, making the system run more stably.
[0047] The resource utilization rate of embedded devices is improved, and it is applicable to resource-constrained embedded Java environments.
[0048] It should be noted that not all steps and modules in the above-mentioned processes and device structures are necessary, and some steps or modules can be ignored according to actual needs. The execution order of each step is not fixed and can be adjusted as needed. The system structures described in the above embodiments can be physical structures or logical structures, that is, some modules may be implemented by the same physical entity, or some modules may be implemented separately by multiple physical entities, or some components in multiple independent devices may be jointly implemented.
[0049] Embodiment 3
[0050] The present invention further provides an efficient garbage collection device based on a Java virtual machine, including: at least one memory and at least one processor;
[0051] The at least one memory is used to store machine-readable programs;
[0052] The at least one processor is used to call the machine-readable program and execute any one of the efficient garbage collection methods based on a Java virtual machine.
[0053] Regarding the content such as information interaction and execution of readable programs by the processor in the above device, since it is based on the same concept as the method embodiment of the present invention, the specific content can be referred to the description in the method embodiment of the present invention and will not be elaborated here.
[0054] Similarly, the advantages of the device of the present invention are:
[0055] The efficiency of garbage collection is improved, and the system pause time is reduced.
[0056] By means of dynamic generational and incremental collection, the memory space is maximally utilized, the number of collections in the old generation is reduced, and the overall performance is improved.
[0057] The generation of memory fragmentation is avoided, making the system run more stably.
[0058] The resource utilization rate of embedded devices is improved, and it is applicable to resource-constrained embedded Java environments.
[0059] Embodiment 4
[0060] The present invention also provides a computer-readable medium, on which computer instructions are stored. When the computer instructions are executed by a processor, the processor is caused to execute the efficient garbage collection method based on a Java virtual machine. Specifically, a system or device equipped with a storage medium can be provided, on which software program code for implementing the functions of any one of the above embodiments is stored, and the computer (or CPU or MPU) of the system or device is caused to read and execute the program code stored in the storage medium.
[0061] In this case, the program code read from the storage medium itself can implement the functions of any one of the above embodiments. Therefore, the program code and the storage medium storing the program code constitute a part of the present invention.
[0062] Examples of the storage medium for providing the program code include a floppy disk, a hard disk, a magneto-optical disk, an optical disk (such as a CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), a magnetic tape, a non-volatile memory card, and a ROM. Optionally, the program code can be downloaded from a server computer via a communication network.
[0063] Furthermore, it should be clear that not only can the functions of any one of the above embodiments be implemented by executing the program code read by the computer, but also by causing an operating system or the like operating on the computer based on the instructions of the program code to complete part or all of the actual operations.
[0064] In addition, it can be understood that the program code read from the storage medium is written into a memory provided in an expansion board inserted into the computer or into a memory provided in an expansion unit connected to the computer, and then based on the instructions of the program code, a CPU or the like installed on the expansion board or the expansion unit is caused to execute part or all of the actual operations, thereby implementing the functions of any one of the above embodiments.
[0065] The above-described embodiments are merely preferred embodiments given to fully illustrate the present invention, and the protection scope of the present invention is not limited thereto. Equivalent substitutions or transformations made by those skilled in the art on the basis of the present invention are all within the protection scope of the present invention. The protection scope of the present invention is subject to the claims.
Claims
1. An efficient garbage collection method based on a Java virtual machine, characterized in that include: Step 1: Based on the Java virtual machine, the heap space is divided into the permanent area, the old generation, and the new generation. No free space in the old generation is reserved for the permanent area. Each time the new generation garbage collection is performed, the length of the old generation is dynamically increased, and the remaining space of the new generation is used as the new generation. Step 2: Starting from the root object set, scan the heap space, mark the reachable objects as live objects, copy all live objects in the new generation to the old generation, and tightly connect them to the tail of the old generation. When the space in the old generation is insufficient, start the garbage collection of the old generation and compress all live objects in the old generation to the head of the old generation. Step 3: When the length of the new generation is less than the preset threshold, a full collection is performed on the entire heap space. During a full collection, the live objects in the heap space are copied and compressed to avoid memory fragmentation.
2. The efficient garbage collection method based on Java virtual machine according to claim 1, characterized in that In step 2, garbage collection of the old generation is started, including: using incremental collection to collect the old generation in blocks to avoid long system pauses caused by full collection.
3. According to claim 1, an efficient garbage collection method based on a Java virtual machine is characterized in that the threshold for determining the length of the new generation in step 3 is 1 / 6 of the heap space, and when the length of the new generation is less than 1 / 6 of the heap space, a full collection is performed on the entire heap space.
4. An efficient garbage collection device based on a Java virtual machine, characterized in that Including partitioning module and garbage collection module, The partitioning module is based on the Java virtual machine and divides the heap space into the permanent area, the old generation and the new generation in sequence. No free space of the old generation is reserved for the permanent area. Each time the garbage collection module performs new generation garbage collection, it dynamically increases the length of the old generation and uses the remaining space of the new generation as the new generation. The garbage collection module starts from the root object set, scans the heap space, marks the reachable objects as live objects, copies all live objects in the new generation to the old generation, and tightly connects them to the tail of the old generation. When the space in the old generation is insufficient, the garbage collection of the old generation is started, and all live objects in the old generation are compressed to the head of the old generation. When the length of the new generation is less than the preset threshold, the garbage collection module performs a full collection on the entire heap space. During the full collection, the live objects in the heap space are copied and compressed to avoid memory fragmentation.
5. The high-efficiency garbage collection device based on Java virtual machine according to claim 4 is characterized in that the garbage The collection module starts garbage collection of the old generation, including: using incremental collection to collect the old generation in blocks to avoid long system pauses caused by full collection.
6. The high-efficiency garbage collection device based on Java virtual machine according to claim 4, characterized in that the garbage The collection module determines the threshold of the new generation length as 1 / 6 of the heap space. When the new generation length is less than 1 / 6 of the heap space, a full collection is performed on the entire heap space.
7. An efficient garbage collection device based on a Java virtual machine, characterized in that include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is used to call the machine-readable program to execute an efficient garbage collection method based on a Java virtual machine according to any one of claims 1 to 3.
8. A computer readable medium, characterized in that The computer-readable medium stores computer instructions, and when the computer instructions are executed by a processor, the processor executes an efficient garbage collection method based on a Java virtual machine as described in any one of claims 1 to 3.