CN115525404B - Methods, equipment and storage media to release memory - Google Patents
Methods, equipment and storage media to release memory Download PDFInfo
- Publication number
- CN115525404B CN115525404B CN202210161930.XA CN202210161930A CN115525404B CN 115525404 B CN115525404 B CN 115525404B CN 202210161930 A CN202210161930 A CN 202210161930A CN 115525404 B CN115525404 B CN 115525404B
- Authority
- CN
- China
- Prior art keywords
- application
- memory
- frozen
- foreground
- background
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000000034 method Methods 0.000 title claims abstract description 345
- 230000008569 process Effects 0.000 claims abstract description 286
- 230000008014 freezing Effects 0.000 claims abstract description 34
- 238000007710 freezing Methods 0.000 claims abstract description 34
- 238000011084 recovery Methods 0.000 claims abstract description 5
- 230000006870 function Effects 0.000 claims description 120
- 238000004590 computer program Methods 0.000 claims description 11
- 238000007726 management method Methods 0.000 description 45
- 238000004064 recycling Methods 0.000 description 40
- 238000010586 diagram Methods 0.000 description 13
- 230000005236 sound signal Effects 0.000 description 9
- 230000009286 beneficial effect Effects 0.000 description 8
- 230000006835 compression Effects 0.000 description 6
- 238000007906 compression Methods 0.000 description 6
- 229920001621 AMOLED Polymers 0.000 description 4
- 238000005516 engineering process Methods 0.000 description 4
- 238000005192 partition Methods 0.000 description 3
- 230000004044 response Effects 0.000 description 3
- 238000004891 communication Methods 0.000 description 2
- 238000013500 data storage Methods 0.000 description 2
- 230000014509 gene expression Effects 0.000 description 2
- 230000007246 mechanism Effects 0.000 description 2
- 238000010926 purge Methods 0.000 description 2
- 230000000717 retained effect Effects 0.000 description 2
- 230000001960 triggered effect Effects 0.000 description 2
- 230000001133 acceleration Effects 0.000 description 1
- 230000002411 adverse Effects 0.000 description 1
- 210000000988 bone and bone Anatomy 0.000 description 1
- 238000004364 calculation method Methods 0.000 description 1
- 230000001413 cellular effect Effects 0.000 description 1
- 239000013256 coordination polymer Substances 0.000 description 1
- 230000003993 interaction Effects 0.000 description 1
- 230000002452 interceptive effect Effects 0.000 description 1
- 239000004973 liquid crystal related substance Substances 0.000 description 1
- 238000010295 mobile communication Methods 0.000 description 1
- 229920001690 polydopamine Polymers 0.000 description 1
- 239000002096 quantum dot Substances 0.000 description 1
- 230000009467 reduction Effects 0.000 description 1
- 238000009877 rendering Methods 0.000 description 1
- 230000000007 visual effect Effects 0.000 description 1
- 239000002699 waste material Substances 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/48—Program initiating; Program switching, e.g. by interrupt
- G06F9/4806—Task transfer initiation or dispatching
- G06F9/4843—Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
- G06F9/4881—Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5016—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Power Sources (AREA)
- Telephone Function (AREA)
Abstract
Description
技术领域Technical field
本申请涉及内存管理技术领域,尤其涉及一种释放内存的方法、设备和存储介质。The present application relates to the technical field of memory management, and in particular to a method, device and storage medium for releasing memory.
背景技术Background technique
电子设备中,应用的每个进程都包含一个内存分配器,进程通过内存分配器从操作系统(Operating System,OS)获取运行所需的内存(又称随机存取存储器,RandomAccess Memory,RAM)。当应用切换到后台运行时,对应的进程通过内存分配器将内存释放给OS。释放内存时,有些内存分配器仅释放一部分获取到的内存,剩余的另一部分内存仍由内存分配器持有。In electronic devices, each process of an application contains a memory allocator. The process obtains the memory (also known as Random Access Memory, RAM) required for operation from the operating system (Operating System, OS) through the memory allocator. When the application switches to the background, the corresponding process releases the memory to the OS through the memory allocator. When releasing memory, some memory allocators only release part of the acquired memory, and the remaining part of the memory is still held by the memory allocator.
上述情况导致后台应用的进程占用电子设备过多的内存,进而对其他进程的运行造成不良影响。The above situation causes the processes of background applications to occupy too much memory of the electronic device, thereby adversely affecting the operation of other processes.
发明内容Contents of the invention
本申请提供了一种释放内存的方法、设备和存储介质,以避免后台应用的进程占用过多内存。This application provides a method, device, and storage medium to release memory to prevent background application processes from occupying too much memory.
为了实现上述目的,本申请提供了以下技术方案:In order to achieve the above objectives, this application provides the following technical solutions:
本申请第一方面提供一种释放内存的方法,包括:The first aspect of this application provides a method for releasing memory, including:
应用放后台时,将所述应用对应的进程确定为待冻结进程;When the application is placed in the background, the process corresponding to the application is determined as the process to be frozen;
通过强制回收的方式释放所述待冻结进程占用的内存;Release the memory occupied by the process to be frozen through forced recycling;
冻结所述待冻结进程。Freeze the process to be frozen.
本实施例的有益效果在于:The beneficial effects of this embodiment are:
在应用被放后台后,主动触发放后台的应用对应的待冻结进程按强制回收的方式释放内存,可以使待冻结进程所占用的内存尽可能多地被释放给操作系统,缓解了后台应用的进程占用过多内存资源的问题,提高电子设备前台的应用运行的流畅度。After the application is placed in the background, the process to be frozen corresponding to the application placed in the background is actively triggered to release memory by forced recycling, which can release as much memory occupied by the process to be frozen as possible to the operating system, easing the problem of background applications. It solves the problem of processes occupying too much memory resources and improves the smoothness of application running in the foreground of electronic devices.
在一些可选的实施例中,所述应用放后台时,将所述应用对应的进程确定为待冻结进程之前,还包括:In some optional embodiments, when the application is placed in the background, before determining the process corresponding to the application as a process to be frozen, the method further includes:
响应用户操作,将当前在前台运行的应用放后台。In response to user operations, put the application currently running in the foreground into the background.
上述用户操作可以是返回桌面,打开其他应用等操作。The above user operations may be operations such as returning to the desktop, opening other applications, etc.
示例性的,当视频应用在前台运行时,电子设备响应用户对显示屏顶部弹出的聊天消息的点击操作,打开聊天应用,同时将当前在前台运行的视频应用放后台For example, when the video application is running in the foreground, the electronic device responds to the user's click operation on the chat message that pops up at the top of the display screen, opens the chat application, and at the same time puts the video application currently running in the foreground into the background.
在一些可选的实施例中,所述应用放后台时,将所述应用对应的进程确定为待冻结进程之前,还包括:In some optional embodiments, when the application is placed in the background, before determining the process corresponding to the application as a process to be frozen, the method further includes:
在所述电子设备熄屏时,将当前在前台运行的应用放后台。When the screen of the electronic device is turned off, the application currently running in the foreground is put into the background.
本实施例的有益效果在于:The beneficial effects of this embodiment are:
在熄屏后将前台运行的应用切换到后台,可以降低电子设备的耗电量。Switching applications running in the foreground to the background after turning off the screen can reduce the power consumption of electronic devices.
在一些可选的实施例中,所述应用放后台时,将所述应用对应的进程确定为待冻结进程之前,还包括:In some optional embodiments, when the application is placed in the background, before determining the process corresponding to the application as a process to be frozen, the method further includes:
判断所述应用是否有前台可感知功能;Determine whether the application has foreground-aware functions;
若所述应用没有前台可感知功能,执行所述将所述应用对应的进程确定为待冻结进程步骤。If the application does not have foreground-aware functions, perform the step of determining the process corresponding to the application as a process to be frozen.
本实施例的有益效果在于:The beneficial effects of this embodiment are:
避免将有前台可感知功能的应用冻结,使电子设备能够在运行前台应用的同时提供后台运行的应用的部分功能。Avoid freezing applications with foreground-aware functions so that electronic devices can run foreground applications while providing some functions of applications running in the background.
示例性的,导航应用播报导航信息的功能就属于前台可感知功能,通过本实施例的方法,可以避免将后台的导航应用冻结,使电子设备可以一边运行其他应用一边播报导航信息。For example, the navigation application's function of broadcasting navigation information is a front-end perceptible function. Through the method of this embodiment, freezing of the background navigation application can be avoided, so that the electronic device can broadcast navigation information while running other applications.
在一些可选的实施例中,所述判断所述应用是否有前台可感知功能,包括:In some optional embodiments, determining whether the application has foreground-aware functions includes:
根据前台可感知应用列表判断所述应用是否有前台可感知功能,所述前台可感知应用列表用于记录具有前台可感知功能的应用。It is determined whether the application has foreground-aware functions according to a foreground-aware application list, where the foreground-aware application list is used to record applications with foreground-aware functions.
在一些可选的实施例中,所述电子设备的操作系统包括省电应用和内存管理服务;所述待冻结进程包括函数库和内存分配器;In some optional embodiments, the operating system of the electronic device includes a power-saving application and a memory management service; the process to be frozen includes a function library and a memory allocator;
所述通过强制回收的方式释放所述待冻结进程占用的内存,包括:The method of releasing the memory occupied by the process to be frozen through forced recycling includes:
所述省电应用向所述内存管理服务发送预冻结通知,所述预冻结通知携带所述待冻结进程的进程标识;The power-saving application sends a pre-freeze notification to the memory management service, where the pre-freeze notification carries the process identifier of the process to be frozen;
所述内存管理服务根据所述待冻结进程的进程标识,向所述待冻结进程的函数库发送内存释放信号;The memory management service sends a memory release signal to the function library of the process to be frozen according to the process identifier of the process to be frozen;
所述函数库响应所述内存释放信号,调用所述内存分配器的强制释放接口;The function library responds to the memory release signal and calls the forced release interface of the memory allocator;
所述内存分配器按强制回收方式释放所述待冻结进程占用的内存。The memory allocator releases the memory occupied by the process to be frozen in a forced recycling manner.
在一些可选的实施例中,所述电子设备的操作系统还包括冻结器;In some optional embodiments, the operating system of the electronic device further includes a freezer;
所述冻结待冻结进程,包括:The processes to be frozen include:
所述省电应用向所述冻结器下发进程冻结通知,所述进程冻结通知携带所述待冻结进程的进程标识;The power-saving application sends a process freezing notification to the freezer, and the process freezing notification carries the process identifier of the process to be frozen;
所述冻结器根据所述待冻结进程的进程标识,冻结所述待冻结进程。The freezer freezes the process to be frozen according to the process identifier of the process to be frozen.
在一些可选的实施例中,所述省电应用向所述冻结器下发进程冻结通知,包括:In some optional embodiments, the power-saving application sends a process freezing notification to the freezer, including:
所述省电应用获得所述内存分配器上报的内存释放完毕的通知后,向所述冻结器下发进程冻结通知。After receiving the memory release completion notification reported by the memory allocator, the power-saving application sends a process freezing notification to the freezer.
本实施例的有益效果在于:The beneficial effects of this embodiment are:
省电应用在收到内存释放完毕的通知后再冻结进程,可以确保待冻结进程在内存分配器尽可能地释放完内存之后才被冻结,从而避免被冻结的进程占用电子设备的内存资源。The power-saving application freezes the process after receiving the notification that the memory is released, which can ensure that the process to be frozen is not frozen until the memory allocator has released as much memory as possible, thereby preventing the frozen process from occupying the memory resources of the electronic device.
在一些可选的实施例中,所述省电应用向所述冻结器下发进程冻结通知,包括:In some optional embodiments, the power-saving application sends a process freezing notification to the freezer, including:
所述省电应用在发送所述预冻结通知且经过预设的等待时间后,向所述冻结器下发进程冻结通知。After the power-saving application sends the pre-freezing notification and a preset waiting time has elapsed, it sends a process freezing notification to the freezing device.
本申请第二方面提供一种电子设备,包括存储器和一个或多个处理器;A second aspect of the application provides an electronic device, including a memory and one or more processors;
所述存储器用于存储计算机程序。The memory is used to store computer programs.
所述一个或多个处理器用于执行所述计算机程序,具体用于实现本申请第一方面任意一项所提供的释放内存的方法。The one or more processors are used to execute the computer program, specifically to implement the method for releasing memory provided in any one of the first aspects of this application.
本申请第三方面提供一种计算机存储介质,用于存储计算机程序,所述计算机程序被执行时,具体用于实现本申请第一方面任意一项所提供的释放内存的方法。A third aspect of this application provides a computer storage medium for storing a computer program. When the computer program is executed, it is specifically used to implement the method for releasing memory provided in any one of the first aspects of this application.
本申请实施例提供一种释放内存的方法、设备和存储介质,方法包括:应用放后台时,将应用对应的进程确定为待冻结进程;通过强制回收的方式释放待冻结进程占用的内存;冻结待冻结进程。在进程被冻结前,本方案通过主动触发待冻结进程按强制回收的方式释放内存,增加了待冻结进程所能释放的内存资源,从而缓解后台应用的进程占用电子设备过多内存资源的问题。Embodiments of the present application provide a method, device, and storage medium for releasing memory. The method includes: when an application is placed in the background, determining the process corresponding to the application as a process to be frozen; releasing the memory occupied by the process to be frozen through forced recycling; freezing Process to be frozen. Before the process is frozen, this solution actively triggers the process to be frozen to release memory through forced recycling, increasing the memory resources that can be released by the process to be frozen, thus alleviating the problem of background application processes occupying too much memory resources of electronic devices.
附图说明Description of drawings
图1为本申请实施例提供的一种电子设备的结构示意图;Figure 1 is a schematic structural diagram of an electronic device provided by an embodiment of the present application;
图2为本申请实施例提供的一种电子设备的用户界面示意图;Figure 2 is a schematic diagram of a user interface of an electronic device provided by an embodiment of the present application;
图3为本申请实施例提供的另一种电子设备的用户界面示意图;Figure 3 is a schematic diagram of a user interface of another electronic device provided by an embodiment of the present application;
图4为本申请实施例提供的一种软件架构的示意图;Figure 4 is a schematic diagram of a software architecture provided by an embodiment of the present application;
图5为本申请实施例提供的一种释放内存的方法的时序图;Figure 5 is a timing diagram of a method for releasing memory provided by an embodiment of the present application;
图6为本申请实施例提供的另一种释放内存的方法的时序图;Figure 6 is a timing diagram of another method of releasing memory provided by an embodiment of the present application;
图7为本申请实施例提供的一种释放内存的方法的流程图;Figure 7 is a flow chart of a method for releasing memory provided by an embodiment of the present application;
图8为本申请实施例提供的另一种释放内存的方法的流程图。Figure 8 is a flow chart of another method of releasing memory provided by an embodiment of the present application.
具体实施方式Detailed ways
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述。以下实施例中所使用的术语只是为了描述特定实施例的目的,而并非旨在作为对本申请的限制。如在本申请的说明书和所附权利要求书中所使用的那样,单数表达形式“一个”、“一种”、“所述”、“上述”、“该”和“这一”旨在也包括例如“一个或多个”这种表达形式,除非其上下文中明确地有相反指示。还应当理解,在本申请实施例中,“一个或多个”是指一个、两个或两个以上;“和/或”,描述关联对象的关联关系,表示可以存在三种关系;例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B的情况,其中A、B可以是单数或者复数。字符“/”一般表示前后关联对象是一种“或”的关系。The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. The terminology used in the following examples is for the purpose of describing specific embodiments only and is not intended to limit the application. As used in the specification and appended claims of this application, the singular expressions "a", "an", "said", "above", "the" and "the" are intended to also Expressions such as "one or more" are included unless the context clearly indicates otherwise. It should also be understood that in the embodiments of this application, "one or more" refers to one, two or more than two; "and/or" describes the association relationship of associated objects, indicating that three relationships can exist; for example, A and/or B can mean: A alone exists, A and B exist simultaneously, and B exists alone, where A and B can be singular or plural. The character "/" generally indicates that the related objects are in an "or" relationship.
为了下述各实施例的描述清楚简洁,首先对本申请涉及的相关技术术语或技术进行简要介绍:In order to describe the following embodiments clearly and concisely, the relevant technical terms or technologies involved in this application are first briefly introduced:
内存分配器。在分层架构的系统中,内核层和Native层均有自己的内存分配器,其中Native层的内存分配器又可以称为用户态内存分配器。系统中的进程可以通过malloc或者new函数来调用用户态内存分配器,从而申请内存资源。常见的用户态内存分配器有Scudo,Jemalloc等。为了便于区分,下文统一将用户态内存分配器简称为内存分配器,将内核层的内存分配器记为内存管理模块。Memory allocator. In a layered architecture system, both the kernel layer and the Native layer have their own memory allocators, and the memory allocator of the Native layer can also be called the user-mode memory allocator. Processes in the system can call the user-mode memory allocator through the malloc or new function to apply for memory resources. Common user-mode memory allocators include Scudo, Jemalloc, etc. In order to facilitate the distinction, the user-mode memory allocator will be referred to as the memory allocator in the following, and the kernel-level memory allocator will be recorded as the memory management module.
用户态内存分配器通常可以通过日常回收或强制回收的方式释放申请到的内存资源。The user-mode memory allocator can usually release the requested memory resources through daily recycling or forced recycling.
日常回收(lazy garbage collection,lazyGC),一种内存分配器释放内存的方式。当内存分配器按日常回收的方式释放内存时,基于性能考虑,内存分配器并不会把进程占用的所有空闲内存全部返还给操作系统,而是设置了一定的回收阈值,超过回收阈值的内存释放给操作系统,回收阈值内的内存仍由内存分配器持有。当进程调用free函数释放内存时,该进程的内存分配器可以按日常回收的方式释放内存资源。Daily collection (lazy garbage collection, lazyGC), a way for the memory allocator to release memory. When the memory allocator releases memory according to daily recycling, based on performance considerations, the memory allocator will not return all the free memory occupied by the process to the operating system. Instead, it will set a certain recycling threshold. The memory that exceeds the recycling threshold Released to the operating system, memory within the reclaim threshold is still held by the memory allocator. When a process calls the free function to release memory, the memory allocator of the process can release memory resources in a daily recycling manner.
强制回收(Force garbage collection,ForceGC),另一种内存分配器释放内存的方式。内存分配器一般配置有强制释放接口,当强制释放接口被调用时,内存分配器按强制回收的方式释放内存。按强制回收方式释放内存时,不设置回收阈值,内存分配器将进程占有的空闲内存尽可能地释放给操作系统。也就是说,与日常回收的方式相比,内存分配器通过强制回收的方式通常可以释放更多的内存资源。Force garbage collection (ForceGC), another way for the memory allocator to release memory. The memory allocator is generally configured with a forced release interface. When the forced release interface is called, the memory allocator releases the memory through forced recycling. When releasing memory according to the forced recycling method, no recycling threshold is set, and the memory allocator releases as much free memory occupied by the process to the operating system as possible. In other words, compared with daily recycling, the memory allocator can usually release more memory resources through forced recycling.
内存压缩。一种系统内核回收内存的方式。安卓系统通常基于zram swap技术实现内存压缩。zram swap技术的基本原理是,预先在电子设备的内存中划分一块区域作为swap分区,当可分配的内存资源较少时,内核将被占用的内存中的数据压缩,然后把压缩后的数据存放在内存的swap分区中。例如,系统内核将50MB被占用的内存空间中的数据压缩,如果压缩率是0.4,则压缩后的数据只需占用20MB的内存,即通过内存压缩,内核回收了30MB可分配的内存空间。Memory compression. A way for the system kernel to reclaim memory. Android systems usually implement memory compression based on zram swap technology. The basic principle of zram swap technology is to pre-divide an area in the memory of the electronic device as a swap partition. When there are few allocable memory resources, the kernel compresses the data in the occupied memory and then stores the compressed data. In the swap partition of memory. For example, the system kernel compresses data in 50MB of occupied memory space. If the compression ratio is 0.4, the compressed data only takes up 20MB of memory. That is, through memory compression, the kernel reclaims 30MB of allocable memory space.
应用本申请提供的释放内存的方法电子设备可以是手机、平板电脑、手持计算机、上网本、个人数字助理(Personal Digital Assistant,PDA)、可穿戴电子设备等设备,本申请对应用内存回收方法的电子设备的具体形式不做特殊限制。Electronic devices that apply the memory release method provided by this application can be mobile phones, tablet computers, handheld computers, netbooks, personal digital assistants (Personal Digital Assistants, PDAs), wearable electronic devices and other devices. This application applies the memory recovery method to electronic devices. The specific form of the equipment is not particularly limited.
请参见图1,为应用本申请的电子设备100的结构示意图。电子设备100可以包括:处理器110,外部存储器120,内部存储器121,通用串行总线(universal serial bus,USB)接口130,充电管理模块140,电源管理模块141,电池142,天线1,天线2,移动通信模块150,无线通信模块160,音频模块170,扬声器170A,受话器170B,麦克风170C,耳机接口170D,传感器模块180,按键190,马达191,指示器192,摄像头193,显示屏194,以及用户标识模块(subscriber identification module,SIM)卡接口195等。其中传感器模块180可以包括压力传感器180A,陀螺仪传感器180B,气压传感器180C,磁传感器180D,加速度传感器180E,距离传感器180F,接近光传感器180G,指纹传感器180H,温度传感器180J,触摸传感器180K,环境光传感器180L,骨传导传感器180M等。Please refer to FIG. 1 , which is a schematic structural diagram of an electronic device 100 to which the present application is applied. The electronic device 100 may include: a processor 110, an external memory 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2 , mobile communication module 150, wireless communication module 160, audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone interface 170D, sensor module 180, button 190, motor 191, indicator 192, camera 193, display screen 194, and Subscriber identification module (subscriber identification module, SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyro sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, and ambient light. Sensor 180L, bone conduction sensor 180M, etc.
处理器110可以包括一个或多个处理单元,例如:处理器110可以包括应用处理器(application processor,AP),通信处理器(communication processor,CP,也可以称为调制解调器),图形处理器(graphics processing unit,GPU)等。内部存储器121又可以称为随机存取存储器(Random Access Memory,RAM),简称内存,处理器可以直接和RAM交换数据。RAM可以被随时读写,并且具有很高的读写速度,因此通常作为操作系统或其他正在运行中的程序的临时数据存储介质。The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (application processor, AP), a communication processor (CP, also called a modem), a graphics processor (graphics processor). processing unit, GPU), etc. The internal memory 121 can also be called random access memory (Random Access Memory, RAM), or memory for short, and the processor can directly exchange data with RAM. RAM can be read and written at any time and has high read and write speeds, so it is often used as a temporary data storage medium for the operating system or other running programs.
外部存储器120,一般为采用非易失性存储介质的存储器,例如,至少一个磁盘存储器件,闪存器件,通用闪存存储器(universal flash storage,UFS)等。The external memory 120 is generally a memory using a non-volatile storage medium, such as at least one disk storage device, a flash memory device, universal flash storage (UFS), etc.
非易失性存储器130可以包括存储程序区和存储数据区。其中,存储程序区可存储操作系统,至少一个功能所需的应用程序(比如声音播放功能,图像、视频播放功能等)等。存储数据区可存储电子设备使用过程中所创建的数据(比如音频数据,照片、视频等)等。The nonvolatile memory 130 may include a program storage area and a data storage area. Among them, the stored program area can store the operating system, at least one application program required for a function (such as a sound playback function, an image, a video playback function, etc.), etc. The storage data area can store data created during the use of electronic equipment (such as audio data, photos, videos, etc.).
电子设备100通过GPU,显示屏194,以及应用处理器等实现显示功能。GPU为图像处理的微处理器,连接显示屏194和应用处理器。GPU用于执行数学和几何计算,用于图形渲染。处理器110可包括一个或多个GPU,其执行程序指令以生成或改变显示信息。The electronic device 100 implements display functions through a GPU, a display screen 194, an application processor, and the like. The GPU is an image processing microprocessor and is connected to the display screen 194 and the application processor. GPUs are used to perform mathematical and geometric calculations for graphics rendering. Processor 110 may include one or more GPUs that execute program instructions to generate or alter display information.
显示屏194用于显示图像,视频等。显示屏194包括显示面板。显示面板可以采用液晶显示屏(liquidcrystaldisplay,LCD),有机发光二极管(organic light-emittingdiode,OLED),有源矩阵有机发光二极体或主动矩阵有机发光二极体(active-matrixorganic light emitting diode的,AMOLED),柔性发光二极管(flex light-emittingdiode,FLED),Miniled,MicroLed,Micro-oled,量子点发光二极管(quantum dot lightemitting diodes,QLED)等。在一些实施例中,电子设备100可以包括1个或N个显示屏194,N为大于1的正整数。The display screen 194 is used to display images, videos, etc. Display 194 includes a display panel. The display panel can use a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active matrix organic light emitting diode or an active matrix organic light emitting diode (active-matrix organic light emitting diode). AMOLED), flexible light-emitting diodes (FLED), Miniled, MicroLed, Micro-oled, quantum dot light-emitting diodes (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.
电子设备100的显示屏194上可以显示一系列图形用户界面(graphical userinterface,GUI),这些GUI都是该电子设备100的主屏幕。一般来说,电子设备100的显示屏194的尺寸是固定的,只能在该电子设备100的显示屏194中显示有限的控件。控件是一种GUI元素,它是一种软件组件,包含在应用程序中,控制着该应用程序处理的所有数据以及关于这些数据的交互操作,用户可以通过直接操作(direct manipulation)来与控件交互,从而对应用程序的有关信息进行读取或者编辑。一般而言,控件可以包括图标、按钮、菜单、选项卡、文本框、对话框、状态栏、导航栏、Widget等可视的界面元素。The display screen 194 of the electronic device 100 may display a series of graphical user interfaces (GUIs), and these GUIs are the home screens of the electronic device 100 . Generally speaking, the size of the display screen 194 of the electronic device 100 is fixed, and only limited controls can be displayed on the display screen 194 of the electronic device 100 . A control is a GUI element. It is a software component that is included in an application and controls all the data processed by the application and the interactive operations on these data. The user can interact with the control through direct manipulation. , to read or edit the relevant information of the application. Generally speaking, controls can include visual interface elements such as icons, buttons, menus, tabs, text boxes, dialog boxes, status bars, navigation bars, and widgets.
例如,在本申请实施例中,当一个应用在前台运行时,电子设备100的显示屏可以显示该应用的图形用户界面,当前台运行的应用被切换到后台时,显示屏则不显示该应用的图形用户界面。For example, in the embodiment of the present application, when an application is running in the foreground, the display screen of the electronic device 100 can display the graphical user interface of the application. When the application running in the foreground is switched to the background, the display screen does not display the application. graphical user interface.
电子设备100可以通过音频模块170,扬声器170A,受话器170B,麦克风170C,耳机接口170D,以及应用处理器等实现音频功能。例如音乐播放,录音等。The electronic device 100 can implement audio functions through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone interface 170D, and the application processor. Such as music playback, recording, etc.
音频模块170用于将数字音频信息转换成模拟音频信号输出,也用于将模拟音频输入转换为数字音频信号。音频模块170还可以用于对音频信号编码和解码。在一些实施例中,音频模块170可以设置于处理器110中,或将音频模块170的部分功能模块设置于处理器110中。The audio module 170 is used to convert digital audio information into analog audio signal output, and is also used to convert analog audio input into digital audio signals. Audio module 170 may also be used to encode and decode audio signals. In some embodiments, the audio module 170 may be provided in the processor 110 , or some functional modules of the audio module 170 may be provided in the processor 110 .
扬声器170A,也称“喇叭”,用于将音频电信号转换为声音信号。电子设备100可以通过扬声器170A收听音乐,或收听免提通话。Speaker 170A, also called "speaker", is used to convert audio electrical signals into sound signals. The electronic device 100 can listen to music through the speaker 170A, or listen to hands-free calls.
受话器170B,也称“听筒”,用于将音频电信号转换成声音信号。当电子设备100接听电话或语音信息时,可以通过将受话器170B靠近人耳接听语音。Receiver 170B, also called "earpiece", is used to convert audio electrical signals into sound signals. When the electronic device 100 answers a call or a voice message, the voice can be heard by bringing the receiver 170B close to the human ear.
麦克风170C,也称“话筒”,“传声器”,用于将声音信号转换为电信号。当拨打电话或发送语音信息时,用户可以通过人嘴靠近麦克风170C发声,将声音信号输入到麦克风170C。电子设备100可以设置至少一个麦克风170C。在另一些实施例中,电子设备100可以设置两个麦克风170C,除了采集声音信号,还可以实现降噪功能。在另一些实施例中,电子设备100还可以设置三个,四个或更多麦克风170C,实现采集声音信号,降噪,还可以识别声音来源,实现定向录音功能等。Microphone 170C, also called "microphone" or "microphone", is used to convert sound signals into electrical signals. When making a call or sending a voice message, the user can speak close to the microphone 170C with the human mouth and input the sound signal to the microphone 170C. The electronic device 100 may be provided with at least one microphone 170C. In other embodiments, the electronic device 100 may be provided with two microphones 170C, which in addition to collecting sound signals, may also implement a noise reduction function. In other embodiments, the electronic device 100 can also be provided with three, four or more microphones 170C to collect sound signals, reduce noise, identify sound sources, and implement directional recording functions, etc.
耳机接口170D用于连接有线耳机。耳机接口170D可以是USB接口130,也可以是3.5mm的开放移动电子设备平台(open mobile terminal platform,OMTP)标准接口,美国蜂窝电信工业协会(cellular telecommunications industry association of the USA,CTIA)标准接口。The headphone interface 170D is used to connect wired headphones. The headphone interface 170D may be a USB interface 130, or may be a 3.5 mm open mobile terminal platform (OMTP) standard interface or a Cellular Telecommunications Industry Association of the USA (CTIA) standard interface.
本申请实施例中,音频模块170,扬声器170A,受话器170B,麦克风170C,耳机接口170D既可以被前台运行的应用占用,也可以被后台运行的应用占用。In the embodiment of the present application, the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, and the headphone interface 170D can be occupied by applications running in the foreground or in the background.
例如,用户启动音乐应用后,音乐应用在前台运行,利用电子设备100的音频模块170和扬声器170A(或耳机接口170D)播放音乐。当用户退出音乐应用,打开聊天应用时,音乐应用进入后台运行,后台运行的音乐应用仍然可以占用电子设备100的音频模块170和扬声器170A(或耳机接口170D)播放音乐。For example, after the user starts the music application, the music application runs in the foreground and uses the audio module 170 and speaker 170A (or headphone interface 170D) of the electronic device 100 to play music. When the user exits the music application and opens the chat application, the music application enters the background and the music application running in the background can still occupy the audio module 170 and speaker 170A (or headphone interface 170D) of the electronic device 100 to play music.
用户使用上述电子设备的过程中,可能会打开多个应用,这种情况下,电子设备在前台模式下运行最近一次打开的应用,并将之前打开的应用切换到后台。处于前台模式的应用程序占用电子设备的显示装置(如触摸屏)和输入装置,可以与用户进行交互。When the user uses the above-mentioned electronic device, he or she may open multiple applications. In this case, the electronic device runs the most recently opened application in the foreground mode and switches the previously opened application to the background. Applications in foreground mode occupy the display device (such as a touch screen) and input device of the electronic device and can interact with the user.
切换到后台的应用可能处于后台运行或者冻结状态。Apps switched to the background may be running in the background or frozen.
后台运行是指应用程序处于后台模式,不占用电子设备的显示装置和输入装置,不能与用户进行交互,处于后台运行的应用程序也会占用一定的系统资源,如,内存资源、CPU资源。Background running means that the application is in background mode, does not occupy the display device and input device of the electronic device, and cannot interact with the user. Applications running in the background will also occupy certain system resources, such as memory resources and CPU resources.
冻结状态是指某应用程序被系统冻结,冻结状态的应用程序停止运行,不占用CPU资源,不占用(或占用极少的)内存资源,冻结状态的应用程序再次切换到前台运行时,可以恢复到之前的状态继续运行。The frozen state means that an application is frozen by the system. The application in the frozen state stops running, does not occupy CPU resources, and does not occupy (or occupies very little) memory resources. The application in the frozen state can be restored when it is switched to the foreground again. Continue running to the previous state.
在实际场景中,若切换到后台的应用有前台可感知的功能,例如导航应用切换到后台时继续通过语音播报导航信息,音乐应用切换到后台时继续播放音乐,则电子设备以后台模式继续运行该应用。In actual scenarios, if the application switched to the background has functions that can be perceived by the foreground, such as a navigation application that continues to broadcast navigation information through voice when it is switched to the background, and a music application that continues to play music when it is switched to the background, the electronic device will continue to run in background mode. The application.
若切换到后台的应用没有前台可感知的功能,则电子设备冻结该应用对应的每个进程,对应的进程均被冻结后,应用即进入冻结状态。If the application switched to the background has no functions that can be sensed by the foreground, the electronic device freezes each process corresponding to the application. After the corresponding processes are frozen, the application enters the frozen state.
示例性的,用户打开视频应用时,视频应用进入前台运行,此时,电子设备的显示屏被视频应用占用,显示屏上显示如图2所示的视频应用的界面,电子设备通过视频应用在显示屏上播放视频。For example, when the user opens a video application, the video application enters the foreground and runs. At this time, the display screen of the electronic device is occupied by the video application, and the interface of the video application as shown in Figure 2 is displayed on the display screen. The electronic device operates in the foreground through the video application. Video plays on the display.
用户从视频应用退出并打开聊天应用时,例如图2中用户点击显示屏顶端弹出的聊天应用的消息时,聊天应用进入前台运行,视频应用则被切换到后台。相应的,电子设备的显示屏被聊天应用占用,显示屏上显示如图3所示的聊天应用的界面,切换到后台的视频应用则不再占用显示屏。When the user exits the video application and opens the chat application, for example, when the user clicks on the chat application message that pops up at the top of the display in Figure 2, the chat application enters the foreground and the video application is switched to the background. Correspondingly, the display screen of the electronic device is occupied by the chat application, and the interface of the chat application as shown in Figure 3 is displayed on the display screen. The video application switched to the background no longer occupies the display screen.
处于后台模式的视频应用不具有前台可感知的功能,因而电子设备将后台的视频应用冻结。当用户重启冻结状态的视频应用时,视频应用可以恢复到之前退出时的视频播放进度继续播放视频。The video application in the background mode has no foreground-perceivable functions, so the electronic device freezes the video application in the background. When the user restarts the frozen video application, the video application can resume the video playback progress when exiting and continue playing the video.
由于电子设备的内存资源有限,操作系统会主动回收被冻结的进程的内存资源,以供其他运行中的进程使用。Since the memory resources of electronic devices are limited, the operating system will actively reclaim the memory resources of the frozen process for use by other running processes.
相关技术中,由于被冻结的进程不会主动调用强制释放接口,操作系统一般只能够从被冻结的进程收回该进程申请的一部分的内存资源,另一部分的内存资源则被该进程的内存分配器保留,这部分内存资源相当于仍由被冻结的进程占用,因此无法被操作系统分配给其他运行中的进程。In related technologies, since a frozen process will not actively call the forced release interface, the operating system can generally only take back part of the memory resources requested by the process from the frozen process, and the other part of the memory resources is allocated by the memory allocator of the process. Reserved, this part of the memory resources is still occupied by the frozen process, so it cannot be allocated by the operating system to other running processes.
作为一个示例,视频应用在前台运行时,视频应用对应的进程A通过内存分配器申请了10MB内存,视频应用被切换到后台时,进程A被冻结,操作系统从进程A回收内存。As an example, when a video application is running in the foreground, process A corresponding to the video application applies for 10MB of memory through the memory allocator. When the video application is switched to the background, process A is frozen, and the operating system reclaims memory from process A.
回收内存时,进程A不会主动调用强制释放接口,所以内存分配器不会按强制回收的方式释放内存,导致之前申请的一部分内存(例如5MB内存)被进程A的内存分配器保留,只有一部分内存被释放给操作系统。When recycling memory, process A will not actively call the forced release interface, so the memory allocator will not release the memory in the way of forced recycling. As a result, part of the previously applied memory (for example, 5MB memory) is retained by process A's memory allocator, and only part of it is retained. The memory is released to the operating system.
可见,若进程被冻结前不通过强制回收的方式释放内存,则被冻结的进程占有的内存资源会过多,在用户打开多个应用时,电子设备中可分配的内存资源过少,进而引起前台运行的应用无法流畅运行等问题。It can be seen that if the memory is not released through forced recycling before the process is frozen, the frozen process will occupy too many memory resources. When the user opens multiple applications, there will be too few memory resources that can be allocated in the electronic device, which will cause Applications running in the foreground cannot run smoothly and other issues.
本申请提供的释放内存的方法可以适用于任意一种内存分配器,本实施例对内存分配器的具体种类不做限定。示例性的,本申请所述的内存分配器可以是Jemalloc或Scudo。The method for releasing memory provided in this application can be applied to any memory allocator. This embodiment does not limit the specific type of memory allocator. For example, the memory allocator described in this application can be Jemalloc or Scudo.
为了解决上述问题,本申请提供了一种释放内存的方法,通过实施本申请的方法,电子设备可以在冻结进程之前,调用内存分配器的强制释放接口,使内存分配器按强制回收方式释放待冻结进程所占用的内存,从而大幅减少被冻结的进程占用的内存资源,保障其他处于运行状态的进程正常工作。In order to solve the above problem, this application provides a method for releasing memory. By implementing the method of this application, the electronic device can call the forced release interface of the memory allocator before freezing the process, so that the memory allocator releases the pending memory in a forced recycling manner. Freeze the memory occupied by the process, thereby greatly reducing the memory resources occupied by the frozen process and ensuring the normal operation of other running processes.
本申请所提供的释放内存的方法,可以适用于基于Android操作系统的电子设备。下面以Android系统为例,说明适用本申请的电子设备的软件架构。The method of releasing memory provided by this application can be applied to electronic devices based on the Android operating system. The following takes the Android system as an example to illustrate the software architecture of the electronic device applicable to this application.
请参见图4,为本申请实施例提供的一种电子设备的软件架构示意图。Please refer to FIG. 4 , which is a schematic diagram of the software architecture of an electronic device provided by an embodiment of the present application.
本实施例中,电子设备的软件系统采用分层架构,具体分为应用层,应用框架层,系统库(又称Native层)和内核层。In this embodiment, the software system of the electronic device adopts a layered architecture, which is specifically divided into an application layer, an application framework layer, a system library (also called a Native layer) and a kernel layer.
图4中仅示出了与本申请实施例相关的部分层和部分组件,实际应用中,还可以包括图4中未示出的层级和组件。当然,也可以仅包含图4所示的组件中的部分组件。Figure 4 only shows some layers and some components related to the embodiment of the present application. In actual applications, levels and components not shown in Figure 4 may also be included. Of course, it is also possible to include only some of the components shown in Figure 4 .
应用层包括多种应用程序,例如在本实施例中,应用层包括手机管家201,聊天,视频和导航应用。其中手机管家应用包括省电应用组件202。省电应用是基于Android系统的电子设备中常见的一款节电软件,在实际应用中,省电应用可以基于用户操作而开启节电模式,便捷控制一些耗电的硬件和设置项,还可以发现并提示后台耗电量较大的应用和服务,通过以上措施,省电应用能够减少电子设备的耗电量,延长电子设备的待机时间。The application layer includes a variety of application programs. For example, in this embodiment, the application layer includes mobile phone manager 201, chat, video and navigation applications. The mobile phone manager application includes a power saving application component 202. Power-saving applications are common power-saving software in electronic devices based on the Android system. In practical applications, power-saving applications can turn on power-saving mode based on user operations, conveniently control some power-consuming hardware and settings, and can also Discover and prompt background power-consuming applications and services. Through the above measures, power-saving applications can reduce the power consumption of electronic devices and extend the standby time of electronic devices.
在本实施例中,省电应用还可以发现从前台被切换到后台的应用,并在发现被切换到后台的应用时,向内核层下发进程冻结通知,从而冻结切换到后台的应用对应的进程。In this embodiment, the power-saving application can also discover the application that has been switched from the foreground to the background, and when it discovers the application that has been switched to the background, it sends a process freezing notification to the kernel layer, thereby freezing the corresponding application that has been switched to the background. process.
在一些可选的实施例中,也可以由其他应用的其他组件发现切换到后台的应用并下发进程冻结通知,本实施例对具体实现这一功能的应用不做限定,上述省电应用组件仅作为一个示例。In some optional embodiments, other components of other applications can also discover the application that has been switched to the background and issue a process freeze notification. This embodiment does not limit the applications that specifically implement this function. The above-mentioned power-saving application components Just as an example.
应用框架层包括内存管理服务203,以及通知管理器,内容提供其,窗口管理器,资源管理器,电话管理器和视图系统等模块。内存管理服务用于管控调度电子设备的内存资源,内存管理服务可以和应用框架层的其他调度电子设备硬件资源的服务集成为一个组件,也可以作为应用框架层的一个独立的组件。The application framework layer includes memory management service 203, as well as notification manager, content provider, window manager, resource manager, phone manager and view system modules. The memory management service is used to control and schedule the memory resources of electronic devices. The memory management service can be integrated into a component with other services of the application framework layer that schedule electronic device hardware resources, or can be used as an independent component of the application framework layer.
在本实施例中,内存管理服务可以通过向待冻结进程发送信号的方式,触发待冻结进程的内存分配器执行强制回收。In this embodiment, the memory management service can trigger the memory allocator of the process to be frozen to perform forced recycling by sending a signal to the process to be frozen.
系统库可以包括多个功能模块,如表面管理器,三维图形处理器库,二维图形引擎和媒体库等。本实施例中,系统库可以包括至少一个函数库和至少一个内存分配器。电子设备上运行的每一个进程,都在系统库中有对应的函数库和内存分配器。例如,图4中电子设备运行有待冻结进程A(204)和待冻结进程B(205),其中待冻结进程A包含一个函数库和一个内存分配器,待冻结进程B也包含一个函数库和一个内存分配器。The system library can include multiple functional modules, such as surface manager, 3D graphics processor library, 2D graphics engine and media library, etc. In this embodiment, the system library may include at least one function library and at least one memory allocator. Every process running on an electronic device has a corresponding function library and memory allocator in the system library. For example, in Figure 4, the electronic device runs process A (204) and process B (205) to be frozen. Process A to be frozen contains a function library and a memory allocator, and process B to be frozen also contains a function library and a memory allocator. Memory allocator.
在一些可选的实施例中,上述函数库可以是libc。In some optional embodiments, the above function library may be libc.
函数库包括预先配置的若干个函数,当函数库中的函数被调用时,函数库可以实现该函数对应的功能。例如,当函数库收到一个信号时,函数库中该信号对应的内存释放信号处理函数被进程调用,于是函数库实现该内存释放信号处理函数对应的功能。The function library includes several preconfigured functions. When a function in the function library is called, the function library can implement the function corresponding to the function. For example, when the function library receives a signal, the memory release signal processing function corresponding to the signal in the function library is called by the process, so the function library implements the function corresponding to the memory release signal processing function.
一个进程的内存分配器,用于向内核层的内存管理模块申请该进程运行所需的内存资源,以及用于在合适的时机释放该进程占用的内存资源,例如,当进程调用释放内存的函数,如free函数时,内存分配器释放内存资源。The memory allocator of a process is used to apply to the memory management module of the kernel layer for the memory resources required for the running of the process, and is used to release the memory resources occupied by the process at the appropriate time, for example, when the process calls a function that releases memory , such as the free function, the memory allocator releases memory resources.
内存管理模块一般将电子设备的内存划分为多个内存页(即多个page),并按页管理内存。当进程跳过内存分配器直接向内存管理模块申请内存资源时,内存管理模块只能以页为单位给进程分配内存资源,但一个进程运行时单次申请的内存大小大多数情况下小于一个内存页,这就造成内存资源的浪费。而通过内存分配器申请内存资源,则可以根据进程的实际需求,申请到小于一个内存页的内存资源,从而提高内存资源的利用率,避免浪费内存资源。The memory management module generally divides the memory of the electronic device into multiple memory pages (ie, multiple pages) and manages the memory by page. When a process skips the memory allocator and applies directly to the memory management module for memory resources, the memory management module can only allocate memory resources to the process in units of pages. However, the size of a single application for a process when it is running is usually less than one memory. page, which causes a waste of memory resources. By applying for memory resources through the memory allocator, you can apply for memory resources smaller than one memory page according to the actual needs of the process, thereby improving the utilization of memory resources and avoiding wasting memory resources.
内核层包括系统层安全机制、内存管理、文件系统、进程管理、网络堆栈及一系列驱动模块,是硬件和软件之间的层,提供与硬件的交互。The kernel layer includes system layer security mechanisms, memory management, file systems, process management, network stacks and a series of driver modules. It is the layer between hardware and software and provides interaction with hardware.
在本实施例中,如图4所示,内核层包括冻结器206(freezer)和内存管理模块207(Memory Manager,MM)。其中,冻结器响应省电应用下发的进程冻结通知,将对应的进程冻结,内存管理模块用于管理电子设备的内存资源,具体的,内存管理模块提供了获取内存和释放内存的接口,各个进程的内存分配器通过调用获取内存的接口申请所需的内存资源,通过调用释放内存的接口释放被进程占用的内存资源。In this embodiment, as shown in Figure 4, the kernel layer includes a freezer 206 (freezer) and a memory management module 207 (Memory Manager, MM). Among them, the freezer responds to the process freezing notification issued by the power-saving application and freezes the corresponding process. The memory management module is used to manage the memory resources of the electronic device. Specifically, the memory management module provides an interface for acquiring memory and releasing memory. Each The memory allocator of the process applies for the required memory resources by calling the memory acquisition interface, and releases the memory resources occupied by the process by calling the memory release interface.
为了对上述架构图有更清晰的理解,请参见图5,为基于图4所示的软件架构实现的释放内存的方法的时序图。In order to have a clearer understanding of the above architecture diagram, please refer to Figure 5, which is a timing diagram of the method of releasing memory based on the software architecture shown in Figure 4.
首先,为实现本申请所提供的方法,函数库中需要预先配置内存释放信号量和内存释放信号处理函数,操作系统可以在电子设备开机时将内存释放信号量和内存释放信号处理函数加载到函数库中。First of all, in order to implement the method provided by this application, the memory release semaphore and the memory release signal processing function need to be pre-configured in the function library. The operating system can load the memory release semaphore and the memory release signal processing function into the function when the electronic device is turned on. in the library.
其中,内存释放信号处理函数包含触发内存分配器执行强制回收的代码,当函数库的内存释放信号处理函数被调用时,函数库触发内存分配器执行强制回收。Among them, the memory release signal processing function contains code that triggers the memory allocator to perform forced recycling. When the memory release signal processing function of the function library is called, the function library triggers the memory allocator to perform forced recycling.
信号量,可以理解为特定的信号和该信号所指示的操作的对应关系。本实施例中,内存释放信号用于指示函数库触发内存分配器执行强制回收,而触发内存分配器执行强制回收这一操作由前述内存释放信号处理函数实现,因此内存释放信号量可以为内存释放信号和前述内存释放信号处理函数之间的对应关系。A semaphore can be understood as the correspondence between a specific signal and the operation indicated by the signal. In this embodiment, the memory release signal is used to instruct the function library to trigger the memory allocator to perform forced recycling. The operation of triggering the memory allocator to perform forced recycling is implemented by the aforementioned memory release signal processing function. Therefore, the memory release semaphore can be used for memory release. The correspondence between the signal and the aforementioned memory release signal processing function.
也就是说,基于函数库中配置好的内存释放信号量和内存释放信号处理函数,进程收到内存释放信号后,根据内存释放信号量可以确定需要调用内存释放信号处理函数,然后进程调用函数库中内存释放信号处理函数,随后函数库可以触发内存分配器执行强制回收。That is to say, based on the memory release semaphore and memory release signal processing function configured in the function library, after the process receives the memory release signal, it can determine the need to call the memory release signal processing function based on the memory release semaphore, and then the process calls the function library The memory release signal processing function can then trigger the memory allocator to perform forced recycling.
S01,应用从前台切换到后台。S01, the application switches from the foreground to the background.
在一些可选的实施例中,应用在前台运行时,响应于用户的放后台操作,执行步骤S01,从前台切换到后台。In some optional embodiments, when the application is running in the foreground, in response to the user's background operation, step S01 is executed to switch from the foreground to the background.
在另一些可选的实施例中,不需要用户执行放后台操作,系统也可以主动触发应用放后台。例如,当用户一段时间未操作手机(比如2分钟未操作手机)时,手机会自动熄屏,当手机熄屏时,手机可以主动触发当前的应用从前台切换到后台。In other optional embodiments, the user is not required to perform the background operation, and the system can also actively trigger the application to be backgrounded. For example, when the user does not operate the phone for a period of time (such as not operating the phone for 2 minutes), the phone will automatically turn off the screen. When the phone turns off the screen, the phone can actively trigger the current application to switch from the foreground to the background.
上述应用可以是电子设备安装的任意一个应用。作为示例,上述应用可以是图5所示的视频应用,导航应用或聊天应用。The above application can be any application installed on the electronic device. As an example, the above-mentioned application may be a video application, a navigation application or a chat application shown in FIG. 5 .
放后台操作,包括用户执行的任意一种会导致当前在前台运行的应用被切换到后台的操作。在一些示例中,放后台操作可以是从当前在前台运行的应用返回桌面的操作,也可以是从当前在前台运行的应用切换到另一个应用的操作。Background operations include any operation performed by the user that will cause the application currently running in the foreground to be switched to the background. In some examples, the background operation may be an operation of returning to the desktop from an application currently running in the foreground, or an operation of switching from an application currently running in the foreground to another application.
作为一个示例,用户打开电子设备的视频应用后,电子设备在前台运行视频应用,用户在视频应用中观看视频,一段时间后,用户通过手势操作或者按键操作,从视频应用返回到桌面,此时视频应用就会执行步骤S01,从前台切换到后台,返回桌面时用户所执行的手势操作或按键操作即放后台操作。As an example, after a user opens a video application of an electronic device, the electronic device runs the video application in the foreground and the user watches a video in the video application. After a period of time, the user returns from the video application to the desktop through gesture operation or key operation. The video application will execute step S01, switching from the foreground to the background, and the gesture operations or key operations performed by the user when returning to the desktop will be placed in the background.
作为另一个示例,请参见图2,电子设备在前台运行视频应用时,在后台模式下运行的聊天应用收到服务器下发的一条聊天消息,于是电子设备的显示屏顶部弹出这条聊天消息,用户点击显示屏顶部弹出的消息。电子设备响应用户的点击操作,从视频应用退出并打开聊天应用,后台运行的聊天应用被切换到前台运行,显示屏显示如图3所示的聊天应用的界面,同时,视频应用执行步骤S01,从前台切换到后台。此时用户点击显示屏顶部的聊天消息的操作即放后台操作。As another example, see Figure 2. When the electronic device is running a video application in the foreground, the chat application running in the background mode receives a chat message from the server, so the chat message pops up at the top of the display screen of the electronic device. The user clicks on the message that pops up at the top of the display. The electronic device responds to the user's click operation, exits from the video application and opens the chat application. The chat application running in the background is switched to the foreground. The display screen displays the interface of the chat application as shown in Figure 3. At the same time, the video application executes step S01. Switch from foreground to background. At this time, the user clicks on the chat message at the top of the display screen and the operation is placed in the background.
S02,应用通知应用放后台。S02, the application notifies the application to be placed in the background.
应用执行步骤S01后,向省电应用执行步骤S02,即向省电应用通知对应的应用被放后台。After the application executes step S01, it executes step S02 to the power-saving application, that is, it notifies the power-saving application that the corresponding application is placed in the background.
续接图2的示例,当用户点击聊天应用的消息后,视频应用被放后台,然后视频应用执行步骤S02,从而向省电应用通知,视频应用被切换到后台。Continuing the example of Figure 2, when the user clicks on the message of the chat application, the video application is put into the background, and then the video application executes step S02, thereby notifying the power saving application that the video application is switched to the background.
应用可以通过多种方式将应用放后台这一信息通知给省电应用,本实施例对具体的通知方式不做限定。The application can notify the power-saving application of the information that the application is placed in the background in various ways. This embodiment does not limit the specific notification method.
在一些可选的实施例中,应用可以主动将切换到后台的通知消息推送给省电应用,该通知消息可以携带应用的应用标识,例如携带应用的名称,省电应用收到通知消息后就可以确定应用被切到后台。或者,省电应用可以预先订阅各个应用切换到后台的事件,当应用发生从前台切换到后台的事件时,省电应用就可以通过事件订阅机制发现该应用切到后台。In some optional embodiments, the application can actively push a notification message for switching to the background to the power-saving application. The notification message can carry the application identification of the application, for example, the name of the application. The power-saving application can It can be determined that the application has been switched to the background. Alternatively, the power-saving application can pre-subscribe to the event of each application switching to the background. When the application switches from the foreground to the background, the power-saving application can discover that the application has switched to the background through the event subscription mechanism.
S03,省电应用判断应用是否有前台可感知功能。S03, the power-saving application determines whether the application has foreground-aware functions.
在发现一个应用切到后台后,省电应用执行步骤S03,判断应用是否有前台可感知功能。若应用没有前台可感知功能,则执行步骤S04,若应用有前台可感知功能,则不能冻结该应用,而需要在后台模式下继续运行该应用,相应的也不能释放该应用对应的进程所占用的内存,因此本方法结束。After discovering that an application switches to the background, the power-saving application executes step S03 to determine whether the application has foreground-aware functions. If the application does not have a foreground-aware function, step S04 is executed. If the application has a foreground-aware function, the application cannot be frozen and needs to continue running in the background mode. Correspondingly, the process occupied by the application cannot be released. memory, so this method ends.
在一些可选的实施例中,当一个应用被放后台时,电子设备可以直接冻结该应用对应的进程,而不论该应用是否有前台可感知的功能,这种场景下,可以不执行步骤S03。也就是说,步骤S03为可选的步骤。In some optional embodiments, when an application is placed in the background, the electronic device can directly freeze the process corresponding to the application, regardless of whether the application has foreground-aware functions. In this scenario, step S03 may not be performed. . In other words, step S03 is an optional step.
示例性的,当电子设备的省电模式被开启时,此时,电子设备可以禁止应用在后台运行,不论应用是否有前台可感知功能,应用被放后台时电子设备都可以直接冻结该应用对应的进程,从而降低耗电量。For example, when the power saving mode of the electronic device is turned on, the electronic device can prohibit the application from running in the background. Regardless of whether the application has foreground-aware functions, the electronic device can directly freeze the application when the application is placed in the background. process, thereby reducing power consumption.
电子设备在从应用市场安装应用时,可以从应用市场获得安装的应用的类别,例如聊天类,导航类,购物类,游戏类等,其中部分类别的应用具有前台可感知功能,其他类别的应用则不具有前台可感知功能。例如,音乐类,导航类和聊天类应用具有前台可感知功能,购物类和游戏类应用则不具有前台可感知功能。When an electronic device installs an application from the application market, it can obtain the category of the installed application from the application market, such as chat, navigation, shopping, game, etc. Some of the applications have foreground-perceivable functions, and other categories of applications have It does not have front-end perceptible functions. For example, music, navigation, and chat applications have foreground-aware functions, while shopping and game applications do not have foreground-aware functions.
因此,省电应用可以维护一个前台可感知应用列表,当电子设备安装新应用时,省电应用从应用市场获得新安装的应用的类别,根据类别在前台可感知应用列表将这个应用标记为具有前台可感知功能,或者不具有前台可感知功能。这样,在步骤S03中省电应用就可以从前台可感知应用列表查询当前切换到后台的应用是否具有前台可感知功能。Therefore, the power-saving application can maintain a foreground-aware application list. When the electronic device installs a new application, the power-saving application obtains the category of the newly installed application from the application market, and marks this application as having Foreground-aware functionality, or no foreground-aware functionality. In this way, in step S03, the power-saving application can query from the foreground-aware application list whether the application currently switched to the background has a foreground-aware function.
进一步的,电子设备可以基于用户的配置而禁止一部分应用在后台运行,以降低电子设备的耗电量。当一个应用被禁止在后台运行时,省电应用可以在上述前台可感知应用列表中将这个应用标记为不具有前台可感知功能,这样在该应用被放到后台时,省电应用就可以按下述流程冻结该应用。Furthermore, the electronic device can prohibit some applications from running in the background based on the user's configuration to reduce power consumption of the electronic device. When an application is prohibited from running in the background, the power-saving application can mark the application as not having foreground-aware functions in the foreground-aware application list, so that when the application is placed in the background, the power-saving application can press The following process freezes the application.
续接图2的示例,视频应用放后台后,电子设备在前台可感知应用列表中查询视频应用是否有前台可感知功能,查询后确定视频应用不具有前台可感知功能,于是电子设备执行步骤S04。Continuing from the example in Figure 2, after the video application is placed in the background, the electronic device queries the foreground-aware application list whether the video application has a foreground-aware function. After the query, it is determined that the video application does not have a foreground-aware function, so the electronic device executes step S04. .
需要说明,以上仅为省电应用判断应用是否有前台可感知功能的一种示例性的实现方式,在本申请其他实施例中,省电应用也可以通过其他方式判断应用是否有前台可感知功能,例如,应用可以主动通知省电应用自身是否具有前台可感知功能。本实施例对具体的判断方式不做限定。It should be noted that the above is only an exemplary implementation method for a power-saving application to determine whether an application has a foreground-aware function. In other embodiments of the present application, a power-saving application may also use other methods to determine whether an application has a foreground-aware function. , for example, the application can proactively notify the power-saving application whether it has foreground-aware capabilities. This embodiment does not limit the specific judgment method.
S04,省电应用确定待冻结进程。S04, the power saving application determines the process to be frozen.
判断出切换到后台的应用不具有前台可感知功能后,省电应用就需要冻结该应用对应的进程,因此,省电应用执行步骤S04,确定待冻结进程。After it is determined that the application switched to the background does not have the foreground-aware function, the power-saving application needs to freeze the process corresponding to the application. Therefore, the power-saving application executes step S04 to determine the process to be frozen.
一个运行中的应用可以对应有一个或多个进程,S04所述的待冻结进程,包括当前被放后台的应用所对应的每一个进程。以图4为例,一个应用对应有进程A和进程B,该应用被放后台时,确定出的待冻结进程就包括待冻结进程A和待冻结进程B。A running application may correspond to one or more processes. The processes to be frozen described in S04 include each process corresponding to the application that is currently placed in the background. Taking Figure 4 as an example, an application corresponds to process A and process B. When the application is placed in the background, the determined processes to be frozen include process A and process B to be frozen.
也就是说,在S04中,省电应用根据被放后台的应用的应用标识,找到该应用运行时对应的每一个进程,将这些进程确定为待冻结进程。That is to say, in S04, the power-saving application finds each process corresponding to the running application based on the application identifier of the application placed in the background, and determines these processes as processes to be frozen.
省电应用可以采用多种方式查找到放后台的应用对应的进程,本实施例对具体的查找方式不做限定。The power-saving application can use multiple methods to find the process corresponding to the application placed in the background. This embodiment does not limit the specific search method.
示例性的,省电应用可以将放后台的应用的应用标识发送给内核层的进程管理模块,进程管理模块基于应用标识找到该应用对应的每个进程后,将这些进程的进程标识(可以是进程名,或者进程编号)发送给省电应用,由此,省电应用就可以确定这些进程标识对应的进程为待冻结进程。For example, the power-saving application can send the application identification of the background application to the process management module of the kernel layer. After the process management module finds each process corresponding to the application based on the application identification, the process identification of these processes (which can be Process name, or process number) is sent to the power-saving application, so that the power-saving application can determine that the processes corresponding to these process identifiers are processes to be frozen.
续接图2的示例,省电应用确定视频应用被放后台后,将视频应用的应用标识发送至内核层,从而获得内核层反馈的视频应用对应的进程的进程标识,并确定视频应用对应的进程为待冻结进程。Continuing from the example in Figure 2, after the power-saving application determines that the video application is placed in the background, it sends the application identification of the video application to the kernel layer, thereby obtaining the process identification of the process corresponding to the video application fed back by the kernel layer, and determines the process identification corresponding to the video application. The process is a process to be frozen.
S05,省电应用发送预冻结通知。S05, the power saving application sends a pre-freeze notification.
确定待冻结进程后,省电应用对内存管理服务执行步骤S05,发送预冻结通知。After determining the process to be frozen, the power saving application performs step S05 on the memory management service and sends a pre-freezing notification.
预冻结通知携带有前述待冻结进程对应的进程标识。预冻结通知用于向内存管理服务说明,该通知携带的进程标识对应的进程将要被冻结。The pre-freeze notification carries the process identifier corresponding to the process to be frozen. The pre-freeze notification is used to explain to the memory management service that the process corresponding to the process ID carried in the notification will be frozen.
续接前述示例,省电应用确定视频应用对应的进程后,通过S05所述的预冻结通知将视频应用对应的进程的进程标识发送给内存管理服务,从而向内存管理服务说明视频应用对应的这些进程将被冻结。Continuing from the previous example, after the power-saving application determines the process corresponding to the video application, it sends the process identifier of the process corresponding to the video application to the memory management service through the pre-freeze notification described in S05, thereby explaining to the memory management service the process corresponding to the video application. The process will be frozen.
S06,内存管理服务发送内存释放信号。S06, the memory management service sends a memory release signal.
内存管理服务收到预冻结通知后,对每个待冻结进程执行步骤S06,发送内存释放信号。After receiving the pre-freeze notification, the memory management service executes step S06 for each process to be frozen and sends a memory release signal.
内存释放信号的具体形式可以根据实际情况配置,只要确保内存释放信号和电子设备中已经存在的其他信号不同即可,本实施例对此不做限定。The specific form of the memory release signal can be configured according to the actual situation, as long as it is ensured that the memory release signal is different from other signals that already exist in the electronic device. This embodiment does not limit this.
续接前述示例,收到预冻结通知后,内存管理服务从预冻结通知中获得视频应用对应进程的进程标识,然后基于这些进程标识,向视频应用对应的进程发送内存释放信号。Continuing the previous example, after receiving the pre-freeze notification, the memory management service obtains the process ID of the process corresponding to the video application from the pre-freeze notification, and then sends a memory release signal to the process corresponding to the video application based on these process IDs.
S07,函数库调用强制释放接口。S07, the function library calls the forced release interface.
待冻结进程收到内存释放信号后,待冻结进程的函数库对该进程的内存分配器执行步骤S07,调用强制释放接口。After the process to be frozen receives the memory release signal, the function library of the process to be frozen executes step S07 on the memory allocator of the process and calls the forced release interface.
如前所述,待冻结进程的函数库中预先配置有内存释放信号量和内存释放信号处理函数,其中内存释放信号量可以视为内存释放信号和内存释放信号处理函数的对应关系。As mentioned before, the function library of the process to be frozen is pre-configured with a memory release semaphore and a memory release signal processing function. The memory release semaphore can be regarded as the correspondence between the memory release signal and the memory release signal processing function.
可见,待冻结进程收到内存释放信号后,根据预先配置的内存释放信号量,可以确定需要调用内存释放信号处理函数。于是,待冻结进程调用函数库中的内存释放信号处理函数,使得函数库执行步骤S07。It can be seen that after the frozen process receives the memory release signal, it can be determined that the memory release signal processing function needs to be called based on the preconfigured memory release semaphore. Therefore, the process to be frozen calls the memory release signal processing function in the function library, causing the function library to execute step S07.
不同的内存分配器提供的强制释放接口可能相同,也可能不相同。本实施例对强制释放接口的具体形式不做限定。The forced release interfaces provided by different memory allocators may or may not be the same. This embodiment does not limit the specific form of the forced release interface.
作为一个示例,当内存分配器为Scudo时,Scudo所提供的强制释放接口可以是mallopt(M_PURGE),即在步骤S07中,函数库可以调用mallopt(M_PURGE)来触发内存分配器Scudo执行强制回收。As an example, when the memory allocator is Scudo, the forced release interface provided by Scudo can be mallopt(M_PURGE), that is, in step S07, the function library can call mallopt(M_PURGE) to trigger the memory allocator Scudo to perform forced recycling.
续接前述示例,视频应用对应进程收到内存释放信号后,这些进程的函数库调用内存分配器的强制释放接口,从而触发视频应用对应进程的内存分配器执行强制回收。Continuing the previous example, after the processes corresponding to the video application receive the memory release signal, the function libraries of these processes call the forced release interface of the memory allocator, thereby triggering the memory allocator of the process corresponding to the video application to perform forced recycling.
S08,内存分配器按强制回收方式释放内存。S08, the memory allocator releases memory in a forced recycling manner.
在函数库调用了强制释放接口后,待冻结进程的内存分配器调用内核的内存管理模块的释放内存接口,以执行步骤S08,按强制回收方式释放内存。After the function library calls the forced release interface, the memory allocator of the process to be frozen calls the release memory interface of the kernel's memory management module to execute step S08 and release the memory in a forced recovery manner.
如前所述,在步骤S08中,内存分配器会将待冻结进程占有的空闲内存尽可能地释放给操作系统。因此,通过触发内存分配器按强制回收的方式释放内存,可以有效的减少冻结状态的进程所占用的内存,确保操作系统有更多的内存资源可以分配给其他运行中的进程。使运行中的进程能够高效的运行。As mentioned above, in step S08, the memory allocator will release as much free memory occupied by the process to be frozen to the operating system as possible. Therefore, by triggering the memory allocator to release memory through forced recycling, you can effectively reduce the memory occupied by frozen processes and ensure that the operating system has more memory resources to allocate to other running processes. Enable running processes to run efficiently.
为了便于理解,本实施例仅以一个待冻结进程为示例说明步骤S06至S08。在实际场景中,一个应用被放后台时,省电应用可能确定出多个待冻结应用,因此内存管理服务会向每一个待冻结进程发送内存释放信号,每一个待冻结进程收到内存释放信号后,都会调用自身的函数库中的内存释放信号处理函数,所以每一个待冻结进程的函数库都会执行步骤S07,即调用待冻结进程的内存分配器的强制释放接口,最终每一个待冻结进程的内存分配器都会按强制回收的方式释放该进程占用的内存。For ease of understanding, this embodiment only uses one process to be frozen as an example to illustrate steps S06 to S08. In actual scenarios, when an application is placed in the background, the power-saving application may determine multiple applications to be frozen. Therefore, the memory management service will send a memory release signal to each process to be frozen, and each process to be frozen receives a memory release signal. After that, the memory release signal processing function in its own function library will be called. Therefore, the function library of each process to be frozen will execute step S07, that is, the forced release interface of the memory allocator of the process to be frozen will be called. Finally, each process to be frozen will The memory allocator will release the memory occupied by the process through forced recycling.
续接前述示例,视频应用对应进程的函数库调用了强制释放接口后,这些进程内的内存分配器响应对强制释放接口的调用,开始按前述强制回收的方式释放视频应用对应的进程所占用的内存。Continuing the previous example, after the function library of the process corresponding to the video application calls the forced release interface, the memory allocator in these processes responds to the call to the forced release interface and begins to release the memory occupied by the process corresponding to the video application in the aforementioned forced recycling method. Memory.
S09,省电应用下发进程冻结通知。S09, the power-saving application issues a process freeze notification.
进程冻结通知可以携带省电应用确定出的每一个待冻结进程对应的进程标识,收到进程冻结通知后,内核的冻结器就可以将通知携带的进程标识对应的待冻结进程冻结,从而实现被放后台的应用的冻结。The process freezing notification can carry the process identification corresponding to each process to be frozen determined by the power saving application. After receiving the process freezing notification, the kernel's freezer can freeze the process to be frozen corresponding to the process identification carried in the notification, thereby realizing the process being frozen. Freezing of background applications.
省电应用可以在发送预冻结通知之后执行步骤S09。在本实施例中,为了避免在待冻结进程完成内存释放之前就被冻结,省电应用可以预设一个等待时间,在发送预冻结通知并且经过该等待时间之后,省电应用再执行步骤S09,从而保障每个待冻结进程能够在等待时间内将内存全部释放。The power saving application may perform step S09 after sending the pre-freeze notification. In this embodiment, in order to avoid being frozen before the process to be frozen completes memory release, the power-saving application can preset a waiting time. After the pre-freezing notification is sent and the waiting time has elapsed, the power-saving application then executes step S09. This ensures that each process to be frozen can release all memory within the waiting time.
等待时间的具体数值可以根据实际情况设置,本实施例对此不做限定。作为一个示例,等待时间可以设定为10毫秒(ms)。也就是说,省电应用在向内存管理服务发送了预冻结通知后,等待10ms,然后向冻结器下发进程冻结通知。The specific value of the waiting time can be set according to the actual situation, and this embodiment does not limit this. As an example, the wait time may be set to 10 milliseconds (ms). That is to say, after the power-saving application sends a pre-freezing notification to the memory management service, it waits for 10ms and then sends a process freezing notification to the freezer.
续接前述示例,省电应用在向内存管理服务下发预冻结通知之后,等待10ms,然后向内核的冻结器下发进程冻结通知,进程冻结通知中携带视频应用对应进程的进程标识,冻结器接收进程冻结通知,然后将视频应用对应的进程冻结。Continuing the previous example, after the power-saving application sends a pre-freeze notification to the memory management service, it waits for 10ms, and then sends a process freezing notification to the kernel's freezer. The process freeze notification carries the process identifier of the process corresponding to the video application, and the freezer Receive the process freeze notification, and then freeze the process corresponding to the video application.
本实施例具有如下的有益效果:This embodiment has the following beneficial effects:
通过在冻结进程之前发送内存释放命令,触发每个待冻结进程的内存分配器最大限度的释放本进程所占用的空闲内存,减少了后台被冻结的进程占用的内存资源,从而保障电子设备有足量的内存资源可以分配给运行中的进程。By sending a memory release command before freezing the process, the memory allocator of each process to be frozen is triggered to release the free memory occupied by the process to the maximum extent, reducing the memory resources occupied by the frozen processes in the background, thereby ensuring that electronic devices have sufficient Amount of memory resources can be allocated to running processes.
在本申请另一实施例中,省电应用可以在确定内存释放完毕后再下发进程冻结通知。请参见图6,为基于图6所示的软件架构实现的另一种释放内存的方法的时序图。In another embodiment of the present application, the power-saving application may issue a process freeze notification after confirming that the memory is released. Please refer to Figure 6, which is a timing diagram of another method of releasing memory implemented based on the software architecture shown in Figure 6.
如图6所示,本实施例中,步骤S01至步骤S08均与图5对应的实施例一致,其具体实施方式也可以参见图5对应的实施例,不再赘述。As shown in FIG. 6 , in this embodiment, steps S01 to S08 are consistent with the embodiment corresponding to FIG. 5 , and the specific implementation method can also be referred to the embodiment corresponding to FIG. 5 , which will not be described again.
A08,内存分配器向函数库通知内存释放完毕。A08, the memory allocator notifies the function library that the memory is released.
待冻结进程的内存分配器在释放完待冻结进程的空闲内存后,对函数库执行步骤A08,通知内存释放完毕。After releasing the free memory of the process to be frozen, the memory allocator of the process to be frozen executes step A08 on the function library to notify that the memory release is completed.
A09,函数库向内存管理服务通知内存释放完毕。A09, the function library notifies the memory management service that the memory is released.
函数库得到通知后,将通知传递给内存管理服务,即执行步骤A09,通知内存释放完毕。After the function library is notified, it passes the notification to the memory management service, that is, executes step A09 to notify that the memory release is completed.
A10,内存管理服务向省电应用通知内存释放完毕。A10, the memory management service notifies the power-saving application that the memory release is completed.
最后内存管理服务再将通知传递给省电应用,即执行步骤A10,通知内存释放完毕。Finally, the memory management service delivers the notification to the power-saving application, that is, step A10 is executed to notify that the memory release is completed.
省电应用在获得内存管理服务的内存释放完毕的通知后,再执行步骤S09,即向冻结器下发进程冻结通知。After the power-saving application obtains the memory release notification from the memory management service, step S09 is executed, that is, a process freezing notification is sent to the freezer.
需要说明的,图6为一个待冻结进程的示例。当存在多个待冻结进程时,每个待冻结进程的函数库都会向内存管理服务通知内存释放完毕,内存管理服务可以在获得每一个待冻结进程的内存释放完毕的通知之后,再执行步骤A10。It should be noted that Figure 6 is an example of a process to be frozen. When there are multiple processes to be frozen, the function library of each process to be frozen will notify the memory management service of the completion of memory release. The memory management service can perform step A10 after obtaining the notification of the completion of memory release of each process to be frozen. .
本实施例的有益效果在于:The beneficial effects of this embodiment are:
进程被冻结后,系统只能通过内存压缩的方式回收该进程的内存分配器持有的部分内存,并且内存压缩会占用swap分区的空间。本方案中,省电应用在收到内存释放完毕的通知后再冻结进程,可以确保待冻结进程在内存分配器尽可能地释放完内存之后才被冻结,从而避免被冻结的进程占用电子设备的内存资源。After a process is frozen, the system can only reclaim part of the memory held by the memory allocator of the process through memory compression, and memory compression will occupy the space of the swap partition. In this solution, the power-saving application freezes the process after receiving the notification that the memory is released. This can ensure that the process to be frozen is not frozen until the memory allocator releases as much memory as possible, thereby preventing the frozen process from occupying the electronic device's memory. memory resources.
根据图5和图6所示的释放内存的方法的时序图,可以得到一种释放内存的方法,请参见图7,为本申请提供的一种释放内存的方法的流程图,该方法可以包括如下步骤:According to the timing diagram of the method of releasing memory shown in Figure 5 and Figure 6, a method of releasing memory can be obtained. Please refer to Figure 7, which is a flow chart of a method of releasing memory provided by this application. The method may include Follow these steps:
在一些可选的实施例中,如图1所示的电子设备可以执行预先存储的计算机指令,从而实现下述流程中的步骤。In some optional embodiments, the electronic device shown in Figure 1 can execute pre-stored computer instructions to implement the steps in the following process.
S701,函数库新增内存回收信号量和内存释放信号处理函数。S701, the function library adds memory recycling semaphore and memory release signal processing functions.
步骤S701由电子设备在开机后执行,内存回收信号量和内存释放信号处理函数的定义及其作用参见图5对应的实施例中的说明,此处不再赘述。Step S701 is executed by the electronic device after it is powered on. The definitions and functions of the memory recycling semaphore and the memory release signal processing function are as described in the corresponding embodiment in Figure 5 and will not be described again here.
S702,响应用户的放后台操作,应用从前台放到后台。S702: In response to the user's background operation, the application is moved from the foreground to the background.
用户的放后台操作,是指用户所执行的会触发当前在前台运行的应用切换到后台的操作,示例性的,放后台操作可以是返回桌面的操作,还可以是用户从当前在前台运行的应用直接切换到另一个应用,例如点击另一个应用弹出的消息的操作。The user's background operation refers to the operation performed by the user that triggers the application currently running in the foreground to switch to the background. For example, the background operation can be an operation to return to the desktop, or it can be an operation for the user to switch from the application currently running in the foreground to the background. The application directly switches to another application, such as clicking on a message that pops up from another application.
步骤S702的具体实施过程可以参见图5对应的实施例中的S01。For the specific implementation process of step S702, please refer to S01 in the corresponding embodiment of FIG. 5 .
S703,省电应用获得应用放后台的通知。S703: The power-saving application receives a notification that the application is placed in the background.
省电应用可以通过预先订阅或者放后台的应用主动推送的方式获得上述通知,通过应用放后台的通知,省电应用可以知道具体是哪个应用被从前台切换到后台。Power-saving applications can obtain the above notifications through pre-subscription or active push from applications placed in the background. Through the notification of applications placed in the background, power-saving applications can know which application has been switched from the foreground to the background.
步骤S703的具体实施方式可以参见图5对应的实施例中的S02。For the specific implementation of step S703, please refer to S02 in the corresponding embodiment of FIG. 5 .
S704,判断放后台的应用是否有前台可感知功能。S704: Determine whether the application placed in the background has foreground-aware functions.
若放后台的应用没有前台可感知功能,执行步骤S705,若放后台的应用有前台可感知功能,结束本流程。If the application placed in the background does not have a foreground-aware function, step S705 is executed. If the application placed in the background has a foreground-aware function, this process ends.
例如,音乐应用在后台仍然可以播放音乐,导航应用在后台仍然可以播报导航信息,播放音乐和播报导航信息就属于前台可感知功能。For example, music applications can still play music in the background, and navigation applications can still broadcast navigation information in the background. Playing music and broadcasting navigation information are foreground-perceptible functions.
如前所述,步骤S704为可选的步骤。在一些场景中,电子设备可以在应用放后台时直接冻结该应用,而不论该应用是否有前台可感知功能,此时就可以不执行步骤S704。As mentioned above, step S704 is an optional step. In some scenarios, the electronic device can directly freeze the application when the application is placed in the background, regardless of whether the application has foreground-aware functions. In this case, step S704 does not need to be performed.
步骤S704的具体实施方式可以参见图5对应的实施例中的S03。For the specific implementation of step S704, please refer to S03 in the corresponding embodiment of FIG. 5 .
S705,发送预冻结通知给内存管理服务。S705: Send a pre-freeze notification to the memory management service.
预冻结通知携带有待冻结进程的进程标识,预冻结通知用于向内存管理服务说明对应的进程将被冻结。待冻结进程为放后台的应用对应的进程。例如,在S702中视频应用被从前台切到后台,则上述预冻结通知携带视频应用对应进程的进程标识。The pre-freeze notification carries the process identifier of the process to be frozen. The pre-freeze notification is used to explain to the memory management service that the corresponding process will be frozen. The process to be frozen is the process corresponding to the application placed in the background. For example, in S702, the video application is switched from the foreground to the background, then the above-mentioned pre-freeze notification carries the process identification of the corresponding process of the video application.
待冻结进程由省电应用根据放后台的应用的应用标识确定。The process to be frozen is determined by the power-saving application based on the application ID of the application placed in the background.
步骤S705的具体实施方式可以参见图5所示的实施例的步骤S04和S05。For the specific implementation of step S705, please refer to steps S04 and S05 of the embodiment shown in FIG. 5 .
S706,内存管理服务发送内存释放信号给待冻结进程。S706, the memory management service sends a memory release signal to the process to be frozen.
在S706中,内存管理服务将内存释放信号发送给预冻结通知携带的进程标识对应的进程,从而触发这些进程进行强制回收。In S706, the memory management service sends the memory release signal to the process corresponding to the process identifier carried in the pre-freeze notification, thereby triggering forced recycling of these processes.
例如,视频应用被放后台后,在S706中内存管理服务将内存释放信号发送给视频应用对应的进程。For example, after the video application is placed in the background, in S706, the memory management service sends a memory release signal to the process corresponding to the video application.
步骤S706的具体实现方式可以参见图5对应的实施例中的步骤S06。For the specific implementation of step S706, please refer to step S06 in the corresponding embodiment of FIG. 5 .
S707,待冻结进程执行强制回收。S707, the process to be frozen performs forced recycling.
待冻结进程收到内存释放信号后,待冻结进程的函数库可以响应内存释放信号,调用内存分配器的强制释放接口,从而执行强制回收。After the process to be frozen receives the memory release signal, the function library of the process to be frozen can respond to the memory release signal and call the forced release interface of the memory allocator to perform forced recycling.
步骤S707的具体实现方式可以参见图5对应的实施例的步骤S07和S08。For the specific implementation of step S707, please refer to steps S07 and S08 in the corresponding embodiment of FIG. 5 .
S708,待冻结进程被冻结。S708, the process to be frozen is frozen.
在步骤S708中,省电应用可以向内核的冻结器下发进程冻结通知,进程冻结通知携带有待冻结进程的进程标识,收到进程冻结通知后冻结器就可以将其中的进程标识对应的进程冻结。In step S708, the power-saving application can send a process freezing notification to the kernel freezer. The process freezing notification carries the process identification of the process to be frozen. After receiving the process freezing notification, the freezing device can freeze the process corresponding to the process identification. .
步骤S709的具体实现方式可以参见图5对应的实施例的步骤S09,或者参见图6对应的实施例的步骤A08至A10,以及步骤S09。For the specific implementation of step S709, refer to step S09 of the embodiment corresponding to FIG. 5 , or refer to steps A08 to A10 and step S09 of the embodiment corresponding to FIG. 6 .
上述实施例所具有的有益效果可以参见图5和图6所示的实施例,不再赘述The beneficial effects of the above embodiments can be referred to the embodiments shown in Figures 5 and 6, and will not be described again.
根据图5和图6所示的释放内存的方法的时序图,以及图7所示的流程图,还可以得到如图8所示的释放内存的方法的流程图,该方法可以包括如下步骤:According to the timing diagrams of the method of releasing memory shown in Figures 5 and 6, and the flow chart shown in Figure 7, a flow chart of the method of releasing memory shown in Figure 8 can also be obtained. The method can include the following steps:
S801,应用放后台时,将应用对应的进程确定为待冻结进程。S801: When the application is placed in the background, the process corresponding to the application is determined as the process to be frozen.
在步骤S801中,电子设备可以响应于用户执行的放后台操作,将当前在前台运行的应用放到后台,然后根据被放后台的应用的应用标识找到该应用对应的进程,从而确定待冻结进程。In step S801, the electronic device can respond to the background operation performed by the user, put the application currently running in the foreground into the background, and then find the process corresponding to the application according to the application identifier of the application being placed in the background, thereby determining the process to be frozen .
在一些可选的实施例中,电子设备在执行步骤S801前可以先判断被放后台的应用是否有前台可感知功能,如果有前台可感知功能则不执行S801,并结束本方法,如果没有前台可感知功能,则执行步骤S801。In some optional embodiments, before executing step S801, the electronic device may first determine whether the application placed in the background has a foreground-aware function. If it has a foreground-aware function, S801 will not be executed and the method will end. If there is no foreground-aware function, the electronic device will not execute step S801. If the function can be sensed, step S801 is executed.
步骤S801的具体过程可以参见图5所示的步骤S01至S04。For the specific process of step S801, please refer to steps S01 to S04 shown in FIG. 5 .
S802,通过强制回收的方式释放待冻结进程占用的内存。S802: Release the memory occupied by the process to be frozen through forced recycling.
在步骤S802中,电子设备可以调用待冻结进程的内存分配器的强制释放接口,使得待冻结进程通过强制回收的方式释放占用的内存。In step S802, the electronic device may call the forced release interface of the memory allocator of the process to be frozen, so that the process to be frozen releases the occupied memory through forced recycling.
步骤S802的具体过程可以参见图5所示的步骤S05至S08。For the specific process of step S802, please refer to steps S05 to S08 shown in FIG. 5 .
S803,冻结待冻结进程。S803, freeze the process to be frozen.
在步骤S803中,电子设备可以在内存释放完后,利用内核层的冻结器将待冻结进程冻结。In step S803, after the memory is released, the electronic device can use the freezer of the kernel layer to freeze the process to be frozen.
步骤S803的具体过程可以参见图5所示的步骤S09,以及图6所示的步骤A08至A10。For the specific process of step S803, please refer to step S09 shown in FIG. 5 and steps A08 to A10 shown in FIG. 6 .
上述释放内存的方法所具有的有益效果可以参见图5和图6所示的实施例,不再赘述。The beneficial effects of the above method of releasing memory can be seen in the embodiments shown in FIG. 5 and FIG. 6 and will not be described again.
本申请实施例提供一种电子设备,包括存储器和一个或多个处理器。An embodiment of the present application provides an electronic device, including a memory and one or more processors.
存储器用于存储计算机程序。Memory is used to store computer programs.
一个或多个处理器用于执行计算机程序,具体用于实现本申请任一实施例所提供的释放内存的方法。One or more processors are used to execute computer programs, specifically to implement the method for releasing memory provided in any embodiment of this application.
本申请实施例还提供一种计算机存储介质,用于存储计算机程序,该计算机程序被执行时,具体用于实现本申请任一实施例所提供的释放内存的方法。An embodiment of the present application also provides a computer storage medium for storing a computer program. When the computer program is executed, it is specifically used to implement the method of releasing memory provided by any embodiment of the present application.
在本说明书中描述的参考“一个实施例”或“一些实施例”等意味着在本申请的一个或多个实施例中包括结合该实施例描述的特定特征、结构或特点。由此,在本说明书中的不同之处出现的语句“在一个实施例中”、“在一些实施例中”、“在其他一些实施例中”、“在另外一些实施例中”等不是必然都参考相同的实施例,而是意味着“一个或多个但不是所有的实施例”,除非是以其他方式另外特别强调。术语“包括”、“包含”、“具有”及它们的变形都意味着“包括但不限于”,除非是以其他方式另外特别强调。Reference in this specification to "one embodiment" or "some embodiments" or the like means that a particular feature, structure or characteristic described in connection with the embodiment is included in one or more embodiments of the application. Therefore, the phrases "in one embodiment", "in some embodiments", "in other embodiments", "in other embodiments", etc. appearing in different places in this specification are not necessarily References are made to the same embodiment, but rather to "one or more but not all embodiments" unless specifically stated otherwise. The terms "including," "includes," "having," and variations thereof all mean "including but not limited to," unless otherwise specifically emphasized.
本申请实施例涉及的多个,是指大于或等于两个。需要说明的是,在本申请实施例的描述中,“第一”、“第二”等词汇,仅用于区分描述的目的,而不能理解为指示或暗示相对重要性,也不能理解为指示或暗示顺序。The plurality involved in the embodiments of this application refers to more than or equal to two. It should be noted that in the description of the embodiments of this application, words such as "first" and "second" are only used for the purpose of distinguishing the description, and cannot be understood as indicating or implying relative importance, nor can they be understood as indicating. Or suggestive order.
Claims (7)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210161930.XA CN115525404B (en) | 2022-02-22 | 2022-02-22 | Methods, equipment and storage media to release memory |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210161930.XA CN115525404B (en) | 2022-02-22 | 2022-02-22 | Methods, equipment and storage media to release memory |
Publications (2)
Publication Number | Publication Date |
---|---|
CN115525404A CN115525404A (en) | 2022-12-27 |
CN115525404B true CN115525404B (en) | 2023-09-19 |
Family
ID=84693989
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202210161930.XA Active CN115525404B (en) | 2022-02-22 | 2022-02-22 | Methods, equipment and storage media to release memory |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN115525404B (en) |
Citations (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104252389A (en) * | 2013-06-27 | 2014-12-31 | 腾讯科技(深圳)有限公司 | Application operation method, system and application |
CN104375828A (en) * | 2014-10-27 | 2015-02-25 | 小米科技有限责任公司 | Method and device for optimizing memory |
CN106648865A (en) * | 2016-12-15 | 2017-05-10 | 北京奇虎科技有限公司 | Intelligent terminal, game operating environment optimizing method and system |
CN108287760A (en) * | 2017-08-08 | 2018-07-17 | 珠海市魅族科技有限公司 | Terminal equipment control method and device, terminal device and computer readable storage medium |
CN110018905A (en) * | 2018-01-10 | 2019-07-16 | 广东欧珀移动通信有限公司 | Information processing method, device, computer equipment and computer readable storage medium |
CN112433831A (en) * | 2020-11-17 | 2021-03-02 | 珠海格力电器股份有限公司 | Application freezing method, storage medium and electronic device |
-
2022
- 2022-02-22 CN CN202210161930.XA patent/CN115525404B/en active Active
Patent Citations (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104252389A (en) * | 2013-06-27 | 2014-12-31 | 腾讯科技(深圳)有限公司 | Application operation method, system and application |
CN104375828A (en) * | 2014-10-27 | 2015-02-25 | 小米科技有限责任公司 | Method and device for optimizing memory |
CN106648865A (en) * | 2016-12-15 | 2017-05-10 | 北京奇虎科技有限公司 | Intelligent terminal, game operating environment optimizing method and system |
CN108287760A (en) * | 2017-08-08 | 2018-07-17 | 珠海市魅族科技有限公司 | Terminal equipment control method and device, terminal device and computer readable storage medium |
CN110018905A (en) * | 2018-01-10 | 2019-07-16 | 广东欧珀移动通信有限公司 | Information processing method, device, computer equipment and computer readable storage medium |
CN112433831A (en) * | 2020-11-17 | 2021-03-02 | 珠海格力电器股份有限公司 | Application freezing method, storage medium and electronic device |
Also Published As
Publication number | Publication date |
---|---|
CN115525404A (en) | 2022-12-27 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
KR102148948B1 (en) | Multi tasking method of electronic apparatus and electronic apparatus thereof | |
WO2018032782A1 (en) | Method and device for starting application | |
WO2022257748A1 (en) | Virtual memory management method and electronic device | |
CN116560771A (en) | Method for executing drawing operation by application and electronic equipment | |
EP4328705A1 (en) | Storage space arrangement method and electronic device | |
CN114020652B (en) | Application program management method and electronic equipment | |
CN114185494A (en) | Memory anonymous page processing method, electronic device and readable storage medium | |
CN115858046B (en) | Method, electronic device and chip system for preloading memory pages | |
CN115292199B (en) | Video memory leakage processing method and related device | |
WO2023202429A1 (en) | Garbage recycling method and electronic device | |
CN117130541A (en) | Storage space configuration method and related equipment | |
CN115525404B (en) | Methods, equipment and storage media to release memory | |
CN112783418B (en) | Method for storing application program data and mobile terminal | |
CN117707639B (en) | Application start acceleration method, electronic device and storage medium | |
CN116916093B (en) | Methods, electronic equipment and storage media to identify lagging | |
WO2024027544A1 (en) | Memory management method and electronic device | |
WO2023185684A1 (en) | Process killing method for application, and electronic device | |
EP4513334A1 (en) | Memory management method and electronic device | |
CN117076089B (en) | Application management method, terminal device and storage medium | |
CN116382809B (en) | Application program starting method and electronic equipment | |
CN116844196B (en) | Dynamic effect picture display method and electronic equipment | |
CN118885098B (en) | Camera control method, electronic device, chip system and readable storage medium | |
CN116049113B (en) | File system organization method, electronic device and computer-readable storage medium | |
CN118466799A (en) | Application detection method, terminal equipment and storage media | |
WO2024032430A1 (en) | Memory management method and electronic device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant | ||
CP03 | Change of name, title or address |
Address after: Unit 3401, unit a, building 6, Shenye Zhongcheng, No. 8089, Hongli West Road, Donghai community, Xiangmihu street, Futian District, Shenzhen, Guangdong 518040 Patentee after: Honor Terminal Co.,Ltd. Country or region after: China Address before: 3401, unit a, building 6, Shenye Zhongcheng, No. 8089, Hongli West Road, Donghai community, Xiangmihu street, Futian District, Shenzhen, Guangdong Patentee before: Honor Device Co.,Ltd. Country or region before: China |