Memory reclaim method and electronic device
By pausing and resuming memory recycling tasks when specific events are detected, it solves the problem of lag caused by resource preemption during memory recycling, improves application fluency and memory utilization, and enhances user experience and compatibility.
Patent Information
- Application Number
- PCT/CN2024/080562
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-03-20
- Filing Date
- 2024-03-07
- Publication Date
- 2024-09-26
AI Technical Summary
Existing technologies will seize processor resources when recycling memory, causing applications to freeze or pause, affecting user experience.
By pausing and resuming memory recycling tasks when detecting specific events, it ensures that device resources are obtained first when responding to events, improving fluency, and resuming memory recycling tasks after the event is over to avoid resource shortages.
It improves the response smoothness and memory utilization of applications, reduces the impact on user experience, reduces development costs, and enhances compatibility with applications and system services.
Smart Images

Figure CN2024080562_26092024_PF_FP
Abstract
Description
Method for recycling memory and electronic device
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office on March 20, 2023, with application number 2023103046576 and application name “Method and electronic device for recycling memory”, the entire contents of which are incorporated by reference into this application. Technical Field
[0002] The present application relates to the field of terminal technology, and in particular to a method for recycling memory and an electronic device. Background Art
[0003] During the running process of an application, a large amount of memory is allocated. This memory can be used to store various objects such as variables of the process. After the process uses and discards these objects, if the memory occupied by these objects is not cleaned up in time, it will seriously consume the total memory of the electronic device, and then cause other applications that cannot allocate memory to crash and other problems.
[0004] Currently, electronic devices can reclaim an app's memory when its used memory approaches a preset threshold. However, since this memory reclaiming process hogs the electronic device's processor and other device resources, this can deplete the device resources of other threads in the app, causing the app to freeze or even pause. If the app is in the foreground, this can cause the screen on the electronic display to freeze or drop frames, severely impacting the user experience.
[0005] Summary of the Invention
[0006] In view of this, the present application provides a method for recycling memory and an electronic device to improve the smoothness of application operation and enhance user experience.
[0007] To achieve the above objectives, in a first aspect, an embodiment of the present application provides a method for reclaiming memory, the method comprising:
[0008] If a first event is detected in the first process, the kernel layer is instructed to suspend a memory reclaiming task, where the memory reclaiming task is used to reclaim the memory corresponding to the first process;
[0009] 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.
[0010] 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 for the first process, allowing the first process to respond to the first event after suspending memory recovery for the first process, thereby improving the problem of memory recovery tasks 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 for the first process in a timely manner, thereby improving the delay in the memory recovery task due to responding to the first event and thereby improving the problem of insufficient memory.
[0011] 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.
[0012] The first application may be a system application or a third-party application.
[0013] 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.
[0014] In some implementations, instructing the kernel layer to suspend the memory reclaiming task includes:
[0015] Sending a suspend memory recovery instruction to the kernel layer through the first interface;
[0016] The instructing the kernel layer to resume the memory reclaiming task includes:
[0017] Send a recovery and reclaim memory instruction to the kernel layer through the second interface.
[0018] 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 suspending 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 suspending and resuming the memory recovery task, further improves the smoothness of responding to the first event, and further improves the problem of insufficient memory.
[0019] 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.
[0020] In some implementations, instructing the kernel layer to suspend the memory reclaiming task includes:
[0021] Instructing a first virtual machine to suspend the memory reclaiming task, the first virtual machine corresponding to the first process;
[0022] The instructing the kernel layer to resume the memory reclaiming task includes:
[0023] Instruct the first virtual machine to resume the memory reclaiming task.
[0024] In some implementations, the first process is a process of the first application, and instructing the kernel layer to suspend the memory reclaiming task includes:
[0025] If the condition for suspending the memory reclaiming task is met, the kernel layer is instructed to suspend the memory reclaiming task, where the condition for suspending the memory reclaiming task includes at least one of the following:
[0026] The first application belongs to a preset whitelist or the first application does not belong to a preset blacklist;
[0027] The first event is an event of a preset event type.
[0028] The preset whitelist and the preset blacklist may include identity information of at least one application.
[0029] In some implementations, the identity information of the application program may include an application identifier.
[0030] 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.
[0031] Of course, in actual applications, it is also possible to determine whether the conditions for suspending the memory recovery task are met by other means. In some embodiments, if the first process is an application running in the foreground, it is determined that the conditions for suspending the memory recovery task are 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 conditions for suspending the memory recovery task are 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 conditions for suspending the memory recovery task are 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 conditions for suspending the memory recovery task are met.
[0032] In some embodiments, the first event is any one of launching a second application, exiting the second application, page switching, 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 landscape and portrait display, and switching between a first window and a second window. The first window and the second window may differ in at least one window attribute, such as window size and aspect ratio. In some embodiments, the first window and the second window may be full-screen windows and non-full-screen windows, respectively.
[0033] 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 exit second application animation effect 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.
[0034] 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.
[0035] In some embodiments, at least one of the following information: the identity information of the first application, the event type of the first event, a timeout period, etc., may be sent to the first virtual machine. In some embodiments, at least one of the following information: the identity information of the first application, the event type of the first event, the timeout period, etc., may be included in the suspend memory reclamation instruction.
[0036] In some embodiments, the first virtual machine may be instructed to suspend the memory reclaiming task, but the method in which the first virtual machine suspends the memory reclaiming task may not be perceived. In other embodiments, the method in which the first virtual machine suspends the memory reclaiming task may be instructed. In some embodiments, the method in which the memory reclaiming task is suspended may be instructed via a suspend memory reclaiming instruction.
[0037] In a second aspect, an embodiment of the present application provides a method for reclaiming memory, including:
[0038] If a suspend memory reclaiming instruction is received, suspending the memory reclaiming task, wherein the suspend memory reclaiming instruction is an instruction triggered by a first event occurring in the first process, and the memory reclaiming task is used to reclaim memory corresponding to the first process;
[0039] If a resume memory reclaiming instruction is received, the memory reclaiming task is resumed, where the resume memory reclaiming instruction is an instruction triggered by the first process ending in response to the first event.
[0040] 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.
[0041] 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.
[0042] In some embodiments, the method further comprises:
[0043] If the pause duration of the memory reclaiming task is greater than or equal to the first duration, the memory reclaiming task is resumed.
[0044] In some implementations, the first duration corresponds to at least one of the first process and an event type of the first event.
[0045] In some implementations, suspending the memory reclaiming task includes:
[0046] suspending allocation of device resources to the memory recovery task;
[0047] The resuming the memory recovery task includes:
[0048] Resuming allocation of device resources to the memory reclaiming task.
[0049] In some implementations, the device resource may include a time slice that occupies a processor.
[0050] In some implementations, suspending the memory reclaiming task includes:
[0051] Increasing a watermark value of the first process, where the watermark value of the first process is used to trigger the memory reclaiming task;
[0052] The resuming the memory recovery task includes:
[0053] The increased waterline value of the first process is restored to the value before the increase.
[0054] In some embodiments, 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 embodiments, the watermark value of the first process can be increased to the maximum value allowed by the watermark value setting.
[0055] In some embodiments, the memory reclaiming task may be suspended in a manner instructed by the first process or the second process to suspend the memory reclaiming task. In some embodiments, the memory reclaiming task may be suspended in a manner instructed by a suspend memory reclaiming instruction to suspend the memory reclaiming task.
[0056] In some embodiments, a 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 reclamation, the identity information of the first application, and the event type of the first event may be obtained.
[0057] In a third aspect, an embodiment of the present application provides a method for reclaiming memory, including:
[0058] If a first event occurs in the first process, suspending a memory reclaiming task, wherein the memory reclaiming task is used to reclaim memory corresponding to the first process;
[0059] The first process responds to the first event;
[0060] If the first process ends in response to the first event, the memory reclaiming task is resumed.
[0061] In some implementations, suspending the memory reclaiming task includes:
[0062] The kernel layer suspends the memory recovery task;
[0063] The resuming the memory recovery task includes:
[0064] The kernel layer resumes the memory reclaiming task.
[0065] In some embodiments, the method further comprises:
[0066] If the first event occurs in the first process, the first process instructs the kernel layer to suspend the memory reclaiming task;
[0067] If the first process ends in response to the first event, the first process instructs the kernel layer to resume the memory reclaiming task.
[0068] In some embodiments, the method further comprises:
[0069] 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 reclaiming task;
[0070] If the second process detects that the first process ends in response to the first event, the second process instructs the kernel layer to resume the memory reclaiming task.
[0071] In some embodiments, the method further comprises:
[0072] If the pause duration of the memory reclaiming task is greater than or equal to the first duration, the memory reclaiming task is resumed.
[0073] Fourthly, embodiments of the present application provide a memory recovery device that implements the aforementioned aspects and possible implementations of the aforementioned aspects. The functions can be implemented through hardware or through hardware executing corresponding software implementations. The hardware or software includes one or more modules or units corresponding to the aforementioned functions. For example, a transceiver module or unit, a processing module or unit, an acquisition module or unit, etc.
[0074] 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 above when calling the computer program.
[0075] In a sixth aspect, an embodiment of the present application 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 any one of the first aspect, any one of the second aspect, or any one of the third aspect.
[0076] The chip system may be a single chip or a chip module composed of multiple chips.
[0077] In a seventh aspect, an embodiment of the present application provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, it implements the method described in any one of the first aspect, any one of the second aspect, or any one of the third aspect.
[0078] In an eighth aspect, an embodiment of the present application provides a computer program product, which, when executed on an electronic device, enables the electronic device to execute the method described in any one of the first, second, or third aspects above.
[0079] 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
[0080] FIG1 is a schematic structural diagram of an electronic device provided in an embodiment of the present application;
[0081] FIG2 is a software structure block diagram of an electronic device provided in an embodiment of the present application;
[0082] FIG3 is a timing diagram of memory recycling provided by an embodiment of the present application;
[0083] FIG4 is a schematic diagram of a display page provided in an embodiment of the present application;
[0084] FIG5 is a schematic diagram of another display page provided in an embodiment of the present application;
[0085] FIG6 is a schematic diagram of another display page provided in an embodiment of the present application;
[0086] FIG7 is a schematic diagram of the structure of a memory recycling system provided in an embodiment of the present application;
[0087] FIG8 is a timing diagram of another memory recycling method provided in an embodiment of the present application;
[0088] FIG9 is a flowchart of a method for reclaiming memory provided in an embodiment of the present application;
[0089] FIG10 is a flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0090] FIG11 is a flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0091] FIG12 is a flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0092] FIG13 is a flowchart of another method for reclaiming memory provided in an embodiment of the present application;
[0093] FIG14 is a flowchart of another method for reclaiming memory provided in an embodiment of the present application. DETAILED DESCRIPTION
[0094] The memory recovery method provided in the embodiments of the present application can be applied to electronic devices such as mobile phones, tablet computers, wearable devices, in-vehicle devices, augmented reality (AR) / virtual reality (VR) devices, laptop computers, ultra-mobile personal computers (UMPCs), netbooks, and personal digital assistants (PDAs). The embodiments of the present application do not impose any restrictions on the specific types of electronic devices.
[0095] 1 is a schematic diagram of the structure of an electronic device 100 according to 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.
[0096] Among them, the processor 110 may include one or more processing units, and the memory 120 is used to store program code 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 recycled. If the processor 110 needs to use the instruction or data again, it can be called directly from the memory. Repeated access is reduced, the waiting time of the processor 110 is reduced, and the efficiency of the system is improved. 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 may be limited by volume, power consumption or other reasons, and the electronic device 100 may include a memory 120 with smaller storage space.
[0097] 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. For example, 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.
[0098] 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 (NFC) module, etc., and can realize the interaction between the electronic device 100 and other electronic devices in a variety of different ways.
[0099] Optionally, the electronic device 100 may further include a display screen 140 , which may display images or videos in a human-computer interaction interface.
[0100] Optionally, the electronic device 100 may further include a peripheral device 150 , such as a mouse, a keyboard, a speaker, a microphone, and the like.
[0101] It should be understood that, other than the various components or modules listed in FIG1 , the embodiments of the present application do not specifically limit the structure of the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown, or combine or separate certain components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0102] FIG2 is a block diagram of the software structure of the electronic device 100 according to an embodiment of the present application.
[0103] A layered architecture divides software into several layers, each with distinct roles and responsibilities. Layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers: the application layer, the application framework layer, the Android runtime and system libraries, and the kernel layer.
[0104] The application layer can include a series of application packages.
[0105] As shown in FIG2 , the application package may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, and short message.
[0106] The application framework layer provides an application programming interface (API) and programming framework for applications in the application layer. The application framework layer includes some predefined functions.
[0107] As shown in FIG2 , the application framework layer may include a window manager, a content provider, a view system, a telephony manager, a resource manager, a notification manager, and the like.
[0108] The window manager is used to manage window programs. The window manager can obtain the display size, determine whether there is a status bar, lock the screen, take screenshots, etc.
[0109] 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.
[0110] The view system includes visual controls, such as those for displaying text and images. The view system is used to build applications. A display interface can consist of one or more views. For example, a display interface containing a text notification icon might include a view for displaying text and a view for displaying images.
[0111] The phone manager is used to provide communication functions of the electronic device 100, such as management of call status (including answering, hanging up, etc.).
[0112] The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.
[0113] The Notification Manager allows applications to display notifications in the status bar. These messages can be displayed briefly and then disappear automatically without user interaction. For example, the Notification Manager is used to notify users of completed downloads and message reminders. The Notification Manager can also display notifications in the top status bar of the system as icons or scrolling text, such as notifications from background applications, or as dialog windows on the screen. Examples include text messages in the status bar, beeps, vibrations on electronic devices, and flashing indicator lights.
[0114] The Android runtime includes the core library and the virtual machine. The Android runtime is responsible for scheduling and management of the Android system.
[0115] The core library consists of two parts: one is the function that needs to be called by the Java language, and the other is the Android core library.
[0116] The application layer and application framework layer run in a virtual machine. The virtual machine executes Java files in the application layer and application framework layer as binary files. The virtual machine manages object lifecycles, stack management, thread management, security and exception management, and garbage collection.
[0117] In some embodiments, the virtual machine may include a Java virtual machine (JVM). The JVM is an abstract computer that can be used to parse and run application programs. The JVM can run on the operating system of the electronic device 100 without directly interacting with the hardware devices of the electronic device 100. Therefore, the JVM can shield the underlying hardware environment of the electronic device 100, allowing applications to run on platforms with different characteristics. In some embodiments, one JVM can correspond to one application.
[0118] The system library can 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.
[0119] The surface manager is used to manage the display subsystem and provide fusion of 2D and 3D layers for multiple applications.
[0120] The media library supports playback and recording of a variety of common audio and video formats, as well as static image files. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
[0121] The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, compositing, and layer processing.
[0122] A 2D graphics engine is a drawing engine for 2D drawings.
[0123] The kernel layer is the layer between hardware and software. The kernel layer includes at least display drivers, camera drivers, audio drivers, and sensor drivers. In some implementations, a virtual machine can run in the kernel layer.
[0124] The following describes the workflow of the software and hardware of the electronic device 100 in conjunction with starting the camera program.
[0125] 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 a raw input event (including touch coordinates, timestamp of the touch operation and other information). The raw input event is stored in the kernel layer. The application framework layer obtains the raw 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 as 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 the various hardware devices such as the camera included in the electronic device 100.
[0126] 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.
[0127] During the running process of an application, a large amount of memory is allocated. This memory is used to store various objects, such as process variables. If these objects are not promptly cleared after the application is finished using them, the memory occupied by these objects will seriously consume the total memory of the electronic device, causing other applications that cannot allocate memory to crash and other problems. The JVM can start a garbage collection (GC) thread. The GC thread reclaims the memory occupied by the application's garbage.
[0128] 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 reclaim the application's memory.
[0129] When the GC thread is reclaiming memory, it will seize the electronic device's CPU and other device resources. 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 in the foreground, it will cause the electronic display to freeze or drop frames, seriously affecting the user experience.
[0130] Please refer to Figure 3, which is a timing diagram of memory reclaiming provided by an embodiment of the present application. Any time period during the application's execution, such as the three time periods t1-t2, t4-t5, and t6-t8 shown in Figure 3, may trigger the GC thread to reclaim memory, and the application is unaware of the timing of the GC thread's memory reclaiming.
[0131] The application displays a video invitation page as shown in Figure 4. The video invitation page includes a prompt message "Inviting you to a video call", a reject button, an answer button, and a voice call button.
[0132] At time t3, the user clicks the answer button. If no GC thread preempts resources, the application's user interface (UI) thread can display an animated transition effect from the video invitation page shown in Figure 4 to the video call page shown in Figure 5. The video call page includes images of both parties in the video call, a hang-up button, a voice chat button, and buttons to switch between the front and rear cameras.
[0133] However, because the time period t4-t5 falls within the time period t3-t7, and the time period t6-t8 partially falls within the time period t3-t7, the GC thread will preempt the electronic device's device resources, and the UI thread may not be able to obtain sufficient device resources to display the animation switching effect. Therefore, after time t3, the video invitation page shown in Figure 4 may remain, or a stuck page as shown in Figure 6 may be displayed. Figure 6 includes part of the video invitation page shown in Figure 4 and part of the video call page shown in Figure 5. If the user sees Figure 6, they cannot tell whether the video call is connected.
[0134] To address at least some of the above technical issues, the present application provides a method for memory recovery. The technical solution of the present application is described in detail below using specific embodiments. The following specific embodiments may be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.
[0135] Please refer to Figure 7, which is a schematic diagram of the structure of a memory recycling system provided in an embodiment of the present application. The system can be run on the electronic device 100 shown in Figure 1 above. The system includes application / system services, virtual machines, and control services.
[0136] 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 reclamation. 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 policies can be configuration policies for system applications and third-party applications. In some embodiments, the configuration policies can be configuration policies for third-party applications.
[0137] In some embodiments, the configuration policy may include a master switch. When the master switch is on, applications, system services, and virtual machines may reclaim memory in accordance with the memory reclaiming method provided in the embodiments of the present application. When the master switch is off, applications, system services, and virtual machines may not reclaim memory in accordance with the memory reclaiming method provided in the embodiments of the present application.
[0138] 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 level 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 or exiting an application, page switching transitions, sliding within a page, display mode switching, and game animations. In some embodiments, display mode switching may include at least one of switching between horizontal 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 and aspect ratio, may be different. In some embodiments, the first window and the second window may be full-screen windows and non-full-screen windows, respectively.
[0139] In some embodiments, the configuration policy 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 events 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 understandable that, in actual 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.
[0140] In some embodiments, the configuration policy may include a preset blacklist or a preset whitelist. The preset blacklist and the preset whitelist may include the identity information of at least one application. In some embodiments, if the identity information of a certain 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 a certain 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.
[0141] 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 embodiment of this application does not limit the type of identity information of the application.
[0142] It is understood 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 can be set for different applications. In some embodiments, different applications can correspond to different timeout periods, that is, different timeout periods can be set for different applications. In some embodiments, different event types can correspond to different timeout periods, that is, different timeout periods can be set for different event types.
[0143] For example, the timeout for events such as page transitions, page scrolling, and display mode switching can be 500ms. The timeout for events such as launching or exiting an application can be 1000ms.
[0144] For another example, the timeout period corresponding to the start and exit events of application A may be 1000ms, and the timeout period corresponding to the start and exit events of application B may be 5000ms.
[0145] In some implementations, the control service may read a related configuration file and perform initialization, thereby obtaining the configuration policy included in the configuration file.
[0146] In some embodiments, the control service may provide a query interface, which may be used to query any parameter or configuration included in the configuration policy.
[0147] In some implementations, the control service may control configuration policies, such as controlling a master switch to be on or off, adding or deleting event types, adjusting timeout durations, updating blacklists / whitelists, and the like.
[0148] 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 preempted by the memory recycling process to occupy device resources, to ensure that the input event can be responded to smoothly and to improve the user experience, the application may send a pause GC instruction to the virtual machine before responding to the event. The pause GC instruction may be used to instruct the virtual machine to suspend the memory recycling task. In order to ensure that the virtual machine can continue to reclaim memory and improve memory utilization, the application may send a resume GC instruction to the virtual machine after responding to the event. The resume GC instruction may be used to instruct the virtual machine to resume the memory recycling task.
[0149] In some implementations, when an event occurs in an application, the application can determine, based on the aforementioned configuration policy, whether it is necessary to suspend memory reclamation tasks before responding to the event. If so, a pause 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 will be appreciated that in actual applications, the application may also determine whether it is necessary to suspend memory reclamation tasks before responding to the event through other means.
[0150] 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.
[0151] 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.
[0152] The kernel layer, specifically a virtual machine corresponding to an application or system service in the kernel layer, can be used to execute a memory reclaiming task, suspend the memory reclaiming task based on a suspend GC instruction, and resume the memory reclaiming task based on a resume GC instruction.
[0153] 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 via the suspend GC interface and a resume GC instruction via the resume GC interface. In some embodiments, the suspend GC interface and the resume GC interface may be combined into one interface.
[0154] The following will describe in detail the operation of the memory recycling system shown in FIG7 with reference to specific examples.
[0155] 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 to obtain the configuration policy.
[0156] For example, while application A is running in the foreground of electronic device 100, a user swipes within the page displayed by application A. Since swiping within a page is an event type included in the configuration policy, and application A's package name is com.AA, which is included in the whitelist in the configuration policy, it is determined that GC peak shifting is required. Application A sends a suspend GC instruction to virtual machine A corresponding to application A through the suspend GC interface.
[0157] When virtual machine A receives the suspend GC instruction, it suspends the memory reclaim task. After application A sends the suspend GC instruction, there is no memory reclaim task operation to preempt the device resources of electronic device 100. Therefore, application A can apply for sufficient device resources to respond to the page sliding event, thereby smoothly displaying the page sliding effect to the user.
[0158] After responding to the sliding event within the page, application A can send a resume GC instruction to virtual machine A through the resume GC interface, and virtual machine A resumes executing the memory recovery task to improve the insufficient memory problem.
[0159] Please refer to FIG. 8 , which is another timing diagram of memory recycling provided in an embodiment of the present application.
[0160] The application displays a video invitation page as shown in Figure 4. The video invitation page includes a prompt message "Inviting you to a video call", a reject button, an answer button, and a voice call button.
[0161] 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 after responding to the answer button click event, it instructs the virtual machine to resume the memory recovery task through the resume GC interface. Therefore, during the t3-t4 time period, the virtual machine will not execute the GC thread, and the UI thread can apply for sufficient device resources to smoothly display the animation switching effect from the video invitation page shown in Figure 4 to the video call page shown in Figure 5. And because the application has already instructed the virtual machine to resume the memory recovery task at time t4, the GC thread can reclaim memory again in the t5-t6 time period after time t4, thereby improving the insufficient memory problem.
[0162] 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. The first application may be a system application or a third-party application. In some embodiments, the first process may be the main process of the first application. In some embodiments, the first process may be replaced by the first application or the first system service.
[0163] 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.
[0164] In some implementations, the first virtual machine in the following embodiments may be replaced by a kernel layer.
[0165] Please refer to Figure 9, which is a flowchart of a method for reclaiming memory tasks provided in an embodiment of the present application. It should be noted that the method is not limited to the specific order shown in Figure 9 and described below. 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:
[0166] S901: A first event occurs in a first process.
[0167] 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 multiple windows, and the images in the multiple windows are combined and displayed on the display screen of the electronic device to form the page viewed by the user. The user can operate any window (i.e., user input) by touching the screen or pressing the keyboard, and the window manager in the electronic device can obtain the first event of the user input from hardware devices such as the display screen and keyboard, and then distribute the first event to the first process.
[0168] 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.
[0169] It should be noted that the embodiment of the present application is not limited to the situation where the first event occurs in the first process.
[0170] S902: The first process determines whether to instruct the first virtual machine to suspend the memory reclaiming task. If so, execute S903; otherwise, execute S904.
[0171] The memory reclaiming task may be used to reclaim the memory of the first process.
[0172] In some implementations, the first process may correspond to a first virtual machine, and the first process may run in the first virtual machine.
[0173] In some embodiments, when detecting the first event, 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.
[0174] In some embodiments, the first process is a process of a first application, and a configuration policy may be used to determine whether a condition for suspending the memory reclaim task is met. 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 reclaim task includes at least one of the following: Condition 1: the first application is on the preset whitelist or the first application is not on the preset blacklist; Condition 2: the first event is an event of a preset event type.
[0175] Of course, in actual applications, it is also possible to determine whether the conditions for suspending the memory recovery task are met by other means. In some embodiments, if the first process is an application running in the foreground, it is determined that the conditions for suspending the memory recovery task are 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 conditions for suspending the memory recovery task are 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 conditions for suspending the memory recovery task are 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 conditions for suspending the memory recovery task are met.
[0176] 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 the 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 size and determination method 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.
[0177] 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 without executing S902 to determine whether to instruct the first virtual machine to suspend the memory recovery task, so S902 can be omitted.
[0178] S903: The first process instructs the first virtual machine to suspend the memory reclaiming task.
[0179] In some embodiments, the first process may send a pause memory reclamation instruction to the first virtual machine. The pause reclamation instruction may be used to instruct the first virtual machine to suspend memory reclamation tasks. By sending the pause reclamation instruction to the first virtual machine, the first process instructs the first virtual machine to suspend memory reclamation corresponding to the first process, thereby conserving device resources required for memory reclamation and prioritizing response to the first event, thereby improving the smoothness of responding to the first event.
[0180] In some embodiments, the first process may send a pause memory reclamation instruction to the first virtual machine via the first interface. In some embodiments, the pause memory reclamation instruction may include a pause garbage collection instruction, and the first interface may include a pause garbage collection interface. Of course, it is understood that in actual applications, the pause memory reclamation instruction may also include other instructions that can instruct to pause the memory reclamation task.
[0181] In some embodiments, the first process may send at least one of the following information to the first virtual machine: identity information of the first application, the event type of the first event, a timeout period, etc. In some embodiments, at least one of the following information may be included in the suspend memory reclamation instruction.
[0182] In some embodiments, the first process may only instruct the first virtual machine to suspend the memory reclaiming task, but may not be aware of the manner in which the first virtual machine suspends the memory reclaiming task. In other embodiments, the first process may also instruct the first virtual machine to suspend the memory reclaiming task. In some embodiments, the first process may instruct the first virtual machine to suspend the memory reclaiming task using a suspend memory reclaiming instruction.
[0183] S904: The first process responds to the first event.
[0184] Since the memory reclaiming 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.
[0185] In some implementations, the first event may be responded to based on the event type of the first event.
[0186] 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 exit second application animation effect 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.
[0187] 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 embodiments, the second application and the first application may be the same application.
[0188] 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.
[0189] S905: After finishing responding to the first event, the first process instructs the first virtual machine to resume the memory reclaiming task.
[0190] In some embodiments, the first process may send a resume memory reclaiming instruction to the first virtual machine. The resume memory reclaiming instruction may instruct the first virtual machine to resume the memory reclaiming task. After completing the response to the first event, the first process may instruct the first virtual machine to resume the memory reclaiming task to enable the first virtual machine to continue reclaiming memory and improve memory utilization.
[0191] In some embodiments, the first process may send a resume and reclaim instruction to the first virtual machine via the second interface. In some embodiments, the resume and reclaim memory instruction may include a resume GC instruction, and the second interface may include a resume GC interface. Of course, it is understood that in actual applications, the resume and reclaim memory instruction may also include other instructions that can instruct the resumption and reclaim memory task.
[0192] In some embodiments, after executing S904 to start responding to the first event, the first process may detect whether the response to the first event is completed. If so, the first virtual machine is instructed to resume the memory reclaiming task. Otherwise, the first event is detected again to determine whether the response to the first event is completed.
[0193] 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 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 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 memory recovery tasks 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 memory recovery tasks, further improving the fluency of responding to the first event, and further improving the problem of insufficient memory.
[0194] Please refer to Figure 10, which is a flowchart of a method for reclaiming memory provided in an embodiment of the present application. It should be noted that the method is not limited to the specific sequence shown in Figure 10 and below. 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:
[0195] S1001: If the second process detects that a first event has occurred in the first process, the second process determines whether to instruct the first virtual machine to suspend the memory recovery task. If so, the process proceeds to S1002; otherwise, the process ends.
[0196] 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.
[0197] It should be noted that the way 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 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.
[0198] S1002: The second process instructs the first virtual machine to suspend the memory reclaiming task.
[0199] In some embodiments, the second process may send a pause memory reclamation instruction to the first virtual machine via the first interface. In some embodiments, the pause memory reclamation 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 reclamation instruction may also include other instructions that can instruct to pause the memory reclamation task.
[0200] It should be noted that the way in which the second process instructs the first virtual machine to suspend the memory reclaiming task may 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 .
[0201] 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.
[0202] In some embodiments, the second process may send a resume and reclaim instruction to the first virtual machine via the second interface. In some embodiments, the resume and reclaim memory instruction may include a resume GC instruction, and the second interface may include a resume GC interface. Of course, it is understood that in actual applications, the resume and reclaim memory instruction may also include other instructions that can instruct the resumption and reclaim memory task.
[0203] In some embodiments, after the first process starts responding to the first event, the second process can detect whether the response to the first event is completed. If so, it can instruct the first virtual machine to resume the memory recovery task. Otherwise, it can detect whether the response to the first event is completed again.
[0204] It should be noted that the manner in which the second process instructs the first virtual machine to resume the memory reclaiming task may 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 .
[0205] 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 smoothness 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 is completed, 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 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, 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.
[0206] Please refer to Figure 11, which is a flowchart of a method for reclaiming memory provided in an embodiment of the present application. It should be noted that the method is not limited to the specific sequence shown in Figure 11 and described below. 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:
[0207] S1101, triggering a memory recovery task.
[0208] 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.
[0209] It should be noted that the embodiment of the present application does not limit the size of the first numerical value.
[0210] It should also be noted that the memory recycling task can also be triggered in other ways.
[0211] S1102: The first virtual machine determines whether the memory reclaiming task is in a suspended state, and if so, executes S1103; otherwise, executes S1105.
[0212] In some implementations, the first virtual machine may suspend the memory reclaiming task upon receiving the suspend memory reclaiming instruction.
[0213] In some embodiments, pausing the memory reclaiming task may include stopping allocating device resources to the memory reclaiming task, such as occupying a processor time slice. In some embodiments, the memory reclaiming task may be executed by a garbage collection (GC) thread, and the GC thread may be paused by calling a sleep function. The first virtual machine may determine that the memory reclaiming task is in a paused state when the paused memory reclaiming task does not occupy device resources such as a processor time slice, and otherwise determine that the memory reclaiming task is not in a paused state.
[0214] It is understandable that, in actual applications, the GC thread may be paused in other ways, or allocation of device resources to the suspended task may be stopped in other ways.
[0215] In some embodiments, pausing the memory reclaim task may include increasing a 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 reduced to less than the increased watermark value for a period of time or permanently, thereby preventing the memory reclaim task from continuing to be executed. The first virtual machine may determine whether the memory reclaim task is in a suspended state when the current watermark value is greater than or equal to a second value.
[0216] In some embodiments, 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). The first virtual machine can 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.
[0217] 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.
[0218] In some embodiments, the first virtual machine may suspend the memory reclaiming task in a manner instructed by the first process or the second process to suspend the memory reclaiming task. In some embodiments, the first virtual machine may suspend the memory reclaiming task in a manner instructed by the suspend memory reclaiming instruction to suspend the memory reclaiming task.
[0219] S1103: The first virtual machine determines whether the pause duration is greater than or equal to the first duration, and if so, executes S1104; otherwise, the process ends.
[0220] If the memory reclaim task is already suspended, the first virtual machine can obtain the duration of the memory reclaim task suspension, which can be the duration from the start of the suspension of the memory reclaim task to the current state. To mitigate issues such as insufficient memory caused by suspending the memory reclaim task for too long, the first virtual machine can compare the suspension duration with the first duration to determine whether the suspension duration has expired.
[0221] In some embodiments, the first duration can be a global duration, that is, the length 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 relevant technicians can select any timeout duration from the multiple timeout durations as the first duration. The first virtual machine can obtain the first duration indicated by the configuration policy.
[0222] In some embodiments, the first duration may correspond to at least one of a first application and a 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 at least one application's identity information and / or at least one event type, and the corresponding timeout duration may be obtained from the configuration policy by the first virtual machine based on at least one of the first application's identity information and the event type of the first event.
[0223] In some embodiments, the first virtual machine can obtain a first duration indicated by the first process or the second process. In some embodiments, the first virtual machine can 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 can obtain at least one of the first duration, the identity information of the first application, and the event type of the first event carried in the instruction to suspend memory reclamation.
[0224] S1104: The first virtual machine resumes the memory reclaiming task.
[0225] If the memory reclaiming 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 reclaiming task has been responded to and completed, or even if the first event has not been responded to and completed, failure to execute the memory reclaiming task may result in severe memory shortage. Therefore, the first virtual machine can resume the memory reclaiming task to improve the memory shortage problem.
[0226] 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 was increased. It will be understood that based on the different ways of suspending the memory recovery task, the ways of resuming the memory recovery task may also be different.
[0227] In some implementations, the first virtual machine may resume the memory reclaiming task upon receiving the resume memory reclaiming instruction.
[0228] S1105: The first virtual machine executes a memory reclaiming task.
[0229] 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. Therefore, the first virtual machine can execute the memory reclaiming task to improve the insufficient memory problem.
[0230] In some implementations, the first virtual machine may run a GC thread to perform memory reclaiming tasks through the GC thread.
[0231] It is understandable that, in actual applications, the first virtual machine may also perform the memory reclaiming task in other ways.
[0232] In an embodiment of the present application, when a task to reclaim 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, then 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, then 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.
[0233] Please refer to Figure 12, which is a flowchart of a method for reclaiming memory provided in an embodiment of the present application. It should be noted that the method is not limited to the specific sequence shown in Figure 12 and below. 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:
[0234] S1201: If a first event occurs in the first process, suspend the memory recovery task.
[0235] The manner in which the first event occurs in the first process may refer to the related description in S901 mentioned above.
[0236] In some implementations, the first virtual machine may suspend the memory reclaiming task.
[0237] In some implementations, the first virtual machine may suspend the memory reclaiming task upon receiving the suspend memory reclaiming instruction.
[0238] In some implementations, the first process may execute operations identical or similar to the aforementioned S901 - S903 , and if the first event occurs, send a suspend memory reclaiming instruction to the first virtual machine.
[0239] In some embodiments, the second process may perform operations identical or similar to the aforementioned S1001 - S1002 , and if it is detected that the first event occurs in the first process, a suspend memory reclaiming instruction is sent to the first virtual machine.
[0240] In some embodiments, the second process may be integrated into 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.
[0241] S1202: The first process responds to the first event.
[0242] The manner in which the first process responds to the first event may refer to the related description in the aforementioned S904.
[0243] S1203: If the first process ends in response to the first event, the memory recovery task is resumed.
[0244] In some implementations, the first virtual machine may resume the memory reclaiming task upon receiving the resume memory reclaiming instruction.
[0245] 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 memory recovery instruction is sent to the first virtual machine.
[0246] In some embodiments, the second process may perform the same or similar operations as the aforementioned S1003, and send a recovery memory reclaiming instruction to the first virtual machine if it is detected that the first process ends in response to the first event.
[0247] In some embodiments, the second process may be integrated into 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.
[0248] In an embodiment of the present application, if a first event occurs in a first process, memory recovery of the first process can be suspended. The first process responds to the first event after suspending memory recovery of the first process, thereby improving the problem of memory recovery tasks occupying device resources and improving the smoothness of responding to the first event. After the first process finishes responding to the first event, memory recovery of the first process can be resumed in a timely manner, thereby reducing the delay in the memory recovery task due to responding to the first event and further improving the problem of insufficient memory.
[0249] Please refer to Figure 13, which is a flowchart 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 limited to the specific order described in Figure 13 and below. 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:
[0250] S1301: If a first event is detected in a first process, the kernel layer is instructed to suspend a memory reclaiming task, where the memory reclaiming task is used to reclaim memory corresponding to the first process.
[0251] 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 reclaiming task.
[0252] 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 second process, and will not be repeated here.
[0253] 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 for the first process, allowing the first process to respond to the first event after suspending memory recovery for the first process, thereby improving the problem of memory recovery tasks 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 for the first process in a timely manner, thereby improving the delay in the memory recovery task due to responding to the first event and thereby improving the problem of insufficient memory.
[0254] Please refer to Figure 14, which is a flowchart 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 is not limited to Figure 14 and the specific order described below. 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:
[0255] S1401: If a suspend memory recovery instruction is received, the memory recovery task is suspended. The suspend memory recovery instruction is an instruction triggered by a first event occurring in a first process. The memory recovery task is used to reclaim memory corresponding to the first process.
[0256] S1402: If a resume memory reclaiming instruction is received, the memory reclaiming task is resumed. The resume memory reclaiming instruction is an instruction triggered by the first process ending in response to the first event.
[0257] 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.
[0258] In an 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 a timely manner, thereby improving the delay time of the memory recovery task due to responding to the first event and thus improving the problem of insufficient memory.
[0259] The following will illustrate the method for reclaiming memory described in the embodiments of the present application with reference to specific examples.
[0260] 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.
[0261] The electronic device runs the desktop application and displays a desktop page on the display screen, where the desktop page includes an icon of the video application.
[0262] When a user clicks on the video application icon, a video application launch event is triggered, and the desktop application can launch the main process of the video application through other related system services. The desktop application can be used as the first application or the first system service, the desktop application process can be used as the first process, and the video application launch event can be used as the first event. Therefore, before responding to the video application launch event, the electronic device can suspend recycling the memory corresponding to the desktop application, and after completing the response to the video application launch event, the electronic device can resume recycling the memory corresponding to the desktop application.
[0263] When the desktop application launches the main process of the video application through other related system services, it can trigger a page display event for the video application, which can display the main page of the video application. The video application can be the first application, the main process of the video application can be the first process, and the page display event can be the first event. Therefore, before responding to the page display event, the electronic device can suspend recycling the memory corresponding to the video application, and resume recycling the memory corresponding to the video application after completing the response to the page display event.
[0264] During the foreground display of a video application on an electronic device, a user switches the landscape mode to the portrait mode, thereby triggering a landscape mode to portrait mode switching event. The video application can be a first application, the main process of the video application can be a first process, and the landscape mode to portrait mode switching event can be a first event. Therefore, before responding to the landscape mode to portrait mode switching event, the electronic device can suspend recycling of the memory corresponding to the video application, and after completing the response to the landscape mode to portrait mode switching event, the electronic device can resume recycling of the memory corresponding to the video application.
[0265] Based on the same inventive concept, an embodiment of the present application further provides an electronic device comprising: 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 the computer program is called.
[0266] The electronic device provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effects are similar, which will not be repeated here.
[0267] 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 executes a computer program stored in the memory to implement the method described in the above method embodiment.
[0268] The chip system may be a single chip or a chip module composed of multiple chips.
[0269] An embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon, which implements the method described in the above method embodiment when the computer program is executed by a processor.
[0270] An embodiment of the present application further 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.
[0271] 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 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 a processor, it can implement the steps of each of the above-mentioned method embodiments. The computer program includes computer program code, which 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 capable of carrying computer program code to the camera / terminal device, recording medium, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal and software distribution medium. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk.
[0272] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0273] Those skilled 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 beyond the scope of this application.
[0274] In the embodiments provided in this 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 merely a logical function division. In actual implementation, there may be other division methods, 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.
[0275] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, integers, steps, operations, elements and / or components, but does not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or collections thereof.
[0276] It will also be understood that the term "and / or" used in this specification and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
[0277] As used in this specification and the appended claims, the term "if" can be interpreted as "when" or "upon" 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 "upon determination" or "in response to determining" or "upon detection of [described condition or event]" or "in response to detecting [described condition or event]," depending on the context.
[0278] 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.
[0279] References to "one embodiment" or "some embodiments" in this specification mean that a particular feature, structure, or characteristic described in conjunction with that embodiment is included in one or more embodiments of the present application. Thus, phrases such as "in one embodiment," "in some embodiments," "in other embodiments," and "in other embodiments" appearing in various places in this specification do not necessarily refer to the same embodiment, but rather mean "one or more but not all embodiments," unless otherwise specifically emphasized. The terms "including," "comprising," "having," and variations thereof all mean "including but not limited to," unless otherwise specifically emphasized.
[0280] 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 them. 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 make equivalent replacements for some or all of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for reclaiming memory, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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, It is 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.
Citation Information
Patent Citations
Junk memory recycling method and device, electronic equipment and storage medium
CN111258921A
Electronic terminal and application control method
CN112764891A
Memory recovery method, electronic equipment and computer readable storage medium
CN115292052A
Memory management method and system for android system, and storage medium
US20230037219A1
