A method, device and medium for recording object references based on threads in java
By establishing a statistics interface in Java to distinguish and count the Java object reference types held by C++, the problems of resource leaks and program crashes are solved, and more accurate resource management and leak detection are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- WUHAN DOUYU NETWORK TECHNOLOGY CO LTD
- Filing Date
- 2021-04-29
- Publication Date
- 2026-05-29
AI Technical Summary
In Java, when C++ holds Java objects, it is difficult to distinguish between different reference types, leading to resource leaks and program crashes.
By establishing a statistics interface within the encapsulation class, the number of references for different reference types, including local references, global references, and global weak references, is counted. Objects are created and deleted using the JNI interface, and function call stacks are recorded to identify resource leaks.
It enables the investigation of resource leaks of different reference types, prevents program crashes, and improves the accuracy and efficiency of resource management.
Smart Images

Figure CN115269029B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer application technology, and in particular to a method for recording object references based on threads in Java. Background Technology
[0002] When developing features for the Android platform, functionalities developed in C++ are often encapsulated into an SO module. However, when using Java objects in C++, special attention must be paid to the object's lifecycle to prevent object leaks that could lead to resource leaks and program crashes. Furthermore, different reference types must be distinguished when using Java objects; incorrect usage can cause program crashes or resource leaks. Summary of the Invention
[0003] This invention provides a method for recording object references in Java based on threads. It takes into account all reference types that C++ holds for Java objects, establishes statistical interfaces in the encapsulation class, and then counts the number of references corresponding to different reference types, thereby obtaining the resource leakage situation of any reference type, which is beneficial for investigation based on reference type.
[0004] In a first aspect, embodiments of the present invention provide a method for recording object references based on threads in Java, including:
[0005] Get all reference types of C++ objects that hold Java objects in the system;
[0006] Create reference interfaces according to the reference types described above;
[0007] All referenced interfaces are then categorized to obtain multiple sets of referenced interfaces;
[0008] Create separate wrapper classes to encapsulate any set of referenced interfaces;
[0009] Establish a statistics interface within the encapsulation class to count the number of references for any reference type.
[0010] Preferably, the process of creating reference interfaces according to the reference type includes:
[0011] Create a function template that contains at least two template parameters: a Java object and its corresponding reference type.
[0012] Based on the passed reference type, create a jobject crete function interface, which contains the encapsulation class as a parameter;
[0013] Calling the jobject crete function interface allows the creation of a reference object corresponding to the reference type.
[0014] Preferably, a wrapper class is created to encapsulate any set of reference interfaces. The creation process includes:
[0015] Create multiple classes to encapsulate the reference interface set;
[0016] Create them respectively within the encapsulation class:
[0017] An object function that takes a primitive Java object as input and returns a corresponding reference object with a reference count.
[0018] The delete function deletes the original Java object and returns the corresponding deleted object with a reference count.
[0019] Preferably, it also includes:
[0020] Each of the encapsulation classes creates an inspection interface to check whether the reference type corresponds to the original Java object; wherein the reference type includes local references, global references, and weak global references.
[0021] Preferably, the step of establishing a statistical interface within the encapsulation class to count the number of references corresponding to any reference type specifically includes:
[0022] Get the ID of the current thread;
[0023] Count the number of references to any wrapper class within the current thread;
[0024] Calculate the number of function resources created, the number of function resources released, and the function resource creation efficiency within the encapsulated class;
[0025] If the number of times the object function is called is inconsistent with the number of times the deletion function is called, then it is determined that the current thread has a resource leak.
[0026] Preferably, the step of establishing a statistical interface within the encapsulation class to count the number of references corresponding to any reference type includes:
[0027] Any reference interface `jobject` can be obtained through the system function `GetObjectClass`;
[0028] And obtain the jclass object of the Java class corresponding to any of the referenced interfaces jobject;
[0029] The name of the method in a Java class is obtained through the system function GetMethodID.
[0030] The corresponding class name is obtained according to the name retrieval method in the Java class.
[0031] Calculate the number of function resources created, the number of function resources released, and the efficiency of function resource creation within the Java class.
[0032] If the number of times the object function is called is inconsistent with the number of times the deletion function is called, then it is determined that a resource leak has occurred in the current Java class.
[0033] Preferably, the number of function resources created is equal to the number of times the object function is called;
[0034] The release of the function resource is defined as the deletion of the number of function calls;
[0035] The efficiency of function resource creation is the number of times the object function is called per unit of time.
[0036] Secondly, embodiments of the present invention provide an apparatus for recording object references based on threads in Java, comprising:
[0037] The module retrieves all reference types that C++ holds Java objects in the system.
[0038] Create a module that creates reference interfaces according to the reference types;
[0039] The classification module is used to classify all referenced interfaces and obtain multiple sets of referenced interfaces;
[0040] The encapsulation module is used to create encapsulation classes to encapsulate any set of reference interfaces.
[0041] The statistics module is used to establish statistics interfaces within the encapsulation class to count the number of references for any reference type.
[0042] Thirdly, embodiments of the present invention provide an electronic device, including an application processor and a memory, wherein the processor is configured to implement the method of recording object references based on threads in Java when a computer management program stored in the memory is implemented.
[0043] Fourthly, embodiments of the present invention provide a computer-readable storage medium storing a computer management class program thereon, characterized in that, when the computer management class program is executed by a processor, it implements the steps of the method for recording object references based on threads in Java.
[0044] Beneficial effects
[0045] This invention provides a method for recording object references in Java based on threads. It takes into account all reference types that C++ holds for Java objects, establishes statistical interfaces in the encapsulation class, and then counts the number of references corresponding to different reference types, thereby obtaining the resource leakage situation of any reference type, which is beneficial for investigation based on reference type. Attached Figure Description
[0046] Figure 1 This is a flowchart illustrating the method for recording object references based on threads in Java, as described in this invention.
[0047] Figure 2 This is a flowchart illustrating the method for creating an encapsulation class to encapsulate any set of reference interfaces as described in this invention.
[0048] Figure 3 This is a flowchart illustrating the creation process of creating reference interfaces based on reference types, as described in this invention.
[0049] Figure 4 This is a flowchart illustrating how to establish statistical interfaces within the encapsulated class to count the number of references for any reference type, as described in this invention.
[0050] Figure 5 This is a flowchart of another embodiment of the present invention, which establishes a statistical interface in the encapsulation class to count the number of references corresponding to any reference type.
[0051] Figure 6 This is a schematic diagram of the unit structure of an embodiment of the thread-based object reference recording device in Java according to the present invention.
[0052] Figure 7 This is a schematic diagram of the hardware structure of a thread-based object reference recording device in Java, provided as an embodiment of the present invention.
[0053] Figure 8 A schematic diagram illustrating a possible embodiment of an electronic device provided by an embodiment of the present invention;
[0054] Figure 9 This is a schematic diagram illustrating one possible embodiment of a computer-readable storage medium provided in this invention. Detailed Implementation
[0055] The following specific embodiments illustrate the implementation of the present invention. Those skilled in the art can easily understand other advantages and effects of the present invention from the content disclosed in this specification. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0056] It should be noted that in the description of this invention, terms such as "center," "upper," "lower," "horizontal," and "inner," indicating directional or positional relationships, are based on the directional or positional relationships shown in the accompanying drawings. These are merely for ease of description and do not indicate or imply that the device or element must have a specific orientation, or be constructed and operated in a specific orientation; therefore, they should not be construed as limitations on this invention. Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.
[0057] Furthermore, it should be noted that, in the description of this invention, unless otherwise explicitly specified and limited, the terms "set," "install," "connect," and "link" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal communication between two components. Those skilled in the art can understand the specific meaning of the above terms in this invention according to the specific circumstances.
[0058] like Figure 1 As shown, based on the technical problems raised in the background, this invention provides a method for recording object references based on threads in Java, including:
[0059] Step S110: Obtain all reference types of C++ objects holding Java objects in the system, and create reference interfaces according to each reference type; specifically:
[0060] When designing C++ to hold Java objects, different reference counting methods are used in different situations. In C++, the system provides three different types of references for holding Java objects: local references, global references, and global weak references.
[0061] It's important to note that different types need to be distinguished in specific use cases. Using a local reference where a global reference is needed will cause program errors. Conversely, using a global reference when a local reference should be used will prevent the object from being automatically released, leading to resource leaks. This approach achieves the beneficial effect of providing basic functionality through well-designed categories, thereby facilitating the subsequent separate statistics of all reference creations.
[0062] Step S120: Classify all reference interfaces to obtain multiple reference interface sets; specifically, obtain local reference interface sets, global reference interface sets, and weak global reference interface sets, and create wrapper classes to wrap any reference interface set.
[0063] like Figure 2 As shown, creating a wrapper class to encapsulate any set of referenced interfaces involves the following steps:
[0064] Step S121: Create multiple classes and encapsulate the referenced interface set respectively;
[0065] Step S122: Create object functions and delete functions respectively within the encapsulated class:
[0066] An object function that takes a primitive Java object as input and returns a corresponding reference object with a reference count.
[0067] The delete function deletes the original Java object and returns the corresponding deleted object with a reference count.
[0068] In another embodiment, it further includes:
[0069] Step S123: Create an inspection interface within the encapsulation class to check whether the reference type corresponds to the original Java object; wherein the reference type includes local reference, global reference, and weak global reference.
[0070] Specifically, wrapper classes are created to encapsulate the reference interfaces for three types of references: local references, global references, and global weak references. This is implemented using the following code:
[0071] 1) Regarding local references, create a wrapper class to encapsulate the set of local reference interfaces.
[0072] First, design a class that encapsulates all interfaces for local references, as implemented in the following code:
[0073] Create a local reference class: Class LocalRef{
[0074] Provides a creation interface that requires a primitive Java object as input: objjobject crete(jobject obj)const{;
[0075] auto ref=env->NewLocalRef(obj);
[0076] It is created using the JNI interface NewLocalRef, and the return value `ref` is a new Java object with local reference computation. `Return ref;` returns the result.
[0077] Void delete(jobject obj){;
[0078] Provide an interface to delete the object, reduce the reference count, and prevent resource leaks.
[0079] env->DeleteLocalRef(reference);}
[0080] bool check(jobject obj){
[0081] Since all reference types are ultimately just jobject objects, an interface is provided to check whether any Java object is a local reference, thus providing a checking function to prevent misuse.
[0082] jobjectRefType type=env->GetObjectRefType(obj);
[0083] If the type of the current object reference is JNILocalRefType, it indicates that it is a local reference; otherwise, it is not a local reference.
[0084] 2) Regarding global references, create a wrapper class to encapsulate the set of global reference interfaces.
[0085] First, design a class to encapsulate all interfaces referenced globally. This is implemented using the following code:
[0086] Class GlobalRef { Local reference class.}
[0087] object create(jobject obj)const{
[0088] Provide a creation interface that takes a primitive Java object `obj` as input and then creates a corresponding new object with a reference.
[0089] `auto ref = env->NewGlobalRef(obj);` creates a new Java object with global reference computation using the JNI interface `NewLocalRef`, and returns `ref`. `Return ref;` returns the result.
[0090] Void delete(jobject obj)}
[0091] Provide an interface to delete the object, reduce the reference count, and prevent resource leaks.
[0092] env->DeleteGlobalRef(reference);}
[0093] bool check(jobject obj){
[0094] Since all reference types are essentially jobject objects, an interface is provided to check whether any Java object is a global reference in order to prevent misuse.
[0095] jobjectRefType type=env->GetObjectRefType(obj);}
[0096] If the type of the current reference to obj is JNIGlobalRefType, it indicates that it is a global reference; otherwise, it is not a global reference.
[0097] 3) Regarding global weak references, create a wrapper class to encapsulate the set of global weak reference interfaces.
[0098] First, design a class to encapsulate all interfaces for global weak references. This is implemented using the following code:
[0099] Class WeakGlobal { Local weak reference class.}
[0100] object crete(jobject obj)const{
[0101] Provide a creation interface that takes a primitive Java object `obj` as input and then creates a corresponding new object with a reference.
[0102] `auto ref = env->NewWeakGlobalRef(obj);` creates a new Java object with a globally weak reference computed value (`ref`) using the JNI interface `NewWeakGlobalRef`. The return value is `ref`. `Return ref;` returns the result.
[0103] Void delete(jobject obj){
[0104] Provide an interface to delete the object, reduce the reference count, and prevent resource leaks.
[0105] env->DeleteWeakGlobalRef(reference);}
[0106] bool check(jobject obj){
[0107] Since all reference types are essentially jobject objects, an interface is provided to check whether any Java object is a global weak reference, thus providing a checking function to prevent misuse.
[0108] jobjectRefType type=env->GetObjectRefType(obj);
[0109] If the type of the current reference to `obj` is `JNIWeakGlobalRefType`, it indicates that it is a global weak reference; otherwise, it is not.
[0110] like Figure 3 As shown, in step S110, reference interfaces are created according to the reference type. The creation process includes:
[0111] Step S111: Create a function template that contains at least two template parameters: a Java object and its corresponding reference type. Specifically, design a generic template to adapt to different reference types, implemented as shown in the following code:
[0112] template<typename T,typename type>
[0113] jobjectmakeref(constjobject&obj){
[0114] It provides two template parameters: one is the specific Java object ypename T, and the other is the corresponding reference type typename type, so that all reference types use the same interface.
[0115] auto ref = type.crete(obj);
[0116] Based on the passed-in type object, the create interface of that object is called to create the corresponding reference object.
[0117] If a WeakGlobal instance is passed in, the WeakGlobal interface `crete` is used to create the corresponding object, which is then a globally weakly referenced object.
[0118] It is worth noting that developers write code based on the provided template interface, thereby avoiding errors. With a unified interface, the creation and recording of object references are made easier.
[0119] Step S112: Based on the passed reference type, create a `jobject crete` function interface, which includes the wrapper class as a parameter; wherein, calling the `jobject crete` function interface can create a reference object corresponding to the reference type, specifically implemented by the following code:
[0120] 1) Write the interface for creating local references.
[0121] jobject crete_local(jobject ref){
[0122] Similarly, we can write a `crete_local` function to create corresponding local reference types for different Java objects. The parameter is the corresponding Java object. Here, we can call the previously written basic functionality to create the corresponding local reference object.
[0123] Returnmakeref<jobject,LocalRef> (ref));
[0124] The underlying class LocalRef, which was written earlier, is passed in to create the corresponding local reference.
[0125] 2) Write the interface for creating global references.
[0126] jobject crete_global(jobjectref){
[0127] Similarly, we can write a `crete_global` function to create corresponding global reference types for different Java objects. The parameter is the corresponding Java object. Here, we can call the previously written basic functionality to create the corresponding global reference object.
[0128] Returnmakeref<jobject,GlobalRef> (ref));
[0129] It takes the previously written base class GlobalRef as input to create the corresponding global reference.
[0130] 3) Write the interface for creating global weak references.
[0131] jobject crete_WeakGlobal(jobject ref){
[0132] Similarly, a `crete_WeakGlobal` function can be written to create corresponding global weak reference types for different Java objects. The parameter is the corresponding Java object. Here, the previously written basic functionality can be called to create the corresponding global weak reference object.
[0133] Returnmakeref<jobject,GlobalRef> (ref));
[0134] It takes the previously written base class GlobalRef as input to create the corresponding global weak reference.
[0135] Step S130: Establish a statistics interface within the encapsulation class to count the number of references for any reference type. Based on the previous basic functionality, the creation of reference objects of different types is designed, providing creation and deletion interfaces for each type. All creation and deletion are based on the encapsulated interfaces, thus facilitating the counting of creations and deletions for each type of object.
[0136] like Figure 4 As shown, since all creations are unified within a pre-defined interface, the function call stack for each type of reference can be retrieved during creation. Based on the function call stack, it can be determined which function created each instance, thus counting the number of instances created by each function. Therefore, when encountering resource leaks or excessive usage, it's possible to identify the specific function causing the problem. This involves the following steps:
[0137] Step S131: Obtain the ID of the current thread;
[0138] Step S132: Count the number of references to any wrapper class within the current thread;
[0139] Step S133: Count the number of function resources created, the number of function resources released, and the function resource creation efficiency within the encapsulated class;
[0140] Step S134: If the number of times the object function is called is inconsistent with the number of times the deletion function is called, then it is determined that a resource leak has occurred in the current thread.
[0141] Wherein, the number of function resources created is the number of times the object function is called; the release of function resources is the number of times the deletion function is called; and the function resource creation efficiency is the number of times the object function is called per unit time.
[0142] Specifically, in the three encapsulated classes LocalRef, GlobalRef, and WeakGlobal, creation and delete functions were written for each, and call statistics were inserted into them. Since three types were provided, statistics were collected separately for each type.
[0143] Each time a function is created and called, the current function call stack is accessed. Based on the function call stack, the function call chain and the function call hierarchy can be identified. That is, it can be identified that function A calls function B. Therefore, based on the function call chain, the number of function calls at each level can be identified to determine the number of resources allocated to each function. Thus, when each function call ends, its resource usage can be identified, thereby determining whether there is a resource leak in that function.
[0144] The specific implementation is as follows:
[0145] In the three encapsulated classes LocalRef, GlobalRef, and WeakGlobal, we wrote create and delete functions respectively, and therefore inserted call statistics for them.
[0146] Since three types are provided, please do not count the three types separately.
[0147] By recording the number of times each type of resource is created, each call to the `create` function allocates a resource, and each call to the `delete` function releases a resource. This allows us to track how many types of resources a thread creates, how many resources are created per thread's runtime, and whether all created resources have been released. If the number of `create` and `delete` calls is inconsistent, it indicates a resource leak.
[0148] Each time a thread is created, its current thread ID is retrieved, and the thread ID is used for categorization and statistics.
[0149] It's important to note that when referencing and using Java objects in C++, there are often multiple threads involved, each handling different tasks. Therefore, this article proposes a method to count object creation based on the usage of each thread. Thread-based statistics reveal the usage of each thread, rather than focusing on functions, thus achieving both function-based and thread-based statistics. This is beneficial for troubleshooting and resolving issues more effectively.
[0150] like Figure 5 As shown, in another embodiment, a statistical interface can also be established within the encapsulation class to obtain the name of the Java class and count the number of references corresponding to any reference type in any Java class. Specifically, this includes the following steps:
[0151] Step S141: Obtain any reference interface jobject through the system function GetObjectClass;
[0152] Step S142: Obtain the jclass object of the Java class corresponding to any referenced interface jobject;
[0153] Step S143: Obtain the name retrieval method in the Java class through the system function GetMethodID;
[0154] Step S144: Obtain the corresponding class name based on the name retrieval method in the Java class;
[0155] Step S145: Count the number of function resources created, the number of function resources released, and the efficiency of function resource creation within the Java class.
[0156] Step S146: If the number of times the object function is called is inconsistent with the number of times the deletion function is called, then it is determined that a resource leak has occurred in the current Java class.
[0157] Wherein, the number of function resources created is the number of times the object function is called; the release of function resources is the number of times the deletion function is called; and the function resource creation efficiency is the number of times the object function is called per unit time.
[0158] In the steps above, we provided three encapsulated classes: LocalRef, GlobalRef, and WeakGlobal, each with its own `create` and `delete` functions, and inserted call statistics for each. However, since these functions all pass in or return `jobject` objects, we don't know which specific Java class was created. Therefore, this article provides a method to more precisely identify which Java class was created during creation statistics, and also to determine which class's resource leak occurred. Knowing the Java class makes troubleshooting much easier. The specific implementation is as follows:
[0159] Get the Java class corresponding to the jobject.
[0160] jclass cls = env->GetObjectClass(jobject); The system-provided function GetObjectClass can be used to obtain the Java jclass object corresponding to a jobject.
[0161] Get the method name of a Java class. `jmethodID getName = env->GetMethodID(cls,"getName","()Ljava / lang / String;");`
[0162] The "getName" method in a Java class can be obtained using the system-provided function GetMethodID.
[0163] Get the name of a Java class using its methods.
[0164] The function `jstring name = env->CallObjectMethod(cls, getName)` calls the system function `CallObjectMethod` to invoke methods within a Java class. In this case, `getName` is called to retrieve the name of the corresponding class. Knowing the name of the Java class allows for convenient categorization and viewing of data based on class name during statistical analysis.
[0165] Specifically, two statistical methods were used to count data based on job objects. However, based on this implementation, the count can be based on the creation count of each class, allowing identification of the specific class responsible for the leak. Furthermore, the statistical results from these two methods are mapped to specific Java classes, making troubleshooting much easier for developers.
[0166] In another embodiment, based on the previous basic functionality, the creation of corresponding different types of reference objects is designed, and a creation interface is provided for each type. Therefore, by scanning the code developed by the developer, it is possible to know the calls made to each of the above different objects. Typically, defined local objects are used within functions, while defined global objects and global weak reference objects belong to the global scope and should not be used in local functions. Therefore, scanning can perform an initial check for incorrect creation, thus alerting developers to potential misuse.
[0167] Figure 6 This is a schematic diagram of the unit structure of an embodiment of a thread-based object reference recording device in Java according to the present invention, as shown below. Figure 6 As shown, this embodiment of the invention provides a device for recording object references based on threads in Java, comprising: an acquisition module 210, used to acquire all reference types of C++ holding Java objects in the system; a creation module 220, used to create reference interfaces according to the reference types; a classification module 230, used to classify all reference interfaces to obtain multiple reference interface sets; an encapsulation module 240, used to create encapsulation classes to encapsulate any reference interface set; and a statistics module 250, used to establish statistics interfaces in the encapsulation classes to count the number of references corresponding to any reference type.
[0168] Figure 6The thread-based object reference recording device in Java in this application embodiment has been described from the perspective of modular functional entities. The following is a detailed description of the thread-based object reference recording device in Java in this application embodiment from the perspective of hardware processing. Figure 6 This is a schematic diagram of the hardware structure of a thread-based object reference recording device in Java, provided in an embodiment of the present invention; please refer to [link / reference]. Figure 7 In this embodiment of the invention, a thread-based object reference recording device 300 in Java is recommended as an example, including:
[0169] Input device 301, output device 302, processor 303, and memory 304 (where the number of processors 303 can be one or more). Figure 3 (Taking a processor 303 as an example). In some embodiments of the present invention, the input device 301, the output device 302, the processor 303, and the memory 304 may be connected via a bus or other means, wherein... Figure 3 Taking the example of a connection between China and Israel via a bus.
[0170] Specifically, by calling the operation instructions stored in memory 304, processor 303 executes the following steps:
[0171] Get all reference types of C++ objects that hold Java objects in the system;
[0172] Create reference interfaces according to the reference types described above;
[0173] All referenced interfaces are then categorized to obtain multiple sets of referenced interfaces;
[0174] Create separate wrapper classes to encapsulate any set of referenced interfaces;
[0175] Each of the encapsulated classes establishes a statistics interface to count the number of references corresponding to any reference type. This is achieved by calling the operation instructions stored in memory 304. The processor 303 is also used to execute... Figure 1 Any of the methods in the corresponding embodiments.
[0176] Figure 8 A schematic diagram illustrating a possible embodiment of an electronic device provided by an embodiment of the present invention, such as... Figure 5 As shown.
[0177] In another embodiment, the present invention also provides an electronic device, including a memory 410, a processor 420, and a computer program 411 stored on and executable on the processor 420. When the processor 420 executes the computer program 411, it performs the following steps:
[0178] Get all reference types of C++ objects that hold Java objects in the system;
[0179] Create reference interfaces according to the reference types described above;
[0180] All referenced interfaces are then categorized to obtain multiple sets of referenced interfaces;
[0181] Create separate wrapper classes to encapsulate any set of referenced interfaces;
[0182] Establish a statistics interface within the encapsulation class to count the number of references for any reference type.
[0183] In practical implementation, when the processor 420 executes the computer program 411, it can achieve... Figure 1-5 Any of the corresponding implementation methods in the embodiments.
[0184] Since the electronic device described in this embodiment is a device used to implement a device with a program core code protection function in the embodiment of the present invention, those skilled in the art can understand the specific implementation method and various variations of the electronic device in this embodiment based on the method described in the embodiment of the present invention. Therefore, how the electronic device implements the method in the embodiment of the present invention will not be described in detail here. Any device used by those skilled in the art to implement the method in the embodiment of the present invention is within the scope of protection of this application.
[0185] Figure 9 Please refer to the schematic diagram of a possible embodiment of a computer-readable storage medium provided in this invention. Figure 9 .
[0186] like Figure 9 As shown, this embodiment provides a computer-readable storage medium 500 on which a computer program 511 is stored. When the computer program 511 is executed by a processor, it performs the following steps:
[0187] Get all reference types of C++ objects that hold Java objects in the system;
[0188] Create reference interfaces according to the reference types described above;
[0189] All referenced interfaces are then categorized to obtain multiple sets of referenced interfaces;
[0190] Create separate wrapper classes to encapsulate any set of referenced interfaces;
[0191] Establish a statistics interface within the encapsulation class to count the number of references for any reference type.
[0192] In practical implementation, when the computer program 511 is executed by the processor, it can achieve the following: Figure 1Any of the corresponding implementation methods in the embodiments.
[0193] It should be noted that the descriptions of each embodiment in the above embodiments have different focuses. For parts that are not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.
[0194] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0195] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded computer, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create a machine for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0196] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0197] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0198] This invention also provides a computer program product comprising computer software instructions that, when executed on a processing device, cause the processing device to perform actions such as... Figure 1 The process of protecting the core program code in the corresponding embodiment.
[0199] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions may be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a server or data center that integrates one or more available media. The available medium may be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state disk (SSD)).
[0200] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0201] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection between apparatuses or units through some interfaces, and may be electrical, mechanical, or other forms.
[0202] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0203] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0204] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0205] This invention provides a method for recording object references in Java based on threads. It utilizes all reference types held by C++ for Java objects; establishes statistical interfaces within the encapsulation class to count the number of references for different reference types, thereby obtaining information on resource leaks for any reference type, facilitating investigation based on reference type.
[0206] The technical solution of the present invention has been described above with reference to the preferred embodiments shown in the accompanying drawings. However, it will be readily understood by those skilled in the art that the scope of protection of the present invention is obviously not limited to these specific embodiments. Without departing from the principles of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after these changes or substitutions will all fall within the scope of protection of the present invention.
Claims
1. A method for recording object references based on threads in Java, characterized in that, include: Get all reference types of C++ objects that hold Java objects in the system; Create reference interfaces according to the reference types described above; All referenced interfaces are then categorized to obtain multiple sets of referenced interfaces; Create separate wrapper classes to encapsulate any set of referenced interfaces; Establish a statistics interface within the encapsulation class to count the number of references for any reference type; The process of creating reference interfaces according to the reference type includes: Create a function template that contains at least two template parameters: a Java object and its corresponding reference type. Based on the passed reference type, create a jobject crete function interface, which contains the encapsulation class as a parameter; Calling the jobject crete function interface allows the creation of a reference object corresponding to the reference type.
2. The method for recording object references based on threads in Java according to claim 1, characterized in that, The process of creating a wrapper class to encapsulate any set of referenced interfaces includes: Create multiple classes to encapsulate the reference interface set; Create them respectively within the encapsulation class: An object function that takes a primitive Java object as input and returns a corresponding reference object with a reference count. The delete function deletes the original Java object and returns the corresponding deleted object with a reference count.
3. The method for recording object references based on threads in Java according to claim 2, characterized in that, Also includes: Each of the encapsulation classes creates an inspection interface to check whether the reference type corresponds to the original Java object; wherein the reference type includes local references, global references, and weak global references.
4. The method for recording object references based on threads in Java according to claim 3, characterized in that, The step of establishing a statistical interface within the encapsulation class to count the number of references for any reference type specifically includes: Get the ID of the current thread; Count the number of references to any wrapper class within the current thread; Calculate the number of function resources created, the number of function resources released, and the function resource creation efficiency within the encapsulated class; If the number of times the object function is called is inconsistent with the number of times the deletion function is called, then it is determined that a resource leak has occurred in the current thread.
5. The method for recording object references based on threads in Java according to claim 3, characterized in that, The step of establishing a statistical interface within the encapsulation class to count the number of references for any reference type includes: Any reference interface `jobject` can be obtained through the system function `GetObjectClass`; And obtain the jclass object of the Java class corresponding to any of the referenced interfaces jobject; The name of the method in a Java class is obtained through the system function GetMethodID. The corresponding class name is obtained according to the name retrieval method in the Java class. Calculate the number of function resources created, the number of function resources released, and the efficiency of function resource creation within the Java class. If the number of times the object function is called is inconsistent with the number of times the deletion function is called, then it is determined that a resource leak has occurred in the current Java class.
6. The method for recording object references based on threads in Java according to claim 4 or 5, characterized in that, The number of function resources created is equal to the number of times the object function is called; The release of the function resource is defined as the deletion of the number of function calls; The efficiency of function resource creation is the number of times the object function is called per unit of time.
7. An electronic device comprising an application processor and a memory, characterized in that, The processor is used to implement the method of recording object references based on threads in Java as described in any one of claims 1-6 when implementing the computer management program stored in the memory.
8. A computer-readable storage medium storing computer management programs thereon, characterized in that, When the computer management class program is executed by the processor, it implements the steps of the method for recording object references based on threads in Java as described in any one of claims 1-6.