Method for recovering memory and electronic equipment
By pausing and resuming memory recycling tasks, the problem of resource preemption caused by applications occupying memory is solved, user experience and fluency are improved, and memory management efficiency is achieved.
Patent Information
- Application Number
- CN202310304657.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-20
- Publication Date
- 2024-09-20
AI Technical Summary
If an application occupies a large amount of memory during operation, if it is not recycled in time, it will cause other applications to crash, and the memory recycling process will seize processor resources, resulting in lag and reduced user experience.
By detecting when specific events occur in the process, the memory recycling task is paused and resumed to ensure that the process resumes memory recycling in a timely manner after responding to the event, avoiding resource preemption and improving fluency and response speed.
It effectively improves the memory shortage problem and user experience, improves the fluency and response speed of the application, and reduces development costs.
Smart Images

Figure CN118672935A
Abstract
Description
Technical Field
[0001] The present application relates to the field of terminal technology, and in particular to a method for recovering memory and an electronic device. Background Art
[0002] During the running process of an application, a large amount of memory is requested, which can be used to store various objects such as variables of the process. After the process uses these objects and discards them, if the memory occupied by these objects is not cleaned up in time, it will seriously occupy the total memory of the electronic device, and then cause other applications that cannot allocate memory to crash.
[0003] Currently, electronic devices can start to reclaim the memory of an application when the used memory of the application increases to a pre-set waterline value. However, since the memory recycling process will occupy the device resources such as the processor of the electronic device, this will cause the device resources of other threads of the application to be insufficient, causing the application to freeze or even pause. If the application is the foreground application, it will cause the screen displayed by the electronic device to freeze or drop frames, seriously affecting the user experience. Summary of the invention
[0004] In view of this, the present application provides a method for reclaiming memory and an electronic device to improve the smoothness of application operation and enhance user experience.
[0005] In order to achieve the above objectives, in a first aspect, an embodiment of the present application provides a method for reclaiming memory, the method comprising:
[0006] If it is detected that a first event occurs in the first process, instruct the kernel layer to suspend a memory recovery task, where the memory recovery task is used to recover memory corresponding to the first process;
[0007] If it is detected that the first process ends in response to the first event, the kernel layer is instructed to resume the memory reclaiming task.
[0008] In an embodiment of the present application, if a first event is detected in a first process, the kernel layer can be instructed to suspend memory recovery of the first process, so that the first process can respond to the first event after suspending memory recovery of the first process, thereby improving the problem of memory recovery task occupying device resources and improving the smoothness of responding to the first event. After the first process finishes responding to the first event, the kernel layer can be instructed to resume memory recovery of the first process in a timely manner, thereby improving the delay time of the memory recovery task due to responding to the first event, and further improving the problem of insufficient memory.
[0009] In some embodiments, the first process is a process of a first application, or the first process is a process of a first system service. In some embodiments, the first process may be a main process of the first application.
[0010] The first application may be a system application or a third-party application.
[0011] In some implementations, the method is applied to the first process or the second process, where the second process is a process of a second system service.
[0012] In some implementations, the instructing the kernel layer to suspend the memory recovery task includes:
[0013] Sending a suspend memory recovery instruction to the kernel layer through the first interface;
[0014] The instructing the kernel layer to resume the memory recovery task includes:
[0015] A memory recovery and reclaiming instruction is sent to the kernel layer through the second interface.
[0016] The first process can send a pause memory recovery instruction to the first virtual machine through the first interface, and send a resume memory recovery instruction to the first virtual machine through the second interface. That is, the timing of pausing and resuming the memory recovery task can be determined by the first process that obtains and responds to the first event, which improves the accuracy of the timing of pausing and resuming the memory recovery task, further improves the smoothness of responding to the first event, and further improves the problem of insufficient memory.
[0017] The second process can send a pause memory recovery instruction to the first virtual machine through the first interface, and send a resume memory recovery instruction to the first virtual machine through the second interface. Therefore, there is no need to upgrade the first application and the first system service, which improves the compatibility of the first application and the first system service and reduces the development cost of the first application and the first system service.
[0018] In some implementations, the instructing the kernel layer to suspend the memory recovery task includes:
[0019] Instructing a first virtual machine to suspend the memory reclaiming task, the first virtual machine corresponding to the first process;
[0020] The instructing the kernel layer to resume the memory recovery task includes:
[0021] Instruct the first virtual machine to resume the memory reclaiming task.
[0022] In some implementations, the first process is a process of the first application, and the instructing the kernel layer to suspend the memory recovery task includes:
[0023] If the condition for suspending the memory recovery task is met, the kernel layer is instructed to suspend the memory recovery task, and the condition for suspending the memory recovery task includes at least one of the following:
[0024] The first application belongs to a preset whitelist or the first application does not belong to a preset blacklist;
[0025] The first event is an event of a preset event type.
[0026] The preset whitelist and the preset blacklist may include identity information of at least one application.
[0027] In some implementations, the identity information of the application program may include an application identifier.
[0028] In some implementations, a configuration policy may be obtained, where the configuration policy includes any one of a preset whitelist and a preset blacklist and / or at least one preset event type.
[0029] Of course, in actual applications, it is also possible to determine whether the condition for suspending the memory recovery task is met by other means. In some embodiments, if the first process is an application running in the foreground, it is determined that the condition for suspending the memory recovery task is met. In some embodiments, if the first event is an event that needs to be responded to in the foreground, it is determined that the condition for suspending the memory recovery task is met. In some embodiments, if the device resources required to respond to the first event are greater than a preset device resource threshold, it is determined that the condition for suspending the memory recovery task is met. In some embodiments, if the fluency required to respond to the first event is greater than a preset fluency threshold, it is determined that the condition for suspending the memory recovery task is met.
[0030] In some embodiments, the first event is any one of starting the second application, exiting the second application, page switching transition, sliding within a page, display mode switching, and game animation. In some embodiments, the display mode switching may include at least one of switching between horizontal screen display and vertical screen display, and switching between the first window and the second window. Among them, at least one window attribute such as window size, aspect ratio, etc. of the first window and the second window may be different. In some embodiments, the first window and the second window may be full screen windows and non-full screen windows, respectively.
[0031] In some embodiments, if the event type of the first event is sliding within the page, a page sliding effect can be displayed; if the event type of the first event is page switching transition, a page transition animation effect can be displayed; if the event type of the first event is starting a second application, an animation effect of starting the second application can be displayed; if the event type of the first event is exiting the second application, an animation effect of exiting the second application can be displayed; if the event type of the first event is switching from full screen to window, an animation effect of switching from full screen display to window display can be displayed; if the event type of the first event is switching from window to full screen, an animation effect of switching from window display to full screen display can be displayed; if the event type of the first event is switching from horizontal screen to vertical screen, an animation effect of switching from horizontal screen mode to vertical screen mode can be displayed.
[0032] For example, the first process may be a process of a desktop application in an operating system, and the second application may be any application in the desktop.
[0033] In some implementations, at least one of the identity information of the first application, the event type of the first event, the timeout duration, and other information may be sent to the first virtual machine. In some implementations, at least one of the identity information of the first application, the event type of the first event, the timeout duration, and other information may be carried in the suspend memory recovery instruction.
[0034] In some embodiments, the first virtual machine may be instructed to suspend the memory recovery task, but the manner in which the first virtual machine suspends the memory recovery task may not be perceived. In other embodiments, the manner in which the first virtual machine suspends the memory recovery task may be instructed. In some embodiments, the manner in which the memory recovery task is suspended may be instructed by a suspend memory recovery instruction.
[0035] In a second aspect, an embodiment of the present application provides a method for reclaiming memory, including:
[0036] If a suspend memory recovery instruction is received, suspending the memory recovery task, wherein the suspend memory recovery instruction is an instruction triggered by a first event occurring in the first process, and the memory recovery task is used to recover memory corresponding to the first process;
[0037] If a resume memory recovery instruction is received, the memory recovery task is resumed, and the resume memory recovery instruction is an instruction triggered by the first process ending in response to the first event.
[0038] In some embodiments, when the difference between the used memory of the first process and the watermark value of the first process is less than or equal to a first value (i.e., the used memory of the first process is close to the watermark value), a memory recovery task can be triggered, and the memory recovery task can reclaim the memory of the first process.
[0039] In some embodiments, the method may be applied to a kernel layer. In some embodiments, the method may be applied to a first virtual machine corresponding to a first process.
[0040] In some embodiments, the method further comprises:
[0041] If the suspension duration of the memory reclaiming task is greater than or equal to the first duration, the memory reclaiming task is resumed.
[0042] In some implementations, the first duration corresponds to at least one of the first process and an event type of the first event.
[0043] In some implementations, the suspending the memory recovery task includes:
[0044] suspending the allocation of device resources to the memory recovery task;
[0045] The resuming the memory recovery task includes:
[0046] Resuming allocation of device resources to the memory reclaiming task.
[0047] In some implementations, the device resources may include time slices that occupy a processor.
[0048] In some implementations, the suspending the memory recovery task includes:
[0049] Increasing a watermark value of the first process, where the watermark value of the first process is used to trigger the memory recovery task;
[0050] The resuming the memory recovery task includes:
[0051] The increased waterline value of the first process is restored to the value before the increase.
[0052] In some implementations, the watermark value of the first process can be increased to the total memory allocated to the first process by the operating system (i.e., the sum of used memory and free memory). In some implementations, the watermark value of the first process can be increased to the maximum value allowed by the watermark value.
[0053] In some embodiments, the memory recovery task may be suspended in a manner indicated by the first process or the second process to suspend the memory recovery task. In some embodiments, the memory recovery task may be suspended in a manner indicated by the suspend memory recovery instruction to suspend the memory recovery task.
[0054] In some embodiments, the first duration indicated by the first process or the second process may be obtained. In some embodiments, at least one of the identity information of the first application and the event type of the first event indicated by the first process or the second process may be obtained. In some embodiments, at least one of the first duration carried by the instruction to suspend memory recovery, the identity information of the first application and the event type of the first event may be obtained.
[0055] In a third aspect, an embodiment of the present application provides a method for reclaiming memory, including:
[0056] If a first event occurs in the first process, suspending a memory recovery task, wherein the memory recovery task is used to recover memory corresponding to the first process;
[0057] The first process responds to the first event;
[0058] If the first process ends in response to the first event, the memory reclaiming task is resumed.
[0059] In some implementations, the suspending the memory recovery task includes:
[0060] The kernel layer suspends the memory recovery task;
[0061] The resuming the memory recovery task includes:
[0062] The kernel layer resumes the memory recovery task.
[0063] In some embodiments, the method further comprises:
[0064] If the first event occurs in the first process, the first process instructs the kernel layer to suspend the memory recovery task;
[0065] If the first process ends in response to the first event, the first process instructs the kernel layer to resume the memory reclaiming task.
[0066] In some embodiments, the method further comprises:
[0067] If the second process detects that the first event occurs in the first process, the second process instructs the kernel layer to suspend the memory recovery task;
[0068] If the second process detects that the first process ends responding to the first event, the second process instructs the kernel layer to resume the memory reclaiming task.
[0069] In some embodiments, the method further comprises:
[0070] If the suspension duration of the memory reclaiming task is greater than or equal to the first duration, the memory reclaiming task is resumed.
[0071] In a fourth aspect, an embodiment of the present application provides a device for reclaiming memory, which has the function of realizing the above aspects and possible implementation methods of the above aspects. The function can be implemented by hardware, or by hardware executing corresponding software. The hardware or software includes one or more modules or units corresponding to the above functions. For example, a transceiver module or unit, a processing module or unit, an acquisition module or unit, etc.
[0072] In a fifth aspect, an embodiment of the present application provides an electronic device, comprising: a memory and a processor, the memory being used to store a computer program; the processor being used to execute the method described in any one of the first aspect, any one of the second aspect, or any one of the third aspect when calling the computer program.
[0073] In a sixth aspect, an embodiment of the present application provides a chip system, comprising a processor, wherein the processor is coupled to a memory, and the processor executes a computer program stored in the memory to implement the method described in any one of the first aspect, any one of the second aspect, or any one of the third aspect.
[0074] The chip system may be a single chip or a chip module composed of multiple chips.
[0075] In a seventh aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the method described in any one of the first aspect, any one of the second aspect, or any one of the third aspect is implemented.
[0076] In an eighth aspect, an embodiment of the present application provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the method described in any one of the first aspect, any one of the second aspect, or any one of the third aspect.
[0077] It can be understood that the beneficial effects of the second to eighth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0078] Figure 1 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0079] Figure 2 A software structure block diagram of an electronic device provided in an embodiment of the present application;
[0080] Figure 3 A timing diagram of memory recovery provided in an embodiment of the present application;
[0081] Figure 4 A schematic diagram of a display page provided in an embodiment of the present application;
[0082] Figure 5 A schematic diagram of another display page provided in an embodiment of the present application;
[0083] Figure 6 A schematic diagram of another display page provided in an embodiment of the present application;
[0084] Figure 7 A schematic diagram of the structure of a memory recycling system provided in an embodiment of the present application;
[0085] Figure 8 Another timing diagram for reclaiming memory provided in an embodiment of the present application;
[0086] Fig. 9 A flowchart of a method for reclaiming memory provided in an embodiment of the present application;
[0087] Fig.10 A flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0088] Fig.11 A flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0089] Fig.12 A flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0090] Fig.13 A flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0091] Fig.14 A flowchart of another method for reclaiming memory provided in an embodiment of the present application. DETAILED DESCRIPTION
[0092] The memory recovery method provided in the embodiment of the present application can be applied to electronic devices such as mobile phones, tablet computers, wearable devices, vehicle-mounted devices, augmented reality (AR) / virtual reality (VR) devices, laptop computers, ultra-mobile personal computers (UMPC), netbooks, personal digital assistants (PDA), etc. The embodiment of the present application does not impose any restrictions on the specific type of electronic devices.
[0093] Figure 11 is a schematic diagram of the structure of an electronic device 100 provided in an embodiment of the present application. The electronic device 100 may include a processor 110, a memory 120, a communication module 130, and the like.
[0094] Among them, the processor 110 may include one or more processing units, and the memory 120 is used to store program codes and data. In an embodiment of the present application, the processor 110 can execute computer-executable instructions stored in the memory 120 to control and manage the actions of the electronic device 100. In some embodiments, the memory 120 is a cache memory. The memory can save instructions or data that the processor 110 has just used or circulated. If the processor 110 needs to use the instruction or data again, it can be directly called from the memory. Reduce repeated access, reduce the waiting time of the processor 110, and thus improve the efficiency of the system. In some embodiments, the memory 120 may include a random access memory (RAM). In some embodiments, when the electronic device 100 is an embedded device, it is limited by volume, power consumption or other reasons, and the electronic device 100 may include a memory 120 with a smaller storage space.
[0095] The communication module 130 can be used for communication between various internal modules of the electronic device 100, or communication between the electronic device 100 and other external electronic devices. Exemplarily, if the electronic device 100 communicates with other electronic devices via a wired connection, the communication module 130 may include an interface, such as a USB interface. The USB interface may be an interface that complies with USB standard specifications, specifically a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface can be used to connect a charger to charge the electronic device 100, and can also be used to transmit data between the electronic device 100 and peripheral devices. It can also be used to connect headphones to play audio through the headphones. The interface can also be used to connect other electronic devices, such as AR devices.
[0096] Alternatively, the communication module 130 may include an audio device, a radio frequency circuit, a Bluetooth chip, a wireless fidelity (Wi-Fi) chip, a near-field communication technology (NFC) module, etc., and can achieve interaction between the electronic device 100 and other electronic devices in a variety of different ways.
[0097] Optionally, the electronic device 100 may further include a display screen 140 , and the display screen 140 may display images or videos in a human-computer interaction interface.
[0098] Optionally, the electronic device 100 may further include a peripheral device 150 , such as a mouse, a keyboard, a speaker, a microphone, etc.
[0099] It should be understood that, in addition to Figure 1 In addition to the various components or modules listed in the figure, the embodiment of the present application does not specifically limit the structure of the electronic device 100. In other embodiments of the present application, the electronic device 100 may also include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure can be implemented in hardware, software, or a combination of software and hardware.
[0100] Figure 2 It is a software structure block diagram of the electronic device 100 according to an embodiment of the present application.
[0101] The layered architecture divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom, namely, the application layer, the application framework layer, the Android runtime and system library, and the kernel layer.
[0102] The application layer can include a series of application packages.
[0103] like Figure 2 As shown, the application package may include camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message and other applications.
[0104] The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer includes some predefined functions.
[0105] like Figure 2 As shown, the application framework layer may include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like.
[0106] The window manager is used to manage window programs. The window manager can obtain the display screen size, determine whether there is a status bar, lock the screen, capture the screen, etc.
[0107] Content providers are used to store and retrieve data and make it accessible to applications. The data may include videos, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.
[0108] The view system includes visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. A display interface can be composed of one or more views. For example, a display interface including a text notification icon can include a view for displaying text and a view for displaying images.
[0109] The phone manager is used to provide communication functions of the electronic device 100, such as management of call status (including connecting, hanging up, etc.).
[0110] The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.
[0111] The notification manager enables applications to display notification information in the status bar. It can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be a notification that appears in the system top status bar in the form of a chart or scroll bar text, such as notifications of applications running in the background, or a notification that appears on the screen in the form of a dialog window. For example, a text message is displayed in the status bar, a prompt sound is emitted, an electronic device vibrates, an indicator light flashes, etc.
[0112] Android runtime includes core libraries and virtual machines. Android runtime is responsible for scheduling and management of the Android system.
[0113] The core library consists of two parts: one part is the function that needs to be called by the Java language, and the other part is the Android core library.
[0114] The application layer and the application framework layer run in a virtual machine. The virtual machine executes the Java files of the application layer and the application framework layer as binary files. The virtual machine is used to perform functions such as object life cycle management, stack management, thread management, security and exception management, and garbage collection.
[0115] In some embodiments, the virtual machine may include a Java virtual machine (JVM). JVM is an abstract computer that can be used to parse and run applications. JVM can run on the operating system of the electronic device 100 without directly interacting with the hardware devices of the electronic device 100, so JVM can shield the hardware environment of the bottom layer of the electronic device 100, so that the application can run on platforms with different characteristics. In some embodiments, one JVM can correspond to one application.
[0116] The system library may include multiple functional modules, such as surface manager, media library, 3D graphics processing library (such as openGL ES), 2D graphics engine (such as SGL), etc.
[0117] The surface manager is used to manage the display subsystem and provide the fusion of 2D and 3D layers for multiple applications.
[0118] The media library supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
[0119] The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, compositing, and layer processing.
[0120] A 2D graphics engine is a drawing engine for 2D drawings.
[0121] The kernel layer is a layer between hardware and software. The kernel layer includes at least display driver, camera driver, audio driver, and sensor driver. In some embodiments, a virtual machine can run in the kernel layer.
[0122] The following is an illustrative description of the workflow of the software and hardware of the electronic device 100 in conjunction with starting the camera program.
[0123] The user touches the camera icon on the display screen 140. When the touch sensor in the display screen 140 receives the touch operation, the corresponding hardware interrupt is sent to the kernel layer. The kernel layer processes the touch operation into an original input event (including touch coordinates, timestamp of the touch operation and other information). The original input event is stored in the kernel layer. The application framework layer obtains the original input event from the kernel layer and identifies the control corresponding to the input event. Taking the touch operation as a touch single-click operation, and the control corresponding to the single-click operation is the control of the camera application icon as an example, the android runtime creates a JVM, the JVM loads the camera program to create a process corresponding to the camera program, and displays the shooting interface to the user on the display screen 140. That is, the camera program runs in the JVM, and through the JVM and the operating system of the electronic device 100, calls various hardware devices such as the camera included in the electronic device 100.
[0124] In order to facilitate understanding of the technical solutions in the embodiments of the present application, the application scenarios of the embodiments of the present application are first introduced below.
[0125] During the running process of an application, a large amount of memory is requested, which can be used to store various objects such as variables of the process. After the application uses these objects and discards them, if the memory occupied by these objects is not cleaned up in time, it will seriously occupy the total memory of the electronic device, and then cause other applications that cannot allocate memory to crash. The JVM can start the garbage collection (GC) thread. The GC thread can recycle the memory occupied by the garbage of the application.
[0126] In some implementations, a watermark value can be set in advance. As the application runs, the used memory of the application gradually increases and the free memory gradually decreases. When the used memory of the application increases to close to the watermark value, that is, the free memory is close to 0, the GC thread can be triggered to execute the memory recovery task to recycle the application's memory.
[0127] When the GC thread is performing the memory recovery task, it will occupy the CPU and other device resources of the electronic device, which will cause the device resources of other threads of the application to be insufficient, causing the application to freeze or even pause. If the application is the foreground application, it will cause the screen displayed on the electronic device to freeze or drop frames, seriously affecting the user experience.
[0128] Please refer to Figure 3 , is a timing diagram of memory recovery provided by an embodiment of the present application. Any time period during the running of the application, such as Figure 3 The three time periods t1-t2, t4-t5, and t6-t8 shown may all trigger the GC thread to reclaim memory, and the application is not aware of the timing of the GC thread reclaiming memory.
[0129] The application displays Figure 4 The video invitation page shown in the figure includes the prompt message "Inviting you to a video call", a rejection button, an answer button and a voice call button.
[0130] At time t3, the user clicks the answer button. If no GC thread seizes resources, the application's user interface (UI) thread can display the following information: Figure 4 The video invitation page shown switches to Figure 5 The animated switching effect of the video call page shown, the video call page may include images of the two parties in the video call, a hang-up button, a voice switch button, and a switch button between the front and rear cameras.
[0131] However, since the time period t4-t5 is within the time period t3-t7, and part of the time period t6-t8 is within the time period t3-t7, the GC thread will seize the device resources of the electronic device, and the UI thread may not be able to obtain enough device resources to display the animation switching effect. Therefore, after t3, it may still remain as Figure 4 The video invitation page shown here, or it may show something like Figure 6 The stuck page shown. Among them, Figure 6 Also included Figure 4 Part of the video invitation page shown and Figure 5 Part of the video call page shown. If the user sees Figure 6 , you cannot tell whether the video call is connected.
[0132] In order to solve at least part of the above technical problems, the present application provides a method for reclaiming memory. The technical solution of the present application is described in detail with specific embodiments below. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments.
[0133] Please refer to Figure 7 , is a schematic diagram of a memory recovery system provided in an embodiment of the present application. The system can be operated in the aforementioned Figure 1 The electronic device 100 is shown. The system includes application / system services, virtual machines and control services.
[0134] The control service can be used to obtain, query and control configuration policies, wherein the configuration policies can be used to indicate parameters and configurations related to memory recovery. In some embodiments, the control service can be implemented as a system service. Of course, in actual applications, the configuration service can also be implemented as a system application or a third-party application. In some embodiments, the configuration policy can be a configuration policy for a system application and a third-party application. In some embodiments, the configuration policy can be a configuration policy for a third-party application.
[0135] In some embodiments, the configuration policy may include a master switch. When the master switch is on, the application, system service, and virtual machine may reclaim memory in the manner of reclaiming memory provided in the embodiment of the present application. When the master switch is off, the application, system service, and virtual machine may not reclaim memory in the manner of reclaiming memory provided in the embodiment of the present application.
[0136] In some embodiments, the configuration strategy may include at least one preset event type. The at least one event type may be an event type that is easily perceived by the user and has a greater impact on the user experience, or the at least one event type may be an event type that requires a higher degree of response fluency. In some embodiments, the at least one event type may be a foreground input event. In some embodiments, the at least one event type may include one or more of starting and exiting an application, page switching transitions, sliding within a page, display mode switching, and game animations. In some embodiments, the display mode switching may include at least one of switching between horizontal screen display and vertical screen display, and switching between a first window and a second window. Among them, at least one window attribute of the first window and the second window, such as window size, aspect ratio, etc., may be different. In some embodiments, the first window and the second window may be full-screen windows and non-full-screen windows, respectively.
[0137] In some embodiments, the configuration strategy may include at least one timeout duration. The timeout duration may be used to indicate the maximum duration for pausing memory recovery. In some embodiments, the timeout duration may be greater than the duration required to respond to an event of all or part of the event types in the aforementioned at least one event type. In some embodiments, the at least one timeout duration may include 500ms (milliseconds), 1000 milliseconds, and 5000 milliseconds. It is to be understood that, in practical applications, the numerical value of the timeout duration may also be other numerical values, and the embodiments of the present application do not limit the specific numerical value of the timeout duration.
[0138] In some embodiments, the configuration policy may include a preset blacklist or a preset whitelist. The preset blacklist and the preset whitelist may include identity information of at least one application. In some embodiments, if the identity information of an application is not in the preset blacklist, that is, the application does not belong to the preset blacklist, then the memory can be reclaimed for the application in accordance with the memory reclaiming method provided in the embodiment of the present application. In some embodiments, if the identity information of an application is in the preset whitelist, that is, the application belongs to the preset whitelist, then the memory can be reclaimed for the application in accordance with the memory reclaiming method provided in the embodiment of the present application.
[0139] It should be noted that the identity information of an application may include application identifiers such as the application package name and the application name. Of course, in actual applications, the identity information of an application may also include other information that can explain the identity of the application. The embodiments of the present application do not limit the type of the identity information of the application.
[0140] It is understandable that the various parameters included in the configuration policy can be combined with each other. In some embodiments, different applications can correspond to different event types, that is, different event types are set for different applications. In some embodiments, different applications can correspond to different timeout durations, that is, different timeout durations are set for different applications. In some embodiments, different event types can correspond to different timeout durations, that is, different timeout durations are set for different event types.
[0141] For example, the timeout duration for event types such as page switching, page sliding, and display mode switching can be 500ms. The timeout duration for event types such as starting and exiting an application can be 1000ms.
[0142] For another example, the timeout period corresponding to the start and exit events of application A may be 1000 ms, and the timeout period corresponding to the start and exit events of application B may be 5000 ms.
[0143] In some implementations, the control service may read a related configuration file and perform initialization, thereby acquiring the configuration policy included in the configuration file.
[0144] In some implementations, the control service may provide a query interface, which may be used to query any parameter or configuration included in the configuration policy.
[0145] In some implementations, the control service may control the configuration policy, such as controlling the main switch to be on or off, adding or deleting event types, adjusting the timeout duration, updating the blacklist / whitelist, etc.
[0146] Applications may include system applications or third-party applications. Applications may obtain the events that occur and respond to the events, wherein the manner of responding to the events may match the event type of the event. Taking responding to input events as an example, if the input event is an in-page sliding event, responding to the input event may include displaying an in-page sliding effect. In order to improve the problem of the process of responding to the event being occupied by the memory recovery process and to ensure that the input event can be responded to smoothly and the user experience can be improved, the application may send a pause GC instruction to the virtual machine before responding to the event, and the pause GC instruction may be used to instruct the virtual machine to pause the memory recovery task. In order to ensure that the virtual machine can continue to recycle memory and improve memory utilization, the application may send a resume GC instruction to the virtual machine after responding to the event, and the resume GC instruction may be used to instruct the virtual machine to resume the memory recovery task.
[0147] In some implementations, when an event occurs in the application, it can be determined based on the aforementioned configuration policy whether it is necessary to suspend the memory recovery task before responding to the event. If so, a suspend GC instruction is sent to the virtual machine before responding to the event, and a resume GC instruction is sent to the virtual machine after responding to the event. It is understandable that in actual applications, the application can also determine whether it is necessary to suspend the memory recovery task before responding to the event in other ways.
[0148] Similarly, when an event occurs in the system service, a pause GC instruction may be sent to the virtual machine before responding to the event, and a resume GC instruction may be sent to the virtual machine after responding to the event in the same or similar manner as the application.
[0149] In some implementations, another system service may send a pause GC instruction to the virtual machine before detecting an event in the application or system service and responding to the event, and send a resume GC instruction to the virtual machine after detecting that the application or system service ends responding to the event.
[0150] The kernel layer, specifically a virtual machine corresponding to an application or a system service in the kernel layer, can be used to execute a memory recovery task, suspend the memory recovery task based on a suspend GC instruction, and resume the memory recovery task based on a resume GC instruction.
[0151] In some embodiments, the kernel layer may include a suspend GC interface and a resume GC interface. The kernel layer may receive a suspend GC instruction through the suspend GC interface and receive a resume GC instruction through the resume GC interface. In some embodiments, the suspend GC interface and the resume GC interface may be combined into one interface.
[0152] The following will combine specific examples to Figure 7 The operation of the reclaim memory system is described in detail.
[0153] The relevant technicians update the operating system of the electronic device 100, including adding control services, related configuration files, and pausing and resuming the GC interface. When the electronic device 100 is turned on, the control service starts, obtains the related configuration files and initializes them, and obtains the configuration strategy.
[0154] Taking application A as an example, when application A is running in the foreground of electronic device 100, the user slides within the page displayed by application A. Since the page sliding event is an event type included in the configuration policy, the package name of application A is com.AA, and com.AA is the package name included in the whitelist in the configuration policy, it can be determined that GC peak shifting needs to be performed. Application A sends a pause GC instruction to virtual machine A corresponding to application A through the pause GC interface.
[0155] When receiving the suspend GC instruction, virtual machine A suspends the memory recovery task. After application A sends the suspend GC instruction, since there will be no memory recovery task operation to seize the device resources of the electronic device 100, application A can apply for sufficient device resources to respond to the page sliding event, thereby smoothly showing the page sliding effect to the user.
[0156] After responding to the sliding event within the page, application A can send a GC recovery instruction to virtual machine A through the GC recovery interface, and virtual machine A resumes executing the memory recovery task to improve the problem of insufficient memory.
[0157] Please refer to Figure 8 , which is another timing diagram for reclaiming memory provided in an embodiment of the present application.
[0158] The application displays Figure 4 The video invitation page shown in the figure includes the prompt message "Invite you to a video call", a rejection button, an answer button and a voice call button.
[0159] At time t3, the user clicks the answer button. Before responding to the answer button click event, the application instructs the virtual machine to suspend the memory recovery task through the suspend GC interface, and in response to the answer button click event, instructs the virtual machine to resume the memory recovery task through the resume GC interface. Therefore, during the t3-t4 period, the virtual machine will not execute the GC thread, and the UI thread can apply for sufficient device resources to display the content smoothly. Figure 4 The video invitation page shown switches to Figure 5 The animation switching effect of the video call page is shown. And because the application has instructed the virtual machine to resume the memory recovery task at time t4, the GC thread can recycle memory again in the time period t5-t6 after time t4, improving the problem of insufficient memory.
[0160] In some embodiments, the first process in the following embodiments may be a process of a first application or a process of a first system service, and the first application may be a system application or a third-party application. In some embodiments, the first process may be a main process of the first application. In some embodiments, the first process may be replaced by the first application or the first system service.
[0161] In some implementations, the second process in the following embodiments may be a process of a second system service. In some implementations, the second process may be replaced by a second system service.
[0162] In some implementations, the first virtual machine in the following embodiments may be replaced by a kernel layer.
[0163] Please refer to Fig. 9 , is a flow chart of a method for reclaiming memory tasks provided in an embodiment of the present application. It should be noted that the method does not Fig. 9 The specific order described below is a limitation, and it should be understood that in other embodiments, the order of some steps in the method can be interchanged according to actual needs, or some steps can be omitted or deleted. The method includes the following steps:
[0164] S901: A first event occurs in a first process.
[0165] In some embodiments, the first event is an input event, such as a user clicking or sliding a page. In some embodiments, the first process may include a plurality of windows, and the images in the plurality of windows are combined and displayed on the display screen of the electronic device to form a page seen by the user. The user may operate any window by touching the screen or pressing the keyboard (i.e., user input), and the window manager in the electronic device may obtain the first event input by the user from hardware devices such as the display screen and the keyboard, and then distribute the first event to the first process.
[0166] In some embodiments, the first event may also include other events. For example, the first event may be an event triggered by the first process, or the first event may be an event triggered by the operating system of the electronic device or other applications to the first process.
[0167] It should be noted that the embodiment of the present application does not limit the situation where the first event occurs in the first process.
[0168] S902, the first process determines whether to instruct the first virtual machine to suspend the memory recovery task. If yes, execute S903, otherwise execute S904.
[0169] The memory recovery task may be used to recover the memory of the first process.
[0170] In some implementations, the first process may correspond to a first virtual machine, and the first process may run in the first virtual machine.
[0171] In some embodiments, when the first event is detected, the first process may determine whether the condition for suspending the memory reclaiming task is met, and if so, instruct the first virtual machine to suspend the memory reclaiming task; otherwise, the first virtual machine is not instructed to suspend the memory reclaiming task.
[0172] In some embodiments, the first process is a process of the first application, and whether the condition for suspending the memory recovery task is met can be determined by configuring a policy. In some embodiments, the configuration policy may include at least one of a preset whitelist, a preset blacklist, and a preset event type. In some embodiments, the condition for suspending the memory recovery task includes at least one of the following: Condition 1: the first application belongs to the preset whitelist or the first application does not belong to the preset blacklist; Condition 2: the first event is an event of a preset event type.
[0173] Of course, in actual applications, it is also possible to determine whether the condition for suspending the memory recovery task is met by other means. In some embodiments, if the first process is an application running in the foreground, it is determined that the condition for suspending the memory recovery task is met. In some embodiments, if the first event is an event that needs to be responded to in the foreground, it is determined that the condition for suspending the memory recovery task is met. In some embodiments, if the device resources required to respond to the first event are greater than a preset device resource threshold, it is determined that the condition for suspending the memory recovery task is met. In some embodiments, if the fluency required to respond to the first event is greater than a preset fluency threshold, it is determined that the condition for suspending the memory recovery task is met.
[0174] It should be noted that the device resources required to respond to the first event, the device resource threshold, the fluency required to respond to the first event, and the fluency threshold can be obtained through prior settings, such as being determined by a developer. Of course, in actual applications, the device resources required to respond to the first event, the device resource threshold, the fluency required to respond to the first event, and the fluency threshold can also be determined by other means. The embodiments of the present application do not limit the numerical values and determination methods of the device resources required to respond to the first event, the device resource threshold, the fluency required to respond to the first event, and the fluency threshold.
[0175] In some embodiments, the first process can be any application, and the first event can also be any event, that is, when any application obtains any event, it can directly execute the following S903, and no longer execute S902 to determine whether to instruct the first virtual machine to suspend the memory recovery task, so S902 can be omitted.
[0176] S903: The first process instructs the first virtual machine to suspend the memory reclaiming task.
[0177] In some embodiments, the first process may send a pause memory recovery instruction to the first virtual machine. The pause recovery instruction may be used to instruct the first virtual machine to suspend the memory recovery task. The first process sends a pause recovery instruction to the first virtual machine to instruct the first virtual machine to suspend the recovery of memory corresponding to the first process, so as to save device resources required for memory recovery and give priority to responding to the first event, thereby improving the fluency of responding to the first event.
[0178] In some embodiments, the first process may send a pause recovery instruction to the first virtual machine through the first interface. In some embodiments, the pause memory recovery instruction may include a pause GC instruction, and the first interface may include a pause GC interface. Of course, it is understood that in actual applications, the pause memory recovery instruction may also include other instructions that can instruct to suspend the memory recovery task.
[0179] In some implementations, the first process may send at least one of the identity information of the first application, the event type of the first event, the timeout period, etc. to the first virtual machine. In some implementations, at least one of the identity information of the first application, the event type of the first event, the timeout period, etc. may be carried in the suspend memory recovery instruction.
[0180] In some embodiments, the first process may only instruct the first virtual machine to suspend the memory recovery task, but may not perceive the manner in which the first virtual machine suspends the memory recovery task. In other embodiments, the first process may also instruct the first virtual machine to suspend the memory recovery task. In some embodiments, the first process may instruct the manner in which the memory recovery task is suspended by a suspend memory recovery instruction.
[0181] S904: The first process responds to the first event.
[0182] Since the memory recovery task has been suspended, the first process can obtain sufficient device resources to respond to the first event, thereby ensuring the smoothness of responding to the first event.
[0183] In some implementations, the first event may be responded to based on the event type of the first event.
[0184] For example, if the event type of the first event is sliding within the page, the page sliding effect can be displayed; if the event type of the first event is page switching transition, the page transition animation effect can be displayed; if the event type of the first event is starting the second application, the animation effect of starting the second application can be displayed; if the event type of the first event is exiting the second application, the animation effect of exiting the second application can be displayed; if the event type of the first event is switching from full screen to window, the animation effect of switching from full screen display to window display can be displayed; if the event type of the first event is switching from window to full screen, the animation effect of switching from window display to full screen display can be displayed; if the event type of the first event is switching from horizontal screen to vertical screen, the animation effect of switching from horizontal screen mode to vertical screen mode can be displayed.
[0185] The second application may be a system application or a third-party application. For example, the first process may be a process of a desktop application in an operating system, and the second application may be a process of any application in the desktop. In some implementations, the second application and the first application may be the same application.
[0186] It is understandable that, in actual applications, the event type of the first event may also be other event types, and the first process may also respond to the first event in other ways. The embodiment of the present application does not limit the way of responding to the first event.
[0187] S905: After finishing responding to the first event, the first process instructs the first virtual machine to resume the memory reclaiming task.
[0188] In some embodiments, the first process may send a resume memory recovery instruction to the first virtual machine. The resume memory recovery instruction may instruct the first virtual machine to resume the memory recovery task. After the first event is completed, in order to enable the first virtual machine to continue to reclaim memory and improve memory utilization, the first process may instruct the first virtual machine to resume the memory recovery task.
[0189] In some embodiments, the first process may send a recovery and reclaim instruction to the first virtual machine through the second interface. In some embodiments, the recovery and reclaim memory instruction may include a recovery GC instruction, and the second interface may include a recovery GC interface. Of course, it is understood that in actual applications, the recovery and reclaim memory instruction may also include other instructions that may instruct the recovery and reclaim memory task.
[0190] In some implementations, after executing S904 to start responding to the first event, the first process may detect whether the response to the first event is complete. If so, the first virtual machine is instructed to resume the memory recovery task. Otherwise, the first event is detected again to determine whether the response to the first event is complete.
[0191] In an embodiment of the present application, the first process can instruct the first virtual machine to suspend memory recovery of the first process when the first event occurs. The first process responds to the first event after instructing the first virtual machine to suspend memory recovery of the first process, thereby improving the problem of the first virtual machine reclaiming memory and occupying device resources, and improving the fluency of responding to the first event. The first process can also instruct the first virtual machine to resume memory recovery of the first process in a timely manner after responding to the first event, thereby improving the delay time of the memory recovery task due to responding to the first event, and further improving the problem of insufficient memory. In addition, the first process can send a suspend memory recovery instruction to the first virtual machine through the first interface, and send a resume memory recovery instruction to the first virtual machine through the second interface, that is, the timing of suspending and resuming the memory recovery task can be determined by the first process that obtains and responds to the first event, thereby improving the accuracy of the timing of suspending and resuming the memory recovery task, further improving the fluency of responding to the first event, and further improving the problem of insufficient memory.
[0192] Please refer to Fig.10 , is a flow chart of a method for reclaiming memory provided in an embodiment of the present application. It should be noted that the method does not Fig.10 The specific order described below is a limitation, and it should be understood that in other embodiments, the order of some steps in the method can be interchanged according to actual needs, or some steps can be omitted or deleted. The method includes the following steps:
[0193] S1001, if the second process detects that the first event occurs in the first process, the second process determines whether to instruct the first virtual machine to suspend the memory recovery task. If so, execute S1002, otherwise end.
[0194] Since the first process needs to rely on the operating system of the electronic device to use the various device capabilities of the electronic device to run on the electronic device, the electronic device can monitor and manage the first process through the second process in the operating system, including monitoring and obtaining the identity information of the first process, the event type of the first event obtained by the first process, whether the first process responds to the first event in the foreground, and other information related to the memory recovery task, etc.
[0195] It should be noted that the way in which the second process determines whether to instruct the first virtual machine to suspend the memory recovery task can be the same or similar to the way in which the first process determines whether to instruct the first virtual machine to suspend the memory recovery task in the aforementioned S902, and will not be repeated here.
[0196] S1002: The second process instructs the first virtual machine to suspend a memory reclaiming task.
[0197] In some embodiments, the second process may send a pause recovery instruction to the first virtual machine through the first interface. In some embodiments, the pause memory recovery instruction may include a pause GC instruction, and the first interface may include a pause GC interface. Of course, it is understandable that in actual applications, the pause memory recovery instruction may also include other instructions that can instruct to suspend the memory recovery task.
[0198] It should be noted that the way in which the second process instructs the first virtual machine to suspend the memory reclaiming task may also be the same as or similar to the way in which the first process instructs the first virtual machine to suspend the memory reclaiming task in S903 above.
[0199] S1003: After the first process finishes responding to the first event, the second process instructs the first virtual machine to resume the memory reclaiming task.
[0200] In some embodiments, the second process may send a recovery and reclaim instruction to the first virtual machine through the second interface. In some embodiments, the recovery and reclaim memory instruction may include a recovery GC instruction, and the second interface may include a recovery GC interface. Of course, it is understood that in actual applications, the recovery and reclaim memory instruction may also include other instructions that may instruct the recovery and reclaim memory task.
[0201] In some embodiments, after the first process starts to respond to the first event, the second process can detect whether the first event response is completed. If so, it can instruct the first virtual machine to resume the memory recovery task. Otherwise, it can detect again whether the first event response is completed.
[0202] It should be noted that the manner in which the second process instructs the first virtual machine to resume the memory reclaiming task may also be the same as or similar to the manner in which the first process instructs the first virtual machine to resume the memory reclaiming task in S905 .
[0203] In an embodiment of the present application, the second process can instruct the first virtual machine to suspend memory recovery of the first process when detecting that the first event has occurred in the first process, so that the first process can respond to the first event after the first virtual machine suspends memory recovery of the first process, thereby improving the problem of the first virtual machine reclaiming memory and occupying device resources, and improving the fluency of responding to the first event. The second process can also instruct the first virtual machine to resume memory recovery of the first process in a timely manner after the response to the first event ends, thereby improving the delay time of the memory recovery task due to responding to the first event, and further improving the problem of insufficient memory. In addition, the second process can send a pause memory recovery instruction to the first virtual machine through the first interface, and send a resume memory recovery instruction to the first virtual machine through the second interface, so there is no need to upgrade the first application and the first system service, thereby improving the compatibility of the first application and the first system service, and reducing the development cost of the first application and the first system service.
[0204] Please refer to Fig.11 , is a flow chart of a method for reclaiming memory provided in an embodiment of the present application. It should be noted that the method does not Fig.11 The specific order described below is a limitation, and it should be understood that in other embodiments, the order of some steps in the method can be interchanged according to actual needs, or some steps can be omitted or deleted. The method includes the following steps:
[0205] S1101, triggering a memory recovery task.
[0206] In some embodiments, when the difference between the used memory of the first process and the watermark value of the first process is less than or equal to a first value (i.e., the used memory of the first process is close to the watermark value), a memory recovery task can be triggered, and the memory recovery task can reclaim the memory of the first process.
[0207] It should be noted that the embodiment of the present application does not limit the size of the first numerical value.
[0208] It should also be noted that the memory recovery task can also be triggered in other ways.
[0209] S1102, the first virtual machine determines whether the memory recovery task is in a suspended state. If yes, execute S1103, otherwise execute S1105.
[0210] In some implementations, the first virtual machine may suspend the memory reclaiming task upon receiving the suspend memory reclaiming instruction.
[0211] In some embodiments, pausing the memory recovery task may include stopping allocating device resources to the memory recovery task, such as occupying a processor time slice, etc. In some embodiments, the memory recovery task may be executed by a GC thread, and the GC thread may be suspended by calling a sleep function. The first virtual machine may determine that the memory recovery task is in a suspended state when the memory recovery task is suspended and does not occupy device resources such as a processor time slice, otherwise, determine that the memory recovery task is not in a suspended state.
[0212] It is understandable that, in actual applications, the GC thread may be suspended in other ways, or the allocation of device resources to the suspended task may be stopped in other ways.
[0213] In some implementations, pausing the memory recovery task may include increasing the watermark value of the first process. By increasing the watermark value of the first process, the available memory corresponding to the first process may be smaller than the increased watermark value for a period of time or permanently, thereby preventing the memory recovery task from continuing to be executed. The first virtual machine may determine whether the memory recovery task is in a suspended state when the current watermark value is greater than or equal to the second value.
[0214] In some implementations, the watermark value of the first process may be increased to the total memory allocated to the first process by the operating system (i.e., the sum of used memory and free memory). The first virtual machine may determine that the memory reclaiming task is in a suspended state when detecting that the current watermark value is the total memory allocated to the first process.
[0215] In some implementations, the watermark value of the first process may be increased to a maximum value allowed by the watermark value. The first virtual machine may determine that the memory reclaiming task is in a suspended state when detecting that the current watermark value is the maximum value allowed by the watermark value.
[0216] In some embodiments, the first virtual machine may suspend the memory recovery task in a manner indicated by the first process or the second process to suspend the memory recovery task. In some embodiments, the first virtual machine may suspend the memory recovery task in a manner indicated by the suspend memory recovery instruction to suspend the memory recovery task.
[0217] S1103, the first virtual machine determines whether the pause duration is greater than or equal to the first duration, if yes, executes S1104, otherwise ends.
[0218] If the memory recovery task is already in a suspended state, the first virtual machine can obtain the suspension duration of the memory recovery task, which can be the duration from the start of suspending the memory recovery task to the current state. In order to improve the problem of insufficient memory caused by suspending the memory recovery task for too long, the first virtual machine can compare the suspension duration with the first duration to determine whether the suspension duration has timed out.
[0219] In some embodiments, the first duration may be a global duration, that is, the duration of the first duration is independent of the first process and the first event. In some embodiments, the configuration policy provides multiple timeout durations, and the electronic device, the user, or the relevant technician may select any one of the multiple timeout durations as the first duration, and the first virtual machine may obtain the first duration indicated by the configuration policy.
[0220] In some embodiments, the first duration may correspond to at least one of the first application and the first event, and the first virtual machine may obtain the corresponding first duration based on at least one of the first application and the first event. In some embodiments, the configuration policy includes identity information of at least one application and / or at least one event type, and the corresponding timeout duration, and the first virtual machine may obtain the first duration from the configuration policy based on at least one of the identity information of the first application and the event type of the first event.
[0221] In some embodiments, the first virtual machine may obtain the first duration indicated by the first process or the second process. In some embodiments, the first virtual machine may obtain at least one of the identity information of the first application and the event type of the first event indicated by the first process or the second process. In some embodiments, the first virtual machine may obtain at least one of the first duration carried by the instruction to suspend memory recovery, the identity information of the first application and the event type of the first event.
[0222] S1104: The first virtual machine resumes the memory reclaiming task.
[0223] If the memory recovery task is in a paused state and the pause duration is greater than or equal to the first duration, the first event that may have preempted the device resources by the memory recovery task has been responded to, or, even if the first event has not been responded to, failure to execute the memory recovery task may result in severe memory shortage. Therefore, the first virtual machine can resume the memory recovery task to improve the memory shortage problem.
[0224] In some embodiments, resuming the memory recovery task may include resuming the allocation of device resources to the memory recovery task. In some embodiments, the memory recovery task may be executed by a GC thread, and the GC thread may be restored by calling a notify (wake-up) function. In some embodiments, resuming the memory recovery task may include reducing the watermark value corresponding to the first process. In some embodiments, resuming the memory recovery task may include restoring the watermark value corresponding to the first process to a value before the watermark value increased. It is understandable that based on the different ways of suspending the memory recovery task, the ways of resuming the memory recovery task may also be different.
[0225] In some implementations, the first virtual machine may resume the memory reclaiming task upon receiving the resume memory reclaiming instruction.
[0226] S1105: The first virtual machine executes a memory reclaiming task.
[0227] If the memory reclaiming task is not currently in a paused state, there is no first event that may preempt device resources by the memory reclaiming task, so the first virtual machine can execute the memory reclaiming task to improve the problem of insufficient memory.
[0228] In some implementations, the first virtual machine may run a GC thread to perform a memory reclaiming task through the GC thread.
[0229] It is understandable that, in actual applications, the first virtual machine may also perform the memory reclaiming task in other ways.
[0230] In an embodiment of the present application, when a task to recycle the memory of the first process is triggered, the first virtual machine determines whether the task is in a paused state. If the task is currently in a paused state and the pause duration has not timed out, the first virtual machine may not execute the task, that is, suspend the recovery of the memory of the first process, so that the first process can respond to the first event after the first virtual machine suspends the recovery of the memory of the first process, thereby improving the problem of the first virtual machine's memory recovery task occupying device resources and improving the smoothness of responding to the first event. If the task is not in a paused state, or, although the task is in a paused state but the pause duration has timed out, the first virtual machine can resume the recovery of the memory of the first process in a timely manner, thereby improving the delay duration of the memory recovery task due to responding to the first event, and further improving the problem of insufficient memory.
[0231] Please refer to Fig.12 , is a flow chart of a method for reclaiming memory provided in an embodiment of the present application. It should be noted that the method does not Fig.12 The specific order described below is a limitation, and it should be understood that in other embodiments, the order of some steps in the method can be interchanged according to actual needs, or some steps can be omitted or deleted. The method includes the following steps:
[0232] S1201: If a first event occurs in a first process, suspend the memory recovery task.
[0233] The manner in which the first event occurs in the first process may refer to the related description in S901 mentioned above.
[0234] In some implementations, the first virtual machine may suspend the memory reclaiming task.
[0235] In some implementations, the first virtual machine may suspend the memory reclaiming task upon receiving the suspend memory reclaiming instruction.
[0236] In some implementations, the first process may execute operations that are the same as or similar to the aforementioned S901 - S903 , and if the first event occurs, a suspend memory reclaiming instruction is sent to the first virtual machine.
[0237] In some implementations, the second process may execute operations that are the same or similar to the aforementioned S1001 - S1002 , and if it is detected that the first event occurs in the first process, a suspend memory recovery instruction is sent to the first virtual machine.
[0238] In some implementations, the second process may be integrated on the first virtual machine, that is, the first virtual machine may suspend the memory reclaiming task when detecting that the first event occurs in the first process.
[0239] S1202: The first process responds to the first event.
[0240] The way in which the first process responds to the first event may refer to the related description in the aforementioned S904.
[0241] S1203: If the first process ends in response to the first event, the memory recovery task is resumed.
[0242] In some implementations, the first virtual machine may resume the memory reclaiming task upon receiving the resume memory reclaiming instruction.
[0243] In some implementations, the first process may execute the same or similar operations as the aforementioned S905, and if the response to the first event ends, a recovery memory reclaiming instruction is sent to the first virtual machine.
[0244] In some implementations, the second process may perform the same or similar operations as the aforementioned S1003, and if it is detected that the first process ends in response to the first event, a recovery memory reclaiming instruction is sent to the first virtual machine.
[0245] In some embodiments, the second process may be integrated on the first virtual machine, that is, the first virtual machine may resume the memory reclaiming task when detecting that the first process ends in response to the first event.
[0246] In the embodiment of the present application, if the first event occurs in the first process, the memory recovery of the first process can be suspended, and the first process responds to the first event after suspending the memory recovery of the first process, thereby improving the problem of the memory recovery task occupying device resources and improving the smoothness of responding to the first event. After the first process finishes responding to the first event, the memory recovery of the first process can be resumed in time, thereby improving the delay time of the memory recovery task due to responding to the first event, thereby improving the problem of insufficient memory.
[0247] Please refer to Fig.13 , is a flow chart of a method for reclaiming memory provided in an embodiment of the present application. The method can be applied to the first process or the second process. It should be noted that the method is not based on Fig.13The specific order described below is a limitation, and it should be understood that in other embodiments, the order of some steps in the method can be interchanged according to actual needs, or some steps can be omitted or deleted. The method includes the following steps:
[0248] S1301: If it is detected that a first event occurs in a first process, the kernel layer is instructed to suspend a memory recovery task, where the memory recovery task is used to recover memory corresponding to the first process.
[0249] S1302: If it is detected that the first process ends in response to the first event, the kernel layer is instructed to resume the memory recovery task.
[0250] It should be noted that the execution method of S1301-S1302 can also refer to the relevant descriptions in the steps executed by the aforementioned first process or the second process, and will not be repeated here.
[0251] In an embodiment of the present application, if a first event is detected in a first process, the kernel layer can be instructed to suspend memory recovery of the first process, so that the first process can respond to the first event after suspending memory recovery of the first process, thereby improving the problem of memory recovery task occupying device resources and improving the smoothness of responding to the first event. After the first process finishes responding to the first event, the kernel layer can be instructed to resume memory recovery of the first process in a timely manner, thereby improving the delay time of the memory recovery task due to responding to the first event, and further improving the problem of insufficient memory.
[0252] Please refer to Fig.14 , is a flow chart of a method for reclaiming memory provided in an embodiment of the present application. The method can be applied to the kernel layer, and specifically can be applied to the first virtual machine corresponding to the first process in the kernel layer. It should be noted that the method does not Fig.14 The specific order described below is a limitation, and it should be understood that in other embodiments, the order of some steps in the method can be interchanged according to actual needs, or some steps can be omitted or deleted. The method includes the following steps:
[0253] S1401, if a suspend memory recovery instruction is received, suspend the memory recovery task, the suspend memory recovery instruction is an instruction triggered by a first event occurring in the first process, and the memory recovery task is used to reclaim memory corresponding to the first process.
[0254] S1402: If a memory recovery task is received, the memory recovery task is recovered. The memory recovery task is triggered by the first process ending in response to the first event.
[0255] It should be noted that the execution method of S1401-S1402 can also refer to the relevant descriptions in the steps executed by the aforementioned kernel layer or the first virtual machine, and will not be repeated here.
[0256] In the embodiment of the present application, if a pause recovery instruction is received that is triggered by a first event occurring in a first process, the memory recovery of the first process can be paused, so that the first process can respond to the first event after pausing the memory recovery of the first process, thereby improving the problem of the memory recovery task occupying device resources and improving the smoothness of responding to the first event. If a resume memory recovery instruction is received that is triggered by the first process ending in response to the first event, the memory recovery of the first process can be resumed in time, thereby improving the delay time of the memory recovery task due to responding to the first event, and further improving the problem of insufficient memory.
[0257] The following will illustrate the method for reclaiming memory described in the embodiment of the present application with reference to specific examples.
[0258] The electronic device includes desktop applications and video applications, wherein the desktop applications are system applications or system services, and the video applications are third-party applications.
[0259] The electronic device runs the desktop application and displays a desktop page on the display screen, wherein the desktop page includes an icon of the video application.
[0260] When the user clicks on the icon of the video application, the event of starting the video application is triggered, and the desktop application can pull up the main process of the video application through other related system services. Among them, the desktop application can be used as the first application or the first system service, the process of the desktop application can be used as the first process, and the event of starting the video application can be used as the first event. Therefore, before responding to the event of starting the video application, the electronic device can suspend the recovery of the memory corresponding to the desktop application, and after the response to the event of starting the video application ends, the electronic device can resume the recovery of the memory corresponding to the desktop application.
[0261] When the desktop application starts the main process of the video application through other related system services, it can trigger the page display event of the video application, and the page display event can display the main page of the video application. The video application can be used as the first application program, the main process of the video application can be used as the first process, and the page display event can be used as the first event. Therefore, before responding to the page display event, the electronic device can suspend the recovery of the memory corresponding to the video application, and after finishing responding to the page display event, resume the recovery of the memory corresponding to the video application.
[0262] During the foreground display of the video application electronic device, the user switches the horizontal screen mode to the vertical screen mode, thereby triggering the horizontal screen mode switching to the vertical screen mode event. The video application can be used as the first application program, the main process of the video application can be used as the first process, and the horizontal screen mode switching to the vertical screen mode event can be used as the first event. Therefore, before responding to the horizontal screen mode switching to the vertical screen mode event, the electronic device can suspend the recovery of the memory corresponding to the video application, and after the response to the horizontal screen mode switching to the vertical screen mode event ends, the electronic device can resume the recovery of the memory corresponding to the video application.
[0263] Based on the same inventive concept, an embodiment of the present application further provides an electronic device. The electronic device comprises: a memory and a processor, wherein the memory is used to store a computer program; and the processor is used to execute the method described in the above method embodiment when calling the computer program.
[0264] The electronic device provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.
[0265] Based on the same inventive concept, an embodiment of the present application further provides a chip system, which includes a processor coupled to a memory, and the processor executes a computer program stored in the memory to implement the method described in the above method embodiment.
[0266] The chip system may be a single chip or a chip module composed of multiple chips.
[0267] The embodiment of the present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the method described in the above method embodiment is implemented.
[0268] The embodiment of the present application also provides a computer program product. When the computer program product is run on an electronic device, the electronic device implements the method described in the above method embodiment when executing the computer program product.
[0269] If the above-mentioned integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above-mentioned method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable storage medium may at least include: any entity or device that can carry the computer program code to the camera / terminal device, a recording medium, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electrical carrier signal, a telecommunication signal, and a software distribution medium. For example, a USB flash drive, a mobile hard disk, a disk or an optical disk.
[0270] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0271] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0272] In the embodiments provided in the present application, it should be understood that the disclosed devices / equipment and methods can be implemented in other ways. For example, the device / equipment embodiments described above are merely schematic, for example, the division of the modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0273] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.
[0274] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.
[0275] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.
[0276] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0277] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0278] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned 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 application.
Claims
1. A method for reclaiming memory, characterized in that: The method comprises: If it is detected that a first event occurs in the first process, instruct the kernel layer to suspend a memory recovery task, where the memory recovery task is used to recover memory corresponding to the first process; If it is detected that the first process ends in response to the first event, the kernel layer is instructed to resume the memory reclaiming task.
2. The method according to claim 1, characterized in that The first process is a process of a first application program, or the first process is a process of a first system service.
3. The method according to claim 1 or 2, characterized in that: The method is applied to the first process or the second process, where the second process is a process of a second system service.
4. The method according to any one of claims 1 to 3, characterized in that: The instructing the kernel layer to suspend the memory recovery task includes: Sending a suspend memory recovery instruction to the kernel layer through the first interface; The instructing the kernel layer to resume the memory recovery task includes: A memory recovery and reclaiming instruction is sent to the kernel layer through the second interface.
5. The method according to any one of claims 1 to 4, characterized in that: The instructing the kernel layer to suspend the memory recovery task includes: Instructing a first virtual machine to suspend the memory reclaiming task, the first virtual machine corresponding to the first process; The instructing the kernel layer to resume the memory recovery task includes: Instruct the first virtual machine to resume the memory reclaiming task.
6. The method according to claim 2, characterized in that The first process is a process of the first application program, and the instructing the kernel layer to suspend the memory recovery task includes: If the condition for suspending the memory recovery task is met, the kernel layer is instructed to suspend the memory recovery task, and the condition for suspending the memory recovery task includes at least one of the following: The first application belongs to a preset whitelist or the first application does not belong to a preset blacklist; The first event is an event of a preset event type.
7. The method according to any one of claims 1 to 6, characterized in that: The first event is any one of starting a second application, exiting the second application, page switching, sliding within a page, display mode switching, and game animation.
8. A method for reclaiming memory, characterized in that: include: If a suspend memory recovery instruction is received, suspending the memory recovery task, wherein the suspend memory recovery instruction is an instruction triggered by a first event occurring in the first process, and the memory recovery task is used to recover memory corresponding to the first process; If a resume memory recovery instruction is received, the memory recovery task is resumed, and the resume memory recovery instruction is an instruction triggered by the first process ending in response to the first event.
9. The method according to claim 8, characterized in that The method further comprises: If the suspension duration of the memory reclaiming task is greater than or equal to the first duration, the memory reclaiming task is resumed.
10. The method according to claim 9, characterized in that The first duration corresponds to at least one of the first process and an event type of the first event.
11. The method according to any one of claims 8 to 10, characterized in that: The suspending memory recovery task includes: suspending the allocation of device resources to the memory recovery task; The resuming the memory recovery task includes: Resuming allocation of device resources to the memory reclaiming task.
12. The method according to any one of claims 8 to 10, characterized in that: The suspending memory recovery task includes: Increasing a watermark value of the first process, where the watermark value of the first process is used to trigger the memory recovery task; The resuming the memory recovery task includes: The increased waterline value of the first process is restored to the value before the increase.
13. A method for reclaiming memory, characterized in that: include: If a first event occurs in the first process, suspending a memory recovery task, wherein the memory recovery task is used to recover memory corresponding to the first process; The first process responds to the first event; If the first process ends in response to the first event, the memory reclaiming task is resumed.
14. The method according to claim 13, characterized in that The suspending memory recovery task includes: The kernel layer suspends the memory recovery task; The resuming the memory recovery task includes: The kernel layer resumes the memory recovery task.
15. The method according to claim 14, characterized in that The method further comprises: If the first event occurs in the first process, the first process instructs the kernel layer to suspend the memory recovery task; If the first process ends in response to the first event, the first process instructs the kernel layer to resume the memory reclaiming task.
16. The method according to claim 14, characterized in that The method further comprises: If the second process detects that the first event occurs in the first process, the second process instructs the kernel layer to suspend the memory recovery task; If the second process detects that the first process ends responding to the first event, the second process instructs the kernel layer to resume the memory reclaiming task.
17. The method according to any one of claims 14 to 16, characterized in that: The method further comprises: If the suspension duration of the memory reclaiming task is greater than or equal to the first duration, the memory reclaiming task is resumed.
18. An electronic device, characterized in that: include: A memory and a processor, wherein the memory is used to store a computer program; and the processor is used to execute the method according to any one of claims 1 to 7 when calling the computer program.
19. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
20. A computer program product, characterized in that When the computer program product is executed on an electronic device, the electronic device is enabled to execute the method according to any one of claims 1 to 7.
21. An electronic device, characterized in that: include: A memory and a processor, wherein the memory is used to store a computer program; and the processor is used to execute the method according to any one of claims 8 to 12 when calling the computer program.
22. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 8 to 12 is implemented.
23. A computer program product, characterized in that When the computer program product is run on an electronic device, the electronic device is enabled to execute the method according to any one of claims 8 to 12.
24. An electronic device, characterized in that: include: A memory and a processor, wherein the memory is used to store a computer program; and the processor is used to execute the method according to any one of claims 13 to 17 when calling the computer program.
25. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 13 to 17 is implemented.
26. A computer program product, characterized in that When the computer program product is executed on an electronic device, the electronic device is enabled to execute the method according to any one of claims 13 to 17.
