A method, apparatus, device, and storage medium for garbage collection of memory.
By acquiring the status of memory objects, organizing the data and calculating weight coefficients, generating memory garbage assessment data, sorting and filtering memory garbage values, and optimizing memory reclamation, the problem of low efficiency in existing memory garbage reclamation is solved, and efficient utilization of memory resources and improvement of system performance are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- 深圳市云希谷科技有限公司
- Filing Date
- 2024-11-19
- Publication Date
- 2026-05-26
Smart Images

Figure CN119597679B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of memory garbage recycling technology, and in particular to a method, apparatus, equipment and storage medium for recycling memory garbage. Background Technology
[0002] In the field of memory management, as the complexity of programs and applications in computer systems continues to increase, memory garbage collection has become an important means of maintaining efficient system operation. Memory garbage refers to the invalid memory usage generated in the system due to frequent allocation and release of memory blocks. This garbage occupies a large amount of memory resources, leading to a decline in system performance and even potentially causing system crashes. Therefore, how to efficiently collect memory garbage and release more usable memory space has become a crucial aspect of system performance optimization.
[0003] Existing garbage collection methods primarily employ mark-and-sweep, copying algorithms, and reference counting. These methods periodically scan memory, identify invalid memory objects, and release them. However, these methods suffer from several problems: First, existing collection methods typically require traversing the entire memory space, resulting in low efficiency, especially in memory-intensive systems where this traversal incurs significant system overhead and impacts performance. Second, the mark-and-sweep operations during collection can easily lead to memory fragmentation, hindering the effective utilization of contiguous memory space and further reducing memory efficiency. Finally, existing garbage collection algorithms are relatively simplistic, making it difficult to flexibly adjust collection strategies based on the usage of different memory regions, thus limiting the effectiveness of memory collection.
[0004] In summary, existing memory garbage collection methods are insufficient in terms of efficiency, memory space utilization, and flexibility, and cannot meet the needs of modern complex systems for efficient memory management. Summary of the Invention
[0005] This invention provides a method, apparatus, device, and storage medium for recycling memory garbage, which solves the problem of low recycling efficiency caused by the lack of accurate evaluation and sorting in the existing memory garbage recycling process.
[0006] Firstly, in order to solve the above-mentioned technical problems, the present invention provides a method for garbage collection of memory, comprising:
[0007] Obtain the memory object status in different regions of memory; wherein, the memory object status includes the number of objects, the size of objects, and the lifespan of objects;
[0008] Based on the memory object status, a data cleanup operation is performed to obtain memory garbage assessment data; wherein, the memory garbage assessment data includes object quantity data, object size data, and object lifespan data;
[0009] Based on the memory garbage assessment data, weight coefficients are calculated to obtain memory garbage data weight coefficients; wherein, the memory garbage data weight coefficients include object quantity data weight, object size data weight, and object survival time data weight;
[0010] Based on the memory garbage assessment data and the memory garbage data weighting coefficients, the memory garbage value is calculated to obtain the memory garbage value for different regions;
[0011] Based on the memory garbage values in the different regions, a sorting operation is performed to obtain a memory garbage value sorting table;
[0012] Based on the memory garbage value sorting table, a predetermined threshold is used for filtering to obtain a sorting table of memory to be reclaimed;
[0013] Based on the sorting table of memory to be reclaimed, a reclamation operation is performed to obtain optimized memory.
[0014] Preferably, the step of performing data cleanup operations based on the memory object status to obtain memory garbage assessment data includes:
[0015] Based on the number of objects, a summary and statistical analysis is performed to obtain the object quantity data;
[0016] Based on the object size, perform cumulative calculations to obtain the object size data;
[0017] Based on the object's lifespan, sorting and calculation are performed to obtain the object lifespan data.
[0018] Preferably, the step of calculating weighting coefficients based on the memory garbage assessment data to obtain memory garbage data weighting coefficients includes:
[0019] The weighting coefficient for garbage data in memory is calculated using the following formula:
[0020] W = (α·N) + (β·S) + (γ·T)
[0021] Where W represents the weight coefficient of garbage data in memory, N represents the number of objects, S represents the size of objects, T represents the lifespan of objects, and α, β, and γ are the weight coefficients of the number of objects, the size of objects, and the lifespan of objects, respectively.
[0022] Preferably, the step of calculating memory garbage values based on the memory garbage assessment data and the memory garbage data weighting coefficients to obtain memory garbage values for different regions includes:
[0023] The memory garbage value for different regions is calculated using the following formula:
[0024]
[0025] Among them, G i S represents the memory garbage value of the i-th region. i N represents the size of the object in the i-th region. i T represents the number of objects in the i-th region. i Let W represent the object lifespan in the i-th region, W be the weighting coefficient for garbage data in memory, and M be the weighting coefficient for garbage data in memory. i This represents the total memory capacity of the i-th region.
[0026] Preferably, the step of processing data based on the memory garbage values to obtain a memory garbage value sorting table includes:
[0027] Based on the memory garbage values, a sorting operation is performed to obtain a list to be sorted;
[0028] Based on the list to be sorted, quick sort and merge sort are performed to obtain a sorted table of garbage values in memory.
[0029] Preferably, the step of filtering according to a predetermined threshold based on the memory garbage value sorting table to obtain a memory to be reclaimed sorting table includes:
[0030] Based on the memory garbage value sorting table, obtain the memory garbage values to be filtered;
[0031] Based on the memory garbage values to be screened, a predetermined threshold is compared to obtain memory garbage values that are greater than or equal to the predetermined threshold.
[0032] Based on the memory garbage values that are greater than or equal to a predetermined threshold, the garbage values are summarized to obtain a sorted table of memory to be reclaimed.
[0033] Preferably, the step of performing a reclamation operation based on the sorting table of memory to be reclaimed to obtain optimized memory includes:
[0034] Based on the sorting table of memory to be reclaimed, the memory to be reclaimed is locked to obtain the memory region to be reclaimed;
[0035] Based on the memory area to be reclaimed, data cleanup is performed to obtain optimized memory.
[0036] Secondly, the present invention provides a memory garbage collection device, comprising:
[0037] The data acquisition module retrieves the status of memory objects in different regions of memory.
[0038] The data processing module is used to perform data processing operations based on the status of the memory objects to obtain memory garbage assessment data;
[0039] The weight calculation module is used to calculate the weight coefficients based on the memory garbage assessment data to obtain the weight coefficients of the memory garbage data.
[0040] The garbage value calculation module is used to calculate the memory garbage value based on the memory garbage assessment data and the memory garbage data weight coefficient, so as to obtain the memory garbage value of different regions.
[0041] The sorting module is used to perform sorting operations based on the memory garbage values of the different regions to obtain a memory garbage value sorting table;
[0042] The recycling filtering module is used to perform filtering based on the memory garbage value sorting table according to a predetermined threshold to obtain a sorting table of memory to be recycled;
[0043] The recycling module is used to perform recycling operations according to the sorting table of memory to be recycled, so as to obtain optimized memory.
[0044] Thirdly, the present invention also provides an electronic device, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor, when executing the computer program, implements the memory garbage collection method described in any one of the above.
[0045] Fourthly, the present invention also provides a computer-readable storage medium comprising a stored computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to perform the memory garbage collection method described in any one of the above.
[0046] Compared with existing technologies, the present invention has the following beneficial effects: It acquires the memory object status in different regions of memory; performs data organization based on the memory object status to obtain memory garbage assessment data; calculates weight coefficients based on the memory garbage assessment data to obtain memory garbage data weight coefficients; calculates memory garbage values based on the memory garbage assessment data and the memory garbage data weight coefficients to obtain memory garbage values in different regions; performs sorting operations based on the memory garbage values in different regions to obtain a memory garbage value sorting table; performs filtering based on the memory garbage value sorting table using a predetermined threshold to obtain a memory to be reclaimed sorting table; and performs reclamation operations based on the memory to be reclaimed sorting table to obtain optimized memory.
[0047] This invention discloses a method for garbage collection, comprising: acquiring the status of memory objects in different regions of memory; performing data organization based on the memory object status to obtain memory garbage assessment data; calculating weight coefficients based on the memory garbage assessment data to obtain memory garbage data weight coefficients; calculating memory garbage values based on the memory garbage assessment data and the memory garbage data weight coefficients to obtain memory garbage values for different regions; performing sorting operations based on the memory garbage values for different regions to obtain a memory garbage value sorting table; performing filtering based on the memory garbage value sorting table using a predetermined threshold to obtain a memory to be reclaimed sorting table; and performing a reclamation operation based on the memory to be reclaimed sorting table to obtain optimized memory, thereby improving the utilization rate of memory resources and optimizing system performance. Attached Figure Description
[0048] Figure 1 This is a schematic diagram of the memory garbage collection method provided in the first embodiment of the present invention;
[0049] Figure 2 This is a schematic diagram of the memory garbage collection device provided in the second embodiment of the present invention. Detailed Implementation
[0050] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0051] Reference Figure 1 The first embodiment of the present invention provides a method for garbage collection of memory, including the following steps:
[0052] S11, obtain the memory object status in different regions of memory;
[0053] S12, perform data cleaning operation based on the memory object status to obtain memory garbage assessment data;
[0054] S13, Calculate the weight coefficients based on the memory garbage assessment data to obtain the memory garbage data weight coefficients;
[0055] S14, Calculate the memory garbage value based on the memory garbage assessment data and the memory garbage data weight coefficient to obtain the memory garbage value for different regions;
[0056] S15, perform a sorting operation based on the memory garbage values of the different regions to obtain a memory garbage value sorting table;
[0057] S16, According to the memory garbage value sorting table, a predetermined threshold is used for filtering to obtain a memory to be reclaimed sorting table;
[0058] S17, Perform a recycling operation according to the sorting table of memory to be recycled to obtain optimized memory.
[0059] In step S11, the memory object status in different regions of memory is obtained.
[0060] It's worth noting that during garbage collection, obtaining information about memory objects in different memory regions is crucial, as it forms the foundation for subsequent garbage collection processes. The main goal of this step is to periodically scan the system's memory regions, collecting relevant information for each region, including the number, size, and lifespan of objects. This information provides accurate data for evaluating, sorting, and ultimately reclaiming garbage.
[0061] First, the system scans different memory regions, recording the number of objects in each region. This object count reflects the memory usage intensity of that region and supports further weight calculations. Next, the scan also records the memory size occupied by each object. By accumulating this data, the system can assess the memory usage of each region and determine if there is any memory waste or ineffective use. Finally, the system records the lifespan of objects in memory. Object lifespan is a key factor in determining whether an object is obsolete or no longer needed. The system uses a timer to measure the lifespan of an object from its creation to the present. When this time exceeds a certain threshold, the object may be considered unnecessary and needs to be reclaimed.
[0062] Obtaining the status of these objects involves more than just simple statistics on memory space; it requires processing this data through a series of algorithms to lay the foundation for subsequent garbage value calculations. By comprehensively analyzing the number, size, and lifespan of objects, the system can more accurately determine memory usage efficiency, thus providing a scientific basis for garbage collection.
[0063] In step S12, the data cleanup operation based on the memory object status to obtain memory garbage assessment data includes:
[0064] Based on the number of objects, a summary and statistical analysis is performed to obtain the object quantity data;
[0065] Based on the object size, perform cumulative calculations to obtain the object size data;
[0066] Based on the object's lifespan, sorting and calculation are performed to obtain the object lifespan data.
[0067] It's worth noting that the data cleanup step based on the state of memory objects is crucial in the garbage collection process, as it determines how the system assesses the level of garbage in each memory region. The core objective of this process is to classify, statistically analyze, and process the acquired memory object information in detail, thereby generating evaluation data for subsequent garbage value calculations.
[0068] First, the system summarizes and statistically analyzes the number of memory objects acquired. This object count is a fundamental step in assessing memory usage intensity. By periodically scanning memory regions, the system records the total number of objects in each region and stores this data. The number of objects directly reflects the activity level of that memory region; a higher number of objects in a memory region usually indicates higher resource consumption. By summarizing this object count data, the system can quickly identify regions with dense object usage, providing guidance for subsequent garbage collection operations. This step is not merely a simple statistical analysis; it also considers the allocation of different memory regions to accurately determine the number of objects in each region.
[0069] Next, the system accumulates the object sizes. Object size reflects the actual space each object occupies in memory; this metric is crucial because it directly measures memory usage. By accumulating object sizes, the system can determine the total memory usage for each memory region. The accumulation operation requires precise measurement of the actual size of each memory object and summation according to specific rules. Typically, the system also differentiates between different memory types, such as heap or stack memory, to ensure the accuracy of the accumulation calculation. The accumulated object size data helps the system assess which regions have wasted memory and which regions' memory usage has exceeded reasonable limits, providing necessary data for subsequent garbage collection calculations.
[0070] In addition, the system sorts objects based on their lifespan. Lifespan is a key factor in determining whether an object needs to be reclaimed. The system determines whether an object is still in use by recording its lifespan from allocation to the present. If an object has a long lifespan and has not been frequently accessed during that time, it may have become obsolete. The sorting operation arranges these objects according to their lifespan, prioritizing the objects with the longest lifespans for reclamation. The goal of this sorting calculation is to ensure that the system prioritizes the reclamation of objects that are no longer in use or have excessively long lifespans, thereby freeing up more memory space. To ensure the accuracy of the sorting, the system considers factors such as the object's historical access records and last modification time to comprehensively evaluate the object's lifespan.
[0071] In summary, data cleanup is not merely a simple statistical and computational process; it also involves data classification, filtering, and sorting. By comprehensively analyzing three key indicators—object quantity, size, and lifespan—the system can generate detailed memory garbage assessment data. This data is used for subsequent garbage value calculations, helping the system determine which memory areas need to be reclaimed and which can continue to be used. This process lays a solid foundation for the efficient execution of memory garbage collection, ensuring that the system can maximize the release of memory resources and optimize overall performance without impacting performance.
[0072] In step S13, the step of calculating weight coefficients based on the memory garbage assessment data to obtain memory garbage data weight coefficients includes:
[0073] The weighting coefficient for garbage data in memory is calculated using the following formula:
[0074] W = (α·N) + (β·S) + (γ·T)
[0075] Where W represents the weight coefficient of garbage data in memory, N represents the number of objects, S represents the size of objects, T represents the lifespan of objects, and α, β, and γ are the weight coefficients of the number of objects, the size of objects, and the lifespan of objects, respectively.
[0076] It's worth noting that calculating weighting coefficients is a crucial step in the garbage collection process. Its core purpose is to comprehensively consider the number, size, and lifespan of memory objects, using a weighted approach to evaluate the impact of these factors on the garbage collection value, and ultimately derive the weighting coefficients for the garbage data. First, the system has already obtained evaluation data on the number of objects, object size, and object lifespan in each memory region. These data are key indicators for measuring memory region usage. However, since memory usage varies across different regions, the contribution of each factor to garbage collection also differs. Therefore, weighting coefficient calculation is necessary to reasonably allocate the influence weight of each factor, ensuring accurate garbage collection in the subsequent garbage collection value calculation process.
[0077] The system analyzes the number of objects in memory, using this data as a crucial indicator of memory usage intensity. A higher number of objects in a memory region typically indicates frequent memory use and potentially more invalid or outdated objects; therefore, the number of objects is weighted accordingly. This weight is adjusted by a coefficient α within the system to ensure that the number of objects accurately reflects memory usage in the calculation of garbage values. Next, the system evaluates each memory region based on object size, determining the space occupied by each object. Object size directly impacts memory usage; objects occupying larger amounts of memory may be assigned a higher weight β, as these objects significantly affect memory resource allocation and utilization efficiency. In this way, the system can accurately assess memory usage and prioritize the reclamation of objects that occupy significant space.
[0078] In addition, the system adjusts weights based on object lifespan. Object lifespan is a crucial indicator of whether an object is still valid or needs to be reclaimed. If an object survives in memory for a long time and is not frequently accessed, it is likely to have become obsolete and a candidate for garbage collection. To ensure the system prioritizes the reclamation of these long-unused objects, it assigns a weight γ to object lifespan data, giving it a larger proportion in garbage value calculations. Through this weighting, the system can more effectively filter out invalid objects in memory, ensuring the accuracy and timeliness of garbage collection.
[0079] The entire calculation process of the weighting coefficients is achieved through formulas:
[0080] W = (α·N) + (β·S) + (γ·T)
[0081] Where W is the final weighting coefficient for memory garbage data, reflecting the combined impact of the number of objects, object size, and object lifespan on the memory garbage value. This formula, by assigning appropriate weights to different data, ensures that the influence of various factors is fully considered during the memory garbage collection process. Ultimately, the weighting coefficient W will be used in subsequent memory garbage value calculations to help the system determine which memory regions need to be prioritized for reclamation, thereby optimizing the allocation and use of memory resources and improving the overall system performance.
[0082] In step S14, based on the memory garbage assessment data and the memory garbage data weighting coefficients, the memory garbage value is calculated to obtain the memory garbage value for different regions, including:
[0083] The memory garbage value for different regions is calculated using the following formula:
[0084]
[0085] Among them, G i S represents the memory garbage value of the i-th region. i N represents the size of the object in the i-th region. i T represents the number of objects in the i-th region. i Let W represent the object lifespan in the i-th region, W be the weighting coefficient for garbage data in memory, and M be the weighting coefficient for garbage data in memory. i This represents the total memory capacity of the i-th region.
[0086] It is worth noting that M i The total capacity of a memory region refers to the total amount of memory resources occupied by the i-th memory region. During garbage collection, the total capacity of a memory region is used to normalize garbage values, ensuring the comparability of garbage collection results between different regions. Each memory region occupies a different amount of memory due to differences in the number and size of objects; therefore, its total capacity needs to be considered during garbage collection.
[0087] M i The function of this formula is to divide the amount of garbage memory by the total capacity of the memory region, so that the evaluation result reflects the actual memory usage ratio of each region. This not only accurately measures the degree of garbage memory in each region but also avoids calculation errors caused by differences in memory region size, thus ensuring the rationality and accuracy of the garbage collection strategy.
[0088] It's worth noting that calculating the garbage value based on garbage assessment data and weighting coefficients is a complex and precise step in the garbage collection process. Its core objective is to analyze the usage of each memory region, calculate the garbage value for each region, and provide a basis for subsequent garbage collection. This step combines multiple key factors, such as object size, number of objects, and object lifespan, and performs a weighted calculation to arrive at the final garbage value. This calculation result is crucial for determining which memory regions need to be prioritized for reclamation.
[0089] First, the system extracts three core metrics for each memory region from the memory garbage assessment data: object size, object count, and object lifespan. Object size refers to the total memory space occupied by all objects in a memory region. It directly reflects the memory usage of that region. Larger objects consume more memory resources, making object size an important indicator of memory waste. The object count represents the total number of objects stored in that region. A higher count indicates more intensive memory usage, potentially signifying lower memory utilization efficiency. This is especially true when the number of objects in a region increases while their size remains small, often leading to memory fragmentation. Object lifespan is another crucial factor, referring to the length of time an object exists in memory. If an object exists for a long time but is not frequently accessed, it may mean that the object has lost its initial function and becomes invalid data in memory, thus qualifying for garbage collection.
[0090] After extracting this data, the system needs to perform weighted processing. The purpose of weighting is to balance the impact of different factors on memory garbage collection assessment. The system uses weighting coefficients to assign different weights to each indicator based on its judgment of the relative importance of these indicators. For example, if the system believes that large objects in memory put more pressure on memory, it will assign a higher weight to object size, thus giving object size a larger proportion in garbage value calculation. Conversely, if the system detects that the number of objects in a certain region has a greater impact on memory resources, it will assign a higher weight to the number of objects. Similarly, the weight of object lifespan is also adjusted by the system based on the usage of that memory region. This weighted processing ensures that the system can flexibly adjust its garbage collection strategy according to specific circumstances.
[0091] After weighting the data, the system combines the results to calculate the garbage value for each memory region. This garbage value not only reflects the degree of memory waste in that region but also quantifies the importance of each memory region in overall memory management. The system uses the garbage value to determine which regions should be prioritized for reclamation. At this point, the total capacity of the memory regions is also included in the calculation. The total memory capacity is used to standardize the garbage value of each region, ensuring that memory regions of different sizes can be reasonably compared during evaluation. Larger regions may contain more objects and data; however, by introducing the total capacity standard, the system can fairly measure the memory utilization efficiency of each region. In this way, even if some regions have different memory sizes, the system can adjust according to the total memory capacity to obtain an accurate garbage value.
[0092] Finally, by calculating the garbage value, the system obtains a specific garbage assessment result for each memory region. These assessment results will serve as the basis for the system to decide which memory regions need to be prioritized for reclamation. The system will sort each memory region according to its garbage value; regions with higher garbage values indicate more severe memory resource waste and will be considered for reclamation first. Through this scientific and systematic garbage value calculation, the system can achieve more efficient memory resource management, ensuring that when memory resources are scarce, it can prioritize processing those memory regions that have the greatest impact on system performance, maximizing memory utilization.
[0093] In step S15, data processing is performed based on the memory garbage values of the different regions to obtain a memory garbage value sorting table, including:
[0094] Based on the memory garbage values, a sorting operation is performed to obtain a list to be sorted;
[0095] Based on the list to be sorted, quick sort and merge sort are performed to obtain a sorted table of garbage values in memory.
[0096] It's worth noting that during memory garbage collection, processing the garbage values in different memory regions to generate a garbage value sorting table is one of the core steps in efficiently performing garbage collection. The main purpose of this step is to arrange the garbage values of each memory region in an orderly manner, ensuring that memory regions with higher garbage values are prioritized during subsequent collection processes, thereby maximizing the release of memory resources and improving system performance.
[0097] First, the system calculates the garbage value of each memory region through preliminary steps. These garbage values reflect the memory usage and garbage level of each region, representing the degree of memory resource waste within that region. The system then generates a list to be sorted based on these garbage values. This list records the garbage values of all memory regions along with their corresponding memory region information. After generating the list, the system sorts it according to the size of the garbage values, ensuring that regions with high garbage values appear at the front of the list first. Since the number of memory regions may be large, the system uses two classic sorting algorithms—quicksort and mergesort—to improve sorting efficiency.
[0098] In data processing, quicksort is an efficient comparison sorting algorithm that recursively divides data into smaller intervals, sorts these intervals independently, and finally merges them into a complete ordered list. Quicksort has low time complexity and is suitable for processing large datasets, thus it is used to handle large amounts of garbage data in memory. Quicksort can quickly locate regions with large amounts of garbage values, thereby speeding up data processing. Merge sort, on the other hand, is a more stable sorting algorithm that divides data into smaller ordered segments and gradually merges them into a complete ordered list. The advantage of merge sort lies in its stability, ensuring that the relative order between memory regions is not disrupted during the sorting process. This sorting method is particularly suitable for scenarios requiring precise sorting, ensuring that garbage values in each memory region are accurately arranged.
[0099] By combining quicksort and mergesort, the system can quickly and accurately sort the garbage values of all memory regions, generating a complete garbage value sorting table. This sorting table not only records the garbage value of each memory region but also provides a clear priority basis for subsequent garbage collection operations. Through this sorting table, the system can prioritize processing memory regions with larger garbage values, thereby maximizing the release of memory space within a limited collection time. The sorting table can also be used for further predetermined threshold filtering, that is, based on a system-set threshold range, filtering out regions whose garbage values exceed the threshold for more precise garbage collection operations.
[0100] The execution of the entire sorting and data processing steps lays a solid foundation for memory garbage collection. Through this efficient sorting method, the system can make reasonable garbage collection decisions between different memory regions, ensuring that while maintaining performance, memory resource utilization is optimized and the overall efficiency of the system is improved.
[0101] In step S16, based on the memory garbage value sorting table, a predetermined threshold is used for filtering to obtain a memory to be reclaimed sorting table, including:
[0102] Based on the memory garbage value sorting table, obtain the memory garbage values to be filtered;
[0103] Based on the memory garbage values to be screened, a predetermined threshold is compared to obtain memory garbage values that are greater than or equal to the predetermined threshold.
[0104] Based on the memory garbage values that are greater than or equal to a predetermined threshold, the garbage values are summarized to obtain a sorted table of memory to be reclaimed.
[0105] It's worth noting that during memory garbage collection, the step of filtering based on a predetermined threshold using a memory garbage value sorting table is crucial for ensuring the system can accurately and efficiently identify memory regions that need priority for reclamation. This step involves analyzing, comparing, and summarizing a large number of memory garbage values, ultimately generating a sorting table of memory to be reclaimed, thus providing clear guidance for the system's reclamation operations. The accuracy of this step determines the efficiency of memory resource release and the optimization of overall system performance during the reclamation process.
[0106] First, the system retrieves the garbage value for each memory region based on the generated garbage value sorting table. The garbage value is a comprehensive evaluation result calculated previously, representing the degree of memory resource waste in each memory region. The system uses these garbage values as the basis for filtering, gradually building a list of garbage values to be filtered. Since the garbage value is an important indicator for assessing memory usage status, representing the memory utilization efficiency and urgency of memory reclamation in each memory region, accurately obtaining the garbage value for each region is crucial for subsequent filtering operations.
[0107] Next, the system compares the acquired garbage values one by one according to a pre-defined threshold. The pre-set threshold is a standard used to determine whether a memory region needs to enter the reclamation process. The system compares the garbage value of each memory region with this threshold; if the garbage value is greater than or equal to the threshold, the memory region is considered for reclamation. During this comparison process, the system adjusts in real time based on the specific circumstances of each memory region to ensure the accuracy of the selection. For example, in some cases, the system may dynamically adjust the threshold, especially when memory resources are scarce, potentially lowering the threshold standard to allow more memory regions to enter the reclamation state. Through this process, the system ensures that the selected memory regions not only need to be reclaimed but also can be prioritized using reasonable criteria.
[0108] After completing the threshold comparison, the system aggregates the garbage values of all memory regions that meet the reclamation criteria. The purpose of this aggregation is to group all memory regions greater than or equal to the threshold together, generating a new sorted table of memory to be reclaimed. This sorted table is ordered according to the amount of garbage value, ensuring that memory regions with higher garbage values are processed first during subsequent reclamation. This step is crucial because by aggregating and sorting the filtered garbage values, the system can process the memory regions that most need reclamation within a limited time, avoiding excessive waste of system resources.
[0109] The entire threshold screening and aggregation process provides a precise and efficient decision-making basis for memory garbage collection. By screening out memory regions where garbage values exceed predetermined thresholds, the system can ensure the accuracy and timeliness of garbage collection operations, thereby maximizing the release of system resources and optimizing memory management efficiency.
[0110] In step S17, a reclamation operation is performed according to the sorting table of memory to be reclaimed to obtain optimized memory, including:
[0111] Based on the sorting table of memory to be reclaimed, the memory to be reclaimed is locked to obtain the memory region to be reclaimed;
[0112] Based on the memory area to be reclaimed, data cleanup is performed to obtain optimized memory.
[0113] It's worth noting that in the entire memory garbage collection process, performing the reclamation operation based on the memory to be reclaimed sorting table is a crucial step. This aims to effectively clean up the memory regions to be reclaimed through systematic memory management, ultimately resulting in optimized memory. This process involves multiple steps, including locking the regions to be reclaimed and in-depth data cleanup operations, to ensure that the system's memory resources are released to the maximum extent possible without affecting the normal operation of the system.
[0114] First, the system processes each memory region step by step according to the memory to be reclaimed sorting table. This table is generated through prior threshold filtering and garbage value sorting, and contains all memory regions that need garbage collection, sorted by their garbage value from highest to lowest. Therefore, the system processes these memory regions according to their priority in the table. This priority order ensures that the regions that waste the most memory resources are reclaimed first, thereby effectively relieving system memory pressure.
[0115] Next, the system will perform a reclamation locking operation on each region to be reclaimed. The purpose of reclamation locking is to prevent new data from being written to these regions during reclamation. To ensure data integrity and system stability, the system locks each memory region to be reclaimed before reclamation, ensuring that no new memory resources are allocated or new data is written to these regions during the reclamation process. This step is crucial because if new data is written during reclamation, it may lead to data inconsistency, or even cause system crashes or data loss. The locking operation ensures the safety of the reclamation process and provides a stable working environment for subsequent data cleanup operations.
[0116] After locking the memory regions, the system performs data cleanup operations on these locked regions. Data cleanup is the core of the garbage collection process, and its main task is to release invalid and no longer-used memory objects. First, the system performs a full scan of the locked memory regions to identify which objects have become useless or are considered invalid because they have not been accessed for a long time. These objects are usually memory data marked as garbage objects, including unreferenced objects, outdated data caches, temporary files, etc. The system then deletes this invalid data, thereby releasing the memory space it occupies.
[0117] In addition, data cleanup operations include memory defragmentation, which ensures that the cleaned memory regions have contiguous memory space. Frequent memory allocation and reclamation can lead to memory fragmentation, where free memory blocks are divided into multiple non-contiguous small blocks. While fragmented memory is usable, it may not meet the needs of large-scale data processing in practice. To address this issue, after cleaning up invalid data, the system compresses and merges the reclaimed memory, integrating these fragmented memory blocks into contiguous, large-scale usable memory regions. This step not only improves memory availability but also reduces the impact of memory fragmentation on system performance.
[0118] In addition, after the data cleanup operation is complete, the system generates an optimized memory status report to record the results of this reclamation operation in detail. This report typically includes the amount of memory space reclaimed, the amount of invalid data cleaned, the status of memory fragmentation, and changes in overall system memory utilization. Through this report, the system can comprehensively understand the actual effects of this memory reclamation and provide data support for subsequent memory management operations. After the reclamation operation is completed, the system's memory resources are greatly optimized, free memory space increases, memory fragmentation decreases, and system performance is significantly improved.
[0119] Finally, the steps for reclaiming memory based on the sorting table are very detailed and crucial. By locking the areas to be reclaimed, cleaning up data, and defragmenting memory, the system can safely and effectively release memory resources. This process ultimately achieves optimized memory management, maximizing memory utilization without affecting system operation and ensuring long-term stable operation.
[0120] Reference Figure 2 The second embodiment of the present invention provides a memory garbage collection device, comprising:
[0121] The data acquisition module retrieves the status of memory objects in different regions of memory.
[0122] The data processing module is used to perform data processing operations based on the status of the memory objects to obtain memory garbage assessment data;
[0123] The weight calculation module is used to calculate the weight coefficients based on the memory garbage assessment data to obtain the weight coefficients of the memory garbage data.
[0124] The garbage value calculation module is used to calculate the memory garbage value based on the memory garbage assessment data and the memory garbage data weight coefficient, so as to obtain the memory garbage value of different regions.
[0125] The sorting module is used to perform sorting operations based on the memory garbage values of the different regions to obtain a memory garbage value sorting table;
[0126] The recycling filtering module is used to perform filtering based on the memory garbage value sorting table according to a predetermined threshold to obtain a sorting table of memory to be recycled;
[0127] The recycling module is used to perform recycling operations according to the sorting table of memory to be recycled, so as to obtain optimized memory.
[0128] It should be noted that the memory garbage collection device provided in this embodiment of the invention is used to execute all the process steps of the memory garbage collection method in the above embodiment. The working principle and beneficial effect of the two are one-to-one, so they will not be described again.
[0129] This invention also provides an electronic device. The electronic device includes a processor, a memory, and a computer program stored in the memory and executable on the processor, such as a memory garbage collection program. When the processor executes the computer program, it implements the steps in the various memory garbage collection method embodiments described above, for example... Figure 1 The step S11 shown. Alternatively, when the processor executes the computer program, it implements the functions of each module / unit in the above-described device embodiments, such as the garbage value calculation module.
[0130] For example, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to complete the present invention. The one or more modules / units may be a series of computer program instruction segments capable of performing a specific function, which describe the execution process of the computer program in the electronic device.
[0131] The electronic device may be a desktop computer, laptop, handheld computer, or smart tablet, etc. The electronic device may include, but is not limited to, a processor and memory. Those skilled in the art will understand that the above components are merely examples of electronic devices and do not constitute a limitation on the electronic device. It may include more or fewer components than described above, or combine certain components, or different components. For example, the electronic device may also include input / output devices, network access devices, buses, etc.
[0132] The processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor. The processor is the control center of the electronic device, connecting all parts of the electronic device via various interfaces and lines.
[0133] The memory can be used to store the computer programs and / or modules. The processor implements various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory and by calling data stored in the memory. The memory may mainly include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, phonebook, etc.). In addition, the memory may include high-speed random access memory, and may also include non-volatile memory, such as hard disk, RAM, plug-in hard disk, smart media card (SMC), secure digital (SD) card, flash card, at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0134] Wherein, if the modules / units integrated in the electronic device are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments of the present invention can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium can be appropriately added or removed according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electrical carrier signals and telecommunication signals.
[0135] It should be noted that the device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and 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 modules can be selected to achieve the purpose of this embodiment according to actual needs. Furthermore, in the accompanying drawings of the device embodiments provided by this invention, the connection relationships between modules indicate that they have communication connections, which can be specifically implemented as one or more communication buses or signal lines. Those skilled in the art can understand and implement this without any creative effort.
[0136] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above descriptions are merely specific embodiments of the present invention and are not intended to limit the scope of protection of the present invention. In particular, it should be noted that any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention for those skilled in the art.
Claims
1. A method of garbage collection of memory, characterized by, include: Obtain the memory object status in different regions of memory; wherein, the memory object status includes the number of objects, the size of objects, and the lifespan of objects; Based on the memory object status, a data cleanup operation is performed to obtain memory garbage assessment data; wherein, the memory garbage assessment data includes object quantity data, object size data, and object lifespan data; Based on the memory garbage assessment data, weight coefficients are calculated to obtain memory garbage data weight coefficients; wherein, the memory garbage data weight coefficients include object quantity data weight, object size data weight, and object survival time data weight; Based on the memory garbage assessment data and the memory garbage data weighting coefficients, the memory garbage value is calculated to obtain the memory garbage value for different regions; Based on the memory garbage values in the different regions, a sorting operation is performed to obtain a memory garbage value sorting table; Based on the memory garbage value sorting table, a predetermined threshold is used for filtering to obtain a sorting table of memory to be reclaimed; Based on the sorting table of memory to be reclaimed, a reclamation operation is performed to obtain optimized memory.
2. The method of claim 1, wherein, The step of performing data cleanup operations based on the memory object status to obtain memory garbage assessment data includes: Based on the number of objects, a summary and statistical analysis is performed to obtain the object quantity data; Based on the object size, perform cumulative calculations to obtain the object size data; Based on the object's lifespan, sorting and calculation are performed to obtain the object lifespan data.
3. The method of claim 1, wherein, The step of calculating weighting coefficients based on the memory garbage assessment data to obtain memory garbage data weighting coefficients includes: The weighting coefficient for garbage data in memory is calculated using the following formula: W = (α·N) + (β·S) + (γ·T) Where W represents the weight coefficient of garbage data in memory, N represents the number of objects, S represents the size of objects, T represents the lifespan of objects, and α, β, and γ are the weight coefficients of the number of objects, the size of objects, and the lifespan of objects, respectively.
4. The method of claim 1, wherein, The step of calculating memory garbage values based on the memory garbage assessment data and the memory garbage data weighting coefficients to obtain memory garbage values for different regions includes: The memory garbage value for different regions is calculated using the following formula: wherein G i represents the memory garbage value of the i-th region, S i represents the object size of the i-th region, N i represents the object quantity of the i-th region, T i represents the object survival time of the i-th region, W is a memory garbage data weight coefficient, M i represents the total memory capacity of the i-th region.
5. The memory garbage collection method according to claim 1, characterized in that, The step of processing data based on the memory garbage values of different regions to obtain a memory garbage value sorting table includes: Based on the memory garbage values, a sorting operation is performed to obtain a list to be sorted; Based on the list to be sorted, quick sort and merge sort are performed to obtain a memory garbage value sorting table.
6. The memory garbage collection method according to claim 1, characterized in that, The step of filtering according to a predetermined threshold based on the memory garbage value sorting table to obtain a memory to be reclaimed sorting table includes: Based on the memory garbage value sorting table, obtain the memory garbage values to be filtered; Based on the memory garbage values to be screened, a predetermined threshold is compared to obtain memory garbage values that are greater than or equal to the predetermined threshold. Based on the memory garbage values that are greater than or equal to a predetermined threshold, the garbage values are summarized to obtain a sorted table of memory to be reclaimed.
7. The memory garbage collection method according to claim 1, characterized in that, The step of performing a reclamation operation based on the sorting table of memory to be reclaimed to obtain optimized memory includes: Based on the sorting table of memory to be reclaimed, the memory to be reclaimed is locked to obtain the memory region to be reclaimed; Based on the memory area to be reclaimed, data cleanup is performed to obtain optimized memory.
8. A method and apparatus for garbage collection in memory, characterized in that, include: The data acquisition module retrieves the status of memory objects in different regions of memory. The data processing module is used to perform data processing operations based on the status of the memory objects to obtain memory garbage assessment data; The weight calculation module is used to calculate the weight coefficients based on the memory garbage assessment data to obtain the weight coefficients of the memory garbage data. The garbage value calculation module is used to calculate the memory garbage value based on the memory garbage assessment data and the memory garbage data weight coefficient, so as to obtain the memory garbage value of different regions. The sorting module is used to perform sorting operations based on the memory garbage values of the different regions to obtain a memory garbage value sorting table; The recycling filtering module is used to perform filtering based on the memory garbage value sorting table according to a predetermined threshold to obtain a sorting table of memory to be recycled; The recycling module is used to perform recycling operations according to the sorting table of memory to be recycled, so as to obtain optimized memory.
9. An electronic device, characterized in that, The system includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor, when executing the computer program, implements a memory garbage collection method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored computer program, wherein, when the computer program is executed, it controls the device on which the computer-readable storage medium is located to perform a memory garbage collection method as described in any one of claims 1 to 7.