Garbage collection method and device for application program and electronic equipment
By obtaining GC security points, GC Map information and code type configuration, based on the LLVM compiler framework optimization, the performance problems caused by improper selection of security points in the garbage collection method are solved, and accurate garbage collection and compiled code quality improvement is achieved.
Patent Information
- Application Number
- CN202310837417.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-07
- Publication Date
- 2025-01-07
AI Technical Summary
When selecting a safe point in the existing garbage collection method, if too few, it will cause too long GC waiting time. If too many, it will affect performance and it will be difficult to accurately locate the objects that need to be recycled, affecting the quality of the compiled code.
By obtaining the GC security points, GC Map information and code type configuration of the pending application, it is optimized based on the LLVM compiler framework, insert relevant code and merge it to accurately locate the security points that need to be recycled, reducing unnecessary insertions.
Accurate garbage collection safe point positioning, reduce unnecessary insertion, and improve the quality and performance of compiled code.
Smart Images

Figure CN119271389A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and more specifically, to a garbage collection method, device, electronic device and storage medium for an application. Background Art
[0002] When garbage collection is needed during program execution, the program will only pause and start GC (garbage collection) at specific locations. These specific locations are called safepoints. The selection of safepoints is very important. If there are too few safepoints, GC may wait too long for user threads to execute; if there are too many, GC will be executed too frequently, affecting performance. In related technologies, the impact of premature insertion of GC safepoint codes on subsequent optimization can be reduced by delaying the insertion of GC safepoint related codes. However, current garbage collection methods need to be improved. Summary of the invention
[0003] The present application proposes a garbage collection method, device, electronic device and storage medium for an application program to improve the above-mentioned problem.
[0004] In a first aspect, an embodiment of the present application provides a garbage collection method for an application, which is applied to an electronic device, the method comprising: obtaining a GC safe point during the compilation of the application, the compiler of the application comprising an LLVM compiler framework; obtaining GC Map information corresponding to the GC safe point, the GC Map information being used to track objects to be recycled; obtaining a code type configuration of the GC safe point; obtaining a relevant code type of the GC safe point based on the code type configuration, and inserting GC safe point related code corresponding to the relevant code type into the GC safe point after optimizing the LLVM compiler framework; merging the GC safe point related code with the GC Map information so that the GC safe point related code corresponds to the GC Map information.
[0005] In a second aspect, an embodiment of the present application provides a garbage collection device for an application, which runs on an electronic device, and the device includes: a safety point acquisition module, which is used to acquire GC safety points during the compilation of the application, and the compiler of the application includes an LLVM compiler framework; a tracking information acquisition module, which is used to acquire GC Map information corresponding to the GC safety points, and the GC Map information is used to track objects to be recycled; a configuration information acquisition module, which is used to acquire the code type configuration of the GC safety points; a safety point insertion module, which is used to acquire the relevant code type of the GC safety point based on the code type configuration, and insert the GC safety point related code corresponding to the relevant code type into the GC safety point after optimizing the LLVM compiler framework; a merging module, which is used to merge the GC safety point related code with the GC Map information so that the GC safety point related code corresponds to the GC Map information.
[0006] In a third aspect, the present application provides an electronic device comprising one or more processors and a memory; one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the method of the first aspect above.
[0007] In a fourth aspect, the present application provides a computer-readable storage medium having program code stored therein, wherein the method of the first aspect is executed when the program code is run.
[0008] The present application provides a garbage collection method, device, electronic device and storage medium for an application. The method obtains the GC safe point of the application to be processed; obtains the GC Map information corresponding to the GC safe point, and the GC Map information is used to track the objects to be recycled; obtains the code type configuration of the GC safe point; obtains the relevant code type of the GC safe point based on the code type configuration, and inserts the GC safe point related code corresponding to the relevant code type into the GC safe point; merges the GC safe point related code with the GC Map information so that the GC safe point related code corresponds to the GC Map information. Thus, by the above method, by merging the GC safe point related code with the GC Map information used to track the objects to be recycled, it is possible to accurately locate the safe point that needs to be garbage collected, thereby reducing unnecessary insertions and improving the quality of compiled code. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0010] Figure 1 A flowchart of a garbage collection method for an application provided in an embodiment of the present application is shown.
[0011] Figure 2 A flowchart of a garbage collection method for an application provided in another embodiment of the present application is shown.
[0012] Figure 3 A flowchart of a garbage collection method for an application program provided in yet another embodiment of the present application is shown.
[0013] Figure 4 A structural block diagram of a garbage collection device for an application provided in an embodiment of the present application is shown.
[0014] Figure 5 A structural block diagram of an electronic device provided in an embodiment of the present application is shown.
[0015] Figure 6 A storage unit for storing or carrying program code for implementing a garbage collection method for an application according to an embodiment of the present application is shown. DETAILED DESCRIPTION
[0016] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0017] When garbage collection is needed during program execution, the program will only pause and start GC (garbage collection) at specific locations. These specific locations are called safepoints. The selection of safepoints is very important. If there are too few safepoints, GC may wait too long for user threads to execute; if there are too many, GC will be executed too frequently, affecting performance. In related technologies, the impact of premature insertion of GC safepoint codes on subsequent optimization can be reduced by delaying the insertion of GC safepoint related codes. However, current garbage collection methods need to be improved.
[0018] After long-term research, the inventors found that the GC safe point of the application to be processed can be obtained; the GC Map information corresponding to the GC safe point is obtained, and the GC Map information is used to track the objects to be recycled; the code type configuration of the GC safe point is obtained; based on the code type configuration, the relevant code type of the GC safe point is obtained, and the GC safe point related code corresponding to the relevant code type is inserted into the GC safe point; the GC safe point related code is merged with the GC Map information to make the GC safe point related code correspond to the GC Map information. Thus, by the above method, by merging the GC safe point related code with the GC Map information used to track the objects to be recycled, it is possible to accurately locate the safe point that needs to be garbage collected, thereby reducing unnecessary insertions and improving the quality of compiled code.
[0019] Therefore, in order to improve the above-mentioned problems, the inventors proposed a garbage collection method, device, electronic device and computer-readable storage medium for applications provided in the present application, which can merge the GC safe point related code with the GC Map information used to track the objects to be recycled, so as to accurately locate the safe points where garbage collection is required, thereby reducing unnecessary insertions and improving the quality of compiled code.
[0020] In order to facilitate a better understanding of the solution described in the embodiments of the present application, the following briefly describes the terms involved in the description of the embodiments of the present application:
[0021] ART (Android Runtime): represents the virtual machine that runs Android applications.
[0022] GC (Garbage Collection): represents memory garbage collection.
[0023] AOT (Ahead-of-time compilation): represents ahead-of-time compilation.
[0024] LLVM (Low Level Virtual Machine): represents the compiler framework.
[0025] SIMD (Single Instruction Multiple Data): stands for Single Instruction Multiple Data.
[0026] The embodiments of the present application will be described in detail below with reference to the accompanying drawings.
[0027] See also Figure 1, shows a flowchart of a garbage collection method for an application provided by an embodiment of the present application. This embodiment provides a garbage collection method for an application, which can be applied to an electronic device, which can be a mobile phone, a computer, a tablet or a wearable electronic device, and the type of specific electronic device is not limited. The method includes:
[0028] Step S110: in the process of compiling an application, obtaining a GC safe point, wherein the compiler of the application includes an LLVM compiler framework.
[0029] Among them, the application may include one or more threads. During the GC process, the thread running the application can be called a modifier. The modifier can modify the heap during the running of the application, and the thread that performs garbage collection is called a collector. The collector can analyze the usage of the heap and recycle objects in the heap that are no longer used.
[0030] The collector can analyze whether an object is alive by tracking the object. In the process of tracking the object, the mutator needs to be paused, that is, the application thread is paused. As a way, some code can be inserted into the application running in the mutator, and the mutator can be paused by polling in these codes. Specifically, the corresponding flag bit can be set in the inserted code so that the mutator can find that the flag bit is set when polling and stop. Among them, the polling point in the mutator can be understood as a safe point, and the main insertion function of the safe point is to do GC, so it can be called a GC safe point.
[0031] As a method, GC safe points can be obtained by inserting codes into the application program during the process of compiling the application program through the compiler. In this embodiment, the compiler of the application program introduces the LLVM compiler framework.
[0032] Step S120: Acquire GC Map information corresponding to the GC safe point, where the GC Map information is used to track objects to be recycled.
[0033] Objects in an application include live objects and garbage objects. During the GC process, after inserting relevant polling code at the GC safe point to pause the modifier, in order to perform garbage collection more accurately, the collector can track live objects and then recycle other garbage objects except live objects.
[0034] The collector can track live objects through roots. Specifically, all objects referenced or indirectly referenced by the root set are live objects, and other objects become garbage objects. The root set exists in the execution context of the application. The execution context includes stack frame data, registers, etc., where both objects and non-object data exist. The objects in these contexts are the root sets of object tracking, and the objects in the root set are alive.
[0035] As a way, GC Map information corresponding to the GC safe point can be obtained. The GC Map information is used to track the objects to be recycled. The GC Map information represents the map information of which locations in the context are objects, so that the objects to be garbage collected can be accurately determined in combination with the GC Map information.
[0036] Step S130: Obtain the code type configuration of the GC safety point.
[0037] In this implementation, a code type parameter may be configured in the polling code inserted into the GC safety point, so that the code type configuration of the GC safety point may be determined by reading the value of the code type parameter.
[0038] The code type configuration of the GC safety point may include a first type of code type configuration and a second type of code type configuration. In one embodiment, the first type may be an explicit type and the second type may be an implicit type. The explicit type of code type configuration includes a code that actively polls a flag bit, and the implicit type of code type configuration does not include a code that actively polls a flag bit.
[0039] Step S140: acquiring the relevant code type of the GC safe point based on the code type configuration, and inserting the GC safe point related code corresponding to the relevant code type into the GC safe point after optimizing the LLVM compiler framework.
[0040] As a way, when the value of the code type parameter is true, the explicitly typed related code can be used as the related code type of the GC safe point; when the value of the code type parameter is false, the implicitly typed related code can be used as the related code type of the GC safe point.
[0041] When the relevant code type of the GC safe point is obtained, the GC safe point relevant code corresponding to the relevant code type can be inserted into the GC safe point after optimizing the LLVM compiler framework. Optimizing the LLVM compiler framework can be understood as compiling the application program through the LLVM compiler framework.
[0042] Step S150: merging the GC safety point related code with the GC Map information to make the GC safety point related code correspond to the GC Map information.
[0043] In this implementation, the number of GC Map information may be greater than the number of GC safety points. To facilitate more accurate garbage collection, for each GC safety point, the relevant code of the GC safety point may be merged with the GCMap information of the GC safety point so that the GC safety point relevant code corresponds to the GC Map information. After the merger, the GC safety point relevant code and the GCMap information are adjacent to each other, and there is no limitation on which one comes first and which comes later.
[0044] The garbage collection method for an application provided in this embodiment obtains the GC safe point of the application to be processed; obtains the GC Map information corresponding to the GC safe point, the GC Map information is used to track the object to be recycled; obtains the code type configuration of the GC safe point; obtains the relevant code type of the GC safe point based on the code type configuration, and inserts the GC safe point related code corresponding to the relevant code type into the GC safe point; merges the GC safe point related code with the GC Map information, so that the GC safe point related code corresponds to the GC Map information. Thus, by the above method, by merging the GC safe point related code with the GC Map information used to track the object to be recycled, it is possible to accurately locate the safe point that needs to be garbage collected, thereby reducing unnecessary insertions, and further improving the quality of compiled code.
[0045] At the same time, by inserting the GC safe point related code after compiling with the introduced LLVM compiler framework, delayed insertion of the GC safe point related code is achieved, which can avoid the negative impact and negative optimization on the core optimization logic of the application caused by early insertion.
[0046] See also Figure 2 , shows a flowchart of a garbage collection method for an application provided by another embodiment of the present application. This embodiment provides a garbage collection method for an application, which can be applied to an electronic device. The method includes:
[0047] Step S210: in the process of compiling an application, obtaining a GC safe point, wherein the compiler of the application includes an LLVM compiler framework.
[0048] The specific implementation of step S210 may refer to the relevant description of step S110 in the aforementioned embodiment, which will not be repeated here.
[0049] Step S220: in the process of compiling the relevant code of the GC safe point to generate machine instructions, obtaining unreferenced objects.
[0050] As a method of obtaining GC Map information, unreferenced objects can be obtained during the process of compiling the relevant codes of GC safe points to generate machine instructions. The unreferenced objects include objects that are not referenced by the root set or objects that are not indirectly referenced by other functions.
[0051] Step S230: taking the location of the unreferenced object as GC Map information corresponding to the GC safe point, and the GC Map information is used to track the object to be recycled.
[0052] When an unreferenced object is obtained, the location of the unreferenced object may be used as GC Map information corresponding to a GC safe point, and the GC Map information is used to track the object to be recycled.
[0053] As a specific implementation, the compiler of the application can be an AOT compiler. In the process of compiling and generating machine instructions, the AOT compiler can determine which objects are alive and where they are stored through data flow analysis, and then store the analysis results and the generated compilation instructions in the compilation results. That is, the map information in the compilation results that records which locations in the context are objects when running to a certain instruction can be called GC Map information. It should be noted that these analysis results will not be inserted for each machine instruction, but only at the location of the safe point, and have a one-to-one correspondence with the location of the safe point.
[0054] Step S240: Obtain the code type configuration of the GC safety point.
[0055] The specific implementation of step S240 may refer to the relevant description of step S130 in the aforementioned embodiment, which will not be repeated here.
[0056] Step S251: If the code type is configured as the first type, an explicit type is used as the relevant code type of the GC safe point, and after optimizing the LLVM compiler framework, the GC safe point related code corresponding to the explicit type is inserted into the GC safe point, and the explicit type includes code that actively polls a flag bit, and the flag bit is used to interrupt the thread running of the application.
[0057] In this implementation, combined with the relevant description of the above embodiment, if the code type of the GC safe point is configured as the first type (that is, the value of the code type parameter is true), the explicit type can be used as the relevant code type of the GC safe point, and the GC safe point related code corresponding to the explicit type is inserted into the GC safe point after optimizing the LLVM compiler framework. The explicit type includes code that actively polls the flag bit, and the flag bit is used to interrupt the thread running of the application.
[0058] The processing flow of the GC safe point related code corresponding to the explicit type includes: reading the value of the flag bit, checking the flag bit read back, and jumping to the function where the modifier is paused when the flag bit is true.
[0059] Step S252: If the code type is configured as the second type and the compiled function of the application to be processed contains preset instructions, the explicit type is used as the relevant code type of the GC safe point, and the GC safe point related code corresponding to the explicit type is inserted into the GC safe point after optimizing the LLVM compiler framework.
[0060] The preset instructions represent SIMD instructions.
[0061] As an implementation method, if the code type is configured as the second type (i.e., the value of the code type parameter is false), and the compiled function of the application to be processed contains SIMD instructions, the explicit type can continue to be used as the relevant code type of the GC safe point, and after optimizing the LLVM compiler framework, the GC safe point related code corresponding to the explicit type can be inserted into the GC safe point.
[0062] Step S253: If the code type is configured as the second type and the compiled function of the application to be processed does not contain preset instructions, the implicit type is used as the relevant code type of the GC safe point, and the GC safe point related code corresponding to the implicit type is inserted into the GC safe point after optimizing the LLVM compiler framework, and the implicit type does not include code that will actively poll the flag.
[0063] As an implementation, if the code type is configured as the second type (i.e., the value of the code type parameter is false), and the compiled function of the application to be processed does not contain SIMD instructions, the implicit type can be used as the relevant code type of the GC safe point, and the GC safe point related code corresponding to the implicit type is inserted into the GC safe point after optimizing the LLVM compiler framework. The implicit type does not include the code that actively polls the flag bit.
[0064] When the GC safe point related code corresponding to the implicit type is inserted into the GC safe point, unlike the explicit type, the polling code will be inserted, but a load instruction will be inserted, such as the ldr x21, [x21] instruction. When the ART virtual machine wants to do GC, the content of the x21 register is set to a null value. After being set to a null value, this load instruction will trigger a segmentation fault. At the same time, the segmentation fault is captured and processed by the signal handler and finally the thread pause function is called, so as to achieve the purpose of pausing the modifier and then executing GC garbage collection.
[0065] In this implementation, AOT compilation is based on functions. The runtime overhead of inserting explicit type code is greater than the runtime overhead of inserting implicit type code. Therefore, it is also possible to use implicit types by default when generating GC safe point related code for all functions, and insert code corresponding to implicit types. Explicit types are used only when the function contains SIMD instructions. The reason is that the implicit type callback function only saves the lower 64 bits of the FP (Float Points, SIMD also uses this type of register) register, and if SIMD is used in the function, the upper 64 bits of the FP register may be used. Therefore, if implicit types are used, the upper 64 bits of the register may be corrupted, so it is required to use explicit types in functions containing SIMD.
[0066] Step S260: merging the GC safety point related code with the GC Map information to make the GC safety point related code correspond to the GC Map information.
[0067] The specific implementation of step S260 may refer to the relevant description of step S150 in the aforementioned embodiment, which will not be repeated here.
[0068] The garbage collection method for an application provided in this embodiment obtains a GC safe point during the process of compiling an application, wherein the compiler of the application includes an LLVM compiler framework; in the process of compiling the code related to the GC safe point to generate machine instructions, obtains an unreferenced object; uses the location of the unreferenced object as GC Map information corresponding to the GC safe point, and the GC Map information is used to track objects to be recycled; the code type configuration of the GC safe point is obtained; if the code type is configured as the first type, an explicit type is used as the relevant code type of the GC safe point, and after optimizing the LLVM compiler framework, the GC safe point related code corresponding to the explicit type is inserted into the GC safe point, the explicit type includes code that will actively poll a flag, and the flag is used to interrupt the thread running of the application; if the code type is configured as the second type, and the compiled function of the application to be processed contains preset instructions, the explicit type is used as the relevant code type of the GC safe point, and after optimizing the LLVM compiler framework, the GC safe point related code corresponding to the explicit type is inserted into the GC safe point; if the code type is configured as the second type, and the compiled function of the application to be processed does not contain preset instructions, an implicit type is used as the relevant code type of the GC safe point, and after optimizing the LLVM compiler framework, the GC safe point related code corresponding to the implicit type is inserted into the GC safe point, and the implicit type does not include code that will actively poll a flag; the GC safe point related code and the GC Map information is merged to make the GC safety point related code correspond to the GC Map information. Thus, by merging the GC safety point related code with the GC Map information for tracking the objects to be recycled, it is possible to accurately locate the safety points that need to be garbage collected, thereby reducing unnecessary insertions and improving the quality of compiled code.
[0069] See also Figure 3 , shows a flowchart of a garbage collection method for an application provided by another embodiment of the present application. This embodiment provides a garbage collection method for an application, which can be applied to an electronic device. The method includes:
[0070] Step S310: Generate an intermediate representation of the source code.
[0071] Among them, the source code can be understood as the code of the application, and the intermediate representation can be understood as the LLVM IR file. As an implementation method, the source code of the application can be compiled into the Dalvik bytecode format first, and the Dalvik bytecode is used as the input of the ART compilation. After entering the ART compilation, the Dalvik bytecode format can be converted into the IR file inside the ART, and then optimized based on the IR file. The optimized output is used as the input of the LLVM compilation. After entering the LLVM compilation, the LLVM IR file can be generated first, that is, the intermediate representation of the source code is obtained. By generating the intermediate representation of the source code, reuse can be achieved regardless of the programming language used to compile the application.
[0072] Step S320: inserting the code type configuration of the GC safe point into the intermediate representation file.
[0073] In order to enable the application to have the conditions for delayed insertion of GC safe points when the LLVM compilation link is introduced, some preparations for delayed insertion of GC safe points can be done after the LLVM IR file is generated. Specifically, first, the code type configuration of the GC safe point can be inserted in the LLVM IR file.
[0074] As a specific implementation method, the code type configuration of the GC safe point can be written into the LLVM IR file in the form of a global constant value.
[0075] Step S330: writing both the explicit GC safety point related code and the implicit safety point related code into the intermediate representation file in the format of a function.
[0076] As for the content of the GC safe point related code of the explicit type or implicit type, it can be written into the LLVM IR file in advance as a template. As an implementation method, the explicit GC safe point related code and the implicit safe point related code can be written into the intermediate representation file separately in the format of a function, so that each of them becomes a template.
[0077] In combination with the description of the foregoing embodiment, the GC Map information records the object information of the surviving variables corresponding to a certain instruction, that is, which locations contain the object information when the context is running under a certain instruction, so these GC Map information are related to the current instruction position. Therefore, after the explicit GC safety point related code and the implicit safety point related code are written into the intermediate representation file in the format of a function, these GC Map information can be generated and directly inserted into the compiled function.
[0078] As an implementation method, the GC map information corresponding to the GC safe point may be inserted in a superset insertion manner, that is, the GC map information corresponding to the GC safe point may be inserted into the entry or loop code of all functions.
[0079] This implementation can continue to optimize the compiler of the application based on the LLVM IR file, that is, after inserting the GC Map information corresponding to the GC safe point in the entry or loop code of the function, the code optimization of the compilation process of the application can be performed based on the intermediate representation. After this step of optimization, the structure of the intermediate representation (i.e., the LLVM IR file) can be analyzed to obtain the GC safe point, and a GC safe point set can be obtained. The GC safe point set includes multiple GC safe points, and the safe point configurations corresponding to each of the multiple GC safe points are obtained from the intermediate representation to obtain a safe point configuration set, so that when executing garbage collection tasks later, the GC safe point and the code type configuration of the GC safe point can be quickly determined, thereby improving the overall compilation speed.
[0080] Step S340: in the process of compiling the application, obtaining a GC safe point, wherein the compiler of the application includes an LLVM compiler framework.
[0081] As a method, during the process of compiling the application program, one GC safe point may be obtained from the GC safe point set each time as the GC safe point until all GC safe points in the set are obtained.
[0082] The specific implementation of step S340 may also refer to the relevant description of step S110 in the aforementioned embodiment, which will not be repeated here.
[0083] Step S350: Acquire GC Map information corresponding to the GC safe point, where the GC Map information is used to track objects to be recycled.
[0084] The specific implementation of step S350 may refer to the relevant description of step S120 in the aforementioned embodiment, which will not be repeated here.
[0085] Step S360: Obtain the code type configuration of the GC safety point.
[0086] As an implementation manner, the safety point configuration corresponding to the current GC safety point may be obtained from the safety point configuration set as the code type configuration of the GC safety point.
[0087] The specific implementation of step S360 may also refer to the relevant description of step S130 in the aforementioned embodiment, which will not be repeated here.
[0088] Step S370: obtaining the relevant code type of the GC safe point based on the code type configuration, and inserting the GC safe point related code corresponding to the relevant code type into the GC safe point after optimizing the LLVM compiler framework.
[0089] The specific implementation of step S370 may refer to the relevant description of step S140 in the aforementioned embodiment, which will not be repeated here.
[0090] Step S380: merging the GC safety point related code with the GC Map information to make the GC safety point related code correspond to the GC Map information.
[0091] The specific implementation of step S380 may refer to the relevant description of step S150 in the aforementioned embodiment, which will not be repeated here.
[0092] In the implementation manner of the present application, after the delayed insertion is completed, if there is redundant GC Map information, the redundant GC Map information can be deleted.
[0093] The garbage collection method of the application provided in this embodiment generates an intermediate representation of the source code; inserts the code type configuration of the GC safety point into the file of the intermediate representation; writes both the explicit GC safety point related code and the implicit safety point related code into the file of the intermediate representation in the format of a function; obtains the GC safety point of the application to be processed; obtains the GC Map information corresponding to the GC safety point, and the GC Map information is used to track the object to be recycled; obtains the code type configuration of the GC safety point; obtains the relevant code type of the GC safety point based on the code type configuration, and inserts the GC safety point related code corresponding to the relevant code type into the GC safety point; merges the GC safety point related code with the GC Map information, so that the GC safety point related code corresponds to the GC Map information. Thus, by the above method, by merging the GC safety point related code with the GC Map information used to track the object to be recycled, it is possible to accurately locate the safety point that needs to be garbage collected, thereby reducing unnecessary insertions, and further improving the quality of compiled code.
[0094] By delaying the insertion of GC safe point related code until after LLVM optimization is completed, unnecessary insertions can be reduced, thereby reducing the overall size of the code, thereby increasing the running speed of the code and improving the quality of the compiled code.
[0095] See also Figure 4, is a structural block diagram of a garbage collection device for an application provided in an embodiment of the present application. This embodiment provides a garbage collection device 400 for an application, which can be run on an electronic device. The device 400 includes a safety point acquisition module 410, a tracking information acquisition module 420, a configuration information acquisition module 430, a safety point insertion module 440, and a merging module 450:
[0096] The safety point acquisition module 410 is used to acquire GC safety points during the process of compiling an application program, wherein the compiler of the application program includes an LLVM compiler framework.
[0097] Optionally, the device 400 may also include a garbage collection preprocessing module, which is used to generate an intermediate representation of the source code before obtaining the GC safe point during the process of compiling the application; insert the code type configuration of the GC safe point into the intermediate representation file; and write both the explicit GC safe point related code and the implicit safe point related code into the intermediate representation file in the format of a function.
[0098] As an implementation method, the garbage collection preprocessing module can also be used to insert GC Map information corresponding to the GC safety point into the entry or loop code of the function after writing both the explicit GC safety point related code and the implicit safety point related code into the intermediate representation file in the format of a function.
[0099] As an implementation method, the garbage collection preprocessing module can also be used to perform code optimization based on the intermediate representation after inserting GC Map information corresponding to the GC safety point in the entry or loop code of the function; analyzing the structure of the intermediate representation to obtain GC safety points, and obtaining a GC safety point set, wherein the GC safety point set includes multiple GC safety points; obtaining the safety point configurations corresponding to each of the multiple GC safety points from the intermediate representation, and obtaining a safety point configuration set.
[0100] In the above manner, the safety point acquisition module 410 may be used to acquire a GC safety point from the GC safety point set each time as the GC safety point.
[0101] The tracking information acquisition module 420 is used to acquire GC Map information corresponding to the GC safe point, and the GC Map information is used to track the objects to be recycled.
[0102] As a method, the tracking information acquisition module 420 can be used to obtain unreferenced objects during the process of compiling the relevant code of the GC safe point to generate machine instructions; and use the location of the unreferenced object as the GC Map information corresponding to the GC safe point.
[0103] The configuration information acquisition module 430 is used to obtain the code type configuration of the GC safety point.
[0104] As a method, the configuration information acquisition module 430 may be used to acquire the safety point configuration corresponding to the current GC safety point from the safety point configuration set as the code type configuration of the GC safety point.
[0105] The safety point insertion module 440 is used to obtain the relevant code type of the GC safety point based on the code type configuration, and insert the GC safety point related code corresponding to the relevant code type into the GC safety point after optimizing the LLVM compiler framework.
[0106] As a method, the safety point insertion module 440 can be used to, if the code type is configured as the first type, use an explicit type as the relevant code type of the GC safety point, the explicit type includes a code that actively polls a flag bit, and the flag bit is used to interrupt the thread running of the application; if the code type is configured as the second type, and the compiled function of the application to be processed contains preset instructions, use the explicit type as the relevant code type of the GC safety point; if the code type is configured as the second type, and the compiled function of the application to be processed does not contain preset instructions, use an implicit type as the relevant code type of the GC safety point, and the implicit type does not include a code that actively polls a flag bit.
[0107] The merging module 450 is used to merge the GC safety point related code with the GC Map information to make the GC safety point related code correspond to the GC Map information.
[0108] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and modules can refer to the corresponding processes in the aforementioned method embodiments, and will not be repeated here.
[0109] In several embodiments provided in the present application, the coupling between modules may be electrical, mechanical or other forms of coupling.
[0110] In addition, each functional module in each embodiment of the present application can be integrated into a processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The above integrated modules can be implemented in the form of hardware or software functional modules.
[0111] See also Figure 5Based on the above-mentioned garbage collection method and device for application programs, an embodiment of the present application further provides an electronic device 100 that can execute the above-mentioned garbage collection method for application programs. The electronic device 100 includes a memory 102 and one or more (only one is shown in the figure) processors 104 coupled to each other, and the memory 102 and the processor 104 are connected by a communication line. The memory 102 stores a program that can execute the content of the above-mentioned embodiment, and the processor 104 can execute the program stored in the memory 102.
[0112] Among them, the processor 104 may include one or more processing cores. The processor 104 uses various interfaces and lines to connect various parts of the entire electronic device 100, and executes various functions and processes data of the electronic device 100 by running or executing instructions, programs, code sets or instruction sets stored in the memory 102, and calling data stored in the memory 102. Optionally, the processor 104 can be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field programmable gate array (Field-Programmable Gate Array, FPGA), and programmable logic array (Programmable Logic Array, PLA). The processor 104 can integrate one or a combination of a central processing unit (Central Processing Unit, CPU), a graphics processing unit (Graphics Processing Unit, GPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing display content; and the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 104, but may be implemented separately through a communication chip.
[0113] The memory 102 may include a random access memory (RAM) or a read-only memory (ROM). The memory 102 may be used to store instructions, programs, codes, code sets or instruction sets. The memory 102 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the aforementioned embodiments, etc. The data storage area may also store data (such as a phone book, audio and video data, chat record data) created by the electronic device 100 during use.
[0114] Please refer to Figure 6, which shows a structural block diagram of a computer-readable storage medium provided in an embodiment of the present application. The computer-readable medium 500 stores program codes, which can be called by a processor to execute the method described in the above method embodiment.
[0115] The computer-readable storage medium 500 may be an electronic memory such as a flash memory, an EEPROM (electrically erasable programmable read-only memory), an EPROM, a hard disk, or a ROM. Optionally, the computer-readable storage medium 500 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 500 has storage space for program code 510 that performs any method steps in the above method. These program codes can be read from or written to one or more computer program products. The program code 510 can be compressed, for example, in an appropriate form.
[0116] In summary, the embodiments of the present application provide a method, device, electronic device and storage medium for garbage collection of an application program. The method is applied to an electronic device. The method obtains the GC safe point of the application program to be processed; obtains the GC Map information corresponding to the GC safe point, and the GC Map information is used to track the object to be recycled; obtains the code type configuration of the GC safe point; obtains the relevant code type of the GC safe point based on the code type configuration, and inserts the GC safe point related code corresponding to the relevant code type into the GC safe point; merges the GC safe point related code with the GC Map information so that the GC safe point related code corresponds to the GC Map information. Thus, by the above-mentioned method, by merging the GC safe point related code with the GC Map information used to track the object to be recycled, it is possible to accurately locate the safe point that needs to be garbage collected, thereby reducing unnecessary insertions and improving the quality of compiled code.
[0117] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A garbage collection method for an application, characterized in that: Applied to electronic equipment, the method comprises: In a process of compiling an application, obtaining a GC safe point, wherein the compiler of the application includes an LLVM compiler framework; Acquire GC Map information corresponding to the GC safe point, where the GC Map information is used to track objects to be recycled; Obtain the code type configuration of the GC safe point; Acquire a relevant code type of the GC safe point based on the code type configuration, and insert a GC safe point related code corresponding to the relevant code type into the GC safe point after optimizing the LLVM compiler framework; The GC safety point related code is merged with the GC Map information so that the GC safety point related code corresponds to the GC Map information.
2. The method according to claim 1, characterized in that The acquiring GC Map information corresponding to the GC safety point includes: In the process of compiling the relevant code of the GC safe point to generate machine instructions, obtaining unreferenced objects; The location of the unreferenced object is used as GC Map information corresponding to the GC safe point.
3. The method according to claim 1 or 2, characterized in that: The acquiring the relevant code type of the GC safety point based on the code type configuration includes: If the code type is configured as the first type, an explicit type is used as the relevant code type of the GC safe point, and the explicit type includes a code that actively polls a flag bit, and the flag bit is used to interrupt the thread running of the application program; If the code type is configured as the second type, and the compiled function of the application to be processed includes a preset instruction, the explicit type is used as the relevant code type of the GC safe point; If the code type is configured as the second type and the compiled function of the application to be processed does not contain a preset instruction, an implicit type is used as the relevant code type of the GC safe point, and the implicit type does not include a code that actively polls a flag bit.
4. The method according to any one of claims 1 to 3, characterized in that: In the process of compiling the application program, before obtaining the GC safe point, the method further includes: Generate an intermediate representation of the source code; Inserting a code type configuration of a GC safe point into the intermediate representation file; Both the explicit GC safe point related code and the implicit safe point related code are written into the intermediate representation file in the format of functions.
5. The method according to claim 4, characterized in that After writing both the explicit GC safety point related code and the implicit safety point related code into the intermediate representation file in the format of a function, the method further includes: GC Map information corresponding to the GC safe point is inserted into the entry or loop code of the function.
6. The method according to claim 5, characterized in that After inserting the GC Map information corresponding to the GC safe point into the entry or loop code of the function, the method further includes: Perform code optimization based on the intermediate representation; Analyze the structure of the intermediate representation to obtain GC safety points, and obtain a GC safety point set, wherein the GC safety point set includes multiple GC safety points; The safety point configurations corresponding to each of the plurality of GC safety points are obtained from the intermediate representation to obtain a safety point configuration set.
7. The method according to claim 6, characterized in that The obtaining of the GC safety point of the application to be processed includes: Obtaining a GC safety point from the GC safety point set each time as the GC safety point; The obtaining of the code type configuration of the GC safety point includes: A safety point configuration corresponding to the current GC safety point is acquired from the safety point configuration set as the code type configuration of the GC safety point.
8. A garbage collection device for an application, characterized in that: Running on an electronic device, the device comprises: A safety point acquisition module, used to acquire GC safety points during the process of compiling an application program, wherein the compiler of the application program includes an LLVM compiler framework; A tracking information acquisition module, used to acquire GC Map information corresponding to the GC safety point, wherein the GC Map information is used to track the objects to be recycled; A configuration information acquisition module, used to obtain the code type configuration of the GC safety point; A safety point insertion module, configured to obtain a relevant code type of the GC safety point based on the code type configuration, and insert a GC safety point related code corresponding to the relevant code type into the GC safety point after optimizing the LLVM compiler framework; The merging module is used to merge the GC safety point related code with the GC Map information so that the GC safety point related code corresponds to the GC Map information.
9. An electronic device, characterized in that: including one or more processors and memory; One or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores program code, wherein when the program code is executed by a processor, the method according to any one of claims 1 to 7 is executed.