Patent Description:
Each application program of an Android operating system runs on an independent virtual machine in the Android system, Android Runtime (ART) instance. In a running process of an application program, first, an oat file is loaded into a memory for executing machine code in the oat file. When a method that needs to be invoked is not included in the oat file, the ART loads, into the memory (RAM, Random Access Memory), a dex file corresponding to the method, and interprets and executes a bytecode in the dex file by using an interpreter (interpreter of the ART). When a quantity of times of invoking the method reaches a threshold, method information of the method is recorded to a profile file. When a condition is satisfied (for example, in a standby state with a lock screen and of being charged for four hours), the ART performs AOT (ahead-of-time compilation technology of an Android Java program, Ahead Of Time) compilation on the dex file of the method corresponding to the method information recorded in the profile file, and updates the oat file. This can accelerate starting and running of the program next time.

As can be learned from the foregoing procedure, a code segment of the program mainly includes the machine code obtained through interpretation or compilation. As the program runs, the ART continuously loads, into the memory in a file mapping manner, code in the dex file corresponding to the method to be invoked by the program and code in the oat file. Therefore, memory required by the program also increases continuously. Consequently, as more programs run simultaneously, more memory is consumed, and available memory of the system becomes less.

To reduce consumption of memory of the code segment of the application program, a new important feature Dexlayout is released in an Android O version. When the ART performs AOT compilation on the dex file in a speed-profile form, the ART optimizes a layout of the dex file based on information in the profile file, and a new dex file is generated after the layout optimization, where a layout of classes and methods is changed, and hot classes and methods are centralized at a front part, but cold classes and methods are centralized at an end part. In other words, hot content and cold content in the file are processed differently. The Dexlayout centralizes hot spot code. Less code that is infrequently used in the new dex file is mapped to the memory, and occupation of memory by the dex file is optimized.

As can be learned from above, the method information recorded in the profile file exerts great impact on the AOT compilation, but a method for recording the method information to the profile file in the prior art needs to be further optimized. <NPL>] describes help topics about JProfiler. <CIT> describes a method for selecting active or hot, code traces in an executing program for storage in a code cache. <NPL>), describes a method for compiler optimizations. <CIT> describes a method for computing placement costs. <NPL>, describes the architecture of the Jalapeño Adaptive Optimization System, a system to support leading-edge virtual machine technology and enable ongoing research on online feedback-directed optimizations. The extensible system architecture is described, based on a federation of threads with asynchronous communication. An implementation is presented of the general architecture that supports adaptive multi-level optimization based purely on statistical sampling. It is empirically demonstrated that this profiling technique has low overhead and can improve startup and steady-state performance, even without the presence of online feedback-directed optimizations. The paper also describes and evaluates an online feedback-directed inlining optimization based on statistical edge sampling. The system is written completely in Java, applying the described techniques not only to application code and standard libraries, but also to the virtual machine itself. <NPL>" discusses the runtime environment of Android which is based on its own Java Virtual Machine( JVM) called Dalvik Virtual Machine (DVM) which is said to be having overhead of interpreting every bytecode to machine code during runtime just like other JVMs. There are already well known techniques to overcome runtime overhead of interpreting and Google has adapted one of them which is Just-In-Time Compiler (JITC) since Android <NUM>, Froyo. Google chose trace based JITC to JIT compile only the hottest of hot code by using lazy fashion with counting strategy to minimize memory usage, thus applying various optimization techniques during JIT compilation to generate more efficient machine code is limited. In order to minimize runtime interpreting and compiling overhead, a selective Ahead-Of-Time Compiler (AOTC) which generates machine code at static compile time with abundant optimization techniques for the selected hot methods by profiling is discussed. The experimented results show that AOT compiling hot methods at static compile time and letting the others to aim the benefit of runtime JITC gives <NUM>% of performance upgrade in average on ARMll <NUM> target environment.

In view of the existing technical problem in the prior art, this application provides a method for collecting information.

According to a first aspect, this application provides a method for collecting information as set out in claim <NUM>. Both the first threshold and the second threshold are adjustable, and in an implementation, a threshold adjustment interface may be reserved for adjusting and setting the first threshold and the second threshold based on a user requirement or an application scenario. For a quantity of times of invoking a method, two different thresholds are set. When a quantity of recorded methods increases, the recorded methods can be classified, and information collection can be optimized.

Additionally, according to this method, the stored method information is classified when the method information stored in the profile file increases.

The memory that is occupied by the code of the cold method and needs to be released can be positioned accurately by using the start address and the end address of the hot method in the profile file; redundantly occupied memory can be released with ensured smooth running of a running program, and a speed of starting a new program can also be increased.

According to the method provided in this embodiment of this application, method information of methods that are invoked frequently can be collected, and in addition, methods corresponding to collected method information are classified. The collection of method information of methods is optimized, and more method information of invoked methods is collected without increasing a workload of compiling the method information into machine-recognizable code. In addition, a specific memory page that is occupied by the code of the cold method and needs to be released can be positioned. Therefore, the memory can be accurately released with ensured smooth running of the program, or the memory occupied by the code of the cold method (a method that is invoked relatively infrequently) is accurately released with maintained smooth running of the program.

According to a second aspect, this application provides a computer readable storage medium, where the computer readable storage medium stores a programmable instruction, and when the programmable instruction runs on a computer, the computer performs the method described in the first aspect.

According to a third aspect, this application provides a computer program product comprising computer program instructions that, when executed by a processing apparatus, cause the processing apparatus to perform the method according described in the first aspect.

According to a fourth aspect, the application provides an apparatus for collecting information, comprising: a processor; a transceiver; and a memory coupled to the processor and configured to store a plurality of instructions that, when executed, cause the processor to perform the method according to the first aspect with the transceiver.

To make the objective, technical solutions, and advantages of this application clearer, the following further describes the technical solutions of this application in detail with reference to the accompanying drawings.

A method for collecting information according to an embodiment of this application may be applied to various terminals. The terminal device includes but is not limited to a personal computer, a server computer, a handheld or laptop device, a mobile device (for example, a mobile phone, a mobile phone, a tablet computer, a PDA, or a media player), a consumer electronic device, an in-vehicle computer, a smartwatch, a television, another terminal device having a display screen, or the like.

The following describes specific implementation processes of the embodiments of the present invention in detail by using a mobile phone as an example. The following first briefly describes a specific structure of a mobile phone, as shown in <FIG>.

<FIG> is a schematic diagram of a hardware structure of a mobile phone applied to an embodiment of this application. As shown in <FIG>, the mobile phone <NUM> includes a display screen <NUM>, a processor <NUM>, and a memory <NUM>. The memory <NUM> may be configured to store a computer program and data. The processor <NUM> executes various function applications and data processing of the mobile phone <NUM> by running the computer program and data stored in the memory <NUM>. The memory <NUM> may mainly include a program storage area and a data storage area. The program storage area may store an operating system, an application program required by at least one function (such as an image playing function), or the like. The data storage area may store data (such as audio data, a phone book, or image data) that is created based on use of the mobile phone <NUM>, or the like. In addition, the memory <NUM> may include a high-speed random access memory, and may further include a non-volatile memory, for example, at least one magnetic disk storage device, a flash memory device, or another volatile solid-state memory device. The processor <NUM> is a control center of the mobile phone <NUM>, uses various interfaces and lines to connect all parts of the entire mobile phone, and executes various functions and data processing of the mobile phone <NUM> by running or executing the software program and/or data stored in the memory <NUM>, to perform overall monitoring on the mobile phone. The processor <NUM> may include one or more general purpose processors, or may further include one or more digital signal processors (digital signal processor, DSP), or may include one or more image signal processors (image signal processor, ISP), configured to perform related operations to implement the technical solution provided in this embodiment of this application.

The display screen <NUM> is configured to display information entered by a user or information provided for a user and various menu interfaces of the mobile phone <NUM>. Optionally, for the display screen <NUM>, the display screen <NUM> may be configured in a form such as a liquid crystal display (liquid crystal display, LCD) or an OLED (organic light-emitting diode, organic light-emitting diode).

The mobile phone <NUM> may further include a radio frequency (radio frequency, RF) circuit <NUM>. The RF circuit <NUM> may be configured to receive and transmit information or receive and transmit signals in a call process. Usually, the RF circuit <NUM> includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (low noise amplifier, LNA), a duplexer, or the like.

The mobile phone <NUM> may further include one or more sensors <NUM>, for example, an image sensor, an optical sensor, or a motion sensor.

The mobile phone <NUM> may further include an input/output interface <NUM>, configured to: receive entered digital information, character information, or a contact touch operation or a non-contact gesture, and generate signal input related to a user setting and function control of the mobile phone <NUM>, or the like. Specifically, the input/output interface <NUM> may include but is not limited to one or more of a physical keyboard, a function key (such as a volume control key or an on/off key), a touchscreen, or the like.

In addition, the mobile phone <NUM> further includes a power supply <NUM> (such as a battery) supplying power to each component. The power supply may be logically connected to the processor <NUM> by using a power management system, to implement functions such as charging and discharging management and power consumption management by using the power management system.

Certainly, depending on a requirement in a specific application, the mobile phone <NUM> may further include a camera, an audio circuit, a loudspeaker, or the like. Because the parts are not parts that are frequently used in this embodiment of this application, the parts are not shown in <FIG>, and are not described in detail.

In this embodiment of this application, the mobile phone <NUM> shown in <FIG> is used as an example for description. However, this embodiment of this application may be applied to other types of devices, and this is not limited. As shown in <FIG>, an embodiment of this application describes a method for collecting information. The method is applied to a mobile phone <NUM>, an operating system runs on the mobile phone <NUM>, and the operating system is not limited to an Android operating system or an IOS.

When a quantity of times of invoking a target method is updated, the mobile phone <NUM> obtains, based on a method identifier of the target method, the quantity of times of invoking the target method, where the target method is any one of at least one method invoked by a target program, the target program is any program running in the operating system of the mobile phone <NUM>, the quantity of times of invoking is a quantity of times of invoking the method by the target program in a running lifecycle of the target program, and the method identifier is used to indicate the method invoked by the target program and corresponding to the method identifier.

In this embodiment of this application, the mobile phone <NUM> may obtain, by using a processor <NUM>, the quantity of times of invoking the target method, and may further obtain, from a memory <NUM> of the mobile phone <NUM> by using the processor <NUM>, the quantity of times of invoking the target method. The memory <NUM> stores running data corresponding to the target program, and the quantity of times of invoking the target method is stored in the running data.

The mobile phone <NUM> records method information of the target method based on the quantity of times of invoking the target method, a first threshold, and a second threshold, and sets a method status of the target method, where the method status is used to indicate a status of the method invoked by the target program and corresponding to the method status, the method status is classified into a first state and a second state, the first state corresponds to the first threshold, the second state corresponds to the second threshold, and the second threshold is greater than <NUM> and less than the first threshold. Specifically, a status setting function is called to write the method identifier of the target method and the corresponding method status into a shared mapping, where the shared mapping is used to record a correspondence between the method identifier and the method status. Optionally, the first threshold and the second threshold may be adjustable thresholds. The first threshold and the second threshold may be adjusted based on an objective to be achieved.

In this embodiment of this application, after obtaining the quantity of times of invoking the target method, the mobile phone <NUM> may obtain the first threshold and the second threshold from the memory <NUM> by using the processor <NUM>, and record the method information of the target method based on the quantity of times of invoking the target method, the first threshold, and the second threshold. Specifically, the processor <NUM> records the method information of the target method based on the quantity of times of invoking the target method, the first threshold, and the second threshold, and sets the method status of the target method. More specifically, the processor <NUM> calls the status setting function from the memory <NUM> to write the method identifier of the target method and the corresponding method status into the shared mapping, to achieve an objective of setting the method status of the target method.

As shown in <FIG>, S200 specifically includes the following steps:.

In this embodiment of this application, before S200, the method further includes S300: The mobile phone <NUM> determines the method status of the target method based on the method identifier of the target method. Actually, a result of determining the method status of the target method may include three cases: The corresponding method status of the target method does not exist, the corresponding method status of the target method is the first state, and the corresponding method status of the target method is the second state.

In this embodiment of this application, specifically, the mobile phone <NUM> may determine, by using the processor <NUM>, the method status of the target method based on the method identifier of the target method, or may obtain the method identifier of the target method from the memory <NUM> by using the processor <NUM>, and determine the method status of the target method based on the method identifier of the target method.

In this embodiment of this application, if the result of determining, by the mobile phone <NUM>, the method status of the target method based on the method identifier of the target method is that the method status of the target method does not exist, the mobile phone <NUM> performs S200, to be specific, S201 to S204.

If the result of determining, by the mobile phone <NUM>, the method status of the target method based on the method identifier of the target method is that the method status of the target method is the second state, the mobile phone <NUM> performs the following steps:.

If the result of determining, by the mobile phone <NUM>, the method status of the target method based on the method identifier of the target method is that the method status of the target method is the first state, it indicates that the method information of the target method has been recorded and the method status of the target method does not need to be updated any longer. In other words, the mobile phone <NUM> does not need to perform a further specific step.

In this embodiment of this application, specifically, the mobile phone <NUM> may perform the foregoing step in cooperation with the processor <NUM> and the memory <NUM>.

Optionally, as shown in <FIG>, S200 specifically includes the following steps:.

In this embodiment of this application, before S200, the method further includes S600: The mobile phone <NUM> determines the method status of the target method based on the method identifier of the target method. A result of determining the method status of the target method may include three cases. The cases are not described herein again. For details, refer to corresponding descriptions of the embodiment corresponding to <FIG>.

In this embodiment of this application, if the result of determining, by the mobile phone <NUM>, the method status of the target method based on the method identifier of the target method is that the method status of the target method does not exist, the mobile phone <NUM> performs S200, to be specific, S210 to S240. If the result of determining, by the mobile phone <NUM>, the method status of the target method based on the method identifier of the target method is that the method status of the target method is the second state, the mobile phone <NUM> performs the following steps:.

It should be noted that, content of steps S300 to S500 in the embodiment corresponding to <FIG> may be the same as content of steps S600 to S800 in the embodiment corresponding to <FIG>, and steps S201 to S204 in the embodiment corresponding to <FIG> and steps S210 to S240 in the embodiment corresponding to <FIG> are steps of two possible implementation method steps of step S200.

In this embodiment of this application, as shown in <FIG>, S300 or S600 specifically includes the following steps.

The mobile phone <NUM> calls a status return function to obtain the method status of the target method from the shared mapping based on the method identifier of the target method.

In this embodiment of this application, the mobile phone <NUM> may call the status return function by using the processor <NUM>, to obtain the method status of the target method from the shared mapping based on the method identifier of the target method, or may call the status return function from the memory <NUM> by using the processor <NUM>, and obtain, based on the method identifier of the target method, the method status of the target method from the shared mapping stored in the memory <NUM>.

The mobile phone <NUM> determines the method status of the target method based on a result returned by the status return function. The result returned by the status return function includes three cases. The cases are not described herein again. For details, refer to corresponding descriptions of the embodiment corresponding to <FIG>. In actual implementation, the cases may be defined as follows: If the result returned by the status return function is null, it indicates that the method status does not exist; if the returned result is <NUM>, it indicates that the method status is the first state; or if the returned result is <NUM>, it indicates that the method status is the second state.

In this embodiment of this application, the mobile phone <NUM> may determine, by using the processor <NUM>, the method status of the target method based on the result returned by the status return function.

Optionally, the recording the method information of the target method in the foregoing embodiment is specifically: writing the method information of the target method into a profile file corresponding to the target program, where the profile file is a file used to store the running data of the target program, and the profile file corresponding to the target program includes information about at least one method invoked by the target program. In this embodiment of this application, based on the profile file corresponding to the target program, as shown in <FIG>, the method further includes the following steps.

The mobile phone <NUM> calls the status return function to obtain a method status of a second-class method from a shared mapping based on a method identifier of the second-class method, where the second-class method is used to indicate a method corresponding to the method information in the profile file corresponding to the target program.

In this embodiment of this application, the mobile phone <NUM> may call the status return function by using the processor <NUM>, to obtain the method status of the second-class method from the shared mapping based on the method identifier of the second-class method, or may call the status return function from the memory <NUM> by using the processor <NUM>, and obtain, based on the method identifier of the second-class method, the method status of the second-class method from the shared mapping stored in the memory <NUM>.

The mobile phone <NUM> determines that a method whose corresponding method status is the first state in the second-class method is a first-class method.

In this embodiment of this application, the mobile phone <NUM> may determine, by using the processor <NUM>, that the method whose corresponding method status is the first state in the second-class method is the first-class method.

The mobile phone <NUM> performs ahead-of-time compilation technology AOT compilation on method information of the obtained first-class method.

In this embodiment of this application, the mobile phone <NUM> may perform ahead-of-time compilation technology AOT compilation on the method information of the obtained first-class method by using the processor <NUM>.

According to the method for collecting information provided in this embodiment of this application, method information of methods that are invoked frequently can be collected, and in addition, methods corresponding to collected method information are classified. The collection of method information of methods is optimized, and more method information of invoked methods is collected without increasing a workload of compiling the method information into machine-recognizable code.

It can be learned from the background that, as a program runs, code of a method invoked by the program is continuously loaded into a memory (RAM, Random Access Memory). To ensure smooth running of the running program, for the method information collected based on the profile file, the embodiments of this application provide two methods for releasing memory, and the methods may be applied to various terminals. In the embodiments of this application, the mobile phone <NUM> shown in <FIG> is used as an example for description. However, the embodiments of this application may be applied to other types of devices, and this is not limited. Specifically, the memory <NUM> of the mobile phone <NUM> includes two types of specific storage media: RAM and ROM (Read Only Memory). The method for releasing memory according to the embodiments of this application is used to release memory of the mobile phone <NUM>, to be specific, to release the RAM of the mobile phone <NUM>.

As shown in <FIG>, an embodiment of this application provides a method for releasing memory, and the method includes the following steps.

A mobile phone <NUM> obtains a start address and an end address of a hot method in a profile file corresponding to a target program, where the hot method is used to indicate a method corresponding to method information in the profile file corresponding to the target program, the method information in the profile file may be obtained by using the method for collecting information according to the foregoing embodiment, or may be obtained by using an existing method in the prior art or a method that may emerge subsequently, and a specific implementation of collecting the method information in the profile file is not limited in this embodiment of this application. It should be noted that, if the method information in the profile file is obtained by using the method for collecting information according to the foregoing embodiment, the hot method may be a method corresponding to the method information in the profile file, or may be a method that corresponds to the method information in the profile file and whose method status is the first state. In this application, for example, the hot method is a method corresponding to the method information in the profile file. Correspondingly, if the hot method is a method corresponding to the method information in the profile file, a cold method is used to indicate a method that is invoked by the target program and whose corresponding method information is not written into the profile file; or if the hot method is a method that corresponds to the method information in the profile file and whose method status is the first state, the cold method is used to indicate a method that is invoked by the target program and whose corresponding method information is not written into the profile file and a method that corresponds to the method information in the profile file and whose method status is the second state.

Further, as shown in <FIG>, S2100 specifically includes the following steps.

The mobile phone <NUM> obtains a method identifier of the hot method.

The mobile phone <NUM> obtains the start address of the hot method based on the method identifier of the hot method.

The mobile phone <NUM> obtains a header of the hot method based on the start address of the hot method.

The mobile phone <NUM> calculates, based on the header of the hot method, a size of memory occupied by code of the hot method.

The mobile phone <NUM> obtains the end address of the hot method based on the size of the memory occupied by the code of the hot method and the start address of the hot method.

In this embodiment of this application, the mobile phone <NUM> may perform, by using a processor <NUM>, S2110 to S2150 to obtain the start address and the end address of the hot method in the profile file corresponding to the target program.

The mobile phone <NUM> obtains a start address and an end address of a cold method based on the start address and the end address of the hot method, where the cold method is used to indicate a method that is invoked by the target program and whose corresponding method information is not written into the profile file.

In this embodiment of this application, the mobile phone <NUM> may obtain, by using the processor <NUM>, the start address and the end address of the cold method based on the start address and the end address of the hot method.

The mobile phone <NUM> releases, based on the start address and the end address of the cold method, memory occupied by code of the cold method.

In this embodiment of this application, the mobile phone <NUM> may release, based on the start address and the end address of the cold method by using the processor <NUM>, the memory occupied by the code of the cold method.

Further, in this embodiment of this application, after S2110, the mobile phone <NUM> stores the method identifier of the hot method in a first set.

In addition, S2120 is specifically: the mobile phone <NUM> traverses the first set to obtain the start address of the hot method based on the method identifier of the hot method, and stores the start address of the hot method in a second set based on a value of the start address.

After S2150, the mobile phone <NUM> stores the end address of the hot method in a third set based on a value of the end address.

S2200 specifically includes: obtaining the start address and the end address of the cold method based on the second set and the third set.

Because a code segment stored in a memory includes only hot methods and cold methods, a memory area between hot methods is memory occupied by a cold method, as shown in a schematic diagram of the start address and the end address of the cold method in <FIG>. In this embodiment of this application, the second set stores start addresses of the hot methods, and the third set stores end addresses of the hot methods. Specifically, the mobile phone <NUM> obtains an end address of a hot method <NUM> from the third set by using the processor <NUM>, and obtains a start address of a hot method <NUM> from the second set. A cold method area <NUM> may be obtained based on the end address of the hot method <NUM> and the start address of the hot method <NUM>. The cold method area <NUM> may include one or more cold methods. A plurality of cold method areas may be obtained in this way, to be specific, memory occupied by code of the cold methods.

Actually, the memory uses a page as a unit. Further, S2300 specifically includes: The mobile phone <NUM> calculates, based on the start address and the end address of the cold method, a size of the memory occupied by the code of the cold method; the mobile phone <NUM> determines a cold method memory page based on a size of a single memory page, the size of the memory occupied by the code of the cold method, and the start address and the end address of the cold method, where the cold method memory page is a memory page on which code segment memory is fully occupied by the code of the cold method; and the mobile phone <NUM> releases the cold method memory page.

According to the method for releasing memory provided in this embodiment of this application, a specific memory page that is occupied by the code of the cold method and needs to be released can be positioned. Therefore, the memory can be accurately released with ensured smooth running of the program.

A mobile phone <NUM> obtains a start address and an end address of a hot method, where the hot method is used to indicate a method corresponding to method information in the profile file or a method that corresponds to method information in the profile file and whose method status is the first state.

In this embodiment of this application, the mobile phone <NUM> may obtain the start address and the end address of the hot method by using a processor <NUM>.

The mobile phone <NUM> releases, based on a size of memory occupied by code of the hot method and the start address and the end address of the hot method, memory occupied by a code segment of a method invoked by the target program.

In this embodiment of this application, the mobile phone <NUM> may release, based on the size of the memory occupied by the code of the hot method and the start address and the end address of the hot method by using the processor <NUM>, the memory occupied by the code segment of the method invoked by the target program.

Optionally, before S3200, the method further includes S3100: The mobile phone <NUM> advises releasing memory occupied by code of the method invoked by the target program. Specifically, the mobile phone <NUM> calls a first function to advise releasing, in memory of an operating system, the memory occupied by the code of the method invoked by the target program. For example, in an installed Android operating system, releasing memory is advised, and the memory is not released directly. A method may be marked through access, and memory occupied by code of the marked method is not be released.

Further, as shown in <FIG>, S3200 specifically includes the following steps:.

In this embodiment of this application, the mobile phone <NUM> may perform, by using the processor <NUM>, S3210 to S3250 to obtain the start address and the end address of the hot method in the profile file corresponding to the target program.

Further, as shown in <FIG>, S3300 specifically includes the following steps.

The mobile phone <NUM> determines, based on the size of the memory occupied by the code of the hot method and a size of a single memory page, whether page spanning exists in the hot method, where the page spanning is used to indicate that code of a method or a method area of a same class occupies code segment memory of at least two consecutive memory pages.

If page spanning exists in the hot method, the mobile phone <NUM> accesses the start address, an intermediate address, and the end address of the hot method, and releases a memory page that is not accessed and that is occupied by a method invoked by the target program, where the intermediate address is used to indicate an address at a distance of N single memory pages from the start address in the same method, and N is a positive integer.

As shown in <FIG>, a size of a single memory page is <NUM> KB. In the figure, code of a hot method occupies four pages; in this case, intermediate addresses are: start address + <NUM> KB, and start address + <NUM> KB.

If page spanning does not exist in the hot method, the mobile phone <NUM> accesses the start address and/or the end address of the hot method, and releases a memory page that is not accessed and that is occupied by a method invoked by the target program.

In this embodiment of this application, the mobile phone <NUM> may advise, by using the processor <NUM>, releasing the memory occupied by the code of the method invoked by the target program. Specifically, the mobile phone <NUM> may call, by using the processor <NUM>, the first function from the memory <NUM> to advise releasing, in the memory of the operating system, the memory occupied by the code of the method invoked by the target program.

In actual implementation, for example, in an installed Android operating system, S3300 may be implemented by using the following method: first, calculating a size of memory occupied by the method on a memory page in which the start address is located (first_size = page_size - method_size%page_size), then calculating a quantity of memory pages spanned by the method (number = (method_size - first_size)/page_size + <NUM>); and if the quantity of spanned memory pages is equal to <NUM>, accessing the start address and the end address of the method; or if the quantity of spanned memory pages is greater than <NUM>, accessing the start address, the end address, and the intermediate address, where an intermediate address on a page with a current sequence number (cur_num) is (method_start + page_size × cur_num).

In this embodiment of this application, if the profile file is obtained by using the method for collecting information according to the foregoing embodiment, the method further includes: The mobile phone <NUM> performs ahead-of-time compilation technology AOT compilation on the method information of the method whose method status is the first state in the profile file.

According to the method for releasing memory provided in this embodiment of this application, memory occupied by code of a cold method (a method that is invoked relatively infrequently) can be accurately released with maintained smooth running of the program.

It should be noted that, optionally, the method for releasing memory or the step of releasing memory in the foregoing embodiment is triggered when a condition is satisfied. For example, when memory usage reaches a threshold, the step and method for releasing memory in the foregoing embodiment are triggered; or the step or method for releasing memory in the foregoing embodiment may be triggered at a preset time interval. This is not specifically limited in this application.

Based on the foregoing embodiment, an embodiment of this application further provides a terminal device <NUM>. The terminal device <NUM> is configured to implement the method for collecting information according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG>. Referring to <FIG>, the terminal device <NUM> may include a memory <NUM>, a processor <NUM>, a display screen <NUM>, and an input/output interface <NUM>.

The memory <NUM> is configured to store a computer program executed by the processor <NUM>. The memory <NUM> may mainly include a program storage area and a data storage area. The program storage area may store an operating system, an application program required by at least one function, or the like. The data storage area may store data that is created based on use of the terminal device <NUM>, or the like. The processor <NUM> may be a central processing unit (central processing unit, CPU), a digital processing unit, or the like. The display screen <NUM> is configured to display information entered by a user or information provided for a user. Optionally, for the display screen <NUM>, the display screen <NUM> may be configured in a form such as a liquid crystal display (liquid crystal display, LCD) or an OLED (organic light-emitting diode, organic light-emitting diode).

A specific connection medium between the memory <NUM>, the processor <NUM>, the display screen <NUM>, and the input/output interface <NUM> is not limited in this embodiment of this application. In this embodiment of this application, the memory <NUM>, the processor <NUM>, the display screen <NUM>, and the input/output interface <NUM> are connected by using a bus <NUM> in <FIG>. In <FIG>, the bus <NUM> is represented by a thick line, and connection modes of other components are merely used as an example for description, and are not limited. The bus <NUM> may be classified into an address bus, a data bus, a control bus, and the like. For ease of representation, only one thick line is used to represent the bus in <FIG>, but this does not mean that there is only one bus or only one type of bus.

The memory <NUM> may be a volatile memory (volatile memory), for example, a random access memory (random access memory, RAM); or the memory <NUM> may be a non-volatile memory (non-volatile memory), for example, a read only memory, a flash memory (flash memory), a hard disk (hard disk, HDD), or a solid-state drive (solid-state drive, SSD); or the memory <NUM> is any other medium that can be configured to carry or store expected program code in a form of an instruction or a data structure and can be accessed by a computer, but is not limited thereto. The memory <NUM> may be a combination of the foregoing memories.

The processor <NUM> is configured to invoke the computer program stored in the memory <NUM> to perform the method for collecting information according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG>. For specific method steps, refer to descriptions of corresponding embodiments.

An embodiment of this application further provides an apparatus <NUM> for collecting information, and the apparatus is configured to implement the method described in the embodiment corresponding to <FIG>. For briefly describing corresponding functions of modules of the apparatus and relationships between the modules, related concepts and explanations are not described again. For details, refer to the descriptions of the corresponding method embodiment. As shown in <FIG>, the apparatus <NUM> includes:.

Further, the apparatus <NUM> further includes: a first determining module <NUM>, configured to determine whether the quantity of times of invoking the target method is greater than the first threshold; and
a second determining module <NUM>, configured to: when the first determining module <NUM> determines that the quantity of times of invoking the target method is less than or equal to the first threshold, determine whether the quantity of times of invoking the target method is greater than the second threshold.

The recording module <NUM> is specifically configured to: when the first determining module <NUM> determines that the quantity of times of invoking the target method is greater than the first threshold and the second determining module <NUM> determines that the quantity of times of invoking the target method is greater than the second threshold, record the method information of the target method.

The status setting module <NUM> is specifically configured to: when the first determining module <NUM> determines that the quantity of times of invoking the target method is greater than the first threshold, set the method status of the target method to a first state, and when the second determining module <NUM> determines that the quantity of times of invoking the target method is greater than the second threshold, set the method status of the target method to a second state.

Further, the apparatus <NUM> further includes: a status determining module <NUM>, configured to determine the method status of the target method based on the method identifier of the target method.

Further, the apparatus <NUM> further includes: a fifth determining module <NUM>, configured to: when the status determining module <NUM> determines that the method status of the target method is the second state, determine whether the quantity of times of invoking the target method is greater than the first threshold.

The recording module <NUM> is specifically configured to: when the status determining module <NUM> determines that the method status of the target method does not exist, record the method information of the target method.

The status setting module <NUM> is specifically configured to: when the status determining module <NUM> determines that the method status of the target method does not exist, set the method status of the target method, and when the fifth determining module <NUM> determines that the quantity of times of invoking the target method is greater than the first threshold, set the method status of the target method from the second state to the first state.

Further, the status setting module <NUM> is specifically configured to call a status setting function to write the method identifier of the target method and the corresponding method status into a shared mapping.

Further, the status determining module <NUM> is specifically configured to: call a status return function to obtain the method status of the target method from a shared mapping based on the method identifier of the target method; and determine the method status of the target method based on a result returned by the status return function.

Further, the status setting module <NUM> is further specifically configured to call a status setting function to set the method status of the target method from the second state to the first state.

Further, the recording module <NUM> is specifically configured to write the method information of the target method into a profile file corresponding to the target program.

Optionally, as shown in <FIG>, the first determining module <NUM> of the apparatus <NUM> is replaced with a third determining module <NUM>, and the second determining module <NUM> is replaced with a fourth determining module <NUM>; or the first determining module <NUM>, the second determining module <NUM>, a third determining module <NUM>, and a fourth determining module <NUM> may coexist or may be same modules. In this embodiment of this application, for example, the first determining module <NUM> is replaced with the third determining module <NUM>, and the second determining module <NUM> is replaced with the fourth determining module <NUM>.

The third determining module <NUM> is configured to determine whether the quantity of times of invoking the target method is greater than the second threshold.

The fourth determining module <NUM> is configured to: when the third determining module <NUM> determines that the quantity of times of invoking the target method is greater than the second threshold, determine whether the quantity of times of invoking the target method is greater than the first threshold.

The recording module <NUM> is specifically configured to: when the third determining module <NUM> determines that the quantity of times of invoking the target method is greater than the second threshold, record the method information of the target method.

The status setting module <NUM> is specifically configured to: when the fourth determining module <NUM> determines that the quantity of times of invoking the target method is greater than the first threshold, set the method status of the target method to the first state, and when the fourth determining module <NUM> determines that the quantity of times of invoking the target method is less than or equal to the first threshold, set the method status of the target method to the second state.

It should be noted that, the third determining module <NUM> and the first determining module <NUM> may be a same module, and the fourth determining module <NUM> and the second determining module <NUM> may be a same module.

In this embodiment of this application, the apparatus <NUM> and the terminal device <NUM> are a same device, and each module of the apparatus <NUM> is specifically implemented by the processor <NUM> of the terminal device <NUM> in the embodiment corresponding to <FIG> by invoking the computer program stored in the memory <NUM>.

According to the apparatus for collecting information provided in this embodiment of this application, method information of methods that are invoked frequently can be collected, and in addition, methods corresponding to collected method information are classified. The collection of method information of methods is optimized, and more method information of invoked methods is collected without increasing a workload of compiling the method information into machine-recognizable code.

Based on the foregoing embodiment, an embodiment of this application provides an apparatus <NUM> for releasing memory, and the apparatus is configured to implement the method described in the embodiment corresponding to <FIG>. For briefly describing corresponding functions of modules of the apparatus and relationships between the modules, related concepts and explanations are not described again. For details, refer to the descriptions of the corresponding method embodiment. As shown in <FIG>, the apparatus <NUM> includes:.

Further, the first hot method address obtaining module <NUM> is specifically configured to: obtain a method identifier of the hot method; obtain the start address of the hot method based on the method identifier of the hot method; obtain a header of the hot method based on the start address of the hot method; calculate, based on the header of the hot method, a size of memory occupied by code of the hot method; and obtain the end address of the hot method based on the size of the memory occupied by the code of the hot method and the start address of the hot method.

Further, the first hot method address obtaining module <NUM> is further specifically configured to: after obtaining the method identifier of the hot method, store the method identifier of the hot method in a first set; traverse the first set to obtain the start address of the hot method based on the method identifier of the hot method, and store the start address of the hot method in a second set based on a value of the start address; and after obtaining the end address of the hot method based on the size of the memory occupied by the code of the hot method and the start address of the hot method, store the end address of the hot method in a third set based on a value of the end address.

Further, the cold method address calculation module <NUM> is specifically configured to calculate the start address and the end address of the cold method based on the second set and the third set that are obtained by the first hot method address obtaining module <NUM>.

Further, the apparatus <NUM> further includes: a cold method memory page module <NUM>, configured to: calculate, based on the start address and the end address of the cold method that are obtained by the cold method address calculation module <NUM>, a size of the memory occupied by the code of the cold method; and determine a cold method memory page based on a size of a single memory page, the size of the memory occupied by the code of the cold method, and the start address and the end address of the cold method.

The first memory releasing module <NUM> is specifically configured to release the cold method memory page obtained by the cold method memory page module <NUM>.

Optionally, if the profile file obtained by the apparatus <NUM> is obtained by using the terminal device <NUM> or the apparatus <NUM> described in the foregoing embodiment, for example, in an installed Android operating system, the apparatus <NUM> further includes: a compilation module, configured to perform ahead-of-time compilation technology AOT compilation on method information of a method whose method status is a first state in the profile file.

It should be noted that, the apparatus <NUM> and the apparatus <NUM> described in the foregoing embodiment may be different apparatuses, or may be integrated into a same apparatus.

According to the apparatus for releasing memory provided in this embodiment of this application, a specific memory page that is occupied by the code of the cold method and needs to be released can be positioned. Therefore, the memory can be accurately released with ensured smooth running of the program.

Based on the foregoing embodiment, an embodiment of this application further provides an apparatus <NUM> for releasing memory, and the apparatus is configured to implement the method described in the embodiment corresponding to <FIG>. For briefly describing corresponding functions of modules of the apparatus and relationships between the modules, related concepts and explanations are not described again. For details, refer to the descriptions of the corresponding method embodiment. As shown in <FIG>, the apparatus <NUM> includes:.

Optionally, the apparatus <NUM> further includes a release advising module <NUM>, configured to: before the second hot method address obtaining module <NUM> obtains the start address and the end address of the hot method, advise releasing memory.

Further, the second hot method address obtaining module <NUM> is specifically configured to: obtain a method identifier of the hot method; obtain the start address of the hot method based on the method identifier of the hot method; obtain a header of the hot method based on the start address of the hot method; calculate, based on the header of the hot method, the size of the memory occupied by the code of the hot method; and obtain the end address of the hot method based on the size of the memory occupied by the code of the hot method and the start address of the hot method.

Further, the apparatus <NUM> further includes: a page spanning determining module <NUM>, configured to determine, based on the size of the memory occupied by the code of the hot method and a size of a single memory page, whether page spanning exists in the hot method, where the page spanning is used to indicate that a method occupies code segment memory of at least two memory pages; and.

It should be noted that, the apparatus <NUM> and the apparatus <NUM> described in the foregoing embodiment may be different apparatuses, or may be integrated into a same apparatus. The apparatus <NUM> and the apparatus <NUM> may be integrated into a same apparatus.

According to the apparatus for releasing memory provided in this embodiment of this application, memory occupied by code of a cold method (a method that is invoked relatively infrequently) can be accurately released with maintained smooth running of the program.

Based on the foregoing embodiment, an embodiment of this application provides an operating system, the operating system includes a first threshold, a second threshold, and a method status, the first threshold and the second threshold are positive integers, the second threshold is greater than <NUM> and less than the first threshold, the method status is used to indicate a status of a method, the method status includes a first state and a second state, the first state is used to indicate a state in which a corresponding quantity of times of invoking the method is greater than the first threshold, the second state is used to indicate a state in which a corresponding quantity of times of invoking the method is greater than the second threshold and less than or equal to the first threshold, the quantity of times of invoking is a current quantity of times of invoking the method by a target program in a running lifecycle of the target program, and the target program is any program running in the operating system.

The operating system further includes a shared mapping, a status setting function, and a status return function, where the shared mapping is used to record a correspondence between a method identifier and the method status, and the method identifier is used to indicate the method corresponding to the method identifier; the status setting function is used to write a method identifier and a method status of a same method into the shared mapping or set a method status corresponding to a method identifier of a method in the shared mapping; and the status return function is used to return a corresponding method status based on a method identifier of a method and the shared mapping.

Further, when a quantity of times of invoking a method is greater than the first threshold, the status setting function is used to set a method status of the method to the first state; or when a quantity of times of invoking a method is greater than the second threshold and less than or equal to the first threshold, the status setting function is used to set a method status of the method to the second state.

The operating system provided in this embodiment of this application may implement the method for collecting information according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG>.

The following uses an Android operating system installed in a terminal (for example, the mobile phone <NUM> or the terminal device described in the foregoing embodiment) as an example. In the prior art, each application program of an Android operating system runs on an independent ART (a virtual machine in the Android system, Android Runtime) instance. In a running process of an application program, first, an oat file is loaded into a memory for executing machine code in the oat file. When a method that needs to be invoked is not included in the oat file, the ART loads, into the memory (RAM, Random Access Memory), a dex file corresponding to the method, and interprets and executes a bytecode in the dex file by using an interpreter of the ART. When a quantity of times of invoking the method reaches a threshold (referred to as a first threshold in the following embodiment), method information of the method is recorded to a profile file. When a condition is satisfied (for example, in a standby state with a lock screen and of being charged for four hours), the ART performs AOT (ahead-of-time compilation technology of an Android Java program, Ahead Of Time) compilation on the dex file of the method corresponding to the method information recorded in the profile file, and updates the oat file. This can accelerate starting and running of the program next time.

To enable the existing Android operating system to implement the method for collecting information according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG> and/or the method for releasing memory according to the embodiment corresponding to <FIG>, an embodiment of this application provides a method for improving an Android operating system. The method includes: adding a second threshold to a system attribute of the Android operating system, where the second threshold is greater than <NUM> and less than a first threshold in the Android operating system, and the first threshold and the second threshold are positive integers; adding a method status to a definition of a method class of an ART, where the method status is used to indicate a status of a method, the method status is a first state or a second state, the first state is used to indicate a state in which a quantity of times of invoking the method is greater than the first threshold, the second state is used to indicate a state in which a corresponding quantity of times of invoking the method is greater than the second threshold and less than or equal to the first threshold, and the ART is used to run a program of the Android operating system; and creating a shared mapping from a method identifier to the method status in the Android operating system, where the shared mapping is used to record a correspondence between the method identifier and the method status, and the method identifier is used to indicate the method;.

Claim 1:
A method for collecting information, comprising:
when a quantity of times of invoking a target method in a running lifecycle of a target program increases, obtaining (S100), based on a method identifier of the target method, the quantity of times of invoking the target method, wherein the target program is any running program, and the target method is any one of at least one method invoked by the target program; and
if the quantity of times of invoking the target method is greater than a first threshold, recording (S202) method information of the target method and setting a method status of the target method to a first state,
if the quantity of times of invoking the target method is greater than a second threshold and not greater than the first threshold, recording (S204) the method information of the target method and setting the method status of the target method to the second state, wherein the first threshold and the second threshold are positive integers, and the second threshold is greater than <NUM> and less than the first threshold,
wherein recording (S202, S204) the method information comprises writing the method information of the target method into a profile file corresponding to the target program;
obtaining (S2100) a start address and an end address of a hot method, wherein the hot method a method whose corresponding method information is written into the profile file and whose method status is the first state;
obtaining (S2200) a start address and an end address of a cold method based on the start address and the end address of the hot method, wherein the cold method is a method that is invoked by the target program, whose corresponding method information is not written into the profile file, or the cold method is a method that corresponds to the method information in the profile file and whose method status is the second state;
releasing (S2300), based on the start address and the end address of the cold method, memory occupied by code of the cold method; and
performing ahead-of-time, AOT, compilation on the method information of the hot method.