Method and device for completely releasing metadata of program set during runtime
By collecting and marking assembly metadata at runtime, it solves the problem of inability to effectively release assembly metadata in the existing technology, realizes efficient memory release and dynamic loading and release functions, and improves development and operation efficiency.
Patent Information
- Application Number
- CN202410819418.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-24
- Publication Date
- 2024-09-27
AI Technical Summary
The existing technology cannot effectively release assembly metadata, resulting in memory leaks. Especially in the runtime that supports a single AppDomain, it cannot distinguish whether secondary metadata can be released, resulting in a large amount of metadata that cannot be released.
Collect metadata information of all assemblies at runtime, recursively mark the referenced or indirectly referenced secondary metadata for the root node through the top-level metadata, and clean up the unmarked secondary metadata to achieve complete release.
Achieved 100% memory release rate, ensuring that assemblies can be dynamically loaded and released in the hybrid hot update solution, improving development and operation efficiency.
Smart Images

Figure CN118708255A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer compilation technology, and in particular to a method, apparatus, computing device and computer-readable storage medium for completely releasing assembly metadata during runtime. Background Art
[0002] In the prior art CN 202210078151.3, a hot update method and device for the Unity platform are proposed. In this patent, a new hot update architecture is implemented by customizing the IL2CPP tool and adding a custom interpreter to the IL2CPP tool. The hot update assembly and the main program are loaded into the same runtime, and static compilation and local execution are used for the code that does not need to be updated; for the code in the hot update assembly, the interpreter is used for interpretation and execution. However, the IL2CPP runtime only supports one global AppDomain, that is, it does not support the creation and release of AppDomains, and thus cannot release the loaded assembly assembly.
[0003] The prior art CN202310926987.9 provides a method for releasing assemblies, but the defect of this method is that it can release some top-level or clearly separately managed metadata, but for secondary metadata, since both the unreleased assembly metadata and the assembly metadata to be released may reference them, it is not possible to distinguish whether these secondary metadata can be released. For indistinguishable cases, they are not released, resulting in a large number of metadata memory leaks. Therefore, a solution is urgently needed to solve this problem. Summary of the invention
[0004] In view of this, the present application example provides a method, apparatus, computing device and computer-readable storage medium for completely releasing assembly metadata at runtime to address the technical defects existing in the prior art.
[0005] According to a first aspect of an embodiment of the present application, a method for completely releasing assembly metadata at runtime is provided, comprising:
[0006] Collect metadata information of all assemblies at runtime and save them to the first collection;
[0007] Acquire top-level metadata that does not need to be released, and use it as a root node to recursively mark all secondary metadata referenced or indirectly referenced by the top-level metadata in the first set;
[0008] All top-level metadata to be released and unmarked secondary metadata in the first set are cleared.
[0009] According to a second aspect of an embodiment of the present application, a device for completely releasing assembly metadata during runtime is provided, comprising:
[0010] A collection unit, used for collecting metadata information of all assemblies during runtime and saving the metadata information to a first collection;
[0011] a marking unit, used for acquiring top-level metadata that does not need to be released, and taking the top-level metadata as a root node to recursively mark all secondary metadata referenced or indirectly referenced by the top-level metadata in the first set;
[0012] The release unit is used to clean up all top-level metadata to be released and unmarked secondary metadata in the first set.
[0013] According to a third aspect of an embodiment of the present application, a computing device is provided, comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein when the processor executes the instructions, the steps of the method for completely releasing assembly metadata at runtime are implemented.
[0014] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, which stores computer instructions, and when the instructions are executed by a processor, the steps of the method for completely releasing assembly metadata at runtime are implemented.
[0015] Through the method and device in the embodiment of the present application, in order to realize the complete release of assembly metadata in a runtime that only supports a single AppDomain, based on the internal operating principle of the runtime, the metadata information of all assemblies loaded at the runtime is collected, and the secondary metadata that does not need to be released is marked step by step based on the top-level metadata, so that it can be clearly and accurately determined whether a certain secondary metadata is still referenced, and then the unreferenced secondary metadata can be completely released, achieving a 100% memory release rate. Moreover, in the hybrid hot update solution using this method, the function of releasing the assembly is completed at the runtime level, and the assembly can be dynamically loaded natively and released dynamically, achieving efficient development and operation efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 is a structural block diagram of a computing device provided in an embodiment of the present application;
[0017] Figure 2 It is a flowchart of a method for completely releasing assembly metadata at runtime provided by an embodiment of the present application;
[0018] Figure 3 It is a structural diagram of a device for completely releasing assembly metadata during runtime provided by an embodiment of the present application. DETAILED DESCRIPTION
[0019] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0020] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms of "a", "said" and "the" used in one or more embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more associated listed items.
[0021] It should be understood that, although the terms first, second, etc. may be used to describe various information in one or more embodiments of the present application, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "in response to determination".
[0022] In the present application, a method and apparatus, a computing device, and a computer-readable storage medium for completely releasing assembly metadata at runtime are provided, which are described in detail one by one in the following embodiments.
[0023] Figure 1 The structure block diagram of a computing device 100 according to an embodiment of the present application is shown. The components of the computing device 100 include but are not limited to a memory 110 and a processor 120. The processor 120 is connected to the memory 110 via a bus 130, and the database 150 is used to store data.
[0024] The computing device 100 also includes an access device 140 that enables the computing device 100 to communicate via one or more networks 160. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 140 may include one or more of any type of network interface (e.g., a network interface card (NIC)) whether wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and the like.
[0025] In one embodiment of the present application, the above components of the computing device 100 and Figure 1 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 1 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of the present application. Those skilled in the art may add or replace other components as needed.
[0026] The computing device 100 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or PC. The computing device 100 may also be a mobile or stationary server.
[0027] In the Unity development platform, IL2CPP is a new scripting backend method that processes the IL (intermediate language) compiled by the platform, converts the IL language into static C++ code, and then directly compiles it into executable native assembly code by the C++ compiler of each platform, thereby realizing Unity's cross-platform development. In addition to its high execution efficiency, this method can also use the C++ compiler of each platform to perform compile-time optimization on the code, which can further reduce the size of the final game and increase the running speed of the game. Therefore, almost all Unity-based development processes use the IL2CPP tool.
[0028] Among them, IL2CPP mainly consists of two parts:
[0029] AOT (Ahead Of Time) static compilation compiler (il2cpp.exe);
[0030] Runtime library (libil2cpp);
[0031] The AOT compiler converts IL into C++ code, and then gives it to the C++ compiler of each platform for compilation. Since AOT static compilation is used, the types that need to be used must be completely determined during compilation; the runtime library will provide services and abstractions such as garbage collection, thread / file acquisition, and internal calls to directly modify the native generation of managed data structures.
[0032] In the prior art CN 202210078151.3, by adding an IL interpreter to the IL2CPP tool, the IL2CPP tool has the functions of an AOT compiler, a runtime environment, and an IL interpreter, and has the basis for compilation and hot update of multiple different platforms. Among them, for the code that does not need to be updated, AOT is used to statically compile it into C++ code, and then compiled into a local executable program by the target platform; for the code in the hot update assembly, it is interpreted and executed by the interpreter. The IL2CPP runtime environment provided by the Unity development environment is a simplified implementation based on CLR (Common Language Runtime). It only supports one global AppDomain, so it cannot create and release multiple AppDomains, and thus it cannot release assemblies, because in the CLR specification, only the release of the AppDomain is supported to release the assemblies it contains. In some game scenarios, such as a game hall containing multiple sub-games, since players often enter and exit the mini-game, the developer hopes to release the memory occupied by the assembly where the mini-game is located when exiting the mini-game, otherwise the memory occupied by these assemblies will increase, eventually leading to the crash of the game.
[0033] In order to solve this problem, the prior art CN202310926987.9 provides a method for releasing an assembly, but the defect of this method is that it can release some top-level or explicitly separately managed metadata, but for secondary metadata, since both the unreleased assembly metadata and the assembly metadata to be released may reference them, it is impossible to distinguish whether these secondary metadata can be released. For indistinguishable cases, they are not released, resulting in leaked metadata memory of up to 80% of the assembly size. For frequent release scenarios, this will cause the program process to quickly exceed the maximum allocated memory and crash.
[0034] Therefore, in an embodiment of the present application, in order to solve the above problems, a method and apparatus for completely releasing assembly metadata at runtime are proposed, making it possible to completely release assembly metadata in a single AppDomain runtime such as IL2CPP.
[0035] Instructions attached Figure 1 The processor 120 in the embodiment may execute Figure 2 The flowchart of a method for completely releasing assembly metadata at runtime includes steps 202 to 206.
[0036] Step 202: Collect metadata information of all assemblies during runtime and save it to a first collection.
[0037] In this step, first collect all the top-level metadata information in the il2cpp runtime, for example, Il2CppClass is an internal data structure used by the IL2CPP tool to represent a .NET class or structure. Its instance contains most of the information that a class or structure may need, including its name, namespace, methods, interfaces, fields, properties, events, nested types, generic parameters, etc.
[0038] Il2CppClass instances are dynamically generated at runtime, and the assemblies in the runtime maintain a map of Il2CppClass, so it is possible to obtain their instances.
[0039] These top-level metadata information also includes Il2CppGenericClass and Il2CppGenericMethod. These two types are used to represent generic classes and generic methods. Because they do not belong to any assembly, they are maintained separately, such as a global map.
[0040] It also includes metadata such as Il2CppImage, which is used to contain the metadata of an assembly, including the assembly name and all types and other information. Usually, one assembly corresponds to one Il2CppImage.
[0041] Those skilled in the art should be aware that the above-mentioned examples of top-level metadata in the il2cpp runtime are merely illustrative, and other top-level metadata may also be included in the runtime, which will not be described in detail here.
[0042] Furthermore, each top-level metadata is scanned to obtain the secondary metadata information contained therein, where the secondary metadata information includes but is not limited to fields, functions, field types, function parameters, function parameter types, etc.; these secondary metadata may also hierarchically nest other metadata.
[0043] For example, for the top-level metadata Il2CppClass, you can traverse the MethodInfo**methods field in its data structure to obtain all its method metadata, or traverse the fields field that directly points to the field list to obtain all its field metadata, etc. The MethodInfo list also includes the parameters field of the ParameterInfo list type, and each ParameterInfo contains other secondary metadata. All metadata information is obtained by scanning layer by layer.
[0044] Furthermore, all the acquired top-level metadata and secondary metadata are saved in a first set, and the first set may be a nested data structure, a tree or a dictionary type data structure, which will not be described in detail here.
[0045] Step 204: Acquire the top-level metadata that does not need to be released, and use it as the root node to recursively mark all the secondary metadata referenced or indirectly referenced by the top-level metadata in the first set.
[0046] In this step, a first assembly to be released is obtained according to the release requirement, for example, according to the assembly to be released selected by the user, and then at least one second assembly that does not need to be released is obtained according to the global assembly list at runtime;
[0047] The top-level metadata under the second assembly is obtained, and in the first assembly, the top-level metadata is used as the root node, and all the secondary metadata referenced or indirectly referenced by it are recursively marked.
[0048] For example, for each Il2CppClass instance, it includes the secondary metadata MethodInfo**methods field, and the MethodInfo list also includes the parameters field of the type ParameterInfo list. Each ParameterInfo contains other secondary metadata, which are stored in a hierarchical manner in the first set, for example, by identifying the type of the above metadata itself, so that these secondary metadata can be marked, for example, marked as True, through layer-by-layer recursive scanning:
[0049] Step 206: Clear all top-level metadata to be released and unmarked secondary metadata in the first set.
[0050] In this step, the first assembly to be released in step 204 is obtained, and the top-level metadata in the first set is released according to the information of the first assembly, for example, as mentioned in the above step 202, the relevant Il2CppClass instance in the first set is released according to the mapping relationship between the first assembly instance and the Il2CppClass instance; for another example, the memory allocated by the Il2CppImage structure corresponding to the first assembly is released. These top-level metadata also include but are not limited to Il2CppGenericClass, Il2CppGenericMethod, etc., which will not be repeated here.
[0051] Furthermore, the unmarked secondary metadata in the first set are released. Since the secondary metadata that is still needed in the current runtime environment is marked in step 204, the unmarked secondary metadata are the target objects that can be released.
[0052] In the above embodiment of the application, in order to realize the complete release of assembly metadata in a runtime that only supports a single AppDomain, based on the internal operating principle of the runtime, the metadata information of all assemblies loaded at runtime is collected, and the secondary metadata that does not need to be released is marked based on the top-level metadata of the il2cpp tool. It can clearly and accurately determine whether a certain secondary metadata is still referenced by the unloaded part, and then the unreferenced secondary metadata can be completely released, achieving a 100% memory release rate. In the hybrid hot update solution, the solution of this application enables the function of releasing the assembly to be completed at the runtime level, realizing both native dynamic loading of assemblies and dynamic release, and achieving efficient development and operation efficiency.
[0053] Corresponding to the above method embodiment, the present application also provides an embodiment of a device for completely releasing assembly metadata during runtime, such as Figure 3 As shown, the device comprises:
[0054] A collection unit, used for collecting metadata information of all assemblies during runtime and saving the metadata information to a first collection;
[0055] a marking unit, used for acquiring top-level metadata that does not need to be released, and taking the top-level metadata as a root node to recursively mark all secondary metadata referenced or indirectly referenced by the top-level metadata in the first set;
[0056] The release unit is used to clean up all top-level metadata to be released and unmarked secondary metadata in the first set.
[0057] The above is a schematic scheme of a device for completely releasing assembly metadata at runtime in this embodiment. It should be noted that the technical scheme of the device for completely releasing assembly metadata at runtime and the technical scheme of the method for completely releasing assembly metadata at runtime described above belong to the same concept, and the details of the technical scheme of the device for completely releasing assembly metadata at runtime that are not described in detail can all be found in the description of the technical scheme for completely releasing assembly metadata at runtime described above.
[0058] In one embodiment of the present application, a computing device is also provided, including a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein when the processor executes the instructions, the steps of the method for completely releasing assembly metadata at runtime are implemented.
[0059] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical scheme of the method for completely releasing assembly metadata at runtime described above belong to the same concept, and the details not described in detail in the technical scheme of the computing device can be found in the description of the technical scheme of the method for completely releasing assembly metadata at runtime described above.
[0060] An embodiment of the present application further provides a computer-readable storage medium storing computer instructions, which, when executed by a processor, implement the steps of the method for completely releasing assembly metadata at runtime as described above.
[0061] The above is a schematic scheme of a computer-readable storage medium of this embodiment. It should be noted that the technical scheme of the storage medium and the technical scheme of the method for completely releasing assembly metadata at runtime described above belong to the same concept, and the details not described in detail in the technical scheme of the storage medium can be found in the description of the technical scheme of the method for completely releasing assembly metadata at runtime described above.
[0062] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0063] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0064] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described action sequence, because according to the present application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0065] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0066] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the present application. The present application selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present application, so that those skilled in the art can understand and use the present application well. The present application is only limited by the claims and their full scope and equivalents.
Claims
1. A method to fully release assembly metadata at runtime, It is characterized in that include: Collect metadata information of all assemblies at runtime and save them to the first collection; Acquire top-level metadata that does not need to be released, and use it as a root node to recursively mark all secondary metadata referenced or indirectly referenced by the top-level metadata in the first set; All top-level metadata to be released and unmarked secondary metadata in the first set are cleared.
2. The method according to claim 1, in, The metadata information collected for all assemblies at runtime includes: Collect all top-level metadata information during runtime, scan each top-level metadata instance, and obtain the secondary metadata information it contains.
3. The method according to claim 2, in, Collect all top-level metadata information at runtime including: Get the top-level metadata information based on the mapping relationship between the assembly and the top-level metadata.
4. The method according to claim 1, in, The top-level metadata that does not need to be released includes: A first assembly to be released is obtained, and at least one second assembly that does not need to be released is obtained according to the global assembly list at runtime; the top-level metadata under the second assembly is the top-level metadata that does not need to be released.
5. The method according to claim 1, in, Cleaning up all top-level metadata to be released and unmarked secondary metadata in the first set includes: Acquire a first assembly to be released, and release the top-level metadata in the first set according to information of the first assembly; Furthermore, the unmarked secondary metadata in the first set is released.
6. The method according to claim 1, in, The top-level metadata includes but is not limited to: Il2CppClass, Il2CppImage, Il2CppGenericClass and Il2CppGenericMethod.
7. A device for completely releasing assembly metadata at runtime, It is characterized in that The device includes: A collection unit, used for collecting metadata information of all assemblies during runtime and saving the metadata information to a first collection; a marking unit, used for acquiring top-level metadata that does not need to be released, and taking the top-level metadata as a root node to recursively mark all secondary metadata referenced or indirectly referenced by the top-level metadata in the first set; The release unit is used to clean up all top-level metadata to be released and unmarked secondary metadata in the first set.
8. A computing device comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, It is characterized in that When the processor executes the instructions, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium storing computer instructions. It is characterized in that When the instruction is executed by a processor, the steps of the method described in any one of claims 1 to 6 are implemented.
