Method and electronic device for reclaiming storage space
By monitoring and automatically creating new applications through the memory manager, the problem of dynamic memory not being released in time is solved, and efficient use of storage space and improvement of system performance are achieved.
Patent Information
- Application Number
- CN202310244155.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-14
- Publication Date
- 2025-09-05
- Estimated Expiration
- 2043-03-14
AI Technical Summary
In computer programming languages without garbage collection mechanisms, the allocation and release of dynamic memory are not managed in a timely manner, resulting in the accumulation of useless memory, leading to system resource exhaustion and performance degradation.
The memory manager monitors the memory space used by applications, calculates the specified value and creates a new application when it exceeds the limit, copies the data and destroys the old application to release the unused memory.
Effectively reclaim storage space, avoid useless data occupation, improve system performance, and reduce the burden of programmers managing storage space.
Smart Images

Figure CN116185639B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a memory management mechanism, and in particular to a method and an electronic device for reclaiming storage space. Background Art
[0002] In the computing world, application memory management is a crucial issue. Memory deallocation is the process of releasing data from memory to improve system memory utilization and performance. Applications typically utilize dynamic memory, requiring the application to allocate and deallocate dynamic memory within the application. Generally speaking, dynamic memory allocation is a method for dynamically allocating or reclaiming storage space during application execution.
[0003] In computer programming languages that do not have a garbage collection (GC) mechanism (such as C and C++), the allocation and release of dynamic memory must be specially designed during programming. As long as the programmer does not include a dynamic memory allocation mechanism and does not manually program the release, even if the function in the application ends, the dynamically allocated memory space will not be released, and other functions in the same application can continue to use this memory space. Only when the entire application ends will the system release all the memory space allocated by the application. In this way, unused memory is not released, resulting in a continuous accumulation of abandoned memory, which will eventually consume the system's memory resources, causing system delays or even crashes. Summary of the Invention
[0004] The present invention is directed to a method and electronic device for reclaiming storage space, which can prevent useless data from occupying the storage space in the electronic device.
[0005] According to an embodiment of the present invention, a method for reclaiming storage space is applicable to a memory manager of an electronic device, and the method comprises: after the processor of the electronic device loads a first application into the main memory and drives the first application to run, the memory manager performs the following steps. The monitoring module of the memory manager monitors the current capacity occupied by the first application in the main memory and determines whether the current capacity exceeds a specified value. In response to determining that the current capacity exceeds the specified value, the creation module of the memory manager newly creates a second application corresponding to the first application in the main memory. The acquisition module of the memory manager obtains the operation-related data of the first application. The destruction module of the memory manager destroys the first application in the main memory. The trigger module of the memory manager provides the operation-related data of the first application to the second application. The trigger module of the memory manager drives the second application to start running.
[0006] In a method for reclaiming storage space according to an embodiment of the present invention, before the monitoring module of the memory manager monitors the current capacity occupied by the first application in the main memory, the method further includes: obtaining a first capacity occupied by the first application at a first time point in a specified time segment, and obtaining a second capacity occupied by the first application at a second time point in the time segment, wherein the first time point is earlier than the second time point, and the segment length of the specified time segment = the second time point - the first time point; and calculating the specified value based on the first capacity, the second capacity, the segment length, and a time constant. The specified value is calculated as follows:
[0007] Mmax=Ms+[(Me-Ms)×Tmax / Tt],
[0008] Among them, Mmax is the specified value, Ms is the first capacity, Me is the second capacity, Tt is the segment length, and Tmax is the time constant.
[0009] In the method for reclaiming storage space according to an embodiment of the present invention, the creation module of the memory manager, in response to determining that the current capacity exceeds the specified value, newly creates the second application corresponding to the first application in the main memory, including: newly creating the second application by copying the code of the first application.
[0010] In the method for reclaiming storage space according to an embodiment of the present invention, the step of obtaining the operation-related data of the first application includes: retrieving the operation-related data of the first application from a designated path.
[0011] In the method for reclaiming storage space according to an embodiment of the present invention, it also includes broadcasting a notification to change the message receiving end of the first application to the memory manager in response to destroying the first application in the main memory and before the second application starts running; and caching the received information related to the first application.
[0012] In the method for reclaiming storage space according to an embodiment of the present invention, it also includes broadcasting another notification to change the message receiving end of the first application to the second application after the second application starts running; and providing the information related to the first application cached by the memory manager to the second application.
[0013] According to an embodiment of the present invention, an electronic device includes: a main memory; and a processor coupled to the main memory, including a memory manager, wherein the memory manager includes a monitoring module, a creation module, an acquisition module, a destruction module, and a trigger module, and the memory manager is configured to: load a first application into the main memory and drive the first application to run, then monitor the current capacity occupied by the first application in the main memory through the monitoring module, and determine whether the current capacity exceeds a specified value; in response to determining that the current capacity exceeds the specified value, create a new second application corresponding to the first application in the main memory through the creation module; obtain operation-related data of the first application through the acquisition module. The destruction module is used to destroy the first application in the main memory; and provide the operation-related data of the first application to the second application through the trigger module, and drive the second application to start running.
[0014] Based on the above, the present disclosure utilizes a memory manager to reclaim the storage space occupied by useless data of the application program, thereby effectively utilizing the storage space of the main memory. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 is a block diagram of an electronic device according to an embodiment of the present invention;
[0016] Figure 2 is a schematic diagram of the architecture of a memory manager according to an embodiment of the present invention;
[0017] Figure 3 is a flow chart of a method for reclaiming storage space according to an embodiment of the present invention;
[0018] Figures 4A to 4C FIG. 1 is a schematic diagram illustrating usage of storage space of a main memory according to an embodiment of the present invention.
[0019] Description of Reference Numerals
[0020] 100: Electronic devices
[0021] 110: Processor
[0022] 120: Main memory
[0023] 130: Auxiliary storage
[0024] 140: Memory Manager
[0025] 210: Monitoring module
[0026] 220: Create Module
[0027] 230: Get module
[0028] 240: Destroy module
[0029] 250: Trigger module
[0030] 410: First Application
[0031] 411, 421: Code
[0032] 413, 423: Operation-related data
[0033] 420: Second Application
[0034] S305-S335: Steps of the method for reclaiming storage space DETAILED DESCRIPTION
[0035] Reference will now be made in detail to exemplary embodiments of the present invention, examples of which are illustrated in the accompanying drawings. Whenever possible, the same reference numerals are used in the drawings and the description to refer to the same or like parts.
[0036] Figure 1 is a block diagram of an electronic device according to an embodiment of the present invention. Figure 1 , the electronic device 100 includes a processor 110 , a main memory 120 , and an auxiliary memory 130 . The processor 110 is coupled to the main memory 120 and the auxiliary memory 130 .
[0037] The processor 110 is, for example, a central processing unit (CPU), a physical processing unit (PPU), a programmable microprocessor (Microprocessor), an embedded control chip, a digital signal processor (DSP), an application specific integrated circuit (ASIC), or other similar devices.
[0038] Main memory 120 is a storage space directly addressable by processor 110 and used to store programs and data being processed. Main memory 120 can be implemented using any type of fixed or removable random access memory (RAM), cache, etc. When electronic device 100 is operating, applications and related data are first loaded into main memory 120 and then executed by processor 110.
[0039] Secondary memory 130 can be used to store large amounts of programs and data. When needed by the executing application, the data is read from the secondary memory into main memory 120 for processing. In some embodiments, secondary memory 130 is non-volatile memory (NVM), which retains data even after a power outage. In some embodiments, secondary memory 130 is implemented using a hard disk drive, solid-state drive, optical disk, USB flash drive, or other means.
[0040] Here, the processor 110 also includes a memory manager 140. After the processor 110 loads the first application into the main memory 120 and drives the first application to run, the memory manager 140 monitors the current capacity of the main memory 120 occupied by the first application. In other words, the memory manager 140 is responsible for managing the storage space occupied by the application.
[0041] In some embodiments, the memory manager 140 is a physical circuit integrated or incorporated with the processor 110. In some embodiments, the memory manager 140 may be implemented in firmware or software, wherein the memory manager 140 is, for example, firmware or software stored in the main memory 120 and read by the processor 110 to execute corresponding functions. In some embodiments, the code corresponding to the memory manager 140 may be stored in the secondary memory 130. When needed, the processor 110 loads the memory manager 140 from the secondary memory 130 into the main memory 120, and the processor 110 reads the code to execute corresponding functions.
[0042] In some embodiments, the processor 110 is configured to run an operating system, and one or more applications (e.g., a first application) and the memory manager 140 all run in the user mode of the operating system. In some embodiments, depending on the different settings of the operating system, the memory manager 140 may also run in the kernel mode of the operating system, but the present disclosure is not limited to this. In some embodiments, the memory manager 140 can simultaneously monitor and manage the usage of memory space of multiple applications (e.g., a first application, a second application, etc.) in the operating system. In some embodiments, the application can actively activate or notify the memory manager 140 for monitoring or management when running. In some embodiments, after being activated, the memory manager 140 can be resident in the operating system. When the application (e.g., the first application) starts running, the operating system will send a notification to the memory manager 140 to monitor and manage the running application.
[0043] Figure 2 FIG is a schematic diagram of the architecture of a memory manager according to an embodiment of the present invention. Figure 2 Memory manager 140 includes a monitoring module 210, a creation module 220, an acquisition module 230, a destruction module 240, and a triggering module 250. In some embodiments, depending on the technical solution for implementing memory manager 140, each of these modules may correspond to part or all of a physical circuit, or one or more code segments. The following describes the steps of the method for reclaiming storage space using these modules.
[0044] Figure 3 FIG. 4 is a flow chart of a method for reclaiming storage space according to an embodiment of the present invention. Figures 4A to 4C This is a schematic diagram of the use of the storage space of the main memory according to an embodiment of the present invention. Figures 1 to 3 as well as Figure 4AIn this embodiment, the processor 110 loads the first application 410 into the main memory 120 and drives the first application 410 to run. At this time, in response to the running of the first application 410, the memory manager 140 begins to monitor the usage of the main memory 120 by the first application 410.
[0045] For example, code 411 of a first application 410 is stored in auxiliary memory 130. The processor 110 loads code 411 into main memory 120 and executes code 411 in main memory 120, thereby generating operation-related data 413. During the operation of the first application 410, the first application dynamically obtains space in main memory 120 to store the operation-related data 413. In some embodiments, the first application 410 dynamically requests and obtains the storage space from the operating system during operation. When the operating system receives the request, it allocates a designated address and space in main memory 120 for use by the first application 410. At this point, the operation-related data 413 is stored in the storage space indicated by the designated address in main memory 120. Thus, the monitoring module 210 of the memory manager 140 can obtain the designated address through communication with the first application 410 and, through the designated address, obtain the operating status of the first application 410 and the operation-related data 413. The operation-related data 413 stored at the designated address can be considered as data meaningful to the overall operation of the first application 410. On the other hand, in addition to the operation-related data 413, the execution of the first application 410 also generates some useless data that is meaningless to the overall operation of the first application 410. This useless data may be generated due to the programmer's negligence when writing the code, resulting in the application not carefully considering when obtaining space or setting data, and not properly releasing space after use. This useless data is not stored at a designated address, but rather at an arbitrary address in the main memory 120. Therefore, this useless data cannot be discovered and cannot be cleared while the first application 410 is still executing. In other words, the useless data mentioned here is data that the programmer has forgotten and no one knows how to obtain.
[0046] In step S305, the memory manager 140 monitors the current capacity occupied by the first application 410 in the main memory 120 through the monitoring module 210. In step S310, it is determined whether the current capacity exceeds a specified value.
[0047] Before monitoring the current capacity occupied by the first application 410 in the main memory 120, the memory manager 140 may first calculate and obtain the reasonable maximum usage capacity (i.e., a specified value) of the first application 410 in the main memory 120. In some embodiments, the memory manager 140 may determine the reasonable maximum usage capacity based on past historical records (e.g., historical records of all applications, or past records of the first application 410 itself). In some embodiments, the memory manager 140 may calculate the reasonable maximum usage capacity (i.e., the specified value) based on the usage of the main memory 120 within a specified time segment. For example, the monitoring module 210 may obtain the first capacity occupied by the first application 410 at a first time point in the specified time segment, and obtain the second capacity occupied by the first application 410 at a second time point in the specified time segment. The first time point is earlier than the second time point, and the segment length of the specified time segment = the second time point - the first time point.
[0048] For example, the first capacity occupied by the first application 410 is obtained at a first time point in full operation mode after the first application 410 starts to execute for a period of time, and the second capacity occupied by the first application 410 is obtained at a second time point after a specified time period.
[0049] The specified value is calculated based on the first capacity, the second capacity, the segment duration, and the time constant. The specified value is calculated as follows:
[0050] Mmax=Ms+[(Me-Ms)×Tmax / Tt].
[0051] Mmax is a specified value, Ms is the first capacity, Me is the second capacity, Tt is the segment duration, and Tmax is a time constant. The time constant represents the reasonable normal operation time of the process. For example, the time constant can be set by the user based on experience. Alternatively, it can be derived from historical data. For example, during the training phase, the memory manager 140 monitors and records the usage of the main memory 120 of the first application 410 and generates statistics. In some embodiments, the statistics can be calculated and compared based on the calculation formula of the specified value. The training phase can last for a period of time, collecting operation-related data over a period of time. The memory manager 140 can calculate the storage space usage model of the first application 410 and infer the normal operation time of the first application 410. Storage space usage models include linear growth, step growth, and explicit fluctuation. In some embodiments, the memory manager 140 can also continuously update the reasonable maximum usage capacity of the first application 410 based on the above-mentioned recording and calculation methods.
[0052] In some embodiments, the monitoring module 210 first calculates the reasonable maximum usage capacity (i.e., a specified value) of the first application 410. After the second time point, the monitoring module 210 obtains the current capacity of the first application 410 in the main memory 120 at a given time point at regular intervals and determines whether the current capacity exceeds the specified value. If the current capacity does not exceed the specified value, the process returns to step S305 and obtains the corresponding current capacity at the next sampling time point.
[0053] Please continue to refer to Figures 1 to 3 as well as Figure 4B In response to determining that the current capacity exceeds the specified value, in step S315, the creation module 220 creates a new second application 420 corresponding to the first application 410 in the main memory 120. Next, in step S320, the acquisition module 230 obtains the execution-related data 413 of the first application 410. For example, the creation module 220 obtains the code 421 of the second application 420 by copying the code 411 of the first application 410. The acquisition module 230 obtains the execution-related data 423 of the second application 420 by copying the execution-related data 413 of the first application 410. As a result, the processor 110 can activate the second application 420 by executing the same code 421 as the code 411 of the first application 410 and the same execution-related data 423 as the execution-related data 413. In other words, the second application 420 can be considered a copy of the first application 410.
[0054] In some embodiments, reference Figures 1 to 3 as well as Figure 4C In step S325, the destruction module 240 of the memory manager 140 destroys the first application 410 in the main memory 120. Specifically, after the destruction module 240 deletes the code 411 of the first application 410, the corresponding execution-related data 413 and the unused data whose address is unknown are also deleted. Consequently, the storage space indicated by the designated address and the space occupied by the unused data are freed.
[0055] In some embodiments, after destroying the first application 410 in the main memory 120 and before the second application 420 starts running (active), the memory manager 140 broadcasts a notification to other running programs (e.g., all applications running concurrently in the operating system) that the message receiver of the first application 410 has been changed to the memory manager 140, that is, the memory manager 140 declares itself as the message receiver of the first application 410. Furthermore, upon receiving information related to the first application 410, the memory manager 140 caches the information.
[0056] In step S330, the memory manager 140 provides the execution related data 413 of the first application 410 to the second application 420. That is, the execution related data 423 previously obtained by the acquisition module 230 by copying the execution related data 413 is stored in the storage space indicated by the new designated address.
[0057] In step S335, the trigger module 250 of the memory manager 140 drives the second application 420 to start running. After the second application 420 starts running, the memory manager 140 broadcasts a notification to other active programs, changing the message receiving destination of the first application 410 to the second application 420. Furthermore, the memory manager 140 provides the second application 420 with cached information related to the first application 410.
[0058] In summary, the present disclosure utilizes a memory manager to create a new application to replace a running application whose capacity exceeds a specified value, thereby reclaiming / freeing storage space occupied by useless data, effectively utilizing main memory storage space. The memory manager automatically releases storage space when it is no longer needed, freeing up memory space. This allows programmers to focus on program logic without having to worry about managing the storage space lifecycle. Furthermore, the present disclosure's automatic storage space recovery process does not impact application functionality. Users of the product, and even other applications in the operating system that interact with the application, remain unaware of the storage space recovery process. Furthermore, when programming and developing products using computer programming languages that lack garbage collection mechanisms, programmers can utilize the technical solutions of the present disclosure to automatically reclaim storage space and reduce their workload. For example, when programmers develop network devices, since the firmware for these devices is typically written in mid-level or low-level programming languages (e.g., C / C++) that lack garbage collection mechanisms, the technical solutions of the present disclosure can automatically reclaim storage space during the development of these network devices, reducing the burden on programmers during programming.
[0059] The above-mentioned network devices include, but are not limited to: network set-top boxes, Internet phones, wireless repeaters, gateways, routers (such as routers that support geographic location detection and reporting), wireless access points (such as the new generation of WiFi 7 wireless access points, etc.), fiber optic access terminals (network devices integrated with fiber optic modems of passive optical networks (PONs), such as fiber optic access terminals that support WiFi 6e / 7, or fiber optic access terminals that support automatic detection of high-speed passive optical networks (such as Gigabit-capable PONs (GPON) or 10Gigabit Symmetrical PONs (XGS-PON)), etc.). In some practical applications, the technical solutions of the present disclosure can also be used to develop test platforms for these network devices or develop automated processes for these network devices.
[0060] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the above embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for reclaiming storage space, applicable to a memory manager of an electronic device, characterized in that: After the processor of the electronic device loads the first application into the main memory and drives the first application to run, the following steps are performed, including: monitoring, by a monitoring module of the memory manager, a current capacity occupied by the first application in the main memory, and determining whether the current capacity exceeds a specified value; In response to determining that the current capacity exceeds the specified value, a creation module of the memory manager newly creates a second application corresponding to the first application in the main memory; Obtaining, by the acquisition module of the memory manager, operation-related data of the first application; Destroying the first application in the main memory by a destruction module of the memory manager; Providing the execution-related data of the first application to the second application by a trigger module of the memory manager; and The trigger module of the memory manager drives the second application to start running, Before the monitoring module of the memory manager monitors the current capacity occupied by the first application in the main memory, the method further includes: obtaining a first capacity occupied by the first application at a first time point in a specified time segment, and obtaining a second capacity occupied by the first application at a second time point in the time segment, wherein the first time point is earlier than the second time point, and a segment length of the specified time segment = the second time point - the first time point; and The specified value is calculated based on the first capacity, the second capacity, the segment duration, and a time constant, wherein the specified value is calculated as follows: Mmax=Ms+[(Me-Ms)×Tmax / Tt], Among them, Mmax is the specified value, Ms is the first capacity, Me is the second capacity, Tt is the segment duration, and Tmax is the time constant.
2. The method for reclaiming storage space according to claim 1, characterized in that: In response to determining that the current capacity exceeds the specified value, the creation module of the memory manager newly creates the second application corresponding to the first application in the main memory, including: The second application is newly created by copying the code of the first application.
3. The method for reclaiming storage space according to claim 1, characterized in that: The step of obtaining the operation-related data of the first application program includes: The operation-related data of the first application program is retrieved from the specified path.
4. The method for reclaiming storage space according to claim 1, characterized in that: Also includes: In response to destroying the first application in the main memory and before the second application starts running, broadcasting a notification that a message receiving end of the first application is changed to the memory manager; as well as The received information related to the first application is cached.
5. The method for reclaiming storage space according to claim 4, characterized in that: Also includes: After the second application starts running, broadcast another notification to change the message receiving end of the first application to the second application; as well as The information related to the first application program cached by the memory manager is provided to the second application program.
6. An electronic device comprising: Main memory; as well as A processor, coupled to the main memory, includes a memory manager, wherein the memory manager includes a monitoring module, a creation module, an acquisition module, a destruction module, and a trigger module, and the memory manager is configured to: after loading a first application into the main memory and driving the first application to run, monitoring, by the monitoring module, a current capacity occupied by the first application in the main memory, and determining whether the current capacity exceeds a specified value; In response to determining that the current capacity exceeds the specified value, the creation module newly creates a second application corresponding to the first application in the main memory; Obtaining the operation-related data of the first application program through the acquisition module; destroying the first application in the main memory by the destruction module; as well as Providing the operation-related data of the first application to the second application through the trigger module, and driving the second application to start running, Wherein, the memory manager is configured to: Before monitoring the current capacity occupied by the first application in the main memory, obtaining a first capacity occupied by the first application at a first time point in a specified time segment, and obtaining a second capacity occupied by the first application at a second time point in the time segment, wherein the first time point is earlier than the second time point, and a segment length of the specified time segment = the second time point - the first time point; and The specified value is calculated based on the first capacity, the second capacity, the segment duration, and a time constant, wherein the specified value is calculated as follows: Mmax=Ms+[(Me-Ms)×Tmax / Tt], Among them, Mmax is the specified value, Ms is the first capacity, Me is the second capacity, Tt is the segment duration, and Tmax is the time constant.
7. The electronic device according to claim 6, wherein: The creation module is used to create the second application by copying the code of the first application.
8. The electronic device according to claim 6, wherein: The acquisition module retrieves the operation-related data of the first application from a specified path.
9. The electronic device according to claim 6, wherein: In response to destroying the first application in the main memory and before the second application starts running, the memory manager broadcasts a notification to change the message receiving end of the first application to the memory manager; and caches the received information related to the first application.
10. The electronic device according to claim 9, wherein: The memory manager also broadcasts another notification to change the message receiving end of the first application to the second application after the second application starts running; and provides the information related to the first application cached by the memory manager to the second application.
Citation Information
Patent Citations
Method and device for recycling internal memory
CN103324500A
Method and device of collecting memory
CN107526641A