WO2022143446A1 - 内存释放方法及相关设备 - Google Patents
内存释放方法及相关设备 Download PDFInfo
- Publication number
- WO2022143446A1 WO2022143446A1 PCT/CN2021/141095 CN2021141095W WO2022143446A1 WO 2022143446 A1 WO2022143446 A1 WO 2022143446A1 CN 2021141095 W CN2021141095 W CN 2021141095W WO 2022143446 A1 WO2022143446 A1 WO 2022143446A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- memory
- garbage collection
- release
- electronic device
- thread
- Prior art date
Links
- 238000000034 method Methods 0.000 title claims abstract description 102
- 238000000605 extraction Methods 0.000 claims abstract description 12
- 239000000284 extract Substances 0.000 claims abstract description 11
- 230000008569 process Effects 0.000 claims description 35
- 238000005070 sampling Methods 0.000 claims description 18
- 230000006870 function Effects 0.000 description 13
- 238000010586 diagram Methods 0.000 description 12
- 230000007246 mechanism Effects 0.000 description 11
- 238000012545 processing Methods 0.000 description 6
- 230000009471 action Effects 0.000 description 5
- 230000009286 beneficial effect Effects 0.000 description 4
- 238000004590 computer program Methods 0.000 description 4
- 230000001960 triggered effect Effects 0.000 description 4
- 238000004891 communication Methods 0.000 description 3
- 230000008878 coupling Effects 0.000 description 3
- 238000010168 coupling process Methods 0.000 description 3
- 238000005859 coupling reaction Methods 0.000 description 3
- 238000001514 detection method Methods 0.000 description 3
- 238000005516 engineering process Methods 0.000 description 3
- 239000002699 waste material Substances 0.000 description 3
- 239000004973 liquid crystal related substance Substances 0.000 description 2
- 238000011084 recovery Methods 0.000 description 2
- 238000012216 screening Methods 0.000 description 2
- 230000005236 sound signal Effects 0.000 description 2
- 230000003068 static effect Effects 0.000 description 2
- 230000004083 survival effect Effects 0.000 description 2
- 239000000725 suspension Substances 0.000 description 2
- 241000699670 Mus sp. Species 0.000 description 1
- 230000001133 acceleration Effects 0.000 description 1
- 230000005540 biological transmission Effects 0.000 description 1
- 238000013461 design Methods 0.000 description 1
- 238000007599 discharging Methods 0.000 description 1
- 238000002474 experimental method Methods 0.000 description 1
- 230000005484 gravity Effects 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
- 238000005192 partition Methods 0.000 description 1
- 238000004064 recycling Methods 0.000 description 1
- 238000010079 rubber tapping Methods 0.000 description 1
- 239000007787 solid Substances 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
- 238000010897 surface acoustic wave method Methods 0.000 description 1
- 230000000007 visual effect Effects 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5022—Mechanisms to release resources
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5016—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/455—Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
- G06F9/45533—Hypervisors; Virtual machine monitors
- G06F9/45558—Hypervisor-specific management and integration aspects
- G06F2009/45583—Memory management, e.g. access or allocation
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/455—Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
- G06F9/45533—Hypervisors; Virtual machine monitors
- G06F9/45558—Hypervisor-specific management and integration aspects
Definitions
- the present application relates to the field of computer technology, and in particular, to a memory release method and related equipment.
- Garbage Collection (GC) mechanism is an automatic memory management mechanism. In the programming language running on the virtual machine, when the object in the computer memory is no longer used in the application, the memory block occupied by the object will be released according to the garbage collection mechanism for other applications or objects to use. .
- the garbage collection mechanism originated from the LISP language, and most high-level languages currently support the garbage collection mechanism, such as Java, .net, and Go.
- the embodiments of the present application disclose a memory release method and related equipment, which can speed up memory release and ensure smooth memory release.
- a first aspect of the present application discloses a memory release method, which is applied to an electronic device, where the electronic device includes an application program and a virtual machine, the application program runs in the virtual machine, and the virtual machine creates a garbage collection thread , a memory release trigger and a memory release thread, the electronic device further includes a free memory list and a pre-release memory list, the free memory list records the free memory of the electronic device, and the method includes: if the application is In a low load state, the memory release trigger sends a memory release trigger signal to the garbage collection thread; the garbage collection thread obtains the heap memory usage of the electronic device at multiple time points; receives the memory release trigger signal Then, the garbage collection thread determines the actual extracted memory size according to the heap memory usage; the garbage collection thread extracts the free memory in the free memory list to the pre-release memory according to the actual extracted memory size list; the memory release thread releases the memory in the pre-release memory list to the operating system of the electronic device.
- This application does not need to wait for a specific stage (such as Full GC) to release memory, and can release memory in any garbage collection mode, which speeds up memory release.
- a specific stage such as Full GC
- the present application avoids application program suspension caused by frequently releasing memory, and also avoids additional time overhead caused by releasing memory at inappropriate timing.
- this application extracts the free memory from the free memory list to the pre-release memory list, and releases the free memory in the pre-release memory list to the operating system through the memory release thread.
- the whole process is completely decoupled from the user thread and the garbage collection thread, and does not This can cause user threads or garbage collection threads to stall waiting for free memory to be released to the operating system.
- the virtual machine further creates a sampling thread
- the application program being in a low load state includes: the load statistics value and the garbage collection times statistics value of the electronic device are both less than or equal to a preset threshold ; wherein, the load statistics value is obtained by the memory release trigger performing statistics on load data at multiple time points of the electronic device, and the load data is collected by the sampling thread; the garbage collection times The statistical value is obtained by the memory release trigger performing statistics on the number of garbage collections of the electronic device, and the number of garbage collections is collected by the garbage collection thread according to a preset time interval.
- the low-load state of the application can be more accurately determined according to the load statistics and the statistics of the number of garbage collections, thereby achieving more efficient memory release.
- the load data includes CPU load data of the operating system and CPU load data of the process.
- X is the logical value of the load state of the application
- O is the CPU load statistics value of the operating system
- P is the CPU load statistics value of the process
- T is the CPU load threshold
- I is the cycle of the memory release trigger.
- the performing statistics on the load data of the electronic device at multiple time points includes: calculating a weighted average value of the load data, and using the weighted average value of the load data as the load Statistics.
- the performing statistics on the number of garbage collections includes: calculating a weighted average of the number of garbage collections, and using the weighted average of the number of garbage collections as the statistical value of the number of garbage collections .
- the determining, by the garbage collection thread, the actual size of the extracted memory according to the heap memory usage includes: the garbage collection thread predicting according to the heap memory usage to obtain a predicted value of the heap memory usage ; the garbage collection thread calculates the size of the extractable memory according to the predicted value of the heap memory usage; the garbage collection thread determines the actual size of the extracted memory according to the size of the extractable memory.
- the freed memory can meet the needs of the application without wasting memory space.
- the predicting according to the heap memory usage includes: calculating a weighted average of the heap memory usage, and using the weighted average of the heap memory usage as the heap memory Usage forecast.
- the method before the garbage collection thread calculates the extractable memory size according to the heap memory usage prediction value, the method further includes: the garbage collection thread determines whether the pre-release memory list is Empty; if the pre-release memory list is empty, the garbage collection thread determines whether the size of the heap memory of the electronic device is greater than the predicted value of the heap memory usage; if the size of the heap memory is greater than the predicted value of the heap memory usage , the garbage collection thread calculates the size of the extractable memory according to the predicted value of the heap memory usage.
- the memory space in the intermediate state is the memory space that the operating system cannot allow other processes to reuse, and the virtual machine can no longer use it.
- the application program is a Java application program
- the virtual machine is a Java virtual machine
- the garbage collection thread in the final marking stage of the hybrid garbage collection of the Java virtual machine, is executed to obtain the heap memory usage of the electronic device at multiple time points, the garbage collection thread The actual extracted memory size is determined according to the heap memory usage, and the garbage collection thread extracts the free memory in the free memory list to the pre-release memory list according to the actual extracted memory size.
- G1 will often trigger young generation garbage collection, and arranging memory release in the young generation garbage collection stage will result in frequent memory release. Arranging memory release in the final marking phase of mixed garbage collection can avoid frequent memory release and ensure smooth memory release.
- a second aspect of the present application discloses a computer-readable storage medium, comprising computer instructions, which, when the computer instructions are executed on an electronic device, cause the electronic device to execute the memory release method described in the first aspect.
- a third aspect of the present application discloses an electronic device, the electronic device includes a processor and a memory, the memory is used to store instructions, and the processor is used to call the instructions in the memory, so that the electronic device executes The memory release method as described in the first aspect.
- a fourth aspect of the present application discloses a chip system, which is applied to an electronic device; the chip system includes an interface circuit and a processor; the interface circuit and the processor are interconnected by lines; the interface circuit is used for receiving signals from a memory of the electronic device, A signal is sent to the processor, where the signal includes computer instructions stored in the memory; when the processor executes the computer instructions, the chip system executes the memory release method described in the first aspect.
- the computer-readable storage medium of the second aspect, the electronic device of the third aspect, and the chip system of the fourth aspect provided above all correspond to the method of the first aspect. Therefore, the beneficial effects that can be achieved can be With reference to the beneficial effects in the corresponding methods provided above, details are not repeated here.
- FIG. 1 is a schematic diagram of an application environment of a memory release method disclosed in an embodiment of the present application.
- FIG. 2 is a schematic diagram of a memory life cycle according to an existing memory release method.
- FIG. 3 is a schematic diagram of a main logic unit of an electronic device implementing the memory release method of the present application.
- FIG. 4 is a flowchart of a memory release method disclosed in an embodiment of the present application.
- Figure 5 is a schematic diagram of collecting load data and garbage collection times.
- Figure 6 is a schematic diagram of hybrid garbage collection.
- FIG. 7 is a schematic structural diagram of an electronic device disclosed in an embodiment of the present application.
- An operating system is a computer program that manages computer hardware and software resources.
- the operating system needs to handle basic tasks within the computer, such as managing and configuring memory, prioritizing the supply and demand of system resources, controlling input and output devices, operating the network, and managing the file system.
- the operating system also provides an interface for the user to interact with the system.
- Garbage Collection It is an automatic memory management mechanism. In object-oriented design languages, when an object in computer memory is no longer used in an application, the garbage collection mechanism will release the memory block occupied by the object for reuse by other applications or objects.
- Java is an object-oriented programming language. Java has the characteristics of simplicity, object orientation, distribution, robustness, security, platform independence and portability, multithreading, and dynamism. Java can write desktop applications, web applications, distributed systems and embedded system applications.
- Java Virtual Machine The Java Virtual Machine is the runtime environment of the Java language binary bytecode, which is responsible for loading the bytecode into it and interpreting/compiling it into machine instructions for execution on the corresponding platform.
- G1 Garbage First: G1 is a server-oriented JVM garbage collector (also called a garbage collector), which is suitable for server-side systems with multi-core processors and large memory capacity. G1 focuses on the goal of minimum latency garbage collection while achieving high throughput.
- the STW mechanism is that when garbage collection is performed, except for the garbage collection (GC) thread, other threads of the application are suspended.
- GC garbage collection
- the JVM only allows the GC thread to run, and all other threads will be suspended, waiting for the GC thread to finish executing before running again.
- Full Garbage Collection (Full GC): Usually refers to a complete garbage collection, which recycles the entire heap memory (including the old generation, young generation, metaspace, etc.).
- Young/Minor Garbage Collection (Young/Minor Garbage Collection, Young GC, also called Cenozoic Garbage Collection): Garbage collection is performed on the heap memory of the young/new generation. The memory is divided into the young generation and the old generation. The young generation stores objects that have just been created and have a relatively short survival time, and the old generation stores objects that have a relatively long survival time.
- Full GC, Young GC and Mixed GC are the three garbage collection modes of G1. It is usually time-consuming to release a large amount of memory at one time (that is, release memory in batches) to the operating system. In order to reduce the impact of releasing memory in batches on the operating system, G1 puts the batch memory release operation in the Full GC stage (Young GC and Mixed GC only Record free memory to the free memory list without freeing). On the one hand, it still takes a long time to release memory in batches during the Full GC phase. On the other hand, G1 will suspend the application during Full GC (Stop The World, STW). In order to avoid application freeze, G1 will try to avoid triggering Full GC as much as possible.
- G1's strategy of avoiding triggering Full GC as much as possible will cause the JVM garbage collection mechanism to be in a relatively static state, and will not trigger Full GC (that is, the memory release to the operating system fails).
- Full GC is forced to be executed from the outside
- G1 will keep and occupy all heap memory, and will not release free heap memory to the operating system, resulting in a waste of memory resources.
- G1's failure to release free heap memory to the operating system will cause users to pay unnecessary fees, and cloud providers will not be able to fully utilize their hardware resources.
- FIG. 1 is a schematic diagram of an application environment of a memory release method disclosed in an embodiment of the present application. As shown in FIG. 1 , the memory release method is applied to the electronic device 10 .
- the electronic device 10 includes at least one application 100 (one is shown in the figure), such as a video player, a game, a social application, and the like.
- the electronic device 10 further includes a virtual machine 101 in which the application program 100 runs.
- the memory release method provided by the embodiment of the present application calculates the actual extracted memory size when the application 100 running in the virtual machine 101 is in a low load state, and extracts the free memory from the free memory list to the pre-release free memory list according to the actual extracted memory size , release the free memory in the pre-release free memory list to the operating system of the electronic device 10 .
- the present application can quickly release the memory to ensure smooth memory release.
- the electronic device 10 may include a smart phone, a tablet computer, a desktop computer, a laptop portable computer, an in-vehicle computer, and the like.
- the application 100 may be a Java application
- the virtual machine 101 may be a Java virtual machine.
- the application program 100 and the virtual machine 101 may be application programs and virtual machines corresponding to other programming languages.
- the application 100 may be a Go language application
- the virtual machine 101 may be an LVM virtual machine.
- FIG. 2 is a schematic diagram of a memory life cycle according to an existing memory release method.
- the JVM is used as an example for illustration.
- the memory life cycle includes the following stages:
- the JVM applies for reserved memory.
- the JVM requests the operating system to reserve a portion of memory. At this stage, the operating system does not actually allocate a memory area to the JVM and monopolize the memory area of the specified size.
- the JVM occupies the requested memory.
- the JVM triggers a call to the operating system's mmap method.
- the operating system will allocate a memory area of the specified size to the JVM for exclusive use, and other processes running on the operating system cannot reuse this memory area.
- the JVM divides the applied memory into small memory blocks (Regions), and records these memory blocks in the free memory list.
- the application program creates an object.
- a user thread (ie, an application thread) on the JVM applies for a memory block.
- the user thread on the JVM allocates a memory block for the object in the applied memory block, and stores the object data in the memory block.
- the application releases the object.
- the application releases previously created objects that are no longer in use.
- G1 of the JVM triggers garbage collection and suspends the application.
- Triggered garbage collection can be Young GC, Mixed GC or Full GC.
- a tentative application is an application hang.
- the G1 of the JVM records the memory block occupied by the object to the free memory list.
- G1 records the memory block occupied by the object to the free memory list.
- G1 applies for a new memory block from the free memory list, copies the object data still in use to the new memory block, and records the memory block originally occupied by the object in the free memory list .
- G1 continues to suspend the application.
- G1 releases the free memory to the operating system according to the free memory list while continuing to suspend the application program.
- G1 releases free memory from the free memory list to the operating system during the Full GC phase, and other processes on the operating system can reuse the memory released to the operating system.
- G1 releases free memory from the free memory list to the operating system during the Full GC phase.
- This memory release method is time-consuming and may cause application freezes.
- the present application improves the memory release process to speed up memory release and ensure smooth memory release.
- FIG. 3 is a schematic diagram of a main logic unit of an electronic device implementing the memory release method of the present application.
- the electronic device 30 includes a sampling thread 300 , a garbage collection (GC) thread 301 , a memory release trigger 302 , a memory release thread 303 , and an operating system 304 .
- a virtual machine (not shown in the figure) may create a sampling thread 300 , a garbage collection (GC) thread 301 , a memory release trigger 302 , and a memory release thread 303 at startup.
- the electronic device also includes a policy class 305 and a memory manager 306 .
- the strategy class 305 is used to store various data obtained in the memory release method of the present application, such as CPU load data of the operating system, CPU load data of the Java process, GC times, and heap memory usage.
- the memory manager 306 stores free memory lists, pre-release memory lists, and heap memory information for applications. Heap memory information includes heap memory usage and heap memory size.
- the heap memory usage stored in the memory manager 306 is the real-time heap memory usage of the electronic device 30, that is, real-time data.
- the heap memory usage stored in the policy class 305 is the heap memory usage at multiple time points, that is, time series data.
- the sampling thread 300 is used to collect load data, and the load data in the figure includes the CPU load data of the operating system and the CPU load data of the Java process.
- the sampling thread 300 writes the collected load data into the policy class 305 .
- the garbage collection thread (ie, GC thread) 301 is used to collect the number of garbage collections (ie, the number of GCs), and write the number of garbage collections into the policy class 305 .
- the memory release trigger 302 is used to collect statistics on load data to obtain a load statistical value, and to collect statistics on the number of garbage collections to obtain a statistical value of the number of garbage collections.
- the memory release trigger 302 is further configured to determine whether the application is in a low load state according to the load statistics value and the garbage collection number statistics value, and if the application program is in a low load state, send a memory release trigger signal to the garbage collection thread.
- the garbage collection thread 301 is further configured to obtain the heap memory usage at multiple time points from the memory manager, write the heap memory usage into the policy class 305, and perform prediction according to the heap memory usage to obtain a heap memory usage prediction value.
- the garbage collection thread 301 is further configured to calculate the extractable memory size according to the predicted value of heap memory usage, determine the actual extracted memory size according to the extractable memory size, and extract free memory from the free memory list to the prerelease memory list according to the actual extracted memory size.
- the memory release thread 303 is used to release the memory in the pre-release memory list to the operating system 304 .
- FIG. 4 is a flowchart of a memory release method disclosed in an embodiment of the present application.
- the memory release method is applied to an electronic device.
- the electronic device includes an application program and a virtual machine.
- the application program runs in the virtual machine, and the virtual machine includes a sampling thread, a garbage collection thread, a memory release trigger and a memory release thread.
- the following description takes the application as a Java application and the virtual machine as a JVM as an example.
- the sampling thread collects load data of the electronic device at multiple time points.
- the sampling thread collects the load data of the electronic device every 300 milliseconds.
- the load data includes the CPU load data of the operating system and the CPU load data of the Java process.
- the sampling thread obtains the CPU load data of the operating system from the operating system, and obtains the CPU load data of the Java process from the Java process.
- the sampling thread may write load data into the policy class of G1.
- the memory release trigger collects statistics on the load data to obtain the load statistics value.
- the memory release trigger can read the load data (ie, the time series data of the load data) from the policy class of G1.
- the memory release trigger collects statistics on the CPU load data of the operating system to obtain the CPU load statistics value of the operating system, and collects statistics on the CPU load data of the Java process to obtain the CPU load statistics value of the Java process. .
- the memory release trigger can collect statistics on a preset number (for example, ten) of load data before a preset time point to obtain a load statistics value.
- the memory release trigger may calculate the load statistics value by using a weighted average method (ie, calculating a weighted average value of the load data).
- the garbage collection thread collects the number of times of garbage collection of the electronic device according to a preset time interval (for example, every minute).
- the garbage collection thread may write the number of garbage collections collected according to a preset time interval (ie, the time series data of the number of garbage collections) into the strategy class of G1.
- the garbage collection thread determines whether the statistical duration of the number of garbage collections exceeds a preset time interval, and if it does not exceed the preset time interval, increments the number of garbage collections by 1. If it exceeds the preset time interval, write the current number of garbage collections into the policy class of G1, reset the number of garbage collections to 1, and re-time the statistics of the number of garbage collections.
- the memory release trigger counts the number of garbage collections to obtain a statistical value of the number of garbage collections.
- the memory release trigger can read the number of garbage collections (ie, the time series data of the number of garbage collections) from the policy class of G1.
- the memory release trigger can count the number of garbage collections of a preset number (for example, ten) before a preset time point, and obtain a statistical value of the number of garbage collections.
- a preset number for example, ten
- the memory release trigger may calculate the statistical value of the number of garbage collections by using a weighted average method (ie, calculating the weighted average of the number of garbage collections).
- the memory release trigger determines whether the application is in a low load state according to the load statistics value and the garbage collection times statistics value.
- the memory release trigger can periodically (for example, once every 15 seconds) determine whether the application is in a low load state according to the load statistics and the statistics of the number of garbage collections.
- the load statistics include the CPU load statistics of the operating system and the CPU load statistics of the Java process. If any of the operating system CPU load statistics, the CPU load statistics of the Java process, and the number of garbage collection statistics are less than or equal to the preset threshold, the application is judged to be in a low load state.
- X The logical value of the load state of the application, TRUE (that is, any one of (O>T), (P>T), (G>(60/I) is established) indicates that the application is in a high load state, FALSE ( That is, any one of (O>T), (P>T), (G>(60/I) does not hold) indicates that the application is in a low load state;
- CPU load threshold set according to actual needs, for example, set to 10% of CPU full load
- G GC times statistics
- I The period of the memory release trigger, set according to actual needs, for example, set it to 15 seconds.
- the CPU load statistics of the operating system and the CPU load statistics of the Java process may correspond to the same preset threshold, or may correspond to different preset thresholds.
- the preset threshold value of the CPU load statistics value of the operating system may be 10% of the full CPU load
- the preset threshold value of the CPU load statistics value of the Java process may be 15% of the full CPU load.
- the load data and the number of garbage collections are obtained, the load data is counted to obtain a load statistics value, the number of garbage collections is counted to obtain a statistics value of the number of garbage collections, and whether the application is determined according to the load statistics value and the statistics value of the number of garbage collections In the low-load state, the low-load state of the application can be more accurately determined, thereby achieving more efficient memory release.
- the load data or the number of garbage collections may be obtained, the load data may be counted to obtain a load statistics value, or the number of garbage collections may be counted to obtain a statistics value of the number of garbage collections. The count of times determines whether the application is in a low load state.
- the memory release trigger sends a memory release trigger signal to the garbage collection thread.
- the garbage collection thread obtains the heap memory usage of the electronic device at multiple time points from the memory manager.
- the memory manager stores the real-time heap memory usage of the electronic device, and the garbage collection thread can periodically obtain the heap memory usage from the memory manager to obtain the heap memory usage at multiple time points.
- the garbage collection thread may write the acquired heap memory usage into the policy class of G1.
- the garbage collection thread after receiving the memory release trigger signal, makes predictions according to the heap memory usage to obtain the predicted value of the heap memory usage.
- the garbage collection thread can read the heap memory usage (ie, the time series data of the heap memory usage) from the policy class of G1.
- the garbage collection thread can predict the amount of heap memory usage of a preset number (for example, ten) before the preset time point, and obtain the predicted value of the amount of heap memory usage.
- a preset number for example, ten
- the garbage collection thread may calculate the predicted value of heap memory usage by using a weighted average method (ie, calculating a weighted average of heap memory usage).
- the memory blocks that can be released by the application will be added to the free memory list. If there is too much memory (that is, memory blocks) extracted from the free memory list to the pre-release memory list each time, when the application fluctuates the load becomes high and the free list memory When there are not enough memory blocks, G1 will first trigger GC, and triggering GC will cause the application to hang. The application hangs continuously during this phase as well. If the extraction is too small, it will cause the application to occupy too much free memory that is held, and other processes cannot reuse this part of the memory.
- prediction is performed at the start of each GC, and it is determined that the application program is at a preset time point How much memory will be required.
- the garbage collection thread calculates the size of extractable memory according to the predicted value of heap memory usage.
- the extractable memory size can be calculated according to the following formula:
- K The initial memory size set when the JVM starts
- MAX() means to calculate the maximum value.
- the pre-release memory list is a linked list structure.
- the garbage collection thread before calculating the size of extractable memory according to the predicted value of heap memory usage at a preset time point, the garbage collection thread also determines whether the pre-release memory list is empty. If the pre-release memory list is empty, the garbage collection thread determines whether the size of the heap memory of the electronic device is greater than the predicted value of the heap memory usage. If the size of the heap memory is greater than the predicted value of the heap memory usage, the garbage collection thread calculates the size of the extractable memory according to the predicted value of the heap memory usage at the preset time point. If the pre-release memory list is not empty, or the heap memory size is less than or equal to the predicted value of heap memory usage, the process ends.
- the heap memory is created when the virtual machine starts and is used to store various objects of the application.
- the memory space in the intermediate state is the memory space that the operating system cannot allow other processes to reuse, and the virtual machine can no longer use it.
- the garbage collection thread determines the actual size of the extracted memory according to the size of the extractable memory.
- the purpose of determining the actual extracted memory size according to the extractable memory size is to release free memory more reasonably, so that the memory usage at the preset time point is close to the actual memory usage of the application.
- the extraction ratio can be set according to actual needs, for example, it is set to 10%.
- the extraction ratio can be set by specifying parameters when the JVM starts.
- the garbage collection thread extracts the free memory in the free memory list to the pre-release memory list according to the actual size of the extracted memory.
- GC threads and user threads can only operate memory blocks in the free memory list, and memory blocks in the pre-release memory list cannot be operated by user threads and GC threads. Extracting the free memory in the free memory list to the pre-release memory list can realize that the memory release thread does not affect the execution of the GC thread and the user thread when the free memory is released.
- the memory release thread releases the memory in the pre-release memory list to the operating system.
- the existing memory release method may have the problem that the garbage collection takes a long time and sometimes the garbage collection fails. For example, it will take a long time for G1 to release a large amount of memory at one time during the Full GC phase. When the application load of the JVM drops or is inactive, the strategy of G1 to avoid triggering the Full GC as much as possible will cause the JVM garbage collection mechanism to be in a relatively static state. Full GC will no longer be triggered, resulting in a waste of memory resources.
- this application collects load data (such as the CPU load data of the operating system, the CPU load data of the Java process) and the number of GCs, and determines whether the application is in a low-load state according to the load data and the number of GCs, and when the application is in a low-load state When the free memory is extracted to the pre-release memory list for release.
- This application does not need to wait for a specific stage (such as Full GC) to release memory, and can release memory in any garbage collection mode, which speeds up memory release.
- This application avoids frequent (extraction action depends on GC logic, G1 various types of GC frequency will be very high) to release memory to cause application suspension, and also avoids inappropriate timing (that is, the application is in a high load state) to release memory and cause additional time cost. If the memory is released when the application is under heavy load, the application will re-apply for memory from the OS, which will result in a certain time overhead.
- This application extracts the free memory from the free memory list to the pre-release memory list, and releases the free memory in the pre-release memory list to the OS through the memory release thread.
- the whole process is completely decoupled from the user thread and the GC thread, and will not cause the user thread Or the GC thread gets stuck waiting for free memory to be released to the OS.
- This application makes predictions based on the heap memory usage, calculates the extractable memory size according to the predicted value of the heap memory usage, and determines the actual extracted memory size according to the extractable memory size, so that the released memory usage fits close to the actual memory of the application.
- the amount of usage to achieve smooth release of free memory to the OS, to avoid the negative impact of releasing memory when the application's load fluctuates.
- FIG. 5 it is a schematic diagram of collecting load data and the number of garbage collections.
- the JVM initializes and starts the sampling thread at startup.
- the sampling thread collects CPU load data of the operating system from the operating system.
- the sampling thread writes the CPU load data of the operating system into the policy class of G1.
- the sampling thread collects CPU load data of the Java process from the operating system.
- the sampling thread writes the CPU load data of the Java process into the policy class of G1.
- the garbage collection thread counts the number of times of garbage collection in each time interval.
- the garbage collection thread writes the number of times of garbage collection into the strategy class of G1.
- the releasable free memory may be extracted to the pre-release list in the final mark (Remark) phase of the Mixed GC, so as to further reduce the application program suspension time.
- the garbage collection thread predicts the preset time point according to the heap memory usage, and obtains the predicted value of the heap memory usage.
- the garbage collection thread calculates the extractable memory size according to the predicted value of heap memory usage at a preset time point.
- the garbage collection thread determines the actual extracted memory size according to the size of the extractable memory, and extracts the free memory in the free memory list to the pre-release memory list according to the actual extracted memory size.
- the application suspension time is short, so that the application can be prevented from being suspended for a long time due to memory release.
- Mixed GC includes four stages: initial marking, concurrent marking, final marking, and screening and recycling.
- the Initial Mark phase is used to mark objects that the GC root can directly associate with (short-lived STW).
- the concurrent mark phase is used to traverse from the root in the concurrent mark to mark unreachable objects, which takes a long time but can be concurrent.
- the final mark (Remark) phase is used to collect new garbage (short-lived STW) generated during concurrent marks.
- the screening and recovery (Live Data Counting and Evacuation) stage is used to sort the recovery cost-effectiveness of each Region, and clear the deactivated objects while ensuring that the time is controllable.
- FIG. 7 is a schematic structural diagram of an electronic device disclosed in an embodiment of the present application.
- the electronic device 40 may include: a radio frequency (RF) circuit 701, a memory 702, an input unit 703, a display unit 704, a sensor 705, an audio circuit 706, a wireless fidelity (Wi-Fi) ) module 707, processor 708 and power supply 709 and other components.
- RF radio frequency
- the RF circuit 701 can be used to send and receive information or to receive and transmit signals during a call. In particular, after receiving the downlink information of the base station, it is transferred to the processor 708 for processing; in addition, it sends the uplink data to the base station.
- the RF circuit 701 includes, but is not limited to, an antenna, at least one amplifier, a transceiver, a coupler, a Low Noise Amplifier (LNA), a duplexer, and the like.
- LNA Low Noise Amplifier
- the memory 702 can be used to store software programs and modules, and the processor 708 executes various functional applications and data processing of the electronic device by running the software programs and modules stored in the memory 702 .
- the memory 702 may mainly include a stored program area and a stored data area, wherein the stored program area may store an operating system, an application program (such as a sound playback function, an image playback function, etc.) required for at least one function, and the like; Data created by the use of electronic equipment (such as audio data, phone book, etc.), etc.
- memory 702 may include high-speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
- the input unit 703 may be used to receive input numerical or character information, and generate key signal input related to user settings and function control of the electronic device.
- the input unit 703 may include a touch panel 7031 and other input devices 7032 .
- the touch panel 7031 also known as the touch screen, collects the user's touch operations on or near it (such as the user using a finger, a stylus, etc., any suitable object or accessory on or near the touch panel 7031) operation), and drive the corresponding connection device according to the preset program.
- the touch panel 7031 may include two parts, a touch detection device and a touch controller.
- the touch detection device detects the user's touch orientation, detects the signal brought by the touch operation, and transmits the signal to the touch controller; the touch controller receives the touch information from the touch detection device, converts it into contact coordinates, and then sends it to the touch controller.
- the touch panel 7031 can be implemented in various types such as resistive, capacitive, infrared, and surface acoustic waves.
- the input unit 703 may also include other input devices 7032 .
- other input devices 7032 may include, but are not limited to, one or more of physical keyboards, function keys (such as volume control keys, switch keys, etc.), trackballs, mice, joysticks, and the like.
- the display unit 704 may be used to display information input by the user or information provided to the user and various menus of the electronic device.
- the display unit 704 may include a display panel 7041, and optionally, the display panel 7041 may be configured in the form of a liquid crystal display (Liquid Crystal Display, LCD), an organic light-emitting diode (Organic Light-Emitting Diode, OLED), or the like.
- the touch panel 7031 can cover the display panel 7041. When the touch panel 7031 detects a touch operation on or near it, it transmits it to the processor 708 to determine the type of the touch event, and then the processor 708 determines the type of the touch event according to the touch event. Type provides corresponding visual output on display panel 7041.
- the touch panel 7031 and the display panel 7041 are used as two independent components to realize the input and output functions of the electronic device, but in some embodiments, the touch panel 7031 and the display panel 7041 may be integrated And realize the input and output functions of electronic equipment.
- the electronic device may also include at least one sensor 705, such as light sensors, motion sensors, and other sensors.
- the light sensor may include an ambient light sensor and a proximity sensor, wherein the ambient light sensor may adjust the brightness of the display panel 7041 according to the brightness of the ambient light, and the proximity sensor may turn off the display panel 7041 and the display panel 7041 when the electronic device is moved to the ear. / or backlight.
- the accelerometer sensor can detect the magnitude of acceleration in all directions (generally three axes), and can detect the magnitude and direction of gravity when stationary, and can be used for applications that recognize the posture of electronic devices (such as horizontal and vertical screen switching, related games, magnetometer attitude calibration), vibration recognition related functions (such as pedometer, tapping), etc.; in addition, electronic devices can also be equipped with other sensors such as gyroscopes, barometers, hygrometers, thermometers, infrared sensors, etc. This will not be repeated here.
- Audio circuitry 706, speakers 7061, and microphones 7062 may provide an audio interface between the user and the electronic device.
- the audio circuit 706 can convert the received audio data into an electrical signal, and transmit it to the speaker 7061, and the speaker 7061 converts it into a sound signal for output; on the other hand, the microphone 7062 converts the collected sound signal into an electrical signal, which is converted by the audio circuit 706. After receiving, it is converted into audio data, and then the audio data is output to the processor 708 for processing, and then sent to another electronic device through the RF circuit 701, or the audio data is output to the memory 702 for further processing.
- Wi-Fi is a short-distance wireless transmission technology, and the electronic device can help users to send and receive emails, browse web pages, and access streaming media through the WiFi module 707, which provides users with wireless broadband Internet access.
- FIG. 4 shows the Wi-Fi module 707, it can be understood that it is not a necessary component of the electronic device, and can be completely omitted as required and within the scope of not changing the essence of the invention.
- the processor 708 is the control center of the electronic device, using various interfaces and lines to connect various parts of the entire electronic device, by running or executing the software programs and/or modules stored in the memory 702, and calling the data stored in the memory 702. , perform various functions of electronic equipment and process data, so as to monitor electronic equipment as a whole.
- the processor 708 may include one or more processing units; preferably, the processor 708 may integrate an application processor and a modem, wherein the application processor mainly handles the operating system, user interface, and application programs, and the modem mainly handles Wireless communication. It can be understood that the above-mentioned modulation and demodulation processor may not be integrated into the processor 708 .
- the electronic device also includes a power supply 709 (such as a battery) for supplying power to various components.
- a power supply 709 (such as a battery) for supplying power to various components.
- the power supply can be logically connected to the processor 708 through a power management system, so as to manage charging, discharging, and power consumption management functions through the power management system. .
- the electronic device may further include a camera, a Bluetooth module, and the like, which will not be repeated here.
- the electronic device described in FIG. 4 may be used to implement part or all of the processes in the method embodiment described in FIG. 2 of the present application, and reference may be made to the relevant descriptions in the foregoing embodiment in FIG. 2 , which will not be repeated here.
- This embodiment also provides a computer storage medium, where computer instructions are stored in the computer storage medium, and when the computer instructions are executed on the electronic device, the electronic device executes the above-mentioned relevant method steps to realize the memory release method in the above-mentioned embodiment.
- This embodiment also provides a computer program product, which, when the computer program product runs on the electronic device, causes the electronic device to execute the above-mentioned relevant steps, so as to realize the memory release method in the above-mentioned embodiment.
- the embodiments of the present application also provide an apparatus, which may specifically be a chip, a component or a module, and the apparatus may include a connected processor and a memory; wherein, the memory is used for storing computer execution instructions, and when the apparatus is running, The processor can execute the computer-executed instructions stored in the memory, so that the chip executes the memory release method in each of the foregoing method embodiments.
- the electronic device, computer storage medium, computer program product or chip provided in this embodiment are all used to execute the corresponding method provided above. Therefore, for the beneficial effects that can be achieved, reference can be made to the corresponding provided above. The beneficial effects in the method will not be repeated here.
- the disclosed apparatus and method may be implemented in other manners.
- the device embodiments described above are only illustrative.
- the division of the modules or units is only a logical function division. In actual implementation, there may be other division methods.
- multiple units or components may be combined. Or it may be integrated into another device, or some features may be omitted, or not implemented.
- the shown or discussed mutual coupling or direct coupling or communication connection may be through some interfaces, indirect coupling or communication connection of devices or units, and may be in electrical, mechanical or other forms.
- the units described as separate components may or may not be physically separated, and components shown as units may be one physical unit or multiple physical units, that is, may be located in one place, or may be distributed to multiple different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution in this embodiment.
- each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically alone, or two or more units may be integrated into one unit.
- the above-mentioned integrated units may be implemented in the form of hardware, or may be implemented in the form of software functional units.
- the integrated unit if implemented in the form of a software functional unit and sold or used as an independent product, may be stored in a readable storage medium.
- a readable storage medium including several instructions to make a device (which may be a single-chip microcomputer, a chip, etc.) or a processor (processor) to execute all or part of the steps of the methods described in the various embodiments of the present application.
- the aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), magnetic disk or optical disk and other media that can store program codes .
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Memory System (AREA)
Abstract
一种内存释放方法及相关设备,应用于电子设备,电子设备包括垃圾回收线程、内存释放触发器和内存释放线程,所述方法包括:若应用程序为低负载状态,内存释放触发器向垃圾回收线程发送内存释放触发信号;垃圾回收线程获取多个时间点电子设备的堆内存使用量;接收到内存释放触发信号后,垃圾回收线程根据堆内存使用量确定实际抽取内存大小;垃圾回收线程根据实际抽取内存大小,将空闲内存列表中的空闲内存抽取到预释放内存列表;内存释放线程将预释放内存列表中的内存释放给电子设备的操作系统;能够加快内存释放并保证内存释放顺利进行。
Description
本申请涉及计算机技术领域,尤其涉及一种内存释放方法及相关设备。
垃圾回收(Garbage Collection,GC)机制是一种自动的内存管理机制。在虚拟机上运行的编程语言中,当计算机内存中的对象在应用程序中不再被使用时,根据垃圾回收机制会把这个对象占用的内存区块释放出来,让其他的应用程序或对象使用。垃圾回收机制最早起源于LISP语言,目前大多数的高级语言都支持垃圾回收机制,比如Java、.net、Go等。
目前有很多垃圾回收方法,然而,现有的垃圾回收方法可能存在内存释放时间过长并且内存释放失败的问题。
发明内容
本申请实施例公开了一种内存释放方法及相关设备,能够加快内存释放并保证内存释放顺利进行。
本申请第一方面公开了一种内存释放方法,应用于电子设备,所述电子设备包括应用程序、虚拟机,所述应用程序运行在所述虚拟机中,所述虚拟机创建有垃圾回收线程、内存释放触发器和内存释放线程,所述电子设备还包括空闲内存列表和预释放内存列表,所述空闲内存列表记录所述电子设备的空闲内存,所述方法包括:若所述应用程序为低负载状态,所述内存释放触发器向所述垃圾回收线程发送内存释放触发信号;所述垃圾回收线程获取多个时间点所述电子设备的堆内存使用量;接收到所述内存释放触发信号后,所述垃圾回收线程根据所述堆内存使用量确定实际抽取内存大小;所述垃圾回收线程根据所述实际抽取内存大小,将所述空闲内存列表中的空闲内存抽取到所述预释放内存列表;所述内存释放线程将所述预释放内存列表中的内存释放给所述电子设备的操作系统。
本申请无需等待特定阶段(例如Full GC)进行内存释放,可以在任意的垃圾回收模式下进行内存释放,加快了内存释放。并且,本申请避免了频繁释放内存导致应用程序暂停,也避免了不合适时机释放内存导致额外的时间开销。
此外,本申请将空闲内存从空闲内存列表抽取到预释放内存列表,通过内存释放线程将预释放内存列表里的空闲内存释放给操作系统,整个过程与用户线程和垃圾回收线程完全解耦,不会造成用户线程或垃圾回收线程因等待空闲内存释放给操作系统而停滞。
在一些可选的实施方式中,所述虚拟机还创建有采样线程,所述应用程序为低负载状态包括:所述电子设备的负载统计值和垃圾回收次数统计值都小于或等于预设阈值;其中,所述负载统计值是所述内存释放触发器对所述电子设备多个时间点的负载数据进行统计得到的,所述负载数据是所述采样线程采集得到的;所述垃圾回收次数统计值是 所述内存释放触发器对所述电子设备的垃圾回收次数进行统计得到的,所述垃圾回收次数是所述垃圾回收线程依据预设时间间隔采集得到的。
根据负载统计值和垃圾回收次数统计值可以更加准确地确定应用程序的低负载状态,从而实现更加高效的内存释放。
在一些可选的实施方式中,所述负载数据包括操作系统的CPU负载数据和进程的CPU负载数据。
在一些可选的实施方式中,当下式中X的值为FALSE时,所述应用程序为低负载状态:X=((O>T)||(P>T)||(G>(60/I));其中,X为应用程序的负载状态的逻辑值;O为所述操作系统的CPU负载统计值;P为所述进程的CPU负载统计值;T为CPU负载阈值;G为所述垃圾回收次数统计值;I为所述内存释放触发器的周期。
在一些可选的实施方式中,所述对所述电子设备多个时间点的负载数据进行统计包括:计算所述负载数据的加权平均值,将所述负载数据的加权平均值作为所述负载统计值。
在一些可选的实施方式中,所述对所述垃圾回收次数进行统计包括:计算所述垃圾回收次数的加权平均值,将所述垃圾回收次数的加权平均值作为所述垃圾回收次数统计值。
在一些可选的实施方式中,所述垃圾回收线程根据所述堆内存使用量确定实际抽取内存大小包括:所述垃圾回收线程根据所述堆内存使用量进行预测,得到堆内存使用量预测值;所述垃圾回收线程根据所述堆内存使用量预测值计算可抽取内存大小;所述垃圾回收线程根据所述可抽取内存大小确定所述实际抽取内存大小。
通过对堆内存使用量进行预测,可以使释放的内存能够满足应用程序的需要,又不会造成内存空间浪费。
在一些可选的实施方式中,所述根据所述堆内存使用量进行预测包括:计算所述堆内存使用量的加权平均值,将所述堆内存使用量的加权平均值作为所述堆内存使用量预测值。
在一些可选的实施方式中,所述可抽取内存大小根据下式计算:Y=MAX((U-MAX(C,K)),0);其中,Y为所述可抽取内存大小;U为所述应用程序占用的内存大小;C为所述堆内存使用量预测值;K为所述虚拟机启动时设定的初始化内存大小;MAX()表示计算最大值。
在一些可选的实施方式中,所述垃圾回收线程根据所述堆内存使用量预测值计算可抽取内存大小之前,所述方法还包括:所述垃圾回收线程判断所述预释放内存列表是否为空;若预释放内存列表为空,所述垃圾回收线程判断所述电子设备的堆内存大小是否大于所述堆内存使用量预测值;若所述堆内存大小大于所述堆内存使用量预测值,所述垃圾回收线程根据所述堆内存使用量预测值计算所述可抽取内存大小。
通过判断预释放内存列表是否为空,可以避免因抽取动作比释放动作快的情况下,预释放内存列表里积累大量处于中间状态的内存空间。中间状态的内存空间就是操作系统不能让其他进程复用,虚拟机也不能再使用的内存空间。
在一些可选的实施方式中,所述根据所述可抽取内存大小确定所述实际抽取内存大小包括:根据下式计算所述实际抽取内存大小:Z=Y*R;其中,Z为所述实际抽取内存大小;Y为所述可抽取内存大小;R为抽取比例。
在一些可选的实施方式中,所述应用程序为Java应用程序,所述虚拟机为Java虚拟机。
在一些可选的实施方式中,在所述Java虚拟机的混合垃圾回收的最终标记阶段,执行所述垃圾回收线程获取多个时间点所述电子设备的堆内存使用量、所述垃圾回收线程根据所述堆内存使用量确定实际抽取内存大小、以及所述垃圾回收线程根据所述实际抽取内存大小,将所述空闲内存列表中的空闲内存抽取到所述预释放内存列表。
在Java虚拟机中,G1会经常触发年轻代垃圾回收,将内存释放安排在年轻代垃圾回收阶段会导致频繁地进行内存释放。将内存释放安排在在混合垃圾回收的最终标记阶段,可以避免频繁地进行内存释放并且保证内存释放的顺利进行。
本申请第二方面公开了一种计算机可读存储介质,包括计算机指令,当所述计算机指令在电子设备上运行时,使得所述电子设备执行如第一方面所述的内存释放方法。
本申请第三方面公开了一种电子设备,所述电子设备包括处理器和存储器,所述存储器,用于存储指令,所述处理器用于调用所述存储器中的指令,使得所述电子设备执行如第一方面所述的内存释放方法。
本申请第四方面公开了一种芯片系统,该芯片系统应用于电子设备;芯片系统包括接口电路和处理器;接口电路和处理器通过线路互联;接口电路用于从电子设备的存储器接收信号,并向处理器发送信号,信号包括存储器中存储的计算机指令;当处理器执行该计算机指令时,芯片系统执行如第一方面所述的内存释放方法。
应当理解地,上述提供的第二方面的计算机可读存储介质,第三方面的电子设备,第四方面的芯片系统均与上述第一方面的方法对应,因此,其所能达到的有益效果可参考上文所提供的对应的方法中的有益效果,此处不再赘述。
图1是本申请实施例公开的内存释放方法的应用环境示意图。
图2是根据现有的内存释放方法,内存生命周期的示意图。
图3是实现本申请内存释放方法的电子设备的主要逻辑单元示意图。
图4是本申请实施例公开的内存释放方法的流程图。
图5是采集负载数据和垃圾回收次数的示意图。
图6是混合垃圾回收的示意图。
图7是本申请实施例公开的一种电子设备的结构示意图。
为了便于理解,示例性的给出了部分与本申请实施例相关概念的说明以供参考。
需要说明的是,本申请中“至少一个”是指一个或者多个,“多个”是指两个或多于两个。“和/或”,描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B可以表示:单独存在A,同时存在A和B,单独存在B的情况,其中A,B可以是单数或者复数。本申请的说明书和权利要求书及附图中的术语“第一”、“第二”、“第三”、“第四”等(如果存在)是用于区别类似的对象,而不是用于描述特定的顺序或 先后次序。
操作系统(Operat ing System):操作系统是管理计算机硬件与软件资源的计算机程序。操作系统需要处理计算机内的基本事务,例如管理与配置内存、决定系统资源供需的优先次序、控制输入设备与输出设备、操作网络与管理文件系统等。操作系统也会提供一个让用户与系统交互的操作界面。
垃圾回收(Garbage Collect ion,GC):是一种自动的内存管理机制。在面向对象设计语言中,当计算机内存中的对象在应用程序中不再被使用时,垃圾回收机制会把这个对象占用的内存区块释放出来,让其他的应用程序或对象复用。
Java:Java是一门面向对象的编程语言。Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
Java虚拟机(Java Virtual Machine,JVM):Java虚拟机是Java语言二进制字节码的运行环境,负责装载字节码到其内部,解释/编译为对应平台上的机器指令执行。
G1(Garbage First):G1是面向服务端的JVM的垃圾回收器(也叫垃圾收集器),适用于多核处理器、大内存容量的服务端系统。G1关注最小延时的垃圾回收目标,同时实现高吞吐量。
应用程序挂起(Stop The World,STW):STW机制是在进行垃圾回收时,除了垃圾回收(GC)线程,应用程序的其他线程都被挂起。此时,JVM只允许GC线程运行,其他线程则会全部暂停,等待GC线程执行完毕后才能再次运行。
完整垃圾回收(Full Garbage Collection,Full GC):通常指的是一次完整的垃圾回收,会对整个堆内存(包括老年代、年轻代、元空间(metaspace)等)进行回收。
年轻代垃圾回收(Young/Minor Garbage Collection,Young GC,也叫新生代垃圾回收):对年轻代/新生代的堆内存进行垃圾回收。内存分成年轻代和老年代,年轻代存放刚创建的和存活时间比较短的对象,老年代存放存活时间比较长的对象。
混合垃圾回收(Mixed Garbage Collect ion,Mixed GC):Mixed GC不仅进行新生代垃圾回收,同时也回收部分后台扫描线程标记的老年代分区。
内存区块(Region):在垃圾回收机制中,对应用程序占用的堆内存划分成一个个区块进行管理。
Full GC、Young GC和Mixed GC是G1的三种垃圾回收模式。一次性释放大量内存(即批量释放内存)给操作系统通常比较耗时,为了降低批量释放内存给操作系统造成的影响,G1将批量内存释放操作放在了Full GC阶段(Young GC和Mixed GC只将空闲内存记录到空闲内存列表而不进行释放)。一方面,在Full GC阶段批量释放内存仍然会耗费较长时间。另一方面,G1在Full GC的时候会将应用程序挂起(Stop The World,STW),为了避免应用程序卡顿,G1会尽可能地避免触发Full GC。当JVM的应用程序负载下降或不活跃时,G1尽可能避免触发Full GC的策略会导致JVM垃圾回收机制处于相对静止状态,不会再触发Full GC(即内存释放给操作系统失败)。此时,除非强制从外部执行Full GC,否则G1会一直保留和占用所有堆内存,不会将空闲的堆内存释放给操作系统,从而造成内存资源的浪费。例如,在按资源使用量付费的场景中,G1不将空闲的堆内存释放给操作系统会造成用户多支付不必要的费用,也会导致云提供商无法充分利用其硬件资源。
为了更好地理解本申请实施例公开的内存释放方法及相关设备,下面首先对本申请内存释放方法的应用环境进行描述。
图1是本申请实施例公开的内存释放方法的应用环境示意图。如图1所示,所述内存释放方法应用于电子设备10。电子设备10包括至少一个应用程序100(图中示出了一个),例如视频播放器、游戏、社交应用程序等。电子设备10还包括虚拟机101,应用程序100运行在虚拟机101中。本申请实施例提供的内存释放方法在虚拟机101中运行的应用程序100为低负载状态时,计算实际抽取内存大小,根据实际抽取内存大小将空闲内存从空闲内存列表抽取到预释放空闲内存列表,将预释放空闲内存列表中的空闲内存释放给电子设备10的操作系统。本申请能够快速地进行内存释放,保证内存释放顺利进行。
其中,电子设备10可以包括智能手机、平板电脑、台式计算机、膝上型便携计算机、车载电脑等。
在本申请的一个实施例中,应用程序100可以是Java应用程序,虚拟机101可以是Java虚拟机。
在本申请的其他实施例中,应用程序100和虚拟机101可以是其他编程语言对应的应用程序和虚拟机。例如,应用程序100可以是Go语言应用程序,虚拟机101可以是LVM虚拟机。
图2是根据现有的内存释放方法,内存生命周期的示意图。图中以JVM为例进行说明。
参见图2所示,根据现有的内存释放方法,内存生命周期包括如下阶段:
201,JVM申请保留内存。
在JVM启动期间或内存扩展期间,JVM会向操作系统申请保留一部分内存。在这个阶段,操作系统并不会真正给JVM分配内存区域和独占指定大小的内存区域。
202,JVM占用申请到的内存。
JVM触发调用操作系统的mmap方法。在这个阶段,操作系统会分配指定大小的内存区域给JVM独占,运行在操作系统上的其他进程不能再复用这块内存区域。
203,JVM把申请到的内存分割成一个个小的内存区块(Region),将这些内存区块记录到空闲内存列表。
204,应用程序创建对象。
205,JVM上的用户线程(即应用程序线程)申请内存区块。
JVM上的用户线程会从空闲内存列表中申请一个或多个可用的内存区块。
206,JVM上的用户线程在申请到的内存区块中为对象分配内存区块,并把对象数据存入内存区块。
207,应用程序释放对象。
应用程序释放之前创建的不再使用的对象。
208,JVM的G1触发垃圾回收并且暂停应用程序。
触发的垃圾回收可以是Young GC、Mixed GC或者Full GC。暂定应用程序也就是应用程序挂起。
209,JVM的G1把对象占用的内存区块记录到空闲内存列表。
如果不存在还在使用的对象数据,G1把对象占用的内存区块记录到空闲内存列表。
如果存在还在使用的对象数据,G1从空闲内存列表中申请新的内存区块,把还在使用的对象数据复制到新的内存区块,把对象原先占用的内存区块记录到空闲内存列表。
210,如果触发的垃圾回收是Full GC,G1继续暂停应用程序。
211,G1在继续暂停应用程序的期间,根据空闲内存列表把空闲内存释放给操作系统。
G1在Full GC阶段把空闲内存从空闲内存列表里释放给操作系统,在操作系统上的其他进程可以复用释放给操作系统的内存。
根据现有的内存释放方法,G1在Full GC阶段把空闲内存从空闲内存列表里释放给操作系统,这种内存释放方法比较耗费时间,会造成应用程序卡顿。
本申请对内存释放过程进行改进,以加快内存释放并保证内存释放顺利进行。
图3是实现本申请内存释放方法的电子设备的主要逻辑单元示意图。
电子设备30包括采样线程300、垃圾回收(GC)线程301、内存释放触发器302、内存释放线程303、操作系统304。虚拟机(图上未示出)可以在启动时创建采样线程300、垃圾回收(GC)线程301、内存释放触发器302、内存释放线程303。
电子设备还包括策略类305和内存管理器306。
策略类305用于存储本申请内存释放方法中获取的各种数据,例如操作系统的CPU负载数据、Java进程的CPU负载数据、GC次数、堆内存使用量。
内存管理器306存储应用程序的空闲内存列表、预释放内存列表和堆内存信息。堆内存信息包括堆内存使用量、堆内存大小。
需要说明的是,内存管理器306中存储的堆内存使用量是电子设备30实时的堆内存使用量,即实时数据。策略类305中存储的堆内存使用量是多个时间点的堆内存使用量,即时间序列数据。
采样线程300用于采集负载数据,图中的负载数据包括操作系统的CPU负载数据和Java进程的CPU负载数据。采样线程300将采集的负载数据写入策略类305。
垃圾回收线程(即GC线程)301用于采集垃圾回收次数(即GC次数),将垃圾回收次数写入策略类305。
内存释放触发器302用于对负载数据进行统计,得到负载统计值,对垃圾回收次数进行统计,得到垃圾回收次数统计值。内存释放触发器302还用于根据负载统计值和垃圾回收次数统计值判断应用程序是否为低负载状态,若应用程序为低负载状态,向垃圾回收线程发送内存释放触发信号。
垃圾回收线程301还用于从内存管理器中获取多个时间点的堆内存使用量,将堆内存使用量写入策略类305,根据堆内存使用量进行预测,得到堆内存使用量预测值。
垃圾回收线程301还用于根据堆内存使用量预测值计算可抽取内存大小,根据可抽取内存大小确定实际抽取内存大小,根据实际抽取内存大小将空闲内存从空闲内存列表抽取到预释放内存列表。
内存释放线程303用于将预释放内存列表中的内存释放给操作系统304。
图4是本申请实施例公开的内存释放方法的流程图。所述内存释放方法应用于电子 设备,电子设备包括应用程序和虚拟机,应用程序运行与虚拟机中,虚拟机包括采样线程、垃圾回收线程、内存释放触发器和内存释放线程。以下以应用程序为Java应用程序,虚拟机为JVM为例进行说明。
401,采样线程采集电子设备多个时间点的负载数据。
例如,采样线程每300毫秒一次采集电子设备的负载数据。
在本申请的一个实施例中,负载数据包括操作系统的CPU负载数据和Java进程的CPU负载数据。采样线程从操作系统获取操作系统的CPU负载数据,从Java进程获取Java进程的CPU负载数据。
在本申请的一个实施例中,采样线程可以将负载数据写入G1的策略类中。
402,内存释放触发器对负载数据进行统计,得到负载统计值。
在本申请的一个实施例中,内存释放触发器可以从G1的策略类中读取负载数据(即负载数据的时间序列数据)。
在本申请的一个实施例中,内存释放触发器对操作系统的CPU负载数据进行统计,得到操作系统的CPU负载统计值,对Java进程的CPU负载数据进行统计,得到Java进程的CPU负载统计值。
内存释放触发器可以对预设时间点之前预设数量(例如十个)的负载数据进行统计,得到负载统计值。
在本申请的一个实施例中,内存释放触发器可以通过加权平均的方法(即计算负载数据的加权平均值)计算负载统计值。
403,垃圾回收线程依据预设时间间隔(例如每分钟)采集电子设备的垃圾回收次数。
在本申请的一个实施例中,垃圾回收线程可以将依据预设时间间隔采集的垃圾回收次数(即垃圾回收次数的时间序列数据)写入G1的策略类中。
在本申请的一个实施例中,垃圾回收线程在每次触发垃圾回收时,判断垃圾回收次数统计时长是否超过预设时间间隔,如果没有超过预设时间间隔,则把垃圾回收次数加1。如果超过预设时间间隔,则把当前的垃圾回收次数写入G1的策略类中,再把垃圾回收次数重置为1并对垃圾回收次数统计时长重新计时。
404,内存释放触发器对垃圾回收次数进行统计,得到垃圾回收次数统计值。
在本申请的一个实施例中,内存释放触发器可以从G1的策略类中读取垃圾回收次数(即垃圾回收次数的时间序列数据)。
内存释放触发器可以对预设时间点之前预设数量(例如十个)的垃圾回收次数进行统计,得到垃圾回收次数统计值。
在本申请的一个实施例中,内存释放触发器可以通过加权平均的方法(即计算垃圾回收次数的加权平均值)计算垃圾回收次数统计值。
405,内存释放触发器根据负载统计值和垃圾回收次数统计值判断应用程序是否为低负载状态。
内存释放触发器可以周期性地(例如15秒一次)根据负载统计值和垃圾回收次数统计值判断应用程序是否为低负载状态。
在本申请的一个实施例中,当负载统计值和垃圾回收次数统计值中的任意一个都小于或等于预设阈值时,判断应用程序为低负载状态。若负载统计值和垃圾回收次数统计 值中的任意一个大于预设阈值,判断应用程序为高负载状态。例如,负载统计值包括操作系统的CPU负载统计值、Java进程的CPU负载统计值,若操作系统的CPU负载统计值、Java进程的CPU负载统计值和垃圾回收次数统计值中的任意一个都小于或等于预设阈值,则判断应用程序为低负载状态。
在本申请的一个实施例中,可以根据下式判断应用程序是否为低负载状态:
X=((O>T)||(P>T)||(G>(60/I));
其中:
X:应用程序的负载状态的逻辑值,TRUE(即(O>T)、(P>T)、(G>(60/I)中的任意一个成立)表示应用程序是高负载状态,FALSE(即(O>T)、(P>T)、(G>(60/I)中的任意一个都不成立)表示应用程序是低负载状态;
O:操作系统的CPU负载统计值;
P:Java进程的CPU负载统计值;
T:CPU负载阈值,根据实际需要设置,例如设置为CPU满载的10%;
G:GC次数统计值;
I:内存释放触发器的周期,根据实际需要设置,例如设置为15秒。
应当理解,操作系统的CPU负载统计值和Java进程的CPU负载统计值可以对应相同的预设阈值,也可以对应不同的预设阈值。例如,操作系统的CPU负载统计值的预设阈值可以是CPU满载的10%,Java进程的CPU负载统计值的预设阈值可以是CPU满载的15%。
本实施例中,获取负载数据和垃圾回收次数,对负载数据进行统计得到负载统计值,对垃圾回收次数进行统计得到垃圾回收次数统计值,根据负载统计值和垃圾回收次数统计值判断应用程序是否为低负载状态,可以更加准确地确定应用程序的低负载状态,从而实现更加高效的内存释放。
应当理解,在其他的实施例中,可以获取负载数据或垃圾回收次数,对负载数据进行统计得到负载统计值,或者对垃圾回收次数进行统计得到垃圾回收次数统计值,根据负载统计值或垃圾回收次数统计值判断应用程序是否为低负载状态。
406,若应用程序为低负载状态,内存释放触发器向垃圾回收线程发送内存释放触发信号。
例如,操作系统的CPU负载统计值、Java进程的CPU负载统计值和垃圾回收次数统计值中的任意一个都小于或等于预设阈值,此时判断应用程序为低负载状态,内存释放触发器向垃圾回收线程发送内存释放触发信号。
407,垃圾回收线程从内存管理器获取多个时间点电子设备的堆内存使用量。
内存管理器存储有电子设备实时的堆内存使用量,垃圾回收线程可以周期性地从内存管理器获取堆内存使用量,得到多个时间点的堆内存使用量。
在本申请的一个实施例中,垃圾回收线程可以将获取的堆内存使用量写入G1的策略类中。
408,接收到内存释放触发信号后,垃圾回收线程根据堆内存使用量进行预测,得到堆内存使用量预测值。
在本申请的一个实施例中,垃圾回收线程可以从G1的策略类中读取堆内存使用量(即堆内存使用量的时间序列数据)。
垃圾回收线程可以根据预设时间点之前预设数量(例如十个)的堆内存使用量进行预测,得到堆内存使用量预测值。
在本申请的一个实施例中,垃圾回收线程可以通过加权平均的方法(即计算堆内存使用量的加权平均值)计算堆内存使用量预测值。
应用程序可释放的内存区块会加入空闲内存列表,如果每次从空闲内存列表抽取到预释放内存列表的内存(即内存区块)过多,当应用程序波动负载变高而空闲列表内存中的内存区块不够时,G1首先会触发GC,触发GC会附带着应用程序挂起,如GC完之后空闲列表里的内存还不够,就会向操作系统申请保留内存,并提交和占用内存,在这个阶段也会一直持续应用程序挂起。如果抽取太少,又会造成应用程序占用持有的可释放空闲内存太多,其他进程不能复用这部分内存。为了使抽取后剩余的空闲内存即能满足应用程序的需要,又不会造成太多内存空间浪费,本申请的实施例中,在每次GC开始时进行预测,确定应用程序在预设时间点将会需要多少内存。
409,垃圾回收线程根据堆内存使用量预测值计算可抽取内存大小。
在本申请的一个实施例中,可以根据下式计算可抽取内存大小:
Y=MAX((U-MAX(C,K)),0);
其中:
Y:可抽取内存大小;
U:应用程序占用的内存大小;
C:堆内存使用量预测值;
K:JVM启动时设定的初始化内存大小;
MAX()表示计算最大值。
在本申请的一个实施例中,预释放内存列表为链表结构。
在本申请的一个实施例中,垃圾回收线程根据预设时间点的堆内存使用量预测值计算可抽取内存大小之前,还判断预释放内存列表是否为空。若预释放内存列表为空,垃圾回收线程判断电子设备的堆内存大小是否大于堆内存使用量预测值。若堆内存大小大于堆内存使用量预测值,垃圾回收线程根据预设时间点的堆内存使用量预测值计算可抽取内存大小。若预释放内存列表不为空,或者堆内存大小小于或等于堆内存使用量预测值,则流程结束。
堆内存在虚拟机启动的时候就被创建,用于存储应用程序的各种对象。
通过判断预释放内存列表是否为空,在判断预释放内存列表为空再进行空闲内存抽取,可以避免因抽取动作比释放动作快的情况下,预释放内存列表里积累大量处于中间状态的内存空间。中间状态的内存空间就是操作系统不能让其他进程复用,虚拟机也不能再使用的内存空间。
410,垃圾回收线程根据可抽取内存大小确定实际抽取内存大小。
根据可抽取内存大小确定实际抽取内存大小是为了更合理地释放空闲内存,使预设时间点的内存占用量与应用程序的实际内存使用量拟合贴近。
在本申请的一个实施例中,可以根据下式计算实际抽取内存大小:Z=Y*R,其中:
Z:实际抽取内存大小;
Y:可抽取内存大小;
R:抽取比例。
抽取比例可以根据实际需要设置,例如设置为10%。可以在JVM启动时通过指定参数对抽取比例进行设置。
411,垃圾回收线程根据实际抽取内存大小将空闲内存列表中的空闲内存抽取到预释放内存列表。
GC线程和用户线程只能操作空闲内存列表里的内存区块,预释放内存列表内的内存区块不能被用户线程和GC线程操作。将空闲内存列表中的空闲内存抽取到预释放内存列表,可以实现内存释放线程释放空闲内存时不影响GC线程和用户线程的执行。
412,内存释放线程将预释放内存列表中的内存释放给操作系统。
现有的内存释放方法可能存在垃圾回收时间较长、某些时候垃圾回收失效的问题。例如,G1在Full GC阶段一次性释放大量内存会耗费较长时间,当JVM的应用程序负载下降或不活跃时,G1尽可能避免触发Full GC的策略会导致JVM垃圾回收机制处于相对静止状态,不会再触发Full GC,从而造成内存资源的浪费。
相比较,本申请采集负载数据(例如操作系统的CPU负载数据、Java进程的CPU负载数据)和GC次数,根据负载数据和GC次数确定应用程序是否为低负载状态,在应用程序为低负载状态时把空闲内存抽取到预释放内存列表进行释放。本申请无需等待特定阶段(例如Full GC)进行内存释放,可以在任意的垃圾回收模式下进行内存释放,加快了内存释放。本申请避免了频繁(抽取动作依赖GC逻辑,G1各种类型的GC频率会很高)释放内存导致应用程序暂停,也避免了不合适时机(即应用程序为高负载状态)释放内存导致额外的时间开销。如果在应用程序为高负载状态时释放内存,应用程序会重新向OS申请内存,会产生一定的时间开销。
本申请将空闲内存从空闲内存列表抽取到预释放内存列表,通过内存释放线程将预释放内存列表里的空闲内存释放给OS,整个过程与用户线程和GC线程完全解耦,不会造成用户线程或GC线程因等待空闲内存释放给OS而停滞。
本申请根据堆内存使用量进行预测,根据堆内存使用量预测值计算可抽取内存大小,根据可抽取内存大小确定实际抽取内存大小,从而让释放后的内存占用量拟合贴近应用程序的实际内存使用量,实现平滑释放空闲内存给OS,避免在应用程序的负载波动时释放内存带来的负面影响。
经过实验可知,在应用程序的负载降低后,进程(例如Java进程)占用内存量和进程实际使用内存量逐步趋于拟合。
参阅图5所示,是采集负载数据和垃圾回收次数的示意图。
501,JVM在启动时初始化和启动采样线程。
502,采样线程从操作系统采集操作系统的CPU负载数据。
503,采样线程将操作系统的CPU负载数据写入G1的策略类。
504,采样线程从操作系统采集Java进程的CPU负载数据。
505,采样线程将Java进程的CPU负载数据写入G1的策略类。
506,垃圾回收线程统计每个时间间隔的垃圾回收次数。
507,垃圾回收线程将垃圾回收次数写入G1的策略类。
在本申请的一个实施例中,可以在Mixed GC的最终标记(Remark)阶段把可释放 空闲内存抽取到预释放列表,以进一步减少应用程序挂起时间。具体地,408中,在Mixed GC的Remark阶段,垃圾回收线程根据堆内存使用量对预设时间点进行预测,得到堆内存使用量预测值。409中,在Mixed GC的Remark阶段,垃圾回收线程根据预设时间点的堆内存使用量预测值计算可抽取内存大小。410中,在Mixed GC的Remark阶段,垃圾回收线程根据可抽取内存大小确定实际抽取内存大小,根据实际抽取内存大小将空闲内存列表中的空闲内存抽取到预释放内存列表。Mixed GC的Remark阶段应用程序挂起时间较短,从而能够避免由于内存释放造成应用程序长时间挂起。
为了便于理解最终标记阶段,图6给出了Mixed GC的示意图。Mixed GC包括初始标记、并发标记、最终标记、筛选回收四个阶段。
初始标记(Init ial Mark)阶段用于标记GC root能直接关联的对象(短暂STW)。
并发标记(Concurrent mark)阶段用于从并发标记中的root遍历,对不可达的对象进行标记,耗时长但可并发。
最终标记(Remark)阶段用于收集并发标记期间产生的新垃圾(短暂STW)。
筛选回收(Live Data Counting and Evacuation)阶段用于对各个Region的回收性价比排序,在保证时间可控的情况下清除失活对象。
Mixed GC的更多内容可以参考相关技术,此处不再赘述。
请参见图7,图7是本申请实施例公开的一种电子设备的结构示意图。如图4所示,电子设备40可以包括:射频(Radio Frequency,RF)电路701、存储器702、输入单元703、显示单元704、传感器705、音频电路706、无线保真(wireless fidelity,Wi-Fi)模块707、处理器708以及电源709等部件。本领域技术人员可以理解,图4中示出的电子设备的结构并不构成对手机的限定,可以包括比图示更多或更少的部件,或者组合某些部件,或者不同的部件布置。
RF电路701可用于收发信息或在通话过程中,对信号进行接收和发送,特别地,接收基站的下行信息后,转给处理器708进行处理;另外,将涉及上行的数据发送给基站。通常,RF电路701包括,但不限于:天线、至少一个放大器、收发信机、耦合器、低噪声放大器(Low Noise Amplifier,LNA)、双工器等。
存储器702可用于存储软件程序以及模块,处理器708通过运行存储在存储器702中的软件程序以及模块,从而执行电子设备的各种功能应用以及数据处理。存储器702可主要包括存储程序区和存储数据区,其中,存储程序区可存储操作系统、至少一个功能所需的应用程序(比如声音播放功能、图像播放功能等)等;存储数据区可存储根据电子设备的使用所创建的数据(比如音频数据、电话本等)等。此外,存储器702可以包括高速随机存取存储器,还可以包括非易失性存储器,例如至少一个磁盘存储器件、闪存器件、或其他易失性固态存储器件。
输入单元703可用于接收输入的数字或字符信息,以及产生与电子设备的用户设置以及功能控制有关的键信号输入。具体地,输入单元703可包括触控面板7031以及其他输入设备7032。触控面板7031,也称为触摸屏,可收集用户在其上或附近的触摸操作(比如用户使用手指、触控笔等任何适合的物体或附件在触控面板7031上或在触控面板7031附近的操作),并根据预先设定的程序驱动相应的连接装置。可选地,触控 面板7031可包括触摸检测装置和触摸控制器两个部分。其中,触摸检测装置检测用户的触摸方位,并检测触摸操作带来的信号,将信号传送给触摸控制器;触摸控制器从触摸检测装置上接收触摸信息,并将它转换成触点坐标,再送给处理器708,并接收处理器708发来的命令并加以执行。此外,可以采用电阻式、电容式、红外线以及表面声波等多种类型实现触控面板7031。除了触控面板7031,输入单元703还可以包括其他输入设备7032。具体地,其他输入设备7032可以包括但不限于物理键盘、功能键(比如音量控制按键、开关按键等)、轨迹球、鼠标、操作杆等中的一种或多种。
显示单元704可用于显示由用户输入的信息或提供给用户的信息以及电子设备的各种菜单。显示单元704可包括显示面板7041,可选地,可以采用液晶显示器(Liquid Crystal Display,LCD)、有机发光二极管(Organic Light-Emitting Diode,OLED)等形式来配置显示面板7041。进一步地,触控面板7031可覆盖显示面板7041,当触控面板7031检测到在其上或附近的触摸操作后,传送给处理器708以确定触摸事件的类型,随后处理器708根据触摸事件的类型在显示面板7041上提供相应的视觉输出。虽然在图4中,触控面板7031与显示面板7041是作为两个独立的部件来实现电子设备的输入和输出功能,但是在某些实施例中,可以将触控面板7031与显示面板7041集成而实现电子设备的输入和输出功能。
电子设备还可包括至少一种传感器705,比如光传感器、运动传感器以及其他传感器。具体地,光传感器可包括环境光传感器及接近传感器,其中,环境光传感器可根据环境光线的明暗来调节显示面板7041的亮度,接近传感器可在电子设备移动到耳边时,关闭显示面板7041和/或背光。作为运动传感器的一种,加速计传感器可检测各个方向上(一般为三轴)加速度的大小,静止时可检测出重力的大小及方向,可用于识别电子设备姿态的应用(比如横竖屏切换、相关游戏、磁力计姿态校准)、振动识别相关功能(比如计步器、敲击)等;此外,电子设备还可配置的陀螺仪、气压计、湿度计、温度计、红外线传感器等其他传感器,在此不再赘述。
音频电路706、扬声器7061,传声器7062可提供用户与电子设备之间的音频接口。音频电路706可将接收到的音频数据转换后的电信号,传输到扬声器7061,由扬声器7061转换为声音信号输出;另一方面,传声器7062将收集的声音信号转换为电信号,由音频电路706接收后转换为音频数据,再将音频数据输出处理器708处理后,经RF电路701发送给另一电子设备,或者将音频数据输出至存储器702以便进一步处理。
Wi-Fi属于短距离无线传输技术,电子设备通过WiFi模块707可以帮助用户收发电子邮件、浏览网页和访问流式媒体等,它为用户提供了无线的宽带互联网访问。虽然图4示出了Wi-Fi模块707,但是可以理解的是,其并不属于电子设备的必需构成,完全可以根据需要、在不改变发明本质的范围内进行省略。
处理器708是电子设备的控制中心,利用各种接口和线路连接整个电子设备的各个部分,通过运行或执行存储在存储器702内的软件程序和/或模块,以及调用存储在存储器702内的数据,执行电子设备的各种功能和处理数据,从而对电子设备进行整体监控。可选地,处理器708可包括一个或多个处理单元;优选的,处理器708可集成应用处理器和调制解调器,其中,应用处理器主要处理操作系统、用户界面和应用程序等,调制解调器主要处理无线通信。可以理解的是,上述调制解调处理器也可以不集成到处理器708中。
电子设备还包括给各个部件供电的电源709(比如电池),可选地,电源可以通过电源管理系统与处理器708逻辑相连,从而通过电源管理系统实现管理充电、放电、以及功耗管理等功能。
尽管未示出,电子设备还可以包括摄像头、蓝牙模块等,在此不再赘述。
图4中描述的电子设备可以用于实施本申请图2介绍的方法实施例中的部分或全部流程,可参见前述图2所述实施例中的相关阐述,这里不再赘述。
本实施例还提供一种计算机存储介质,该计算机存储介质中存储有计算机指令,当该计算机指令在电子设备上运行时,使得电子设备执行上述相关方法步骤实现上述实施例中的内存释放方法。
本实施例还提供了一种计算机程序产品,当该计算机程序产品在电子设备上运行时,使得电子设备执行上述相关步骤,以实现上述实施例中的内存释放方法。
另外,本申请的实施例还提供一种装置,这个装置具体可以是芯片,组件或模块,该装置可包括相连的处理器和存储器;其中,存储器用于存储计算机执行指令,当装置运行时,处理器可执行存储器存储的计算机执行指令,以使芯片执行上述各方法实施例中的内存释放方法。
其中,本实施例提供的电子设备、计算机存储介质、计算机程序产品或芯片均用于执行上文所提供的对应的方法,因此,其所能达到的有益效果可参考上文所提供的对应的方法中的有益效果,此处不再赘述。
通过以上的实施方式的描述,所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,仅以上述各功能模块的划分进行举例说明,实际应用中,可以根据需要而将上述功能分配由不同的功能模块完成,即将装置的内部结构划分成不同的功能模块,以完成以上描述的全部或者部分功能。
在本申请所提供的几个实施例中,应该理解到,所揭露的装置和方法,可以通过其他的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,该模块或单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个装置,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其他的形式。
该作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是一个物理单元或多个物理单元,即可以位于一个地方,或者也可以分布到多个不同地方。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。
另外,在本申请各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。
该集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个可读取存储介质中。基于这样的理解,本申请实施例的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该软件产品存储在一个存储介质中,包括若干指令用以使得一个设备(可以是单片机,芯片等)或处理器(processor)执行本申请各个实施例所述方法的全部或 部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(Read-Only Memory,ROM)、随机存取存储器(Random Access Memory,RAM)、磁碟或者光盘等各种可以存储程序代码的介质。
以上所述,仅为本申请的具体实施方式,但本申请的保护范围并不局限于此,任何在本申请揭露的技术范围内的变化或替换,都应涵盖在本申请的保护范围之内。因此,本申请的保护范围应以所述权利要求的保护范围为准。
Claims (16)
- 一种内存释放方法,应用于电子设备,其特征在于,所述电子设备包括应用程序、虚拟机,所述应用程序运行在所述虚拟机中,所述虚拟机创建有垃圾回收线程、内存释放触发器和内存释放线程,所述电子设备还包括空闲内存列表和预释放内存列表,所述空闲内存列表记录所述电子设备的空闲内存,所述方法包括:若所述应用程序为低负载状态,所述内存释放触发器向所述垃圾回收线程发送内存释放触发信号;所述垃圾回收线程获取多个时间点所述电子设备的堆内存使用量;接收到所述内存释放触发信号后,所述垃圾回收线程根据所述堆内存使用量确定实际抽取内存大小;所述垃圾回收线程根据所述实际抽取内存大小,将所述空闲内存列表中的空闲内存抽取到所述预释放内存列表;所述内存释放线程将所述预释放内存列表中的内存释放给所述电子设备的操作系统。
- 如权利要求1所述的内存释放方法,其特征在于,所述虚拟机还创建有采样线程,所述应用程序为低负载状态包括:所述电子设备的负载统计值和垃圾回收次数统计值都小于或等于预设阈值;其中,所述负载统计值是所述内存释放触发器对所述电子设备多个时间点的负载数据进行统计得到的,所述负载数据是所述采样线程采集得到的;所述垃圾回收次数统计值是所述内存释放触发器对所述电子设备的垃圾回收次数进行统计得到的,所述垃圾回收次数是所述垃圾回收线程依据预设时间间隔采集得到的。
- 如权利要求2所述的内存释放方法,其特征在于,所述负载数据包括操作系统的CPU负载数据和进程的CPU负载数据。
- 如权利要求3所述的内存释放方法,其特征在于,当下式中X的值为FALSE时,所述应用程序为低负载状态:X=((O>T)||(P>T)||(G>(60/I));其中,X为应用程序的负载状态的逻辑值;O为所述操作系统的CPU负载统计值;P为所述进程的CPU负载统计值;T为CPU负载阈值;G为所述垃圾回收次数统计值;I为所述内存释放触发器的周期。
- 如权利要求2所述的内存释放方法,其特征在于,所述对所述电子设备多个时间点的负载数据进行统计包括:计算所述负载数据的加权平均值,将所述负载数据的加权平均值作为所述负载统计值。
- 如权利要求2所述的内存释放方法,其特征在于,所述对所述垃圾回收次数进行统 计包括:计算所述垃圾回收次数的加权平均值,将所述垃圾回收次数的加权平均值作为所述垃圾回收次数统计值。
- 如权利要求1所述的内存释放方法,其特征在于,所述垃圾回收线程根据所述堆内存使用量确定实际抽取内存大小包括:所述垃圾回收线程根据所述堆内存使用量进行预测,得到堆内存使用量预测值;所述垃圾回收线程根据所述堆内存使用量预测值计算可抽取内存大小;所述垃圾回收线程根据所述可抽取内存大小确定所述实际抽取内存大小。
- 如权利要求7所述的内存释放方法,其特征在于,所述根据所述堆内存使用量进行预测包括:计算所述堆内存使用量的加权平均值,将所述堆内存使用量的加权平均值作为所述堆内存使用量预测值。
- 如权利要求7所述的内存释放方法,其特征在于,所述可抽取内存大小根据下式计算:Y=MAX((U-MAX(C,K)),0);其中,Y为所述可抽取内存大小;U为所述应用程序占用的内存大小;C为所述堆内存使用量预测值;K为所述虚拟机启动时设定的初始化内存大小;MAX()表示计算最大值。
- 如权利要求7所述的内存释放方法,其特征在于,所述垃圾回收线程根据所述堆内存使用量预测值计算可抽取内存大小之前,所述方法还包括:所述垃圾回收线程判断所述预释放内存列表是否为空;若预释放内存列表为空,所述垃圾回收线程判断所述电子设备的堆内存大小是否大于所述堆内存使用量预测值;若所述堆内存大小大于所述堆内存使用量预测值,所述垃圾回收线程根据所述堆内存使用量预测值计算所述可抽取内存大小。
- 如权利要求7所述的内存释放方法,其特征在于,所述根据所述可抽取内存大小确定所述实际抽取内存大小包括:根据下式计算所述实际抽取内存大小:Z=Y*R;其中,Z为所述实际抽取内存大小;Y为所述可抽取内存大小;R为抽取比例。
- 如权利要求1至11任一项所述的内存释放方法,其特征在于,所述应用程序为Java应用程序,所述虚拟机为Java虚拟机。
- 如权利要求12所述的内存释放方法,其特征在于,在所述Java虚拟机的混合垃圾回收的最终标记阶段,执行所述垃圾回收线程获取多个时间点所述电子设备的堆内存使用量、所述垃圾回收线程根据所述堆内存使用量确定实际抽取内存大小、以及所述垃圾回收线程根据所述实际抽取内存大小,将所述空闲内存列表中的空闲内存抽取到所述预 释放内存列表。
- 一种计算机可读存储介质,其特征在于,包括计算机指令,当所述计算机指令在电子设备上运行时,使得所述电子设备执行如权利要求1至13中任一项所述的内存释放方法。
- 一种电子设备,其特征在于,所述电子设备包括处理器和存储器,所述存储器,用于存储指令,所述处理器用于调用所述存储器中的指令,使得所述电子设备执行如权利要求1至13中任一项所述的内存释放方法。
- 一种芯片系统,该芯片系统应用于电子设备;芯片系统包括接口电路和处理器;接口电路和处理器通过线路互联;接口电路用于从电子设备的存储器接收信号,并向处理器发送信号,信号包括存储器中存储的计算机指令;当处理器执行该计算机指令时,芯片系统执行权利要求1至13中任一项所述的内存释放方法。
Applications Claiming Priority (4)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110002169.0 | 2021-01-04 | ||
CN202110002169 | 2021-01-04 | ||
CN202110031553.3A CN114721812A (zh) | 2021-01-04 | 2021-01-11 | 内存释放方法及相关设备 |
CN202110031553.3 | 2021-01-11 |
Publications (1)
Publication Number | Publication Date |
---|---|
WO2022143446A1 true WO2022143446A1 (zh) | 2022-07-07 |
Family
ID=82234071
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
PCT/CN2021/141095 WO2022143446A1 (zh) | 2021-01-04 | 2021-12-24 | 内存释放方法及相关设备 |
Country Status (2)
Country | Link |
---|---|
CN (1) | CN114721812A (zh) |
WO (1) | WO2022143446A1 (zh) |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115292052A (zh) * | 2022-09-27 | 2022-11-04 | 荣耀终端有限公司 | 内存回收方法、电子设备及计算机可读存储介质 |
CN116089320A (zh) * | 2022-08-31 | 2023-05-09 | 荣耀终端有限公司 | 垃圾回收方法和相关装置 |
CN116302571A (zh) * | 2023-05-17 | 2023-06-23 | 北京趋动智能科技有限公司 | 内存释放方法、装置、客户端、服务端及存储介质 |
Families Citing this family (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116361200B (zh) * | 2022-09-30 | 2023-11-14 | 荣耀终端有限公司 | 一种内存垃圾的回收方法及电子设备 |
CN118069542A (zh) * | 2022-11-22 | 2024-05-24 | 荣耀终端有限公司 | 一种内存垃圾的回收方法及电子设备 |
CN115756872B (zh) * | 2022-12-13 | 2023-05-26 | 北京望京科技孵化服务有限公司 | 基于数据分析的计算机内存释放系统 |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101281458A (zh) * | 2008-05-14 | 2008-10-08 | 华为技术有限公司 | 一种垃圾回收的装置、系统及方法 |
CN101833512A (zh) * | 2010-04-22 | 2010-09-15 | 中兴通讯股份有限公司 | 一种内存回收方法及其装置 |
US20150026428A1 (en) * | 2013-07-18 | 2015-01-22 | International Business Machines Corporation | Memory use for garbage collected computer environments |
CN108255582A (zh) * | 2018-01-16 | 2018-07-06 | 携程旅游信息技术(上海)有限公司 | java虚拟机垃圾回收的方法、系统、设备及存储介质 |
CN111124668A (zh) * | 2019-11-28 | 2020-05-08 | 宇龙计算机通信科技(深圳)有限公司 | 内存释放方法、装置、存储介质及终端 |
-
2021
- 2021-01-11 CN CN202110031553.3A patent/CN114721812A/zh active Pending
- 2021-12-24 WO PCT/CN2021/141095 patent/WO2022143446A1/zh active Application Filing
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101281458A (zh) * | 2008-05-14 | 2008-10-08 | 华为技术有限公司 | 一种垃圾回收的装置、系统及方法 |
CN101833512A (zh) * | 2010-04-22 | 2010-09-15 | 中兴通讯股份有限公司 | 一种内存回收方法及其装置 |
US20150026428A1 (en) * | 2013-07-18 | 2015-01-22 | International Business Machines Corporation | Memory use for garbage collected computer environments |
CN108255582A (zh) * | 2018-01-16 | 2018-07-06 | 携程旅游信息技术(上海)有限公司 | java虚拟机垃圾回收的方法、系统、设备及存储介质 |
CN111124668A (zh) * | 2019-11-28 | 2020-05-08 | 宇龙计算机通信科技(深圳)有限公司 | 内存释放方法、装置、存储介质及终端 |
Cited By (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116089320A (zh) * | 2022-08-31 | 2023-05-09 | 荣耀终端有限公司 | 垃圾回收方法和相关装置 |
CN116089320B (zh) * | 2022-08-31 | 2023-10-20 | 荣耀终端有限公司 | 垃圾回收方法和相关装置 |
CN115292052A (zh) * | 2022-09-27 | 2022-11-04 | 荣耀终端有限公司 | 内存回收方法、电子设备及计算机可读存储介质 |
CN115292052B (zh) * | 2022-09-27 | 2023-08-08 | 荣耀终端有限公司 | 内存回收方法、电子设备及计算机可读存储介质 |
CN116302571A (zh) * | 2023-05-17 | 2023-06-23 | 北京趋动智能科技有限公司 | 内存释放方法、装置、客户端、服务端及存储介质 |
CN116302571B (zh) * | 2023-05-17 | 2023-10-20 | 北京趋动智能科技有限公司 | 内存释放方法、装置、客户端、服务端及存储介质 |
Also Published As
Publication number | Publication date |
---|---|
CN114721812A (zh) | 2022-07-08 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
WO2022143446A1 (zh) | 内存释放方法及相关设备 | |
CN111061516B (zh) | 加速应用的冷启动的方法、装置和终端 | |
JP5723968B2 (ja) | 便宜的マルチタスキング | |
US20190258514A1 (en) | I/O Request Scheduling Method and Apparatus | |
US20170277561A1 (en) | Operating System Hot-Switching Method and Apparatus and Mobile Terminal | |
WO2018036505A1 (zh) | 一种终端后台应用程序的管理方法及装置 | |
EP3937016A1 (en) | Memory management method and apparatus | |
CN107861817B (zh) | 基于线程阻塞的内存优化方法、移动终端及可读存储介质 | |
CN109669784B (zh) | 一种进程间通信的方法及系统 | |
EP3506105B1 (en) | Method and device for processing a memory and storage medium | |
EP3506106B1 (en) | Method for processing application, electronic device, and computer-readable storage medium | |
EP3506114B1 (en) | Memory processing method and device and storage medium | |
CN107479951B (zh) | 进程管控方法、装置、存储介质及电子设备 | |
WO2018045934A1 (zh) | 应用进程的管理方法和终端设备 | |
CN111158910A (zh) | 内存管理方法、装置、存储介质及电子设备 | |
US20160203083A1 (en) | Systems and methods for providing dynamic cache extension in a multi-cluster heterogeneous processor architecture | |
US10719351B2 (en) | Method for controlling states of processes and related device | |
CN108762831A (zh) | 应用程序预加载方法、装置、存储介质及终端 | |
WO2019137252A1 (zh) | 内存处理方法、电子设备、计算机可读存储介质 | |
CN110865884A (zh) | 一种内存管理方法及装置 | |
CN109697088A (zh) | 应用界面加载方法、移动终端及计算机可读存储介质 | |
CN111475299B (zh) | 内存分配方法、装置、存储介质及电子设备 | |
CN107967177B (zh) | 基于核心进程的内存优化方法、移动终端及可读存储介质 | |
WO2024119823A1 (zh) | Gpu计算资源的管理方法、装置、电子设备及可读存储介质 | |
WO2019128542A1 (zh) | 应用处理方法、电子设备、计算机可读存储介质 |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 21914183 Country of ref document: EP Kind code of ref document: A1 |
|
NENP | Non-entry into the national phase |
Ref country code: DE |
|
122 | Ep: pct application non-entry in european phase |
Ref document number: 21914183 Country of ref document: EP Kind code of ref document: A1 |