Memory optimization method, device, terminal, and storage medium
By changing the storage environment of cached object data in the computer language engine and using dynamic memory allocation function to create cache objects in the second environment, the problem of cache objects occupying a large amount of memory in the existing technology is solved, and memory optimization and performance improvement are achieved.
Patent Information
- Application Number
- CN202111248990.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-26
- Publication Date
- 2025-02-18
- Estimated Expiration
- 2041-10-26
AI Technical Summary
Existing computer language engines occupy a large amount of memory resources when using cache objects, resulting in limited memory electronic devices that cannot support cache objects, reducing the compatibility and overall performance of computer language scripts.
By changing the storage environment of cache object data, a dynamic memory allocation function is used to create a cache object in the second environment and transfer the location of its storage area from the first environment to the second environment, thereby reducing memory usage.
Memory optimization is achieved, the compatibility and overall performance of computer language scripts are improved, and the electronic devices with limited memory can support cache objects.
Smart Images

Figure CN113961485B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computers, and in particular to a memory optimization method, device, terminal, and storage medium. Background Art
[0002] With the development of the computer field, people have developed various computer language engines such as Javascript (JS) engines to facilitate practitioners in developing and designing various computer programs. These engines contain various pre-written functions, methods, objects, structures and other contents. When practitioners need them, they can directly use a fixed format to call the corresponding content without rewriting it, which greatly saves time.
[0003] However, general computer language engines need to occupy a large amount of memory resources when using cache objects, resulting in electronic devices with limited memory being unable to support cache objects, reducing the compatibility of computer language scripts and affecting overall performance. Summary of the invention
[0004] The embodiments of the present application disclose a memory optimization method, device, terminal, and storage medium, which reduce memory usage by changing the storage environment of cache object data, achieve memory optimization, and improve the compatibility and overall performance of computer language scripts.
[0005] In a first aspect, an embodiment of the present application provides a memory optimization method, characterized by comprising:
[0006] Receiving a creation instruction in a first environment;
[0007] According to the creation instruction, the dynamic memory allocation function in the second environment is called using the underlying interface of the first environment to create a cache object, and when storing the same data, the memory occupied by the first environment is greater than the memory occupied by the second environment;
[0008] Obtaining pointer information for the cache object returned by the dynamic memory allocation function;
[0009] The pointer information is written into a data record pointer in the first environment.
[0010] In a possible design, the creation instruction includes a first memory parameter, and the step of creating a cache object by calling a dynamic memory allocation function in the second environment using a bottom layer interface of the first environment according to the creation instruction includes:
[0011] Calling a dynamic memory allocation function in a second environment using a bottom layer interface of the first environment according to the creation instruction;
[0012] Passing the first memory parameter to the dynamic memory allocation function;
[0013] The dynamic memory allocation function is executed, where the dynamic memory allocation function is used to create the cache object and allocate memory corresponding to the first memory parameter for the cache object in the second environment.
[0014] It should be noted that only when the creation instruction includes the first memory parameter can the program run correctly and the cache object be successfully created.
[0015] Because the memory occupied by the second environment is smaller than that of the first environment when storing data of the same size, transferring the storage area of the cache object from the first environment to the second environment can reduce the memory occupied by the cache object, thereby achieving memory optimization. In addition, through strict function parameter passing and calling processes, the smooth progress of the cache object creation process is ensured.
[0016] In one possible design, after writing the pointer information into the data record pointer in the first environment, the method further includes:
[0017] Obtain a first function and corresponding parameters called by the cache object in the first environment;
[0018] Determine, according to the first function, a method function used by the underlying interface of the first environment;
[0019] Obtaining the parameters of the method function according to the corresponding parameters and the data record pointer;
[0020] The storage area of the cache object in the second environment is accessed according to the method function and the parameters of the method function, and the operation corresponding to the method function is performed in the storage area of the cache object.
[0021] In a possible design, the first function includes any one of a read function, a write function, and a copy function;
[0022] In the case where the first function comprises a read function, the corresponding parameter comprises a first index of an array of the cache object to be accessed;
[0023] In the case where the first function comprises a write function, the corresponding parameter comprises a second subscript of the array of the cache object to be accessed and the first data to be written;
[0024] In case the first function comprises a copy function, the corresponding parameter comprises a target cache object to be copied.
[0025] In this way, different functions are used for the cache objects in the first environment, and the terminal obtains corresponding parameters according to user input or other input, ensuring that each function can obtain correct parameters, thereby ensuring the correct operation of the program.
[0026] In one possible design, when the first function includes a read function, the method function includes an obtain method function;
[0027] In the case where the first function comprises a write function, the method function comprises a set method function;
[0028] In case the first function comprises a replicated function, the method function comprises a replicated method function.
[0029] This establishes a strict correspondence between the first function and the method function, and the program logic is highly rigorous.
[0030] In one possible design, when the method function includes a get method function, the parameters of the method function include the first subscript and the data record pointer;
[0031] In the case where the method function includes a setting method function, the parameters of the method function include the second subscript, the first data and the data record pointer;
[0032] In the case where the method function includes a copy method function, the parameters of the method function include the target cache object data record pointer, the target cache object length, the cache object length and the data record pointer.
[0033] The contents of the parameters passed vary according to the method. Multiple parameters make the terminal's operations on the cache object more diverse and more precise.
[0034] In a possible design, accessing the storage area of the cache object in the second environment according to the method function and the parameters of the method function, and executing the operation corresponding to the method function in the storage area of the cache object includes:
[0035] In the case where the method function is an acquisition method function, executing the acquisition method function, accessing the storage area of the cache object in the second environment through the parameters of the method function, acquiring the second data corresponding to the first subscript in the storage area, and returning the processed second data to the read function;
[0036] In the case where the method function is a setting method function, executing the setting method function, accessing the storage area of the cache object in the second environment through the parameters of the method function, writing the first data into the storage area corresponding to the second subscript, and returning a write success result to the write function;
[0037] In the case where the method function is a copy method function, the copy method function is executed to call the first copy function in the second environment, the execution result of the first copy function is returned to the copy method function, and the execution result of the copy method function is obtained and returned to the copy function.
[0038] By performing corresponding operations on the storage area of the cache object in the second environment for different method functions, multiple processing of the cache object can be achieved, and the ways of utilizing the cache object are increased.
[0039] In a second aspect, an embodiment of the present application provides a memory optimization device, characterized in that it includes:
[0040] A receiving unit, configured to receive a creation instruction in a first environment;
[0041] A first calling unit is used to call a dynamic memory allocation function in a second environment using a bottom layer interface of the first environment according to the creation instruction to create a cache object, and when storing the same data, the memory occupied by the first environment is greater than the memory occupied by the second environment;
[0042] A first acquisition unit, configured to acquire pointer information for the cache object returned by the dynamic memory allocation function;
[0043] A writing unit is used to write the pointer information into the data record pointer in the first environment.
[0044] In a possible design, the first calling unit is specifically used to:
[0045] Calling a dynamic memory allocation function in a second environment using a bottom layer interface of the first environment according to the creation instruction;
[0046] Passing the first memory parameter to the dynamic memory allocation function;
[0047] The dynamic memory allocation function is executed, where the dynamic memory allocation function is used to create the cache object and allocate memory corresponding to the first memory parameter for the cache object in the second environment.
[0048] In a possible design, the memory optimization device further includes:
[0049] A second calling unit, configured to obtain a first function and corresponding parameters called by the cache object in the first environment;
[0050] A determining unit, configured to determine a method function used by an underlying interface of the first environment according to the first function;
[0051] A second acquisition unit, used for acquiring the parameters of the method function according to the corresponding parameters and the data record pointer;
[0052] An execution unit is used to access the storage area of the cache object in the second environment according to the method function and the parameters of the method function, and execute the operation corresponding to the method function in the storage area of the cache object.
[0053] In a possible design, the first function includes any one of a read function, a write function, and a copy function;
[0054] In the case where the first function comprises a read function, the corresponding parameter comprises a first index of an array of the cache object to be accessed;
[0055] In the case where the first function comprises a write function, the corresponding parameter comprises a second subscript of the array of the cache object to be accessed and the first data to be written;
[0056] In case the first function comprises a copy function, the corresponding parameter comprises a target cache object to be copied.
[0057] In one possible design, when the first function includes a read function, the method function includes an obtain method function;
[0058] In the case where the first function comprises a write function, the method function comprises a set method function;
[0059] In case the first function comprises a replicated function, the method function comprises a replicated method function.
[0060] In one possible design, when the method function includes a get method function, the parameters of the method function include the first subscript and the data record pointer;
[0061] In the case where the method function includes a setting method function, the parameters of the method function include the second subscript, the first data and the data record pointer;
[0062] In the case where the method function includes a copy method function, the parameters of the method function include the target cache object data record pointer, the target cache object length, the cache object length and the data record pointer.
[0063] In a possible design, the execution unit is specifically used to:
[0064] In the case where the method function is an acquisition method function, executing the acquisition method function, accessing the storage area of the cache object in the second environment through the parameters of the method function, acquiring the second data corresponding to the first subscript in the storage area, and returning the processed second data to the read function;
[0065] In the case where the method function is a setting method function, executing the setting method function, accessing the storage area of the cache object in the second environment through the parameters of the method function, writing the first data into the storage area corresponding to the second subscript, and returning a write success result to the write function;
[0066] In the case where the method function is a copy method function, the copy method function is executed to call the first copy function in the second environment, the execution result of the first copy function is returned to the copy method function, and the execution result of the copy method function is obtained and returned to the copy function.
[0067] In a third aspect, an embodiment of the present application provides a terminal, characterized in that it includes a processor and a memory, the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the above method.
[0068] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, characterized in that the computer storage medium stores a computer program, and when the computer program is executed by a processor, the processor executes the steps of the above method.
[0069] The terminal of the embodiment of the present application executes a computer program to receive a creation instruction in a first environment, and then uses the underlying interface of the first environment to call the dynamic memory allocation function in the second environment to create a cache object according to the creation instruction. After the cache object is successfully created, the pointer information for the cache object returned by the dynamic memory allocation function is obtained, and the pointer information is written into the data record pointer in the first environment. By changing the storage environment of the cache object data, memory usage is reduced, memory optimization is achieved, and the compatibility and overall performance of the computer language script are improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0070] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the background technology, the following is a brief introduction to the drawings required for use in the embodiments of the present application or the background technology.
[0071] Figure 1a A flowchart of a memory optimization method provided in an embodiment of the present application;
[0072] Figure 1bA flowchart of another memory optimization method provided in an embodiment of the present application;
[0073] Figure 2a A structural diagram of creating a cache object provided in an embodiment of the present application;
[0074] Figure 2b A structural diagram of recording cache object data provided by an embodiment of the present application;
[0075] Figure 2c A structural diagram of using a cache object provided in an embodiment of the present application;
[0076] Figure 2d A structural diagram of a cache object data read provided in an embodiment of the present application;
[0077] Figure 2e A structural diagram of a cache object writing data provided in an embodiment of the present application;
[0078] Figure 2f A structural diagram of a copy cache object data provided by an embodiment of the present application;
[0079] Figure 3 A cache object life cycle flow chart provided for an embodiment of the present application;
[0080] Figure 4 A schematic diagram of the structure of a memory optimization device provided in an embodiment of the present application;
[0081] Figure 5 A schematic diagram of the structure of a terminal provided in an embodiment of the present application. DETAILED DESCRIPTION
[0082] In order to make the objectives, technical solutions and advantages of the present application clearer, the present application will be further described below in conjunction with the accompanying drawings.
[0083] The terms "first" and "second" in the specification, claims and drawings of this application are only used to distinguish different objects, rather than to describe a specific order. 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 limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units that are inherent to these processes, methods, products or devices.
[0084] The "embodiment" mentioned in this article means that the specific features, structures or characteristics described in conjunction with the embodiment can be included in at least one embodiment of the present application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It can be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0085] In the present application, "at least one (item)" means one or more, "more than one" means two or more, "at least two (items)" means two or three and more than three, and "and / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c".
[0086] It should be understood that, although the various steps in the flowchart in the embodiment of the present application are displayed in sequence according to the indication of the arrows, these steps are not necessarily performed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and it can be performed in other orders. Moreover, at least a portion of the steps in the figure may include a plurality of sub-steps or a plurality of stages, and these sub-steps or stages are not necessarily performed at the same time, but can be performed at different times, and their execution order is not necessarily performed in sequence, but can be performed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.
[0087] As used herein, the words "if" and "if" may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is detected" may be interpreted as "when it is determined" or "in response to determining" or "when detecting (stated condition or event)" or "in response to detecting (stated condition or event)", depending on the context.
[0088] It should be noted that in this article, step codes such as S201, S202, etc. are used for the purpose of expressing the corresponding content more clearly and concisely, and do not constitute a substantial limitation on the sequence. When implementing the step, those skilled in the art may execute S202 first and then S201, etc., but these should all be within the scope of protection of this application.
[0089] It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0090] In the subsequent description, the suffixes such as "module", "component" or "unit" used to represent elements are only used to facilitate the description of the present application, and have no specific meanings. Therefore, "module", "component" or "unit" can be used in a mixed manner.
[0091] The terminal may be implemented in various forms. For example, the terminal described in the present application may include smart terminals such as mobile phones, tablet computers, laptop computers, PDAs, portable media players (PMPs), navigation devices, wearable devices, smart bracelets, pedometers, and fixed terminals such as digital TVs and desktop computers.
[0092] In the subsequent description, a personal computer (PC) will be used as an example. Those skilled in the art will understand that, in addition to components specifically used for fixed purposes, the structure according to the embodiments of the present application can also be applied to mobile type terminals.
[0093] The embodiments of the present application provide a memory optimization method, device, terminal, and storage medium. In order to more clearly describe the solution of the present application, the embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application.
[0094] With the development of the computer field, people have developed various computer language engines such as Javascript (JS) engine and Python engine to facilitate practitioners to develop and design various computer programs. These engines contain various pre-written functions, methods, objects, structures and other contents. When practitioners need, they can directly use the fixed format to call the corresponding content without rewriting, which greatly saves time.
[0095] However, general computer language engines need to occupy a large amount of memory resources when using buffer objects, resulting in electronic devices with limited memory being unable to support the buffer objects, reducing the compatibility of computer language scripts and affecting overall performance.
[0096] In response to the above problems, an embodiment of the present application provides a memory optimization method, which reduces memory usage by changing the storage environment of cache object data, achieves memory optimization, and improves the compatibility and overall performance of computer language scripts.
[0097] See also Figure 1a , is a flow chart of a memory optimization method provided in an embodiment of the present application. The memory optimization method is applied to a terminal, and the method includes but is not limited to the following steps.
[0098] S201: Receive a creation instruction in a first environment.
[0099] It should be noted that the first environment is a computer language environment, and the creation instruction refers to an instruction for creating a cache object.
[0100] Specifically, the terminal receives the creation instruction in the first environment, and the user can input the relevant creation command. For example, if a buffer object is to be created, the relevant creation instruction to be input is to create a new object newBuffer(), which is a format specified by the JavaScript engine and is used to prompt the terminal to create a new buffer object. After the terminal determines that there is this flag, it obtains the relevant data input by the user or the data allocated by the system to form a complete creation instruction for the terminal to execute. After obtaining the above creation instruction of the first environment, the terminal can execute the subsequent step S202 according to the above creation instruction.
[0101] The terminal will execute the corresponding operation only after receiving the creation instruction in the first environment, thereby reducing unnecessary operations and saving the memory space of the terminal.
[0102] S202: Calling a dynamic memory allocation function in a second environment using a bottom-level interface of the first environment according to a creation instruction to create a cache object. When storing the same data, the memory occupied by the first environment is greater than the memory occupied by the second environment.
[0103] It should be noted that the second environment is also a computer language environment. The first environment is a JavaScript environment, and the second environment is a C language environment. The environments represented by the first environment and the second environment are not fixed. The above is only one case of an embodiment of the present application. As long as the first environment occupies more memory than the second environment when storing the same data, it is within the protection scope of the present application.
[0104] For details, please refer to Figure 2a , which is a structural diagram of creating a cache object provided by an embodiment of the present application. Since the terminal has received the creation instruction in the first environment, the above creation instruction includes the three parameters of creating a cache object, the memory required for the cache object, and the source required for the cache object in the figure. It should be noted that the creation of the cache object and the memory required for the cache object in the creation instruction are required, and the remaining restriction parameters or conditions can be added by the user or the system according to actual needs. According to the above creation instruction, the underlying interface in the first environment is called, and the underlying interface then calls the dynamic memory allocation function of the second environment according to the creation instruction requirements. The terminal executes the dynamic memory allocation function to create a cache object.
[0105] Because the memory occupied by the C language environment is smaller than that occupied by the JavaScript environment when storing the same data, transferring the storage area of the cache object from the JavaScript environment to the C language environment can reduce the memory occupied by the cache object and achieve memory optimization.
[0106] Optionally, the creation instruction includes a first memory parameter, and the step of using the underlying interface of the first environment to call a dynamic memory allocation function in the second environment to create a cache object according to the creation instruction includes:
[0107] According to the creation instruction, the underlying interface of the first environment is used to call the dynamic memory allocation function in the second environment;
[0108] Passing the first memory parameter to the dynamic memory allocation function;
[0109] The dynamic memory allocation function is executed, where the dynamic memory allocation function is used to create the cache object and allocate memory corresponding to the first memory parameter for the cache object in the second environment.
[0110] It should be noted that the above-mentioned first memory parameter refers to the size of the memory to be applied for the buffer object in the storage area. The above-mentioned first memory parameter is the data that must be included in the creation instruction. Only after obtaining this data, the terminal knows how much memory to apply for the cache object, and determines whether the remaining memory of the terminal meets the application requirements. If not, it returns insufficient memory information for the terminal or user to make corresponding adjustments; if it meets the requirements, then allocate memory of the same size as the first memory parameter to the cache object.
[0111] Without the first memory parameter, the program cannot run correctly and the cache object cannot be created successfully.
[0112] For details, please refer to Figure 2a, is a structural diagram of creating a cache object provided by an embodiment of the present application. The terminal receives a creation instruction in a first environment. The creation instruction includes creating a cache object newBuffer(), the memory Buffer.alloc() required by the cache object, and the source Buffer.from() required by the cache object. When the terminal recognizes the newBuffer() in the creation instruction, it will call the native.malloc() function in the underlying interface, and then pass the first memory parameter carried in Buffer.alloc() to the native.malloc() function; after the native.malloc() function obtains the first memory parameter, the terminal will call the dynamic memory allocation function malloc() in the second environment according to the native.malloc() function. Passing the above-mentioned first memory parameter to the above-mentioned dynamic memory allocation function is to pass the first memory parameter obtained by the native.malloc() function to the malloc() function. After the malloc() function obtains the first memory parameter, the terminal executes the malloc() function, creates a cache object and allocates a storage area of the same size as the first memory parameter in the second environment for the cache object. If the terminal memory is sufficient, the corresponding memory is allocated to the cache object, and the cache object is created successfully. Otherwise, the creation fails and the reason for the failure is prompted.
[0113] The above steps ensure the smooth creation of cache objects through strict function parameter passing and calling processes. Even if it fails, the terminal or user can know the reason for the failure and make corrections, which reduces the trial and error process and saves time.
[0114] S203: Obtain pointer information for the cache object returned by the dynamic memory allocation function.
[0115] Specifically, after successfully creating the cache object, the terminal obtains the pointer information for the cache object returned by the dynamic memory allocation function, which includes pointer information recording the storage area location of the cache object, the storage area size, the data type and other specific information about the cache object.
[0116] S204: Write the pointer information into the data record pointer in the first environment.
[0117] It should be noted that the data record pointer is a pointer that records the storage location and data information of the cache object, and each cache object has a corresponding data record pointer.
[0118] Specifically, Figure 2b As shown in a structural diagram of recording cache object data provided by an embodiment of the present application, after the terminal obtains the pointer information for the cache object returned by the dynamic memory allocation function, the obtained pointer information is written into the data recording pointer (Handle) in the first environment.
[0119] Because the cache object is created in the second environment, and due to some regulations of the terminal program, some objects established in the two environments cannot be used directly. Therefore, if the cache object is to be used directly in the first environment, it cannot be used directly, because the terminal cannot find the area where the cache object is stored, and it is also impossible to operate the cache object. By using the data record pointer, the storage area address and storage area size corresponding to each cache object can be recorded. Then, when using the cache object in the first environment, the cache object created in the second environment can be used with the help of the data record pointer.
[0120] Because each cache object has a corresponding data record pointer, it is guaranteed that each cache object can access the correct storage location when being used, ensuring that various operations on the cache object can be implemented.
[0121] Optional, see Figure 1b A schematic flow chart of another memory optimization method provided by an embodiment of the present application: After step S204, a cache object has been successfully created. The cache object is created because the cache object is to be used. The steps of using the cache object are described in detail as follows.
[0122] S205: Obtain a first function and corresponding parameters called by a cache object in a first environment.
[0123] Specifically, the terminal obtains the first function to be called by the cache object in the first environment, determines what operation the first function is to perform, and then obtains corresponding parameters according to the requirements of the first function, and the corresponding parameters vary according to the different operations to be performed by the first function.
[0124] Optionally, the first function includes any one of a read function, a write function, and a copy function;
[0125] In the case where the first function comprises a read function, the corresponding parameter comprises a first index of the array of the cache object to be accessed;
[0126] In the case where the first function includes a write function, the corresponding parameter includes the second subscript of the array of the cache object to be accessed and the first data to be written;
[0127] In the case where the first function includes a copy function, the corresponding parameter includes a target cache object to be copied.
[0128] In this way, different functions are used for the cache objects in the first environment, and the terminal obtains corresponding parameters according to user input or other input, ensuring that each function can obtain correct parameters, thereby ensuring the correct operation of the program.
[0129] S206: Determine the method function used by the underlying interface of the first environment according to the first function.
[0130] For details, see Figure 2c , which is a structural diagram of using a cache object provided in an embodiment of the present application.
[0131] It should be noted that, because the first function, i.e., the method in the figure, cannot directly access the second environment, it is necessary to use the method function and data record pointer in the underlying interface to access the cache object storage area in the second environment. At the same time, the garbage collection function is reset for each cache object to ensure that when the cache object is cleared, not only the storage area of the cache object in the second environment is released, but also the corresponding data record pointer in the first environment is released, which greatly saves memory space.
[0132] Because the selected functions in the first function are different, the method functions selected by the underlying interface are also different. After determining the first function, the terminal can determine the method function used by the underlying interface of the first environment according to the first function.
[0133] Optionally, in the case where the first function includes a read function, the method function includes an obtain method function;
[0134] In the case where the first function comprises a write function, the method function comprises a set method function;
[0135] In the case where the first function comprises a copy function, the method function comprises a copy method function.
[0136] Specifically, if the first function is the reading function readUInt8(), the corresponding method function is the obtaining method function native.get(); if the first function is the writing function writeUInt8(), the corresponding method function is the setting method function native.set(); if the first function is the copy function copy(), the corresponding method function is the setting method function native.copy().
[0137] In this way, a strict correspondence is established between the first function and the method function, and the program logic is highly rigorous.
[0138] S207: Obtain the parameters of the method function according to the corresponding parameters and the data record pointer.
[0139] Specifically, after determining the method function to be used, the parameters of the method function must be obtained. The parameters of the method function are generally composed of the corresponding parameters of the first function and a data record pointer, and can be changed according to specific needs.
[0140] Optionally, in the case where the method function includes an acquisition method function, the parameters of the method function include the first subscript and the data record pointer;
[0141] In the case where the method function includes a setting method function, the parameters of the method function include the second subscript, the first data and the data record pointer;
[0142] In the case where the method function includes a copy method function, the parameters of the method function include the target cache object data record pointer, the target cache object length, the cache object length and the data record pointer.
[0143] Specifically, in the case where the above method function is a get method function, see Figure 2d , is a structural diagram of a method for reading cache object data provided by an embodiment of the present application. In the figure, we can see that the terminal obtains parameter 1 of the read function and the data record pointer as parameters of the acquisition method function, and the parameter 1 is the first subscript.
[0144] In the case where the above method function is a setting method function, see Figure 2e , is a structural diagram of a cache object writing data provided by an embodiment of the present application. In the figure, we can see that the terminal obtains parameter 1, parameter 2 and data record pointer of the write function as parameters of the setting method function, the above parameter 1 is the above first data, and the above parameter 2 is the above second subscript.
[0145] In the case where the above method function is a copy method function, see Figure 2f , is a structural diagram of a copy cache object data provided by an embodiment of the present application. In the figure, we can see that the terminal obtains parameter 1 of the copy function, and the above parameter 1 is the above target cache object. The terminal finds the target cache object according to parameter 1, and then passes the data record pointer 1 and length 1 of the cache object and the data record pointer 2 and length 2 of the target cache object to the copy method function as parameters. The above data record pointer 1 corresponds to pointer 1 in the figure, the above length 1 corresponds to the method function parameter length 1 in the figure, the above data record pointer 2 corresponds to pointer 2 in the figure, and the above length 2 corresponds to the method function parameter length 2 in the figure.
[0146] The contents of the parameters passed vary according to the method. Multiple parameters make the terminal's operations on the cache object more diverse and more precise.
[0147] S208: Access the storage area of the cache object in the second environment according to the method function and the parameters of the method function, and execute the operation corresponding to the method function in the storage area of the cache object.
[0148] Specifically, different method functions perform different operations in the second environment. After determining the method function and obtaining the parameters, the corresponding operation can be performed in the storage area of the corresponding cache object according to the parameters. The operation includes but is not limited to adding, deleting, checking and modifying the data in the storage area.
[0149] Optionally, the accessing of the storage area of the cache object in the second environment according to the method function and the parameters of the method function, and performing the operation corresponding to the method function in the storage area of the cache object include:
[0150] In the case where the method function is an acquisition method function, the acquisition method function is executed, the storage area of the cache object in the second environment is accessed through the parameters of the method function, the second data corresponding to the first subscript in the storage area is obtained, and the processed second data is returned to the read function;
[0151] In the case where the method function is a setting method function, the setting method function is executed, the storage area of the cache object in the second environment is accessed through the parameters of the method function, the first data is written into the storage area corresponding to the second subscript, and a write success result is returned to the write function;
[0152] In the case where the above method function is a copy method function, the above copy method function is executed to call the first copy function in the above second environment, the execution result of the above first copy function is returned to the above copy method function, and the execution result of the above copy method function is obtained and returned to the above copy function.
[0153] Specifically, in the case where the above method function is a get method function, see Figure 2d The terminal executes the acquisition method function, accesses the storage area of the cache object in the second environment through the data record pointer in the acquisition method function, and then finds the specific location to be read according to parameter 1, obtains the second data stored in the first subscript position in the storage area, and returns the processed second data to the reading function. If the second data is not successfully obtained, the acquisition method function will fail and throw an exception.
[0154] In the case where the above method function is a setting method function, see Figure 2e The terminal executes the setting method function, accesses the storage area of the above-mentioned cache object in the second environment through the data record pointer in the setting method function, and then finds the specific location to be read according to parameter 2, and writes the above-mentioned first data into the above-mentioned storage area corresponding to the above-mentioned second subscript. After the writing is successful, the setting method function returns the writing success result to the above-mentioned writing function, and throws an exception if it fails.
[0155] In the case where the above method function is a copy method function, see Figure 2f , execute the above copy method function to call the first copy function memcpy() in the above second environment, execute the first copy function and return the execution result of the above first copy function to the above copy method function, obtain the execution result of the above copy method function and return it to the above copy function. In the figure, the parameter pointer 1 of the first copy function comes from the pointer 1 of the copy method function, and the pointer 2 comes from the pointer 2 of the copy method function, and the size is the smaller of the length 1 and the length 2 of the copy method function.
[0156] Different method functions perform different operations in the cache object storage area in the second environment, which can realize multiple processing of the cache object and increase the utilization methods of the cache object.
[0157] From all the above, we can see that the life cycle of a cache object is as follows Figure 3 This is shown in a cache object life cycle flow chart provided in an embodiment of the present application.
[0158] After the terminal receives the creation instruction, the cache object life cycle begins, and the terminal applies for memory according to the creation instruction. If the memory application is successful, the newly created cache object is recorded with the data record pointer, and a garbage collection function is created for the cache object. When the garbage collection function count has not returned to zero, the cache object related business continues to be executed. When it returns to zero, the garbage collection function is called to release the storage area corresponding to the cache object in the second environment, and the cache object life cycle ends; if the memory application is unsuccessful, the cache object life cycle ends.
[0159] In order to facilitate better implementation of the above-mentioned solutions of the embodiments of the present application, the present application also provides a terminal for implementing the above-mentioned solutions.
[0160] See also Figure 4 , is a schematic diagram of the structure of a memory optimization device provided in an embodiment of the present application, such as Figure 4 The memory optimization device 40 shown may include: a receiving unit 401, a first calling unit 402, a first obtaining unit 403, and a writing unit 404. The memory optimization device 40 may be used to perform the relevant descriptions in the above method embodiment.
[0161] A receiving unit 401 is used to receive a creation instruction in a first environment;
[0162] A first calling unit 402 is used to call a dynamic memory allocation function in a second environment using a bottom layer interface of the first environment according to the creation instruction to create a cache object, and when storing the same data, the memory occupied by the first environment is greater than the memory occupied by the second environment;
[0163] A first acquisition unit 403 is used to acquire pointer information for the cache object returned by the dynamic memory allocation function;
[0164] The writing unit 404 is used to write the pointer information into the data record pointer in the first environment.
[0165] In a possible design, the first calling unit 402 is specifically used to:
[0166] According to the creation instruction, the underlying interface of the first environment is used to call the dynamic memory allocation function in the second environment;
[0167] Passing the first memory parameter to the dynamic memory allocation function;
[0168] The dynamic memory allocation function is executed, where the dynamic memory allocation function is used to create the cache object and allocate memory corresponding to the first memory parameter for the cache object in the second environment.
[0169] In a possible design, the above device further includes:
[0170] The second calling unit 405 is used to obtain the first function and corresponding parameters called by the cache object in the first environment;
[0171] A determination unit 406 is used to determine a method function used by the underlying interface of the first environment according to the first function;
[0172] A second obtaining unit 407 is used to obtain the parameters of the method function according to the corresponding parameters and the data record pointer;
[0173] The execution unit 408 is used to access the storage area of the cache object in the second environment according to the method function and the parameters of the method function, and execute the corresponding operation of the method function in the storage area of the cache object.
[0174] In a possible design, the first function includes any one of a read function, a write function, and a copy function;
[0175] In the case where the first function comprises a read function, the corresponding parameter comprises a first index of an array of the cache object to be accessed;
[0176] In the case where the first function comprises a write function, the corresponding parameter comprises a second subscript of the array of the cache object to be accessed and the first data to be written;
[0177] In case the first function comprises a copy function, the corresponding parameter comprises a target cache object to be copied.
[0178] In one possible design, when the first function includes a read function, the method function includes an obtain method function;
[0179] In the case where the first function comprises a write function, the method function comprises a set method function;
[0180] In case the first function comprises a replicated function, the method function comprises a replicated method function.
[0181] In one possible design, when the method function includes a get method function, the parameters of the method function include the first subscript and the data record pointer;
[0182] In the case where the method function includes a setting method function, the parameters of the method function include the second subscript, the first data and the data record pointer;
[0183] In the case where the method function includes a copy method function, the parameters of the method function include the target cache object data record pointer, the target cache object length, the cache object length and the data record pointer.
[0184] In a possible design, the execution unit 408 is specifically used to:
[0185] In the case where the method function is an acquisition method function, the acquisition method function is executed, the storage area of the cache object in the second environment is accessed through the parameters of the method function, the second data corresponding to the first subscript in the storage area is obtained, and the processed second data is returned to the read function;
[0186] In the case where the method function is a setting method function, the setting method function is executed, the storage area of the cache object in the second environment is accessed through the parameters of the method function, the first data is written into the storage area corresponding to the second subscript, and a write success result is returned to the write function;
[0187] In the case where the above method function is a copy method function, the above copy method function is executed to call the first copy function in the above second environment, the execution result of the above first copy function is returned to the above copy method function, and the execution result of the above copy method function is obtained and returned to the above copy function.
[0188] It should be noted that the functions of each functional unit in the memory optimization device 40 described in the embodiment of the present application can be specifically implemented according to the method in the above method embodiment, and will not be repeated here.
[0189] Figure 5A schematic diagram of the structure of a terminal provided in an embodiment of the present application, the terminal 50 may have relatively large differences due to different configurations or performances, and may include one or more central processing units (CPU) 502 (for example, one or more processors) and a memory 508, and one or more storage media 507 (for example, one or more mass storage devices) storing application programs 506 or data 505. Among them, the memory 508 and the storage medium 507 can be short-term storage or permanent storage. The program stored in the storage medium 507 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the electronic device. Furthermore, the central processing unit 502 can be configured to communicate with the storage medium 507 to execute a series of instruction operations in the storage medium 507 on the terminal 50. The terminal 50 can be a software running device provided in the present application.
[0190] The terminal 50 may also include one or more power supplies 503, one or more wired or wireless network interfaces 509, one or more input and output interfaces 510, and / or one or more operating systems 504, such as Windows Server TM, Mac OS X TM, Unix TM, Linux TM, FreeBSD TM, etc.
[0191] The steps performed by the software running device in the above embodiment can be based on the Figure 5 Specifically, the central processor 502 can implement Figure 4 The functions of each unit in it.
[0192] The embodiment of the present application also provides a computer-readable storage medium. All or part of the processes in the above method embodiments can be completed by a computer program to instruct the relevant hardware, and the program can be stored in the above computer storage medium. When the program is executed, it can include the processes of the above method embodiments. The computer-readable storage medium includes: read-only memory (ROM) or random access memory (RAM), a disk or an optical disk, and other media that can store program codes.
[0193] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The above-mentioned computer program product includes one or more computer instructions. When the above-mentioned computer program instructions are loaded and executed on a computer, the above-mentioned process or function according to the embodiment of the present application is generated in whole or in part. The above-mentioned computer may be a general-purpose computer, a special-purpose computer, a computer network or other programmable device. The above-mentioned computer instructions may be stored in a computer-readable storage medium or transmitted by the above-mentioned computer-readable storage medium. The above-mentioned computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or a data center that includes one or more available media integrations. The above-mentioned available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk (SSD)), etc.
[0194] To summarize, by implementing the embodiments of the present application, the terminal receives the creation instruction in the first environment by executing a computer program, and then calls the dynamic memory allocation function in the second environment according to the creation instruction using the underlying interface of the first environment to create a cache object. After the cache object is successfully created, the pointer information for the cache object returned by the dynamic memory allocation function is obtained, and the pointer information is written into the data record pointer in the first environment. By changing the storage environment of the cache object data, memory usage is reduced, memory optimization is achieved, and the compatibility and overall performance of the computer language script are improved.
[0195] At the same time, corresponding functions are set for various operations on the cache object, including read function, write function, and copy function to realize the basic use of the cache object. Users can directly call the function without repeated writing, which saves user time.
[0196] The steps in the method of the embodiment of the present application can be adjusted in order, combined and deleted according to actual needs.
[0197] The modules in the device of the embodiment of the present application can be merged, divided and deleted according to actual needs.
[0198] In summary, the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A memory optimization method, characterized in that: include: Receiving a creation instruction in a first environment; According to the creation instruction, the dynamic memory allocation function in the second environment is called using the underlying interface of the first environment to create a cache object, and when storing the same data, the memory occupied by the first environment is greater than the memory occupied by the second environment; Obtaining pointer information for the cache object returned by the dynamic memory allocation function; Writing the pointer information into a data record pointer in the first environment; the data record pointer is a pointer that records the storage location and data information of the cache object, and each cache object has a corresponding data record pointer; the cache object created in the second environment can be used in the first environment with the help of the data record pointer; The creation instruction includes a first memory parameter, the first memory parameter includes the size of the memory applied for by the cache object in the storage area, and the step of using the underlying interface of the first environment to call the dynamic memory allocation function in the second environment to create the cache object according to the creation instruction includes: Calling a dynamic memory allocation function in a second environment using a bottom layer interface of the first environment according to the creation instruction; Passing the first memory parameter to the dynamic memory allocation function; The dynamic memory allocation function is executed, where the dynamic memory allocation function is used to create the cache object and allocate memory corresponding to the first memory parameter for the cache object in the second environment.
2. The method according to claim 1, characterized in that After writing the pointer information into the data record pointer in the first environment, the method further comprises: Obtain a first function and corresponding parameters called by the cache object in the first environment; Determine, according to the first function, a method function used by the underlying interface of the first environment; Obtaining the parameters of the method function according to the corresponding parameters and the data record pointer; The storage area of the cache object in the second environment is accessed according to the method function and the parameters of the method function, and the operation corresponding to the method function is performed in the storage area of the cache object.
3. The method according to claim 2, characterized in that The first function includes any one of a read function, a write function, and a copy function; In the case where the first function comprises a read function, the corresponding parameter comprises a first index of an array of the cache object to be accessed; In the case where the first function comprises a write function, the corresponding parameter comprises a second subscript of the array of the cache object to be accessed and the first data to be written; In case the first function comprises a copy function, the corresponding parameter comprises a target cache object to be copied.
4. The method according to claim 3, characterized in that In the case where the first function comprises a read function, the method function comprises a get method function; In the case where the first function comprises a write function, the method function comprises a set method function; In case the first function comprises a replicated function, the method function comprises a replicated method function.
5. The method according to claim 4, characterized in that In the case where the method function comprises a get method function, the parameters of the method function comprise the first subscript and the data record pointer; In the case where the method function includes a setting method function, the parameters of the method function include the second subscript, the first data and the data record pointer; In the case where the method function includes a copy method function, the parameters of the method function include a data record pointer of the copied target cache object, a length of the copied target cache object, a length of the cache object and the data record pointer.
6. The method according to claim 5, characterized in that The step of accessing the storage area of the cache object in the second environment according to the method function and the parameters of the method function, and executing the operation corresponding to the method function in the storage area of the cache object includes: In the case where the method function is an acquisition method function, executing the acquisition method function, accessing the storage area of the cache object in the second environment through the parameters of the method function, acquiring the second data corresponding to the first subscript in the storage area, and returning the processed second data to the read function; In the case where the method function is a setting method function, executing the setting method function, accessing the storage area of the cache object in the second environment through the parameters of the method function, writing the first data into the storage area corresponding to the second subscript, and returning a write success result to the write function; In the case where the method function is a copy method function, the copy method function is executed to call the first copy function in the second environment, the execution result of the first copy function is returned to the copy method function, and the execution result of the copy method function is obtained and returned to the copy function.
7. A memory optimization device, characterized in that: include: A receiving unit, configured to receive a creation instruction in a first environment; A first calling unit, configured to call a dynamic memory allocation function in a second environment using a bottom layer interface of the first environment according to the creation instruction to create a cache object, wherein when storing the same data, the memory occupied by the first environment is greater than the memory occupied by the second environment; A first acquisition unit, configured to acquire pointer information for the cache object returned by the dynamic memory allocation function; A writing unit, used for writing the pointer information into a data record pointer in the first environment; the data record pointer is a pointer that records the storage location and data information of the cache object, and each cache object has a corresponding data record pointer; the cache object created in the second environment can be used in the first environment with the help of the data record pointer; The creation instruction includes a first memory parameter, wherein the first memory parameter includes the size of the memory applied for by the cache object in the storage area, and the first calling unit is specifically used to: Calling a dynamic memory allocation function in a second environment using a bottom layer interface of the first environment according to the creation instruction; Passing the first memory parameter to the dynamic memory allocation function; The dynamic memory allocation function is executed, where the dynamic memory allocation function is used to create the cache object and allocate memory corresponding to the first memory parameter for the cache object in the second environment.
8. A terminal, characterized in that: The method comprises a processor and a memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the method according to any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the processor is caused to perform the method according to any one of claims 1 to 6.