Memory optimization method and device for application program and nonvolatile storage medium
By optimizing the memory layout of Java objects, combining member variables and managing shared references, the problem of Java object memory waste is solved, and memory usage efficiency and performance are improved.
Patent Information
- Application Number
- CN202510756869.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-06
- Publication Date
- 2025-09-19
AI Technical Summary
The traditional Java object memory layout leads to significant memory waste, especially when dealing with a large number of similar objects. The memory alignment waste and duplicate reference storage problems are difficult to solve.
By obtaining the target class of the initial object, determining and combining member variables, and optimizing memory layout, including shared reference management and bytecode operations, the memory footprint is reduced.
Effectively reduce application memory consumption, improve memory usage efficiency, reduce garbage collection frequency, and improve the performance and response speed of Java applications.
Smart Images

Figure CN120670155A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a memory optimization method and device for an application program and a non-volatile storage medium. Background Art
[0002] As software engineering continues to evolve, Java applications are becoming increasingly complex. Object memory usage is becoming a key bottleneck limiting performance. Traditionally, developers have relied on techniques such as reference counting, memory pool management, and image compression to optimize memory usage. However, these approaches are often limited to static memory and struggle to address the challenges posed by dynamic runtime object structures.
[0003] Current technologies obtain memory management information and use it to implement specific memory management methods, which to some extent addresses the issues of memory fragmentation and duplicate data. However, as Java objects in applications grow in size and complexity, even with the help of memory management information, memory optimization remains limited. This is especially true when dealing with large numbers of similar objects, where wasted memory alignment and duplicate reference storage become significant issues.
[0004] To address the above-mentioned problems, no effective solutions have been proposed so far. Summary of the Invention
[0005] The embodiments of the present invention provide a memory optimization method, device and non-volatile storage medium for an application program, so as to at least solve the technical problem that the traditional JAVA object memory layout may cause significant memory waste.
[0006] According to one aspect of an embodiment of the present invention, a memory optimization method for an application is provided, comprising: obtaining a target class to which an initial object belongs, wherein the initial object is located in the memory of a target application; determining, based on the target class, a plurality of initial member variables included in the initial object; determining, based on the data types of the plurality of initial member variables, a plurality of target member variables among the plurality of initial member variables; combining the plurality of target member variables to obtain a combined member variable; and determining, based on the combined member variables, a target object, wherein a memory footprint of the target object is less than a memory footprint of the initial object.
[0007] Optionally, when the data types of multiple initial member variables include reference types, it also includes: traversing the initial reference relationships corresponding to multiple objects in the target application, and determining the initial reference objects referenced by the multiple objects, wherein the multiple objects include the initial object; based on the initial reference object, determining the reference object instances corresponding to the multiple objects; judging whether the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to other objects, wherein the other objects are all objects in the multiple objects except the initial object; when the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to the other objects, determining the same reference object instance; based on the same reference object instance, determining the first target reference object; based on the first target reference object, determining the first target reference relationship corresponding to the initial object.
[0008] Optionally, the cumulative number of calls of the first target reference relationship is obtained; when the cumulative number of calls exceeds a first preset range, the second target reference object is determined based on the same reference object instance; based on the second target reference object, the second target reference relationship corresponding to the initial object is determined.
[0009] Optionally, based on the data types of each of the multiple initial member variables, multiple target member variables among the multiple initial member variables are determined, including: based on the data types of each of the multiple initial member variables, determining the target memory usage of each of the multiple initial member variables; based on the target memory usage, sorting the multiple initial member variables to obtain an arrangement order of the multiple initial member variables; and determining the multiple target member variables based on the arrangement order.
[0010] Optionally, based on the data types of the multiple initial member variables, the target memory usage of the multiple initial member variables is determined, including: when the data type is a basic type, based on a preset memory usage rule, determining the target memory usage; or when the data type is a reference type, based on a preset memory usage rule, determining the basic memory usage of the initial member variables; determining the reference memory usage of the objects referenced in the initial member variables; and determining the target memory usage based on the basic memory usage and the reference memory usage.
[0011] Optionally, multiple target member variables are combined to obtain a combined member variable, including: obtaining initial bytecodes of the multiple target member variables; generating target bytecodes corresponding to the combined member variables based on the initial bytecodes; determining a target position where the initial bytecodes are located; and replacing the initial bytecodes with the target bytecodes at the target position to obtain the combined member variables.
[0012] Optionally, the real-time memory usage of the target object is obtained; when the real-time memory usage exceeds a second preset range, the combined member variables are split and restored to multiple target member variables.
[0013] According to another aspect of an embodiment of the present invention, a memory optimization device for an application is also provided, including: an acquisition module, used to acquire a target class to which an initial object belongs, wherein the initial object is located in the memory of a target application; a first determination module, used to determine a plurality of initial member variables included in the initial object based on the target class; a second determination module, used to determine a plurality of target member variables among the plurality of initial member variables based on the data types of the plurality of initial member variables; a combination module, used to combine the plurality of target member variables to obtain a combined member variable; and a third determination module, used to determine a target object based on the combined member variable, wherein the memory footprint of the target object is less than the memory footprint of the initial object.
[0014] According to another aspect of an embodiment of the present invention, a non-volatile storage medium is provided, which includes a stored program, wherein when the program is running, the device where the non-volatile storage medium is located is controlled to execute any one of the above-mentioned memory optimization methods for application programs.
[0015] According to another aspect of an embodiment of the present invention, a computer device is provided. The computer device includes a processor, and the processor is used to run a program. When the program is running, any one of the above-mentioned memory optimization methods for application programs is executed.
[0016] According to yet another aspect of an embodiment of the present invention, a computer program product is provided, including a computer program. When the computer program is executed by a processor, the computer program implements any one of the above-mentioned memory optimization methods for an application program.
[0017] In an embodiment of the present invention, a memory optimization method for an application is adopted, which obtains a target class to which an initial object belongs, wherein the initial object is located in the memory of a target application; based on the target class, multiple initial member variables included in the initial object are determined; based on the data types of the multiple initial member variables, multiple target member variables among the multiple initial member variables are determined; the multiple target member variables are combined to obtain combined member variables; based on the combined member variables, a target object is determined, wherein the memory footprint of the target object is less than the memory footprint of the initial object, thereby achieving the purpose of effectively reducing the memory consumption of the application, thereby realizing the technical effect of improving the memory usage efficiency of the application, and further solving the technical problem that the traditional JAVA object memory layout will cause significant memory waste. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0019] Figure 1 A hardware structure block diagram of a computer terminal for implementing a memory optimization method for an application program is shown;
[0020] Figure 2 1 is a flow chart of a memory optimization method for an application program according to an embodiment of the present invention;
[0021] Figure 3 1 is a flow chart of a memory optimization method based on Java object structure analysis according to an optional embodiment of the present invention;
[0022] Figure 4 4 is a structural block diagram of a memory optimization device for an application program provided according to an embodiment of the present invention. DETAILED DESCRIPTION
[0023] In order to enable those skilled in the art to better understand the solutions of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of the present invention.
[0024] It should be noted that the terms "first", "second", etc. in the description and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0025] According to an embodiment of the present invention, an embodiment of a memory optimization method for an application is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0026] The method embodiment provided in the first embodiment of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Figure 1 FIG. 1 shows a hardware structure block diagram of a computer terminal for implementing a memory optimization method for an application program. Figure 1 As shown, the computer terminal 10 may include one or more (illustrated as 102a, 102b, ..., 102n in the figure) processors (the processor may include but is not limited to a microprocessor MCU or a programmable logic device FPGA and other processing devices), a memory 104 for storing data. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the BUS bus), a network interface, a power supply and / or a camera. It will be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above electronic device. Figure 1 More or fewer components than shown, or with Figure 1 Different configurations shown.
[0027] It should be noted that the one or more processors and / or other data processing circuits described above may generally be referred to herein as "data processing circuitry." The data processing circuitry may be embodied in whole or in part as software, hardware, firmware, or any other combination thereof. Furthermore, the data processing circuitry may be a single, independent processing module, or may be incorporated in whole or in part into any of the other components of the computer terminal 10. As described in the embodiments of the present application, the data processing circuitry serves as a processor control (e.g., selection of a variable resistor terminal path connected to an interface).
[0028] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the memory optimization method for application programs in the embodiments of the present invention. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory 104, that is, implementing the memory optimization method for application programs described above. The memory 104 may include a high-speed random access memory and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory remotely located relative to the processor, and these remote memories may be connected to the computer terminal 10 via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0029] The display may be, for example, a touch screen liquid crystal display (LCD) that enables a user to interact with a user interface of the computer terminal 10 .
[0030] Figure 2 FIG. 1 is a flow chart of a memory optimization method for an application program according to an embodiment of the present invention. Figure 2 As shown, the method includes the following steps:
[0031] Step S201: obtaining a target class to which an initial object belongs, wherein the initial object is located in the memory of a target application.
[0032] In this step, when an object is created and used in a Java program, this object is actually an instance of a specific class, which can be called the "target class." For example, there may be a Person class, and multiple Person objects are created from this class. When performing memory optimization, the focus is on the memory usage of these Person objects, so it is necessary to obtain detailed information about the target class, Person. Reflection is an important feature of the Java language, which allows information such as classes, methods, constructors, and fields to be inspected and modified at runtime (that is, during program execution). The reflection mechanism provides a key means of obtaining target class information. Specifically, the Class object of the class to which the object belongs can be obtained by calling the getClass() method of the object. This Class object represents the target class and encapsulates all metadata about the class, such as member variables, methods, constructors, parent classes, and other information.
[0033] Step S202: determining a plurality of initial member variables included in the initial object based on the target class.
[0034] In this step, once the target class to which the initial object belongs is obtained by calling the getClass() method, Java's reflection API can be used to deeply explore the internal structure of the class, especially its initial member variables. The reflection mechanism allows dynamic acquisition of class information at runtime, which is crucial for analyzing and optimizing memory layout. For each target class, the getDeclaredFields() method can be used to obtain an array of Field objects containing all initial member variables (ie, fields or properties), where each element represents an initial member variable. This includes private initial member variables because the complete memory layout of the object is needed, not just the public part. For example, for each initial member variable, in order to be able to access private member variables, you can call field.setAccessible(true). This step ensures that even private member variables can obtain their relevant information, such as type, name, etc.
[0035] Step S203 : determining a plurality of target member variables among the plurality of initial member variables based on respective data types of the plurality of initial member variables.
[0036] In this step, we focus on contiguous, small member variables. For example, when multiple byte-type member variables are arranged adjacently, due to the 32-bit byte alignment rule, each byte type occupies 4 bytes by default (to align to a 32-bit boundary), which is clearly not optimal for memory usage. Therefore, we can identify target member variables as initial member variables that can be combined into a larger base type (such as int) to reduce memory alignment waste.
[0037] Step S204: combine multiple target member variables to obtain a combined member variable.
[0038] In this step, in order to overcome the waste of memory alignment, multiple target member variables can be combined to form a new, more efficient combined member variable. For example, if a class contains 4 byte type member variables, these four member variables can be merged into an int type member variable, because the 32 bits of int can fully accommodate these 4 byte values, while avoiding alignment waste. For the original methods of accessing multiple target member variables (such as getter and setter methods), if they exist, they need to be modified. One method is to generate new access methods to adapt to the new memory layout. For example, there were originally 4 getByteField(X) methods (X is 1 to 4), and now you need to create a new getIntField() method. Inside this method, the combined int type value is parsed to obtain the original 4 byte type values. Similarly, similar modifications are required for the setter method.
[0039] Step S205 : determining a target object based on the combined member variables, wherein the memory usage of the target object is smaller than the memory usage of the initial object.
[0040] In this step, the optimized target object should theoretically occupy less memory than the original object. The specific amount depends on the object's structure and data type distribution. For example, for a business object containing many small fields (such as multiple bytes), memory usage could be reduced by up to 30%. This not only reduces overall memory consumption but also reduces the frequency of garbage collection, ultimately improving the performance and responsiveness of Java applications.
[0041] Through the above steps, the purpose of effectively reducing application memory consumption is achieved, thereby achieving the technical effect of improving the memory usage efficiency of the application, and further solving the technical problem that traditional JAVA object memory layout will cause significant memory waste.
[0042] As an optional embodiment, when the data types of multiple initial member variables include reference types, it also includes: traversing the initial reference relationships corresponding to multiple objects in the target application, and determining the initial reference objects referenced by the multiple objects, wherein the multiple objects include the initial object; based on the initial reference object, determining the reference object instances corresponding to the multiple objects; judging whether the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to other objects, wherein the other objects are all objects in the multiple objects except the initial object; when the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to the other objects, determining the same reference object instance; based on the same reference object instance, determining the first target reference object; based on the first target reference object, determining the first target reference relationship corresponding to the initial object.
[0043] Optionally, when the data types of multiple initial member variables include reference types, the focus is not limited to a single initial object, but rather a comprehensive traversal of all object instances belonging to the same class in the target application. Optimization of reference types often involves sharing and management between multiple objects. For each object, the reference relationships of its reference type member variables (such as String, custom object references, etc.) can be collected and analyzed to determine which object instances each object references. Through reflection and access control, the specific object instance pointed to by each reference type member variable can be obtained, which is the prerequisite for determining whether reference sharing can be performed.
[0044] After collecting the reference relationships of all objects, you can compare these reference object instances to see if there are multiple object instances pointing to the same reference object. For example, if the address fields of multiple Person objects all reference the same Address object instance, this constitutes a possibility of sharing. Then, based on this first target reference object, you can redetermine the first target reference relationship of all objects involving the shared reference. This means that when an object uses a reference type member variable, it does not directly reference the object instance, but points to the reference in the shared reference pool. In this way, even if there are many object instances, only one memory copy of the object instance needs to be saved, which greatly saves memory space.
[0045] As an optional embodiment, the cumulative number of calls of the first target reference relationship is obtained; when the cumulative number of calls exceeds a first preset range, the second target reference object is determined based on the same reference object instance; based on the second target reference object, the second target reference relationship corresponding to the initial object is determined.
[0046] Optionally, after the first target reference object (a reference object instance in the shared reference pool) is determined, a call counter can be associated with each such reference object instance. This counter is used to record the total number of times the reference is accessed or called during the execution of the target application. During the operation of the application, each time a reference type member variable of an object is accessed or used, the corresponding call count increment is triggered. Specifically, when the object's getter method is called to obtain a reference to a reference type member variable, the call counter increments. For example, if the Address reference in the Person object is accessed, the counter for the Address instance increases. In order to identify the most frequently used reference object instances, a reference threshold can be pre-set, referred to as the "first preset range." This range is set empirically and is typically adjusted based on the characteristics and performance requirements of the target application to balance optimization effects and system overhead. Once the cumulative number of calls exceeds this preset range, it may indicate a problem with the reference relationship. At this point, the process of determining the second target reference object can be initiated.
[0047] If a shared reference is found to have been accidentally modified, affecting the functionality of multiple objects, a new copy of the shared reference, namely the second target reference object, can be created, and the references of the affected objects can be redirected to this new copy, and the relevant management logic in the shared reference pool (such as updating the reference count, etc.) can be repaired. For example, if the reference object in the shared reference pool is accidentally modified to an invalid object, causing errors in multiple objects that use the shared reference, a new valid reference object can be created, the reference in the shared reference pool can be updated to this new object, and the references of all affected objects can be reset to point to this new reference object, while ensuring that the management logic such as the reference count is updated correctly.
[0048] As an optional embodiment, based on the data types of the multiple initial member variables, multiple target member variables among the multiple initial member variables are determined, including: based on the data types of the multiple initial member variables, determining the target memory usage of the multiple initial member variables; based on the target memory usage, sorting the multiple initial member variables to obtain an arrangement order of the multiple initial member variables; and determining the multiple target member variables based on the arrangement order.
[0049] Optionally, you can first use reflection to obtain the type information of all initial member variables in a Java object. For primitive member variables (such as int, byte, and long), the theoretical minimum memory usage can be calculated directly based on the Java virtual machine's memory model. For reference member variables (such as String and List), in addition to calculating the memory occupied by the reference pointer itself, you also need to estimate the actual memory usage of the referenced object to determine the target memory usage. For example, the actual memory usage of a String member variable is estimated based on the string length and implementation details. After obtaining the target memory usage of each initial member variable, you can sort these member variables by memory usage. The goal of sorting is to provide a reasonable order for subsequent memory layout optimization, which will help reduce memory alignment waste and optimize the overall memory layout of the object. Based on the sorting order, you can identify consecutive small initial member variables as target member variables. For example, you can specifically search for consecutive byte member variables and use a counter to count the number of consecutive byte member variables.
[0050] As an optional embodiment, based on the data types of the multiple initial member variables, the target memory occupancy of each of the multiple initial member variables is determined, including: when the data type is a basic type, based on a preset memory occupancy rule, determining the target memory occupancy; or when the data type is a reference type, based on the preset memory occupancy rule, determining the basic memory occupancy of the initial member variables; determining the reference memory occupancy of the object referenced in the initial member variable; and determining the target memory occupancy based on the basic memory occupancy and the reference memory occupancy.
[0051] Optionally, for basic type member variables (such as int, byte, double, etc.), the determination of their target memory usage follows the preset memory usage rules. If the member variable is of byte type, in Java, the byte type theoretically occupies 1 byte of memory. For the int type, in a 32-bit Java virtual machine environment, the int type occupies a fixed 4 bytes of memory. Similarly, for other basic types (such as short, long, float, double, char, boolean), they are also calculated according to the memory usage specified by the Java memory model. For example, the short type occupies 2 bytes, the long type occupies 8 bytes, and so on.
[0052] For reference type member variables, the first thing to determine is the base memory footprint—that is, the memory size of the reference itself. Regardless of the type of object a reference points to, its base memory footprint is generally fixed. In a 32-bit Java Virtual Machine environment, an object reference typically occupies 4 bytes, while in a 64-bit Java Virtual Machine environment, it occupies 8 bytes. Next, you can estimate the actual memory footprint of the object referenced by the reference type member variable. This includes the header information of the referenced object, the memory usage of primitive type member variables, the memory usage of other reference type member variables, the differences between array and non-array objects, and the memory overhead of other objects referenced by the object. For example, for the String type, its internal structure can be considered to estimate the memory footprint. A String object not only stores character data but also has overhead such as the object header. Assuming that the string is encoded in UTF-8, each character occupies a certain number of bytes on average (for example, 2 bytes on average). Adding the object header (assuming approximately 8 bytes, but the specific amount varies depending on the Java Virtual Machine implementation), if the string length is n, the memory footprint of the String type can be roughly estimated to be 8 + 2 * n bytes. For other reference type objects, similar estimation methods can be used based on their internal structural characteristics. Finally, the target memory usage of a reference type member variable is the sum of the memory occupied by the reference itself and the actual memory usage of the object it refers to.
[0053] As an optional embodiment, multiple target member variables are combined to obtain a combined member variable, including: obtaining initial bytecodes of the multiple target member variables; generating target bytecodes corresponding to the combined member variables based on the initial bytecodes; determining a target position where the initial bytecodes are located; and replacing the initial bytecodes with the target bytecodes at the target position to obtain the combined member variables.
[0054] Optionally, you can first use the ClassReader in the bytecode manipulation framework to read the bytecode of the Java class. This allows operations at the class definition level without modifying the source code or directly modifying the object at runtime. You can use ClassVisitor or other bytecode generation tools to create a new bytecode representation based on the properties of the target member variables. For example, if you want to combine four byte type member variables into one int type member variable, you need to generate a new bytecode representation of the int field. In addition to modifying the bytecode of the field, you also need to modify the bytecode of related access methods (such as getter and setter methods) to adapt to the new member variable layout. For example, the method originally used to access a single byte field now needs to be adjusted to a method to access and operate an int field, which may involve complex bit operations and data unpacking processes.
[0055] Next, the exact locations of the original member variables in the bytecode must be known in order to perform the replacement. This typically involves understanding the bytecode structure and determining the order of each field in the class definition. Using ClassVisitor or related tools, the original bytecode at the target location can be replaced with the target bytecode, thus achieving the desired member variable layout. For example, the bytecode for four byte fields could be replaced with the bytecode for a single int field. In addition to replacing the bytecode for the fields themselves, the bytecode for all methods that depend on those fields must also be updated. This may involve adjusting method logic to ensure that methods continue to behave as expected even after the member variable layout has changed. For example, all locations that previously used getByte() and setByte() methods must now use the appropriate getInt() and setInt() methods, performing the necessary conversions. The modified bytecode can be loaded into memory using a custom class loader or Java's dynamic loading mechanism, allowing objects to be created and executed according to the new bytecode structure.
[0056] As an optional embodiment, the real-time memory usage of the target object is obtained; when the real-time memory usage exceeds a second preset range, the combined member variables are split and restored to multiple target member variables.
[0057] Optionally, during the process of optimizing Java object structures, lightweight monitoring code can be embedded in object construction or key method call points to record and calculate the object's memory usage in real time. These monitoring codes can be simple memory metering functions that are called at different stages of the object's lifecycle to collect data on the object's memory usage. During the optimization process, a second preset range can be pre-set as a warning line for memory usage. This range is based on the object type, application scenario, and expected performance indicators, and is intended to prevent side effects of optimization, such as a surge in memory usage and performance degradation. The runtime monitoring module continuously monitors the real-time memory usage of the target object and compares it with the second preset range. If the real-time memory usage exceeds this preset range, it indicates that the current optimization strategy may be counterproductive, resulting in higher-than-expected memory consumption.
[0058] When it is detected that the real-time memory usage is too high, the runtime monitoring and adjustment module will initiate an emergency plan to reversely split the previous composite member variables and restore them to the state of multiple independent basic type member variables. For example, multiple byte fields were previously merged into an int field, but now this int field is split into multiple byte fields. Bytecode manipulation technology can be used to roll back modifications to the class bytecode and restore it to the original layout. This means removing the bytecodes of the getter and setter methods previously added for the composite member variables, as well as all associated memory layout adjustment codes. At the same time, reinsert or generate the bytecode representations of the basic type member variables that were originally replaced by the composite member variables.
[0059] Additionally, you can insert validation logic into the object's method call chain to check for functional anomalies caused by optimization. For example, within the object's business logic methods, add assertions or conditionals to check the integrity of key data or compliance with business rules. If the object's data structure changes after memory optimization (such as combining multiple byte types into an int type), add validation logic to the methods that operate on this data to ensure data correctness. For example, when parsing combined int type data to obtain the original byte type data, add assertions to ensure that the parsing result meets expectations.
[0060] As an optional embodiment, a memory optimization method based on Java object structure analysis is also provided. Figure 3 : is a flow chart of a memory optimization method based on Java object structure analysis according to an optional embodiment of the present invention, such as Figure 3 As shown, first, the object structure is analyzed and the minimum memory usage of member variables is calculated; then, an optimization strategy is formulated, which may include rearranging basic type variables or shared reference management; the system determines whether shared references are suitable, and if so, a shared reference strategy is formulated and implemented; the object structure is modified through bytecode operations; the object status is monitored at runtime to ensure that the optimization is effective and safe; and finally, memory usage is reduced and program performance is improved.
[0061] Through object structure analysis and customized memory optimization strategies, this optional embodiment can significantly reduce the memory usage of Java objects. Experimental results show that for Java applications containing a large number of small objects, the memory usage can be reduced by 10%-30%, depending on the structural complexity of the objects and the distribution of data. Due to the reduced memory usage, the frequency of garbage collection in Java applications is also reduced accordingly. This helps to reduce pause time during garbage collection and improve the performance and responsiveness of the entire application. The runtime monitoring and adjustment module of this optional embodiment ensures that no new functional issues are introduced while optimizing memory, thereby improving the stability and reliability of Java applications.
[0062] It should be noted that for the aforementioned method embodiments, for simplicity of description, they are all expressed as a series of action combinations. However, those skilled in the art should be aware that the present invention is not limited by the order of the actions described, because according to the present invention, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in this specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present invention.
[0063] Through the description of the above implementation methods, those skilled in the art can clearly understand that the memory optimization method for applications according to the above embodiment can be implemented by means of software plus the necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present invention.
[0064] According to an embodiment of the present invention, there is also provided a device for implementing the above-mentioned memory optimization method for an application program. Figure 4 is a structural block diagram of a memory optimization device for an application according to an embodiment of the present invention, such as Figure 4 As shown, the device includes: an acquisition module 41, a first determination module 42, a second determination module 43, a combination module 44 and a third determination module 45. The device is described below.
[0065] The acquisition module 41 is configured to acquire a target class to which an initial object belongs, wherein the initial object is located in the memory of a target application.
[0066] The first determining module 42 is connected to the acquiring module 41 and is configured to determine a plurality of initial member variables included in the initial object based on the target class.
[0067] The second determining module 43 is connected to the first determining module 42 and is configured to determine a plurality of target member variables among the plurality of initial member variables based on respective data types of the plurality of initial member variables.
[0068] The combining module 44 is connected to the second determining module 43 and is used to combine multiple target member variables to obtain a combined member variable.
[0069] The third determining module 45 is connected to the combining module 44 and is configured to determine a target object based on the combined member variables, wherein the memory footprint of the target object is smaller than the memory footprint of the initial object.
[0070] It should be noted that the acquisition module 41, first determination module 42, second determination module 43, combination module 44, and third determination module 45 described above correspond to steps S201 to S205 in the embodiment. The examples and application scenarios implemented by these modules and corresponding steps are the same, but are not limited to those disclosed in the above embodiment. It should be noted that the above modules, as part of the apparatus, can be run in the computer terminal 10 provided in the embodiment.
[0071] An embodiment of the present invention may provide a computer device. Optionally, in this embodiment, the computer device may be located in at least one of a plurality of network devices in a computer network. The computer device includes a memory and a processor.
[0072] Among them, the memory can be used to store software programs and modules, such as the program instructions / modules corresponding to the memory optimization method and device for application programs in the embodiments of the present invention. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, that is, realizing the above-mentioned memory optimization method for application programs. The memory may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely located relative to the processor, and these remote memories can be connected to the computer terminal via a network. Examples of the above-mentioned network include but are not limited to the Internet, corporate intranet, local area network, mobile communication network and combinations thereof.
[0073] The processor can call the information and application stored in the memory through the transmission device to perform the following steps: obtain the target class to which the initial object belongs, wherein the initial object is located in the memory of the target application; based on the target class, determine multiple initial member variables included in the initial object; based on the data types of the multiple initial member variables, determine multiple target member variables among the multiple initial member variables; combine the multiple target member variables to obtain combined member variables; and determine the target object based on the combined member variables, wherein the memory usage of the target object is less than the memory usage of the initial object.
[0074] Optionally, the processor may also execute the program code of the following steps: when the data types of the respective multiple initial member variables include reference types, it also includes: traversing the initial reference relationships corresponding to the respective multiple objects in the target application, and determining the initial reference objects referenced by the respective multiple objects, wherein the multiple objects include the initial object; based on the initial reference object, determining the reference object instances corresponding to the respective multiple objects; judging whether the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to other objects, wherein the other objects are all objects in the multiple objects except the initial object; when the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to the other objects, determining the same reference object instance; determining the first target reference object based on the same reference object instance; and determining the first target reference relationship corresponding to the initial object based on the first target reference object.
[0075] Optionally, the processor may also execute the program code of the following steps: obtaining the cumulative number of calls of the first target reference relationship; determining the second target reference object based on the same reference object instance when the cumulative number of calls exceeds a first preset range; and determining the second target reference relationship corresponding to the initial object based on the second target reference object.
[0076] Optionally, the processor may also execute the program code of the following steps: determining multiple target member variables among the multiple initial member variables based on the data types of the multiple initial member variables, including: determining the target memory usage of the multiple initial member variables based on the data types of the multiple initial member variables; sorting the multiple initial member variables based on the target memory usage to obtain an arrangement order of the multiple initial member variables; and determining multiple target member variables based on the arrangement order.
[0077] Optionally, the processor may also execute the program code of the following steps: determining the target memory occupancy of each of the multiple initial member variables based on their respective data types, including: when the data type is a basic type, determining the target memory occupancy based on a preset memory occupancy rule; or when the data type is a reference type, determining the basic memory occupancy of the initial member variables based on a preset memory occupancy rule; determining the reference memory occupancy of the object referenced in the initial member variable; and determining the target memory occupancy based on the basic memory occupancy and the reference memory occupancy.
[0078] Optionally, the processor may also execute the following program code: combining multiple target member variables to obtain a combined member variable, including: obtaining initial bytecodes of the multiple target member variables; generating target bytecodes corresponding to the combined member variables based on the initial bytecodes; determining a target position where the initial bytecodes are located; and replacing the initial bytecodes with the target bytecodes at the target position to obtain the combined member variables.
[0079] Optionally, the processor may also execute the program code of the following steps: obtaining the real-time memory usage of the target object; and when the real-time memory usage exceeds a second preset range, splitting the combined member variables and restoring them to multiple target member variables.
[0080] An embodiment of the present invention provides a memory optimization method for an application. The method comprises obtaining a target class to which an initial object belongs, wherein the initial object is located in the memory of a target application; determining multiple initial member variables included in the initial object based on the target class; determining multiple target member variables among the multiple initial member variables based on the data types of the multiple initial member variables; combining the multiple target member variables to obtain combined member variables; and determining a target object based on the combined member variables, wherein the memory usage of the target object is less than the memory usage of the initial object, thereby effectively reducing the memory consumption of the application and achieving the technical effect of improving the memory usage efficiency of the application, thereby resolving the technical problem that the traditional JAVA object memory layout causes significant memory waste.
[0081] A person skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing the hardware related to the terminal device through a program, and the program can be stored in a non-volatile storage medium, which may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0082] The embodiment of the present invention further provides a non-volatile storage medium. Optionally, in this embodiment, the non-volatile storage medium can be used to store program codes executed by the memory optimization method for application programs provided in the embodiment.
[0083] Optionally, in this embodiment, the non-volatile storage medium may be located in any computer terminal in a computer terminal group in a computer network, or in any mobile terminal in a mobile terminal group.
[0084] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: obtaining a target class to which the initial object belongs, wherein the initial object is located in the memory of a target application; based on the target class, determining multiple initial member variables included in the initial object; based on the data types of the multiple initial member variables, determining multiple target member variables among the multiple initial member variables; combining the multiple target member variables to obtain combined member variables; and determining the target object based on the combined member variables, wherein the memory footprint of the target object is less than the memory footprint of the initial object.
[0085] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for executing the following steps: in a case where the data types of each of the multiple initial member variables include a reference type, it also includes: traversing the initial reference relationships corresponding to each of the multiple objects in the target application, and determining the initial reference objects referenced by each of the multiple objects, wherein the multiple objects include the initial object; based on the initial reference object, determining the reference object instances corresponding to each of the multiple objects; judging whether the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to other objects, wherein the other objects are all objects in the multiple objects except the initial object; in a case where the reference object instance corresponding to the initial object is the same as the reference object instances corresponding to the other objects, determining the same reference object instance; based on the same reference object instance, determining the first target reference object; based on the first target reference object, determining the first target reference relationship corresponding to the initial object.
[0086] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for executing the following steps: obtaining the cumulative number of calls of the first target reference relationship; when the cumulative number of calls exceeds a first preset range, determining the second target reference object based on the same reference object instance; based on the second target reference object, determining the second target reference relationship corresponding to the initial object.
[0087] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: determining multiple target member variables among the multiple initial member variables based on the data types of each of the multiple initial member variables, including: determining the target memory usage of each of the multiple initial member variables based on the data types of each of the multiple initial member variables; sorting the multiple initial member variables based on the target memory usage to obtain an arrangement order of the multiple initial member variables; and determining multiple target member variables based on the arrangement order.
[0088] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: determining the target memory occupancy of each of the multiple initial member variables based on their respective data types, including: when the data type is a basic type, determining the target memory occupancy based on a preset memory occupancy rule; or when the data type is a reference type, determining the basic memory occupancy of the initial member variables based on a preset memory occupancy rule; determining the reference memory occupancy of the object referenced in the initial member variable; and determining the target memory occupancy based on the basic memory occupancy and the reference memory occupancy.
[0089] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for executing the following steps: combining multiple target member variables to obtain combined member variables, including: obtaining initial bytecodes of the multiple target member variables; generating target bytecodes corresponding to the combined member variables based on the initial bytecodes; determining a target position where the initial bytecodes are located; and replacing the initial bytecodes with the target bytecodes at the target position to obtain the combined member variables.
[0090] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for executing the following steps: obtaining the real-time memory usage of the target object; when the real-time memory usage exceeds a second preset range, splitting the combined member variables and restoring them to multiple target member variables.
[0091] An embodiment of the present invention also provides a computer program product, including a computer program. Optionally, in this embodiment, when the computer program is executed by a processor, it can achieve: obtaining a target class to which the initial object belongs, wherein the initial object is located in the memory of a target application; based on the target class, determining multiple initial member variables included in the initial object; based on the data types of the multiple initial member variables, determining multiple target member variables among the multiple initial member variables; combining the multiple target member variables to obtain combined member variables; and determining the target object based on the combined member variables, wherein the memory footprint of the target object is less than the memory footprint of the initial object.
[0092] The serial numbers of the above embodiments of the present invention are for description only and do not represent the advantages or disadvantages of the embodiments.
[0093] In the above embodiments of the present invention, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0094] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only exemplary. For example, the division of the units can be a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0095] The units described as separate components may or may not be physically separate, and 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 units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0096] In addition, the functional units in the various embodiments of the present invention may be integrated into a single processing unit, each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0097] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a non-volatile storage medium. Based on this understanding, the technical solution of the present invention, 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. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk, and other media that can store program code.
[0098] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should also be regarded as within the scope of protection of the present invention.
Claims
1. A memory optimization method for an application, characterized in that: include: Obtaining a target class to which an initial object belongs, wherein the initial object is located in the memory of a target application; Based on the target class, determining a plurality of initial member variables included in the initial object; Determining a plurality of target member variables among the plurality of initial member variables based on respective data types of the plurality of initial member variables; Combining the multiple target member variables to obtain a combined member variable; A target object is determined based on the combined member variable, wherein a memory footprint of the target object is smaller than a memory footprint of the initial object.
2. The method according to claim 1, characterized in that In the case where the data types of the plurality of initial member variables include reference types, the method further includes: Traversing initial reference relationships corresponding to a plurality of objects in a target application to determine initial reference objects referenced by the plurality of objects, wherein the plurality of objects include the initial object; Based on the initial reference object, determining the reference object instances corresponding to each of the multiple objects; Determining whether the referenced object instance corresponding to the initial object is the same as the referenced object instances corresponding to other objects, wherein the other objects are all objects in the plurality of objects except the initial object; If the reference object instance corresponding to the initial object is the same as the reference object instance corresponding to other objects, determining the same reference object instance; Determining a first target reference object based on the same reference object instance; Based on the first target reference object, a first target reference relationship corresponding to the initial object is determined.
3. The method according to claim 2, characterized in that Also includes: Obtaining the cumulative number of calls of the first target reference relationship; When the cumulative number of calls exceeds a first preset range, determining a second target reference object based on the same reference object instance; Based on the second target reference object, a second target reference relationship corresponding to the initial object is determined.
4. The method according to claim 1, wherein The determining of a plurality of target member variables among the plurality of initial member variables based on respective data types of the plurality of initial member variables comprises: Determining a target memory usage of each of the plurality of initial member variables based on a data type of each of the plurality of initial member variables; Sort the multiple initial member variables based on the target memory usage to obtain an arrangement order of the multiple initial member variables; Based on the arrangement order, the multiple target member variables are determined.
5. The method according to claim 4, characterized in that The determining, based on the data types of the plurality of initial member variables, the target memory usage of each of the plurality of initial member variables comprises: In a case where the data type is a basic type, determining the target memory occupancy based on a preset memory occupancy rule; or when the data type is a reference type, determining the basic memory occupancy of the initial member variable based on the preset memory occupancy rule; Determine the reference memory usage of the object referenced by the initial member variable; The target memory footprint is determined based on the base memory footprint and the reference memory footprint.
6. The method according to claim 1, characterized in that The combining of the plurality of target member variables to obtain a combined member variable includes: Obtaining initial bytecodes of the multiple target member variables; Based on the initial bytecode, generating a target bytecode corresponding to the composite member variable; Determining a target location of the initial bytecode; At the target position, the initial bytecode is replaced with the target bytecode to obtain the combined member variable.
7. The method according to claim 1, characterized in that Also includes: Obtaining the real-time memory usage of the target object; When the real-time memory usage exceeds a second preset range, the combined member variables are split and restored to the multiple target member variables.
8. A memory optimization device for an application, characterized in that: include: an acquisition module, configured to acquire a target class to which an initial object belongs, wherein the initial object is located in a memory of a target application; A first determining module, configured to determine a plurality of initial member variables included in the initial object based on the target class; A second determining module is configured to determine a plurality of target member variables among the plurality of initial member variables based on respective data types of the plurality of initial member variables; A combining module, configured to combine the plurality of target member variables to obtain a combined member variable; The third determining module is configured to determine a target object based on the combined member variable, wherein the memory footprint of the target object is smaller than the memory footprint of the initial object.
9. A non-volatile storage medium, characterized in that: The non-volatile storage medium includes a stored program, wherein when the program is running, the device where the non-volatile storage medium is located is controlled to execute the memory optimization method for an application according to any one of claims 1 to 7.
10. A computer device, characterized in that: include: memory and processor, The memory stores a computer program; The processor is configured to execute a computer program stored in the memory, and when the computer program is run, the processor is enabled to execute the memory optimization method for an application program according to any one of claims 1 to 7.
11. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the memory optimization method for an application program according to any one of claims 1 to 7 is implemented.