WO2023071158A1 - Memory optimization method and apparatus, terminal, and storage medium - Google Patents

Memory optimization method and apparatus, terminal, and storage medium Download PDF

Info

Publication number
WO2023071158A1
WO2023071158A1 PCT/CN2022/094223 CN2022094223W WO2023071158A1 WO 2023071158 A1 WO2023071158 A1 WO 2023071158A1 CN 2022094223 W CN2022094223 W CN 2022094223W WO 2023071158 A1 WO2023071158 A1 WO 2023071158A1
Authority
WO
WIPO (PCT)
Prior art keywords
function
environment
cache object
method function
memory
Prior art date
Application number
PCT/CN2022/094223
Other languages
French (fr)
Chinese (zh)
Inventor
牛亦泽
Original Assignee
西安广和通无线通信有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 西安广和通无线通信有限公司 filed Critical 西安广和通无线通信有限公司
Publication of WO2023071158A1 publication Critical patent/WO2023071158A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F12/00Accessing, addressing or allocating within memory systems or architectures
    • G06F12/02Addressing or allocation; Relocation
    • G06F12/08Addressing or allocation; Relocation in hierarchically structured memory systems, e.g. virtual memory systems
    • G06F12/0802Addressing of a memory level in which the access to the desired data or data block requires associative addressing means, e.g. caches
    • G06F12/0893Caches characterised by their organisation or structure
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory

Definitions

  • the present application relates to the computer field, and in particular to a memory optimization method, device, terminal, and storage medium.
  • Javascript JavaScript, JS
  • JS Javascript
  • object, structure and other content practitioners can directly use the fixed format to call the corresponding content when needed, without rewriting, which greatly saves time.
  • Embodiments of the present application provide a memory optimization method, device, terminal, and storage medium, which can solve the problems of poor compatibility and poor overall performance of computer language scripts.
  • the embodiment of the present application provides a memory optimization method, including:
  • the creation instruction use the underlying interface of the first environment to call the dynamic memory allocation function in the second environment to create a cache object.
  • the memory occupied by the first environment is larger than that occupied by the second environment.
  • the creation instruction includes a first memory parameter
  • the creating a cache object by using the underlying interface of the first environment to call a dynamic memory allocation function in the second environment according to the creation instruction includes:
  • 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.
  • the program can run correctly and the cache object can be successfully created only when the first memory parameter is included in the creation instruction.
  • the memory occupied by the second environment is smaller than the memory occupied by the first environment, so the memory occupied by the cache object can be reduced by transferring the storage area of the cache object from the first environment to the second environment , realizes the optimization of memory, and also ensures the smooth progress of the cache object creation process through strict function parameter passing process and calling process.
  • the method further includes:
  • the first function includes any one of a read function, a write function, and a copy function
  • the corresponding parameter includes a first subscript of the array of cache objects to be accessed
  • the corresponding parameters include the second subscript of the array of the cache object to be accessed and the first data to be written;
  • the corresponding parameter includes a target cache object to be copied.
  • the method function when the first function includes a read function, the method function includes an obtain method function;
  • the method function includes a set method function
  • the method function includes a copy method function.
  • parameters of the method function include the first subscript and the data record pointer
  • the parameters of the method function include the second subscript, the first data, and the data record pointer;
  • 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.
  • the content of the passed parameters is also different. Multiple parameters make the operation of the terminal on the cached object more diverse and more accurate.
  • the access to 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 method in the storage area of the cache object Function corresponding operations include:
  • the method function includes an obtain method function
  • execute the obtain method function access the storage area of the cache object in the second environment through the parameters of the method function, and obtain all the information stored in the storage area
  • the second data corresponding to the first subscript returning the processed second data to the read function
  • the method function includes a setting method function
  • execute the setting method function access the storage area of the cache object in the second environment through the parameter of the method function, and write the first data Enter the storage area corresponding to the second subscript, and return the write success result to the write function;
  • the method function includes a copy method function
  • execute the copy method function to call the first copy function in the second environment, return the execution result of the first copy function to the copy method function, and obtain The execution result of the copy method function is returned to the copy function.
  • Performing corresponding operations on the storage area of the cache object in the second environment for different method functions can realize various processing of the cache object and increase the way of using the cache object.
  • the embodiment of the present application provides a memory optimization device, including:
  • a receiving unit configured to receive a creation instruction in the first environment
  • the first calling unit is used to use 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.
  • the memory occupied by the first environment is larger than memory occupied by the second environment;
  • a first obtaining unit configured to obtain pointer information for the cache object returned by the dynamic memory allocation function
  • a writing unit configured to write the pointer information into the data record pointer in the first environment.
  • the first calling unit is specifically used for:
  • 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.
  • the memory optimization device further includes:
  • a second calling unit configured to obtain a first function called by the cache object in the first environment and corresponding parameters
  • a determining unit configured to determine the method function used by the bottom layer interface of the first environment according to the first function
  • a second acquiring unit configured to acquire parameters of the method function according to the corresponding parameters and the data record pointer
  • An execution unit configured to access the storage area of the cache object in the second environment according to the method function and parameters of the method function, and execute operations corresponding to the method function in the storage area of the cache object.
  • the first function includes any one of a read function, a write function, and a copy function
  • the corresponding parameter includes a first subscript of the array of cache objects to be accessed
  • the corresponding parameters include the second subscript of the array of the cache object to be accessed and the first data to be written;
  • the corresponding parameter includes a target cache object to be copied.
  • the method function when the first function includes a read function, the method function includes an obtain method function;
  • the method function includes a set method function
  • the method function includes a copy method function.
  • parameters of the method function include the first subscript and the data record pointer
  • the parameters of the method function include the second subscript, the first data, and the data record pointer;
  • 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.
  • the execution unit is specifically used for:
  • the method function includes an obtain method function
  • execute the obtain method function access the storage area of the cache object in the second environment through the parameters of the method function, and obtain all the information stored in the storage area
  • the second data corresponding to the first subscript returning the processed second data to the read function
  • the method function includes a setting method function
  • execute the setting method function access the storage area of the cache object in the second environment through the parameter of the method function, and write the first data Enter the storage area corresponding to the second subscript, and return the write success result to the write function;
  • the method function includes a copy method function
  • execute the copy method function to call the first copy function in the second environment, return the execution result of the first copy function to the copy method function, and obtain The execution result of the copy method function is returned to the copy function.
  • an embodiment of the present application provides a terminal, including 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.
  • the embodiment of the present application provides a computer-readable storage medium, where 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.
  • the terminal in the embodiment of the present application receives the creation instruction in the first environment by executing the computer program, 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 acquired, and the pointer information is written into the data record pointer in the first environment.
  • Fig. 1a is a schematic flow diagram of a memory optimization method provided by the embodiment of the present application.
  • Fig. 1b is a schematic flowchart of another memory optimization method provided by the embodiment of the present application.
  • FIG. 2a is a structural diagram for creating a cache object provided by an embodiment of the present application.
  • FIG. 2b is a structural diagram of record cache object data provided by an embodiment of the present application.
  • FIG. 2c is a structural diagram of using a cache object provided by an embodiment of the present application.
  • FIG. 2d is a structural diagram of reading cached object data provided by an embodiment of the present application.
  • FIG. 2e is a structural diagram of data written into a cache object provided by an embodiment of the present application.
  • FIG. 2f is a structural diagram of copying and caching object data provided by an embodiment of the present application.
  • FIG. 3 is a flow chart of a cache object life cycle provided by an embodiment of the present application.
  • FIG. 4 is a schematic structural diagram of a memory optimization device provided in an embodiment of the present application.
  • FIG. 5 is a schematic structural diagram of a terminal provided in an embodiment of the present application.
  • an embodiment means that a particular feature, structure, or characteristic described in connection with the embodiment may be included in at least one embodiment of the present application.
  • the occurrences of this phrase in various places in the specification are not necessarily all referring to the same embodiment, nor are separate or alternative embodiments mutually exclusive of other embodiments. It is understood explicitly and implicitly by those skilled in the art that the embodiments described herein can be combined with other embodiments.
  • At least one (item) means one or more
  • “multiple” means two or more
  • “at least two (items)” means two or three and three
  • “and/or” is used to describe the association relationship of associated objects, which means that there can be three kinds of relationships, for example, "A and/or B” can mean: only A exists, only B exists, and A and B exist at the same time A case where A and B can be singular or plural.
  • the character “/” generally indicates that the contextual objects are an "or” relationship.
  • “At least one of the following” or similar expressions refer to any combination of these items. For example, at least one item (piece) 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 ".
  • the words “if”, “if” as used herein may be interpreted as “at” or “when” or “in response to determining” or “in response to detecting”.
  • the phrases “if determined” or “if detected (the stated condition or event)” could be interpreted as “when determined” or “in response to the determination” or “when detected (the stated condition or event) )” or “in response to detection of (a stated condition or event)”.
  • step codes such as S201 and S202 are used, the purpose of which is to express the corresponding content more clearly and concisely, and does not constitute a substantive limitation on the order.
  • S202 will be executed first, followed by S201, etc., but these should be within the scope of protection of this application.
  • Terminals may be implemented in various forms.
  • the terminals described in this application may include mobile phones, tablet computers, notebook computers, palmtop computers, personal digital assistants (Personal Digital Assistant, PDA), portable media players (Portable Media Player, PMP), navigation devices, portable Smart terminals such as wearable devices, smart bracelets, and pedometers, as well as fixed terminals such as digital TVs and desktop computers.
  • PDA Personal Digital Assistant
  • PMP portable media players
  • navigation devices portable Smart terminals such as wearable devices, smart bracelets, and pedometers
  • portable Smart terminals such as wearable devices, smart bracelets, and pedometers
  • fixed terminals such as digital TVs and desktop computers.
  • PC personal computer
  • the embodiments of the present application provide a memory optimization method, device, terminal, and storage medium.
  • the embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application.
  • the embodiment of the present application provides a memory optimization method.
  • memory usage is reduced, memory optimization is realized, and the compatibility and overall performance of computer language scripts are improved.
  • FIG. 1 a is a schematic flowchart of a memory optimization method provided by 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.
  • the first environment is a computer language environment
  • the creation instruction refers to an instruction to create a cache object.
  • the terminal receives the creation command in the first environment, and the user can input the relevant creation command.
  • the relevant creation command that needs to be input is the new object new Buffer(), which is the format specified by the JavaScript engine , which is used to prompt the terminal to create a new buffer object, and obtain the relevant data input by the user or data allocated by the system to form a complete creation command for the terminal to execute after the terminal determines that there is this flag.
  • the terminal may execute the subsequent step S202 according to the creation instruction.
  • the terminal will execute the corresponding operation after receiving the creation instruction in the first environment, which reduces unnecessary operations and saves the memory space of the terminal.
  • S202 Use 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.
  • the memory occupied by the first environment is larger than the memory occupied by the second environment.
  • the second environment is also a computer language environment
  • the first environment is a JavaScript environment
  • the second environment is a C language environment.
  • the environments represented by the first environment and the second environment are not fixed, and the above are only embodiments of the present application. In one case, as long as the memory occupied by the first environment is greater than that of the second environment when storing the same data, it is within the protection scope of this application.
  • Figure 2a is a structural diagram for creating a cache object provided by the embodiment of the present application.
  • the above creation instruction includes the creation of the cache object and the cache object in the figure.
  • the three parameters of the required memory and the source of the cached object need to be explained that the creation of the cached object and the memory required by the cached object in the creation command are acceptable, and the remaining limiting parameters or conditions can be added by the user or the system according to actual needs.
  • the memory occupied by the C language environment is smaller than the memory occupied by the JavaScript environment, so when the location of the storage area of the cache object is transferred from the JavaScript environment to the C language environment, the memory occupied by the cache object can be reduced, and the realization of Memory optimization.
  • the above-mentioned creation instruction includes a first memory parameter
  • the above-mentioned creation of a cache object by using the underlying interface of the first environment to call the dynamic memory allocation function in the second environment according to the above-mentioned creation instruction includes:
  • the above dynamic memory allocation function is used to create the above cache object and allocate memory corresponding to the above first memory parameter in the above second environment for the above cache object.
  • the above-mentioned first memory parameter refers to the size of the memory to be applied for in the storage area for the buffer object.
  • the above-mentioned first memory parameter is the data that can be included in the creation command. How much memory the object applies for, and judges whether the remaining memory of the terminal meets the requirements of the application. If it is not satisfied, it will return information about insufficient memory for the terminal or user to make corresponding adjustments; if it is satisfied, allocate the cache object with the same size as the first memory parameter Memory.
  • FIG. 2a is a structural diagram for creating a cache object provided by the embodiment of the present application.
  • the terminal receives the creation instruction in the first environment, and the creation instruction includes creating the cache object new Buffer(), and the cache object.
  • Memory Buffer.alloc(), the source Buffer.from() required by the cache object when the terminal recognizes the new Buffer() in the creation command, it will call the native.malloc() function in the underlying interface, and then transfer the Buffer.
  • the first memory parameter carried in alloc() is passed to the native.malloc() function; after the native.malloc() function obtains the first memory parameter, the terminal will call the dynamic memory allocation in the second environment according to the native.malloc() function
  • the function malloc(), passing the above first memory parameter to the above 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 to create a cache object and allocate 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, it allocates corresponding memory for the cache object, and the cache object is created successfully, otherwise The creation fails and the reason for the failure is prompted.
  • the above steps ensure the smooth progress of the cache object creation process through the strict function parameter passing process and calling process. Even if it fails, the terminal or user can know the cause of the failure and make corrections, which reduces the trial and error process and saves time.
  • the terminal obtains the pointer information for the cache object returned by the dynamic memory allocation function. specific information.
  • the data record pointer is a pointer for recording the storage location and data information of the cache object, and each cache object has a corresponding data record pointer.
  • the terminal may write the pointer information into the data record pointer in the first environment.
  • Figure 2b is a structure diagram for recording cache object data provided by the embodiment of the present application
  • the terminal obtains the pointer information for the cache object returned by the dynamic memory allocation function, it writes the obtained pointer information into the first A data record pointer (Handle) in the environment.
  • the cache object is created in the second environment, and because of some regulations of the terminal program, some objects created in the two environments cannot be used directly, so if you want to use the cache object directly in the first environment, you cannot use it directly, because The terminal cannot find the area where the cache object is stored, so it cannot operate on the cache object.
  • the address and size of the storage area corresponding to each cache object can be recorded by using the data record pointer. Then, when using the cache object in the first environment The cache object created in the second environment can be used by means of the data record pointer.
  • each cache object has a corresponding data record pointer, it ensures that each cache object can access the correct storage location when it is used, and ensures that various operations on the cache object can be implemented.
  • FIG. 1 b is a schematic flowchart of another memory optimization method provided by the embodiment of the present application.
  • the cache object has been successfully created.
  • the reason for creating the cache object is to use the cache object.
  • the steps of using the cache object are described in detail as follows.
  • S205 Obtain a first function called by the cache object in the first environment and corresponding parameters.
  • 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 are different according to the operation to be performed by the first function rather different.
  • the above-mentioned first function includes any one of a read function, a write function, and a copy function
  • the above-mentioned corresponding parameter includes the first subscript of the array of the above-mentioned cache object to be accessed;
  • the above-mentioned corresponding parameters include the second subscript of the array of the above-mentioned cache object to be accessed and the first data to be written;
  • the above-mentioned corresponding parameter includes a target cache object to be copied.
  • S206 Determine the method function used by the bottom layer interface of the first environment according to the first function.
  • FIG. 2c is a structural diagram of using a cache object provided by an embodiment of the present application.
  • the garbage collection function is reset for each cache object to ensure that when the cache object is to be cleared, not only the storage area of the cache object in the second environment will be released, but also the corresponding data record pointer in the first environment will be released. Greatly saves memory space.
  • the terminal can determine the method function used by the bottom interface of the first environment according to the first function.
  • the above-mentioned method function includes an obtaining method function
  • the above-mentioned first function includes a write function
  • the above-mentioned method function includes a setting method function
  • the above-mentioned method function includes a copy method function.
  • 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().
  • the parameters of the method function can be obtained.
  • the parameters of the above method function are generally composed of the corresponding parameters of the first function and the data record pointer, which can be changed according to specific needs.
  • the parameters of the above-mentioned method function include the above-mentioned first subscript and the above-mentioned data record pointer;
  • the parameters of the above-mentioned method function include the above-mentioned second subscript, the above-mentioned first data and the above-mentioned data record pointer;
  • the parameters of the above-mentioned method function include the above-mentioned target cache object data record pointer, the above-mentioned target cache object length, the above-mentioned cache object length and the above-mentioned data record pointer.
  • FIG. 2d is a structure diagram of reading cached object data provided by an embodiment of the present application.
  • the terminal obtains the parameter 1 of the reading function and the data record pointer as the parameters of the obtaining method function, and the above parameter 1 is the above first subscript.
  • the terminal acquires 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.
  • FIG. 2f is a structural diagram of copying cache object data provided by an embodiment of the present application.
  • the terminal obtains the parameter 1 of the copy function.
  • the above parameter 1 is the above target cache object.
  • the terminal finds the target cache object according to parameter 1, and then records the pointer 1 and length 1 of the cache object and the target cache object.
  • the data record pointer 2 and length 2 are passed to the copy method function as parameters.
  • the above data record pointer 1 corresponds to the 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 the pointer 2 in the figure.
  • the above length 2 corresponds to the method function parameter length 2 in the figure.
  • the content of the passed parameters is also different. Multiple parameters make the operation of the terminal on the cached object more diverse and more accurate.
  • 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 above method function in the storage area of the cache object.
  • different method functions perform different operations in the second environment. After the method function is determined and the parameters are obtained, the corresponding operation can be performed on the storage area corresponding to the cache object according to the parameters.
  • the operation includes but is not limited to the storage area data Additions, deletions, checks and changes.
  • steps S201-S208 may be a terminal.
  • accessing the storage area of the above-mentioned cache object in the above-mentioned second environment according to the above-mentioned method function and the parameters of the above-mentioned method function, and performing the corresponding operation of the above-mentioned method function in the storage area of the above-mentioned cache object includes:
  • the above-mentioned method function is an obtaining method function
  • execute the above-mentioned obtaining method function access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameter of the above-mentioned method function, and obtain the value corresponding to the above-mentioned first subscript in the above-mentioned storage area second data, returning the processed second data to the read function;
  • the above-mentioned method function is a setting method function
  • execute the above-mentioned setting method function access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameters of the above-mentioned method function, and write the above-mentioned first data into the above-mentioned second subscript Corresponding to the above storage area, return the write success result to the above write function;
  • the above-mentioned method function is a copy method function
  • the terminal executes the obtaining method function, and accesses the storage area of the above-mentioned cache object in the second environment through the data record pointer in the obtaining method function, and then according to Parameter 1 Find the specific location that needs to be read, obtain the second data stored in the first subscript position in the above storage area, and return the processed second data to the above read function, if the above second data is not successfully obtained , the get method function will fail and throw an exception.
  • the terminal executes the setting method function, accesses the storage area of the above cache object in the second environment through the data record pointer in the setting method function, and then finds according to parameter 2 For the specific location that needs to be read, write 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 will return the successful writing result to the above-mentioned writing function. If it fails, an exception will be thrown.
  • the above-mentioned method function is a copy method function
  • execute the above-mentioned copy method function to call the first copy function memcpy() in the above-mentioned second environment, execute the first copy function and then execute the above-mentioned first copy function
  • the result is returned to the above-mentioned copy method function, and the execution result of the above-mentioned copy method function is obtained and returned to the above-mentioned copy function.
  • the parameter pointer 1 of the first copy function comes from the pointer 1 of the copy method function
  • the pointer 2 comes from the pointer 2 of the copy method function
  • the size is the smaller value among length 1 and length 2 of the copy method function.
  • Different method functions perform different operations on the cache object storage area in the second environment, which can realize various processing on the cache object and increase the utilization mode of the cache object.
  • FIG. 3 is a flow chart of a life cycle of a cache object provided in an embodiment of the present application.
  • the life cycle of the cached object begins, and the terminal applies for memory according to the creation command. If the memory application is successful, it uses the data record pointer to record the newly created cached object, and creates a garbage collection function for the cached object at the same time. If it has not been reset to zero, continue to execute the business related to the cache object, and call the garbage collection function to release the storage area corresponding to the cache object in the second environment when it is reset to zero, and the life cycle of the cache object ends; if the memory application is unsuccessful, the life cycle of the cache object ends.
  • the present application further provides a terminal for implementing the above-mentioned solutions.
  • FIG. 4 is a schematic structural diagram of a memory-optimized device provided in the embodiment of the present application.
  • the memory-optimized device 40 shown in FIG. 4 may include: a receiving unit 401, a first calling unit 402, and a first obtaining unit 403 . Write to the unit 404 .
  • the memory optimization apparatus 40 may be used to execute related descriptions in the foregoing method embodiments. in,
  • a receiving unit 401 configured to receive a creation instruction in the first environment
  • the first calling unit 402 is used to use 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 above creation instruction.
  • the memory occupied by the first environment is larger than that of the second environment.
  • the first obtaining unit 403 is configured to obtain the pointer information for the above-mentioned cache object returned by the above-mentioned dynamic memory allocation function;
  • the writing unit 404 is configured to write the pointer information into the data record pointer in the first environment.
  • the above-mentioned first calling unit 402 is specifically used for:
  • the above dynamic memory allocation function is used to create the above cache object and allocate memory corresponding to the above first memory parameter in the above second environment for the above cache object.
  • the above device also includes:
  • the second calling unit 405 is configured to obtain the first function and corresponding parameters called by the cache object in the first environment;
  • a determining unit 406 configured to determine the method function used by the underlying interface of the first environment according to the first function
  • the second obtaining unit 407 is configured to obtain the parameters of the above-mentioned method function according to the above-mentioned corresponding parameters and the above-mentioned data record pointer;
  • the execution unit 408 is configured 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 corresponding operations of the method function in the storage area of the cache object.
  • the first function includes any one of a read function, a write function, and a copy function
  • the corresponding parameter includes a first subscript of an array of the cache object to be accessed
  • the corresponding parameters include the second subscript of the array of the cache object to be accessed and the first data to be written;
  • the corresponding parameter includes a target cache object to be copied.
  • the method function when the first function includes a read function, the method function includes an obtain method function;
  • the method function includes a set method function
  • the method function includes a copy method function.
  • parameters of the method function include the first subscript and the data record pointer
  • the parameters of the method function include the second subscript, the first data, and the data record pointer;
  • 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.
  • the execution unit 408 is specifically used for:
  • the above-mentioned method function is an obtaining method function
  • execute the above-mentioned obtaining method function access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameter of the above-mentioned method function, and obtain the value corresponding to the above-mentioned first subscript in the above-mentioned storage area second data, returning the processed second data to the read function;
  • the above-mentioned method function is a setting method function
  • execute the above-mentioned setting method function access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameters of the above-mentioned method function, and write the above-mentioned first data into the above-mentioned second subscript Corresponding to the above storage area, return the write success result to the above write function;
  • the above-mentioned method function is a copy method function
  • FIG. 5 is a schematic structural diagram of a terminal provided by 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 (central processing units, CPU) 502 (for example, one or more processors) and memory 508, one or more storage media 507 (eg, one or more mass storage devices) for storing applications 506 or data 505 .
  • the memory 508 and the storage medium 507 may be temporary storage or persistent 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 on the electronic device.
  • the central processing unit 502 may be configured to communicate with the storage medium 507 , and execute a series of instruction operations in the storage medium 507 on the terminal 50 .
  • the terminal 50 may be a software running device provided in this application.
  • Terminal 50 can 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 ServerTM, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, etc.
  • operating systems 504 such as Windows ServerTM, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, etc.
  • the steps performed by the software running device in the foregoing embodiments may be based on the terminal structure shown in FIG. 5 .
  • the central processing unit 502 can realize the functions of each unit in FIG. 4 .
  • 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 computer programs instructing related 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: a read-only memory (read-only memory, ROM) or a random access memory (random access memory, RAM), a magnetic disk or an optical disk, and other media that can store program codes.
  • all or part of them may be implemented by software, hardware, firmware or any combination thereof.
  • software When implemented using software, it may be implemented in whole or in part in the form of a computer program product.
  • the computer program product described above comprises one or more computer instructions.
  • the above-mentioned computer program instructions When the above-mentioned computer program instructions are loaded and executed on the computer, all or part of the above-mentioned processes or functions according to the embodiments of the present application will be generated.
  • the above-mentioned computers may be general-purpose computers, special-purpose computers, computer networks, or other programmable devices.
  • the above-mentioned computer instructions may be stored in a computer-readable storage medium, or transmitted through the above-mentioned computer-readable storage medium.
  • the above-mentioned computer-readable storage medium may be any available medium that can be accessed by a computer, or a data storage device such as a server or a data center integrated with one or more available media.
  • the above available medium may be a magnetic medium (for example, a floppy disk, a hard disk, or a magnetic tape), an optical medium (for example, DVD), or a semiconductor medium (for example, a solid state disk (solid state disk, SSD)) and the like.
  • the terminal receives the creation instruction in the first environment by executing the computer program, and then uses the underlying interface of the first environment to call the dynamic memory allocation function in the second environment according to the creation instruction to create a cache object, after the cache object is successfully created, obtain the pointer information for the cache object returned by the dynamic memory allocation function, write the pointer information into the data record pointer in the first environment, and reduce the memory usage by changing the storage environment of the cache object data.
  • Memory optimization which improves the compatibility and overall performance of computer language scripts.
  • the modules in the device of the embodiment of the present application can be combined, divided and deleted according to actual needs.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Memory System Of A Hierarchy Structure (AREA)

Abstract

Embodiments of the present application provide a memory optimization method and apparatus, a terminal, and a storage medium. The method comprises: receiving a creation instruction in a first environment; calling, according to the creation instruction by using an underlying interface of the first environment, a dynamic memory allocation function in a second environment to create a buffer object, wherein when the same data is stored, a memory occupied by the first environment is greater than a memory occupied by the second environment; acquiring pointer information for the buffer object returned by the dynamic memory allocation function; and writing the pointer information into a data record pointer in the first environment. By changing a storage environment of buffer object data, memory occupation is reduced, thereby implementing memory optimization and improving the compatibility and overall performance of a computer language script.

Description

内存优化方法、装置、终端、存储介质Memory optimization method, device, terminal, storage medium
本申请要求于2021年10月26日提交中国专利局、申请号为2021112489907、发明名称为"内存优化方法、装置、终端、存储介质"的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims the priority of the Chinese patent application with the application number 2021112489907 and the title of the invention "memory optimization method, device, terminal, storage medium" filed with the China Patent Office on October 26, 2021, the entire contents of which are incorporated herein by reference In this application.
技术领域technical field
本申请涉及计算机领域,尤其涉及一种内存优化方法、装置、终端、存储介质。The present application relates to the computer field, and in particular to a memory optimization method, device, terminal, and storage medium.
背景技术Background technique
随着计算机领域的发展,人们开发出了各种计算机语言引擎如爪哇脚本(JavaScript,JS)引擎来方便从业人员开发和设计各种计算机程序,这些引擎中包含各种已经书写好的函数、方法、对象、结构体等内容,从业人员需要时直接使用固定格式调用对应内容即可,无需重新书写,极大节约了时间。With the development of the computer field, people have developed various computer language engines such as Javascript (JavaScript, JS) engines to facilitate practitioners to develop and design various computer programs. These engines contain various written functions and methods. , object, structure and other content, practitioners can directly use the fixed format to call the corresponding content when needed, without rewriting, which greatly saves time.
但是,一般的计算机语言引擎在使用缓存对象时需要占用大量内存资源,导致内存有限的电子设备无法支持缓存对象,降低了计算机语言脚本的兼容性,影响整体性能。However, general computer language engines need to occupy a large amount of memory resources when using cached objects, resulting in electronic devices with limited memory being unable to support cached objects, reducing the compatibility of computer language scripts and affecting overall performance.
发明内容Contents of the invention
本申请实施例提供了一种内存优化方法、装置、终端、存储介质,可以解决计算机语言脚本的兼容性差、整体性能差的问题。Embodiments of the present application provide a memory optimization method, device, terminal, and storage medium, which can solve the problems of poor compatibility and poor overall performance of computer language scripts.
第一方面,本申请实施例提供了一种内存优化方法,包括:In the first aspect, the embodiment of the present application provides a memory optimization method, including:
接收第一环境中的创建指令;receiving a creation instruction in the first environment;
依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,在存储相同数据时,所述第一环境占用的内存大于所述第二环境占用的内存;According to the creation instruction, use the underlying interface of the first environment to call the dynamic memory allocation function in the second environment to create a cache object. When storing the same data, the memory occupied by the first environment is larger than that occupied by the second environment. Memory;
获取所述动态内存分配函数返回的针对所述缓存对象的指针信息;Obtain 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.
在一种可能的设计中,所述创建指令包括第一内存参数,所述依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象包括:In a possible design, the creation instruction includes a first memory parameter, and the creating a cache object by using the underlying interface of the first environment to call a dynamic memory allocation function in the second environment according to the creation instruction includes:
依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数;calling a dynamic memory allocation function in the second environment by using the underlying interface of the first environment according to the creation instruction;
将所述第一内存参数传递给所述动态内存分配函数;passing the first memory parameter to the dynamic memory allocation function;
执行所述动态内存分配函数,所述动态内存分配函数用于创建所述缓存对象并且为所述缓存对象在所述第二环境中分配与所述第一内存参数对应的内存。Executing the dynamic memory allocation function, 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.
需要说明的是,创建指令中包含第一内存参数,程序才能正确运行,缓存对象才能够成功创建。It should be noted that the program can run correctly and the cache object can be successfully created only when the first memory parameter is included in the creation instruction.
因为在存储相同大小的数据时,第二环境占用的内存小于第一环境占用的内存,所以在把缓存对象的存储区的位置从第一环境转移到第二环境,可以减少缓存对象占用的内存,实现了内存的优化,还通过严格的函数参数传递过程和调用过程,确保缓存对象创建过程的顺利进行。Because when storing data of the same size, the memory occupied by the second environment is smaller than the memory occupied by the first environment, so the memory occupied by the cache object can be reduced by transferring the storage area of the cache object from the first environment to the second environment , realizes the optimization of memory, and also ensures the smooth progress of the cache object creation process through strict function parameter passing process and calling process.
在一种可能的设计中,在所述将所述指针信息写入所述第一环境中的数据记录指针之后,该方法还包括:In a possible design, after writing the pointer information into the data record pointer in the first environment, the method further includes:
获取所述第一环境中所述缓存对象调用的第一函数和对应参数;Obtain a first function and corresponding parameters called by the cache object in the first environment;
依据所述第一函数确定所述第一环境的底层接口使用的方法函数;determining the method function used by the bottom layer interface of the first environment according to the first function;
依据所述对应参数和所述数据记录指针获取所述方法函数的参数;Obtaining parameters of the method function according to the corresponding parameters and the data record pointer;
依据所述方法函数和所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,在所述缓存对象的存储区执行所述方法函数对应操作。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 corresponding operation of the method function in the storage area of the cache object.
在一种可能的设计中,所述第一函数包括读取函数、写入函数、复制函数中的任一种;In a possible design, the first function includes any one of a read function, a write function, and a copy function;
在所述第一函数包括读取函数的情况下,所述对应参数包括待访问的所述缓存对象的数组的第一下标;In the case where the first function includes a read function, the corresponding parameter includes a first subscript of the array of cache objects to be accessed;
在所述第一函数包括写入函数的情况下,所述对应参数包括待访问的所述缓存对象的数组的第二下标和待写入的第一数据;In the case where the first function includes a write function, the corresponding parameters include the second subscript of the array of the cache object to be accessed and the first data to be written;
在所述第一函数包括复制函数的情况下,所述对应参数包括被复制的目标缓存对象。Where the first function includes a copy function, the corresponding parameter includes a target cache object to be copied.
这样对于第一环境中缓存对象使用不同的函数,终端就根据用户输入或者其他输入获取对应参数,确保每个函数都能获取到正确的参数,保证了程序的正确运行。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 inputs, so as to ensure that each function can obtain correct parameters and ensure the correct operation of the program.
在一种可能的设计中,在所述第一函数包括读取函数的情况下,所述方法函数包括获得方法函数;In a possible design, when the first function includes a read function, the method function includes an obtain method function;
在所述第一函数包括写入函数的情况下,所述方法函数包括设置方法函数;Where the first function includes a write function, the method function includes a set method function;
在所述第一函数包括复制函数的情况下,所述方法函数包括复制方法函数。Where the first function includes a copy function, the method function includes a copy method function.
使得第一函数和方法函数建立了严格的对应关系,程序逻辑严密性高。A strict corresponding relationship is established between the first function and the method function, and the program logic is highly rigorous.
在一种可能的设计中,在所述方法函数包括获得方法函数的情况下,所述方法函数的参数包括所述第一下标和所述数据记录指针;In a possible design, when the method function includes an obtain method function, parameters of the method function include the first subscript and the data record pointer;
在所述方法函数包括设置方法函数的情况下,所述方法函数的参数包括所述第二下标、所述第一数据和所述数据记录指针;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;
在所述方法函数包括复制方法函数的情况下,所述方法函数的参数包括所述目标缓存对象数据记录指针、所述目标缓存对象长度、所述缓存对象长度和所述数据记录指针。When 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.
根据方法的不同,传递的参数内容也不同,多种参数使终端对缓存对象的操作更加多样,也更加精确。Depending on the method, the content of the passed parameters is also different. Multiple parameters make the operation of the terminal on the cached object more diverse and more accurate.
在一种可能的设计中,所述依据所述方法函数和所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,在所述缓存对象的存储区执行所述方法函数对应操作包括:In a possible design, the access to 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 method in the storage area of the cache object Function corresponding operations include:
在所述方法函数包括获得方法函数的情况下,执行所述获得方法函数,通过所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,获取所述存储区中所述第一下标对应的第二数据,将处理后的所述第二数据返回给所述读取函数;In the case that the method function includes an obtain method function, execute the obtain method function, access the storage area of the cache object in the second environment through the parameters of the method function, and obtain all the information stored in the storage area The second data corresponding to the first subscript, returning the processed second data to the read function;
在所述方法函数包括设置方法函数的情况下,执行所述设置方法函数,通过所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,将所述第一数据写入所述第二下标对应的所述存储区,将写入成功结果返回给所述写入函数;When the method function includes a setting method function, execute the setting method function, access the storage area of the cache object in the second environment through the parameter of the method function, and write the first data Enter the storage area corresponding to the second subscript, and return the write success result to the write function;
在所述方法函数包括复制方法函数的情况下,执行所述复制方法函数调用所述第二环境中的第一复制函数,将所述第一复制函数执行结果返回给所述复制方法函数,获得所述复制方法函数执行结果并返回给所述复制函数。When the method function includes a copy method function, execute the copy method function to call the first copy function in the second environment, return the execution result of the first copy function to the copy method function, and obtain The execution result of the copy method function is returned to the copy function.
对不同的方法函数在第二环境中的缓存对象的存储区执行对应操作,可以实现对缓存对象的多种处理,增加了利用缓存对象的方式。Performing corresponding operations on the storage area of the cache object in the second environment for different method functions can realize various processing of the cache object and increase the way of using the cache object.
第二方面,本申请实施例提供了一种内存优化的装置,包括:In the second aspect, the embodiment of the present application provides a memory optimization device, including:
接收单元,用于接收第一环境中的创建指令;a receiving unit, configured to receive a creation instruction in the first environment;
第一调用单元,用于依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,在存储相同数据时,所述第一环境占用的内存大于所述第二环境占用的内存;The first calling unit is used to use 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. When storing the same data, the memory occupied by the first environment is larger than memory occupied by the second environment;
第一获取单元,用于获取所述动态内存分配函数返回的针对所述缓存对象的指针信息;a first obtaining unit, configured to obtain pointer information for the cache object returned by the dynamic memory allocation function;
写入单元,用于将所述指针信息写入所述第一环境中的数据记录指针。A writing unit, configured to write the pointer information into the data record pointer in the first environment.
在一种可能的设计中,所述第一调用单元,具体用于:In a possible design, the first calling unit is specifically used for:
依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数;calling a dynamic memory allocation function in the second environment by using the underlying interface of the first environment according to the creation instruction;
将所述第一内存参数传递给所述动态内存分配函数;passing the first memory parameter to the dynamic memory allocation function;
执行所述动态内存分配函数,所述动态内存分配函数用于创建所述缓存对象并且为所述缓存对象在所述第二环境中分配与所述第一内存参数对应的内存。Executing the dynamic memory allocation function, 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.
在一种可能的设计中,所述内存优化的装置还包括:In a possible design, the memory optimization device further includes:
第二调用单元,用于获取所述第一环境中所述缓存对象调用的第一函数和对应参数;A second calling unit, configured to obtain a first function called by the cache object in the first environment and corresponding parameters;
确定单元,用于依据所述第一函数确定所述第一环境的底层接口使用的方法函数;A determining unit, configured to determine the method function used by the bottom layer interface of the first environment according to the first function;
第二获取单元,用于依据所述对应参数和所述数据记录指针获取所述方法函数的参数;a second acquiring unit, configured to acquire parameters of the method function according to the corresponding parameters and the data record pointer;
执行单元,用于依据所述方法函数和所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,在所述缓存对象的存储区执行所述方法函数对应操作。An execution unit, configured to access the storage area of the cache object in the second environment according to the method function and parameters of the method function, and execute operations corresponding to the method function in the storage area of the cache object.
在一种可能的设计中,所述第一函数包括读取函数、写入函数、复制函数中的任一种;In a possible design, the first function includes any one of a read function, a write function, and a copy function;
在所述第一函数包括读取函数的情况下,所述对应参数包括待访问的所述缓存对象的数组的第一下标;In the case where the first function includes a read function, the corresponding parameter includes a first subscript of the array of cache objects to be accessed;
在所述第一函数包括写入函数的情况下,所述对应参数包括待访问的所述缓存对象的数组的第二下标和待写入的第一数据;In the case where the first function includes a write function, the corresponding parameters include the second subscript of the array of the cache object to be accessed and the first data to be written;
在所述第一函数包括复制函数的情况下,所述对应参数包括被复制的目标缓存对象。Where the first function includes a copy function, the corresponding parameter includes a target cache object to be copied.
在一种可能的设计中,在所述第一函数包括读取函数的情况下,所述方法函数包括获得方法函数;In a possible design, when the first function includes a read function, the method function includes an obtain method function;
在所述第一函数包括写入函数的情况下,所述方法函数包括设置方法函数;Where the first function includes a write function, the method function includes a set method function;
在所述第一函数包括复制函数的情况下,所述方法函数包括复制方法函数。Where the first function includes a copy function, the method function includes a copy method function.
在一种可能的设计中,在所述方法函数包括获得方法函数的情况下,所述方法函数的参数包括所述第一下标和所述数据记录指针;In a possible design, when the method function includes an obtain method function, parameters of the method function include the first subscript and the data record pointer;
在所述方法函数包括设置方法函数的情况下,所述方法函数的参数包括所述第二下标、所述第一数据和所述数据记录指针;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;
在所述方法函数包括复制方法函数的情况下,所述方法函数的参数包括所述目标缓存对象数据记录指针、所述目标缓存对象长度、所述缓存对象长度和所述数据记录指针。When 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.
在一种可能的设计中,所述执行单元,具体用于:In a possible design, the execution unit is specifically used for:
在所述方法函数包括获得方法函数的情况下,执行所述获得方法函数,通过所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,获取所述存储区中所述第一下标对应的第二数据,将处理后的所述第二数据返回给所述读取函数;In the case that the method function includes an obtain method function, execute the obtain method function, access the storage area of the cache object in the second environment through the parameters of the method function, and obtain all the information stored in the storage area The second data corresponding to the first subscript, returning the processed second data to the read function;
在所述方法函数包括设置方法函数的情况下,执行所述设置方法函数,通过所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,将所述第一数据写入所述第二下标对应的所述存储区,将写入成功结果返回给所述写入函数;When the method function includes a setting method function, execute the setting method function, access the storage area of the cache object in the second environment through the parameter of the method function, and write the first data Enter the storage area corresponding to the second subscript, and return the write success result to the write function;
在所述方法函数包括复制方法函数的情况下,执行所述复制方法函数调用所述第二环境中的第一复制函数,将所述第一复制函数执行结果返回给所述复制方法函数,获得所述复制方法函数执行结果并返回给所述复制函数。When the method function includes a copy method function, execute the copy method function to call the first copy function in the second environment, return the execution result of the first copy function to the copy method function, and obtain The execution result of the copy method function is returned to the copy function.
第三方面,本申请实施例提供了一种终端,包括处理器和存储器,所述存储器存储有计算机程序,所述计算机程序被所述处理器执行时,所述处理器执行上述方法的步骤。In a third aspect, an embodiment of the present application provides a terminal, including 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.
第四方面,本申请实施例提供了一种计算机可读存储介质,所述计算机存储介质存储有计算机程序,所述计算机程序被处理器执行时,使所述处理器执行上述方法的步骤。In a fourth aspect, the embodiment of the present application provides a computer-readable storage medium, where 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.
本申请实施例的终端通过执行计算机程序,接收第一环境中的创建指令,再依据创建指令使用第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,缓存对象创建成功后获取动态内存分配函数返回的针对缓存对象的指针信息,将指针信息写入第 一环境中的数据记录指针。本申请的一个或多个实施例的细节在下面的附图和描述中提出。本申请的其它特征、目的和优点将从说明书、附图以及权利要求书变得明显。The terminal in the embodiment of the present application receives the creation instruction in the first environment by executing the computer program, 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 acquired, and the pointer information is written into the data record pointer in the first environment. The details of one or more embodiments of the application are set forth in the accompanying drawings and the description below. Other features, objects and advantages of the present application will be apparent from the description, drawings and claims.
附图说明Description of drawings
为了更清楚地说明本申请实施例或背景技术中的技术方案,下面将对本申请实施例或背景技术中所需要使用的附图作简单的介绍。In order to more clearly illustrate the technical solutions in the embodiments of the present application or the background art, the following will briefly introduce the drawings that are required in the embodiments of the present application or the background art.
图1a为本申请实施例提供的一种内存优化方法的流程示意图;Fig. 1a is a schematic flow diagram of a memory optimization method provided by the embodiment of the present application;
图1b为本申请实施例提供的另一种内存优化方法的流程示意图;Fig. 1b is a schematic flowchart of another memory optimization method provided by the embodiment of the present application;
图2a为本申请实施例提供的一种创建缓存对象的结构图;FIG. 2a is a structural diagram for creating a cache object provided by an embodiment of the present application;
图2b为本申请实施例提供的一种记录缓存对象数据的结构图;FIG. 2b is a structural diagram of record cache object data provided by an embodiment of the present application;
图2c为本申请实施例提供的一种使用缓存对象的结构图;FIG. 2c is a structural diagram of using a cache object provided by an embodiment of the present application;
图2d为本申请实施例提供的一种读取缓存对象数据的结构图;FIG. 2d is a structural diagram of reading cached object data provided by an embodiment of the present application;
图2e为本申请实施例提供的一种缓存对象写入数据的结构图;FIG. 2e is a structural diagram of data written into a cache object provided by an embodiment of the present application;
图2f为本申请实施例提供的一种复制缓存对象数据的结构图;FIG. 2f is a structural diagram of copying and caching object data provided by an embodiment of the present application;
图3为本申请实施例提供的一种缓存对象生命周期流程图;FIG. 3 is a flow chart of a cache object life cycle provided by an embodiment of the present application;
图4为本申请实施例提供的一种内存优化的装置的结构示意图;FIG. 4 is a schematic structural diagram of a memory optimization device provided in an embodiment of the present application;
图5为本申请实施例提供的一种终端的结构示意图。FIG. 5 is a schematic structural diagram of a terminal provided in an embodiment of the present application.
具体实施方式Detailed ways
为了使本申请的目的、技术方案和优点更加清楚,下面将结合附图对本申请作进一步地描述。In order to make the purpose, technical solution and advantages of the application clearer, the application will be further described below in conjunction with the accompanying drawings.
本申请的说明书、权利要求书及附图中的术语“第一”和“第二”等仅用于区别不同对象,而不是用于描述特定顺序。此外,术语“包括”和“具有”以及它们的任何变形,意图在于覆盖不排他的包含。例如包含了一系列步骤或单元的过程、方法、***、产品或设备等,没有限定于已列出的步骤或单元,而是可选地还包括没有列出的步骤或单元等,或可选地还包括对于这些过程、方法、产品或设备等固有的其它步骤或单元。The terms "first" and "second" in the specification, claims and drawings of the present application are only used to distinguish different objects, not to describe a specific order. Furthermore, the terms "comprising" and "having", as well as any variations thereof, are intended to cover a non-exclusive inclusion. 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 optionally also includes steps or units that are not listed, or optionally It also includes other steps or units inherent to these processes, methods, products, or devices.
在本文中提及的“实施例”意味着,结合实施例描述的特定特征、结构或特性可以包含在本申请的至少一个实施例中。在说明书中的各个位置出现该短语并不一定均是指相同的实施例,也不是与其它实施例互斥的独立的或备选的实施例。本领域技术人员可以显式地和隐式地理解的是,本文所描述的实施例可以与其它实施例相结合。Reference herein to "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment may be included in at least one embodiment of the present application. The occurrences of this phrase in various places in the specification are not necessarily all referring to the same embodiment, nor are separate or alternative embodiments mutually exclusive of other embodiments. It is understood explicitly and implicitly by those skilled in the art that the embodiments described herein can be combined with other embodiments.
在本申请中,“至少一个(项)”是指一个或者多个,“多个”是指两个或两个以上,“至少两个(项)”是指两个或三个及三个以上,“和/或”,用于描述关联对象的关联关系,表示可以存在三种关系,例如,“A和/或B”可以表示:只存在A,只存在B以及同时存在A和B三种情况,其中A,B可以是单数或者复数。字符“/”一般表示前后关联对象是一种“或”的关系。“以下至少一项(个)”或其类似表达,是指这些项中的任意组合。例如,a,b或c中的至少一项(个),可以表示:a,b,c,“a和b”,“a和c”,“b和c”,或“a和b和c”。In this application, "at least one (item)" means one or more, "multiple" means two or more, "at least two (items)" means two or three and three Above, "and/or" is used to describe the association relationship of associated objects, which means that there can be three kinds of relationships, for example, "A and/or B" can mean: only A exists, only B exists, and A and B exist at the same time A case where A and B can be singular or plural. The character "/" generally indicates that the contextual objects are an "or" relationship. "At least one of the following" or similar expressions refer to any combination of these items. For example, at least one item (piece) 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 ".
应该理解的是,虽然本申请实施例中的流程图中的各个步骤按照箭头的指示依次显示,但是这些步骤并不是必然按照箭头指示的顺序依次执行。除非本文中有明确的说明,这些步骤的执行并没有严格的顺序限制,其可以以其他的顺序执行。而且,图中的至少一部分步骤可以包括多个子步骤或者多个阶段,这些子步骤或者阶段并不必然是在同一时刻执行完成,而是可以在不同的时刻执行,其执行顺序也不必然是依次进行,而是可以与其他步骤或者其他步骤的子步骤或者阶段的至少一部分轮流或者交替地执行。It should be understood that although the various steps in the flow chart in the embodiment of the present application are displayed sequentially as indicated by the arrows, these steps are not necessarily executed sequentially in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some of the steps in the figure may include multiple sub-steps or multiple stages, these sub-steps or stages are not necessarily executed at the same time, but may be executed at different times, and the execution order is not necessarily sequential Instead, it may be performed alternately or alternately with at least a part of other steps or sub-steps or stages of other steps.
取决于语境,如在此所使用的词语“如果”、“若”可以被解释成为“在……时”或“当…… 时”或“响应于确定”或“响应于检测”。类似地,取决于语境,短语“如果确定”或“如果检测(陈述的条件或事件)”可以被解释成为“当确定时”或“响应于确定”或“当检测(陈述的条件或事件)时”或“响应于检测(陈述的条件或事件)”。Depending on the context, the words "if", "if" as used herein may be interpreted as "at" or "when" or "in response to determining" or "in response to detecting". Similarly, depending on the context, the phrases "if determined" or "if detected (the stated condition or event)" could be interpreted as "when determined" or "in response to the determination" or "when detected (the stated condition or event) )" or "in response to detection of (a stated condition or event)".
需要说明的是,在本文中,采用了诸如S201、S202等步骤代号,其目的是为了更清楚简要地表述相应内容,不构成顺序上的实质性限制,本领域技术人员在具体实施时,可能会先执行S202后执行S201等,但这些均应在本申请的保护范围之内。It should be noted that, in this article, step codes such as S201 and S202 are used, the purpose of which is to express the corresponding content more clearly and concisely, and does not constitute a substantive limitation on the order. Those skilled in the art may, during specific implementation, S202 will be executed first, followed by S201, etc., but these should be within the scope of protection of this application.
应当理解,此处所描述的具体实施例用以解释本申请,并不用于限定本申请。It should be understood that the specific embodiments described here are used to explain the present application, not to limit the present application.
在后续的描述中,使用用于表示元件的诸如“模块”、“部件”或者“单元”的后缀仅为了有利于本申请的说明,其本身没有特定的意义。因此,“模块”、“部件”或者“单元”可以混合地使用。In the following description, the use of suffixes such as 'module', 'part' or 'unit' for denoting elements is only for facilitating the description of the present application and has no specific meaning by itself. Therefore, 'module', 'part' or 'unit' may be mixedly used.
终端可以以各种形式来实施。例如,本申请中描述的终端可以包括诸如手机、平板电脑、笔记本电脑、掌上电脑、个人数字助理(Personal Digital Assistant,PDA)、便捷式媒体播放器(Portable Media Player,PMP)、导航装置、可穿戴设备、智能手环、计步器等智能终端,以及诸如数字TV、台式计算机等固定终端。Terminals may be implemented in various forms. For example, the terminals described in this application may include mobile phones, tablet computers, notebook computers, palmtop computers, personal digital assistants (Personal Digital Assistant, PDA), portable media players (Portable Media Player, PMP), navigation devices, portable Smart terminals such as wearable devices, smart bracelets, and pedometers, as well as fixed terminals such as digital TVs and desktop computers.
后续描述中将以个人计算机(personal computer,PC)为例进行说明,本领域技术人员将理解的是,除了特别用于固定目的的元件之外,根据本申请的实施方式的构造也能够应用于移动类型的终端。In the subsequent description, a personal computer (PC) will be taken as an example for illustration, and those skilled in the art will understand that, in addition to the components specially used for fixing purposes, the configuration according to the embodiments of the present application can also be applied to Mobile type terminal.
本申请实施例提供了一种内存优化方法、装置、终端、存储介质,为了更清楚的描述本申请的方案,下面结合本申请实施例中的附图对本申请实施例进行描述。The embodiments of the present application provide a memory optimization method, device, terminal, and storage medium. In order to describe the solution of the present application more clearly, the embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application.
随着计算机领域的发展,人们开发出了各种计算机语言引擎如爪哇脚本(JavaScript,JS)引擎、Python引擎等来方便从业人员开发和设计各种计算机程序,这些引擎中包含各种已经书写好的函数、方法、对象、结构体等内容,从业人员需要时直接使用固定格式调用对应内容即可,无需重新书写,极大节约了时间。With the development of the computer field, people have developed various computer language engines such as Javascript (JavaScript, JS) engines, Python engines, etc. to facilitate practitioners to develop and design various computer programs. For functions, methods, objects, structures, etc., practitioners can directly use the fixed format to call the corresponding content when needed, without rewriting, which greatly saves time.
但是,一般的计算机语言引擎在使用缓存(buffer)对象时需要占用大量内存资源,导致内存有限的电子设备无法支持缓存对象,降低了计算机语言脚本的兼容性,影响整体性能。However, general computer language engines need to occupy a large amount of memory resources when using cache (buffer) objects, causing electronic devices with limited memory to be unable to support cache objects, reducing the compatibility of computer language scripts and affecting overall performance.
针对上述问题本申请实施例提供了一种内存优化方法,通过改变缓存对象数据的存储环境,降低内存占用,实现了内存优化,提高了计算机语言脚本的兼容性和整体性能。In view of the above problems, the embodiment of the present application provides a memory optimization method. By changing the storage environment of cached object data, memory usage is reduced, memory optimization is realized, and the compatibility and overall performance of computer language scripts are improved.
请参见图1a,为本申请实施例提供的一种内存优化方法的流程示意图,该内存优化方法应用于终端,该方法包括但不限于以下步骤。Please refer to FIG. 1 a , which is a schematic flowchart of a memory optimization method provided by 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.
S201:接收第一环境中的创建指令。S201: Receive a creation instruction in the first environment.
需要说明的是,第一环境是计算机语言环境,创建指令指的是创建缓存对象的指令。It should be noted that the first environment is a computer language environment, and the creation instruction refers to an instruction to create a cache object.
具体的,终端接收第一环境中的创建指令,用户可以输入相关创建命令,如要新建一个缓存(buffer)对象需要输入的相关创建指令就是新建对象new Buffer(),这是JavaScript引擎规定的格式,用来提示终端要新建一个buffer对象,在终端判定有这个标志后获取用户输入的相关数据或者***分配的数据组成一个完整的创建指令供终端执行。在获得第一环境的上述创建指令后,终端就可根据上述创建指令执行后续步骤S202。Specifically, the terminal receives the creation command in the first environment, and the user can input the relevant creation command. If you want to create a new buffer (buffer) object, the relevant creation command that needs to be input is the new object new Buffer(), which is the format specified by the JavaScript engine , which is used to prompt the terminal to create a new buffer object, and obtain the relevant data input by the user or data allocated by the system to form a complete creation command for the terminal to execute after the terminal determines that there is this flag. After obtaining the creation instruction of the first environment, the terminal may execute the subsequent step S202 according to the creation instruction.
在接收第一环境中的创建指令之后,终端才会执行对应操作,减少了不必要操作的进行,节约了终端的内存空间。The terminal will execute the corresponding operation after receiving the creation instruction in the first environment, which reduces unnecessary operations and saves the memory space of the terminal.
S202:依据创建指令使用第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,在存储相同数据时,上述第一环境占用的内存大于上述第二环境占用的内存。S202: Use 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. When storing the same data, the memory occupied by the first environment is larger than the memory occupied by the second environment.
需要说明的是,第二环境也是计算机语言环境,第一环境为JavaScript环境,第二环境为C语言环境,第一环境和第二环境代表的环境不是固定不变,上述仅为本申请实施例的一种情况,只要满足在存储相同数据时第一环境占用内存大于第二环境,就符合本申请保 护范围。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, and the above are only embodiments of the present application. In one case, as long as the memory occupied by the first environment is greater than that of the second environment when storing the same data, it is within the protection scope of this application.
具体的,可参照图2a,为本申请实施例提供的一种创建缓存对象的结构图,由于终端已经接收第一环境中的创建指令,上述创建指令就包括图中的创建缓存对象、缓存对象需要的内存、缓存对象需要的来源这三个参数,需要说明的是创建指令中创建缓存对象和缓存对象需要的内存是可以的,其余限制参数或条件用户或者***可根据实际需要添加。根据上述创建指令调用第一环境中的底层接口,底层接口再根据创建指令要求调用第二环境的动态内存分配函数,终端执行动态内存分配函数创建缓存对象。Specifically, refer to Figure 2a, which is a structural diagram for creating a cache object provided by the embodiment of the present application. Since the terminal has received the creation instruction in the first environment, the above creation instruction includes the creation of the cache object and the cache object in the figure. The three parameters of the required memory and the source of the cached object need to be explained that the creation of the cached object and the memory required by the cached object in the creation command are acceptable, and the remaining limiting parameters or conditions can be added by the user or the system according to actual needs. Call the underlying interface in the first environment according to the creation instruction above, and then call the dynamic memory allocation function of the second environment according to the creation instruction, and the terminal executes the dynamic memory allocation function to create a cache object.
因为在存储相同的数据时,C语言环境占用的内存小于JavaScript环境占用的内存,所以在把缓存对象的存储区的位置从JavaScript环境转移到C语言环境,可以减少缓存对象占用的内存,实现了内存的优化。Because when storing the same data, the memory occupied by the C language environment is smaller than the memory occupied by the JavaScript environment, so when the location of the storage area of the cache object is transferred from the JavaScript environment to the C language environment, the memory occupied by the cache object can be reduced, and the realization of Memory optimization.
可选的,上述创建指令包括第一内存参数,上述依据上述创建指令使用上述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象包括:Optionally, the above-mentioned creation instruction includes a first memory parameter, and the above-mentioned creation of a cache object by using the underlying interface of the first environment to call the dynamic memory allocation function in the second environment according to the above-mentioned creation instruction includes:
依据上述创建指令使用上述第一环境的底层接口调用第二环境中的动态内存分配函数;Call the dynamic memory allocation function in the second environment by using the underlying interface of the first environment according to the above creation instruction;
将上述第一内存参数传递给上述动态内存分配函数;passing the above-mentioned first memory parameter to the above-mentioned dynamic memory allocation function;
执行上述动态内存分配函数,上述动态内存分配函数用于创建上述缓存对象并且为上述缓存对象在上述第二环境中分配与上述第一内存参数对应的内存。Executing the above dynamic memory allocation function, the above dynamic memory allocation function is used to create the above cache object and allocate memory corresponding to the above first memory parameter in the above second environment for the above cache object.
需要说明的是,上述第一内存参数是指要为buffer对象在存储区申请的内存的大小,上述第一内存参数是创建指令中可以包含的数据,在获得这个数据后终端才知道要为缓存对象申请多少内存,并且判断终端剩余内存是否满足申请的要求,如果不满足就返回内存不足的信息供终端或者用户进行对应的调整;如果满足那就为缓存对象分配与第一内存参数大小相同的内存。It should be noted that the above-mentioned first memory parameter refers to the size of the memory to be applied for in the storage area for the buffer object. The above-mentioned first memory parameter is the data that can be included in the creation command. How much memory the object applies for, and judges whether the remaining memory of the terminal meets the requirements of the application. If it is not satisfied, it will return information about insufficient memory for the terminal or user to make corresponding adjustments; if it is satisfied, allocate the cache object with the same size as the first memory parameter Memory.
没有上述第一内存参数,程序无法正确运行,缓存对象也无法成功创建。Without the above-mentioned first memory parameter, the program cannot run correctly, and the cache object cannot be successfully created.
具体的,也可参照图2a,为本申请实施例提供的一种创建缓存对象的结构图,终端接收第一环境中的创建指令,创建指令包括创建缓存对象new Buffer()、缓存对象需要的内存Buffer.alloc()、缓存对象需要的来源Buffer.from(),终端在识别到创建指令中的new Buffer()时,就会调用底层接口中的native.malloc()函数,再将Buffer.alloc()中携带的第一内存参数传递给native.malloc()函数;native.malloc()函数获得第一内存参数后,终端会根据native.malloc()函数调用第二环境中的动态内存分配函数malloc(),将上述第一内存参数传递给上述动态内存分配函数就是把native.malloc()函数获得的第一内存参数再传递给malloc()函数,malloc()函数获得第一内存参数后,终端执行malloc()函数,创建缓存对象并且为缓存对象在第二环境中分配与第一内存参数相同大小的存储区,如果终端内存足够就为缓存对象分配对应内存,缓存对象创建成功,否则创建失败并提示失败原因。Specifically, refer to FIG. 2a, which is a structural diagram for creating a cache object provided by the embodiment of the present application. The terminal receives the creation instruction in the first environment, and the creation instruction includes creating the cache object new Buffer(), and the cache object. Memory Buffer.alloc(), the source Buffer.from() required by the cache object, when the terminal recognizes the new Buffer() in the creation command, it will call the native.malloc() function in the underlying interface, and then transfer the Buffer. The first memory parameter carried in alloc() is passed to the native.malloc() function; after the native.malloc() function obtains the first memory parameter, the terminal will call the dynamic memory allocation in the second environment according to the native.malloc() function The function malloc(), passing the above first memory parameter to the above 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 to create a cache object and allocate 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, it allocates corresponding memory for the cache object, and the cache object is created successfully, otherwise The creation fails and the reason for the failure is prompted.
上述步骤通过严格的函数参数传递过程和调用过程,确保缓存对象创建过程的顺利进行。即使失败,终端或者用户也可知道失败原因,进行修正,减少了试错过程,节约时间。The above steps ensure the smooth progress of the cache object creation process through the strict function parameter passing process and calling process. Even if it fails, the terminal or user can know the cause of the failure and make corrections, which reduces the trial and error process and saves time.
S203:获取动态内存分配函数返回的针对缓存对象的指针信息。S203: Obtain the pointer information for the cache object returned by the dynamic memory allocation function.
具体的,终端创建缓存对象成功后,终端获取动态内存分配函数返回的针对缓存对象的指针信息,上述指针信息包括记录缓存对象的存储区位置的指针信息、存储区大小、数据类型等关于缓存对象的具体信息。Specifically, after the terminal successfully creates the cache object, the terminal obtains the pointer information for the cache object returned by the dynamic memory allocation function. specific information.
S204:将指针信息写入第一环境中的数据记录指针。S204: Write the pointer information into the data record pointer in the first environment.
需要说明的是,数据记录指针是记录缓存对象存储位置和数据信息的指针,每个缓存对象都有对应的数据记录指针。终端可以将指针信息写入第一环境中的数据记录指针。It should be noted that the data record pointer is a pointer for recording the storage location and data information of the cache object, and each cache object has a corresponding data record pointer. The terminal may write the pointer information into the data record pointer in the first environment.
具体的,如图2b为本申请实施例提供的一种记录缓存对象数据的结构图所示,在终端获得动态内存分配函数返回的针对缓存对象的指针信息后,将获得的指针信息写入第一环 境中的数据记录指针(Handle)。Specifically, as shown in Figure 2b, which is a structure diagram for recording cache object data provided by the embodiment of the present application, after the terminal obtains the pointer information for the cache object returned by the dynamic memory allocation function, it writes the obtained pointer information into the first A data record pointer (Handle) in the environment.
因为缓存对象在第二环境中创建,又因为终端程序的一些规定,两个环境中建立的一些对象不能直接使用,所以如果要在第一环境中直接使用缓存对象,是无法直接使用的,因为终端无法找到存储缓存对象的区域,也就无法对缓存对象进行操作,而使用数据记录指针就可以记录每个缓存对象对应的存储区地址和存储区大小,那么在第一环境中使用缓存对象时借助数据记录指针就可使用第二环境中创建的缓存对象了。Because the cache object is created in the second environment, and because of some regulations of the terminal program, some objects created in the two environments cannot be used directly, so if you want to use the cache object directly in the first environment, you cannot use it directly, because The terminal cannot find the area where the cache object is stored, so it cannot operate on the cache object. However, the address and size of the storage area corresponding to each cache object can be recorded by using the data record pointer. Then, when using the cache object in the first environment The cache object created in the second environment can be used by means of the data record pointer.
因为每个缓存对象都有对应的数据记录指针,保证了每个缓存对象在被使用时都可以访问到正确的存储位置,确保对缓存对象各种操作可被实施。Because each cache object has a corresponding data record pointer, it ensures that each cache object can access the correct storage location when it is used, and ensures that various operations on the cache object can be implemented.
可选的,请参见图1b为本申请实施例提供的另一种内存优化方法的流程示意图。在步骤S204之后已经成功创建缓存对象,创建缓存对象就是因为要使用缓存对象,使用缓存对象的步骤详细描述如下。Optionally, please refer to FIG. 1 b , which is a schematic flowchart of another memory optimization method provided by the embodiment of the present application. After step S204, the cache object has been successfully created. The reason for creating the cache object is to use the cache object. The steps of using the cache object are described in detail as follows.
S205:获取第一环境中缓存对象调用的第一函数和对应参数。S205: Obtain a first function called by the cache object in the first environment and corresponding parameters.
具体的,终端获取第一环境中缓存对象要调用的第一函数,确定第一函数要执行什么操作,再根据第一函数的需求获取对应参数,对应参数根据第一函数要执行的操作的不同而不同。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 are different according to the operation to be performed by the first function rather different.
可选的,上述第一函数包括读取函数、写入函数、复制函数中的任一种;Optionally, the above-mentioned first function includes any one of a read function, a write function, and a copy function;
在上述第一函数包括读取函数的情况下,上述对应参数包括待访问的上述缓存对象的数组的第一下标;In the case where the above-mentioned first function includes a read function, the above-mentioned corresponding parameter includes the first subscript of the array of the above-mentioned cache object to be accessed;
在上述第一函数包括写入函数的情况下,上述对应参数包括待访问的上述缓存对象的数组的第二下标和待写入的第一数据;In the case where the above-mentioned first function includes a write function, the above-mentioned corresponding parameters include the second subscript of the array of the above-mentioned cache object to be accessed and the first data to be written;
在上述第一函数包括复制函数的情况下,上述对应参数包括被复制的目标缓存对象。In the case that the above-mentioned first function includes a copy function, the above-mentioned corresponding parameter includes a target cache object to be copied.
这样对于第一环境中缓存对象使用不同的函数,终端就根据用户输入或者其他输入获取对应参数,确保每个函数都能获取到正确的参数,保证了程序的正确运行。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 inputs, so as to ensure that each function can obtain correct parameters and ensure the correct operation of the program.
S206:依据第一函数确定第一环境的底层接口使用的方法函数。S206: Determine the method function used by the bottom layer interface of the first environment according to the first function.
具体的,请参见图2c,为本申请实施例提供的一种使用缓存对象的结构图。Specifically, please refer to FIG. 2c, which is a structural diagram of using a cache object provided by an embodiment of the present application.
需要说明的是,因为第一函数也就是图中的方法无法直接访问第二环境,所以需要借助底层接口中的方法函数和数据记录指针才可以访问第二环境中的缓存对象存储区。同时还为每个缓存对象重新设置了垃圾回收函数,确保要清除缓存对象时,不仅释放在第二环境中的缓存对象的存储区,第一环境中的对应数据记录指针也会被释放,极大的节约了内存空间。It should be noted that because the first function, that is, 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 to be cleared, not only the storage area of the cache object in the second environment will be released, but also the corresponding data record pointer in the first environment will be released. Greatly saves memory space.
因为第一函数中的选用的函数不同那么底层接口选用的方法函数也不同,终端在确定第一函数后,就可根据第一函数确定第一环境的底层接口使用的方法函数。Because the selected functions in the first function are different, the method functions selected by the bottom interface are also different. After determining the first function, the terminal can determine the method function used by the bottom interface of the first environment according to the first function.
可选的,在上述第一函数包括读取函数的情况下,上述方法函数包括获得方法函数;Optionally, in the case where the above-mentioned first function includes a read function, the above-mentioned method function includes an obtaining method function;
在上述第一函数包括写入函数的情况下,上述方法函数包括设置方法函数;Where the above-mentioned first function includes a write function, the above-mentioned method function includes a setting method function;
在上述第一函数包括复制函数的情况下,上述方法函数包括复制方法函数。In the case where the above-mentioned first function includes a copy function, the above-mentioned method function includes a copy method function.
具体的,如果第一函数是读取函数readUInt8()的情况下,对应方法函数就是获得方法函数native.get();如果第一函数是写入函数writeUInt8()的情况下,对应方法函数就是设置方法函数native.set();如果第一函数是复制函数copy()的情况下,对应方法函数就是设置方法函数native.copy()。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().
这样第一函数和方法函数就建立了严格的对应关系,程序逻辑严密性高。In this way, a strict corresponding relationship is established between the first function and the method function, and the program logic is highly rigorous.
S207:依据对应参数和数据记录指针获取方法函数的参数。S207: Obtain the parameters of the method function according to the corresponding parameters and the data record pointer.
具体的,在确定要使用的方法函数后,就可以要获得方法函数的参数,上述方法函数的参数一般由第一函数的对应参数和数据记录指针组成,可根据具体需求更改。Specifically, after the method function to be used is determined, the parameters of the method function can be obtained. The parameters of the above method function are generally composed of the corresponding parameters of the first function and the data record pointer, which can be changed according to specific needs.
可选的,在上述方法函数包括获得方法函数的情况下,上述方法函数的参数包括上述 第一下标和上述数据记录指针;Optionally, in the case where the above-mentioned method function includes an obtaining method function, the parameters of the above-mentioned method function include the above-mentioned first subscript and the above-mentioned data record pointer;
在上述方法函数包括设置方法函数的情况下,上述方法函数的参数包括上述第二下标、上述第一数据和上述数据记录指针;In the case where the above-mentioned method function includes a setting method function, the parameters of the above-mentioned method function include the above-mentioned second subscript, the above-mentioned first data and the above-mentioned data record pointer;
在上述方法函数包括复制方法函数的情况下,上述方法函数的参数包括上述目标缓存对象数据记录指针、上述目标缓存对象长度、上述缓存对象长度和上述数据记录指针。When the above-mentioned method function includes a copy method function, the parameters of the above-mentioned method function include the above-mentioned target cache object data record pointer, the above-mentioned target cache object length, the above-mentioned cache object length and the above-mentioned data record pointer.
具体的,在上述方法函数是获得方法函数的情况下,请参见图2d,为本申请实施例提供的一种读取缓存对象数据的结构图。在图中我们可以看到,终端获取读取函数的参数1和数据记录指针作为获得方法函数的参数,上述参数1就是上述第一下标。Specifically, in the case that the above-mentioned method function is an obtaining method function, please refer to FIG. 2d, which is a structure diagram of reading cached object data provided by an embodiment of the present application. As can be seen in the figure, the terminal obtains the parameter 1 of the reading function and the data record pointer as the parameters of the obtaining method function, and the above parameter 1 is the above first subscript.
在上述方法函数是设置方法函数的情况下,请参见图2e,为本申请实施例提供的一种缓存对象写入数据的结构图。在图中我们可以看到,终端获取写入函数的参数1、参数2和数据记录指针作为设置方法函数的参数,上述参数1就是上述第一数据,上述参数2就是上述第二下标。In the case that the above-mentioned method function is a setting method function, please refer to FIG. 2 e , which is a structure diagram of data written into a cache object provided by an embodiment of the present application. As can be seen in the figure, the terminal acquires 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.
在上述方法函数是复制方法函数的情况下,请参见图2f,为本申请实施例提供的一种复制缓存对象数据的结构图。在图中我们可以看到,终端获取复制函数的参数1,上述参数1就是上述目标缓存对象,终端根据参数1找到目标缓存对象,再把缓存对象的数据记录指针1和长度1以及目标缓存对象的数据记录指针2和长度2传递给复制方法函数做参数,上述数据记录指针1对应图中指针1,上述长度1对应图中方法函数参数长度1,上述数据记录指针2对应图中指针2,上述长度2对应图中方法函数参数长度2。In the case that the above method function is a copy method function, please refer to FIG. 2f, which is a structural diagram of copying cache object data provided by an embodiment of the present application. In the figure, we can see that the terminal obtains the parameter 1 of the copy function. The above parameter 1 is the above target cache object. The terminal finds the target cache object according to parameter 1, and then records the pointer 1 and length 1 of the cache object and the target cache object. The data record pointer 2 and length 2 are passed to the copy method function as parameters. The above data record pointer 1 corresponds to the pointer 1 in the figure, the above length 1 corresponds to the method function parameter length 1 in the figure, and the above data record pointer 2 corresponds to the pointer 2 in the figure. The above length 2 corresponds to the method function parameter length 2 in the figure.
根据方法的不同,传递的参数内容也不同,多种参数使终端对缓存对象的操作更加多样,也更加精确。Depending on the method, the content of the passed parameters is also different. Multiple parameters make the operation of the terminal on the cached object more diverse and more accurate.
S208:依据方法函数和方法函数的参数访问第二环境中的缓存对象的存储区,在缓存对象的存储区执行上述方法函数对应操作。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 above method function in the storage area of the cache object.
具体的,不同的方法函数在第二环境中执行的操作不同,在确定方法函数和获得参数后,就可以根据参数去对应缓存对象的存储区执行对应操作,操作包括但不限于对存储区数据的增删查改。Specifically, different method functions perform different operations in the second environment. After the method function is determined and the parameters are obtained, the corresponding operation can be performed on the storage area corresponding to the cache object according to the parameters. The operation includes but is not limited to the storage area data Additions, deletions, checks and changes.
其中,步骤S201~S208的执行主语可以是终端。Wherein, the execution subject of steps S201-S208 may be a terminal.
可选的,上述依据上述方法函数和上述方法函数的参数访问上述第二环境中的上述缓存对象的存储区,在上述缓存对象的存储区执行上述方法函数对应操作包括:Optionally, accessing the storage area of the above-mentioned cache object in the above-mentioned second environment according to the above-mentioned method function and the parameters of the above-mentioned method function, and performing the corresponding operation of the above-mentioned method function in the storage area of the above-mentioned cache object includes:
在上述方法函数为获得方法函数的情况下,执行上述获得方法函数,通过上述方法函数的参数访问上述第二环境中的上述缓存对象的存储区,获取上述存储区中上述第一下标对应的第二数据,将处理后的上述第二数据返回给上述读取函数;In the case that the above-mentioned method function is an obtaining method function, execute the above-mentioned obtaining method function, access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameter of the above-mentioned method function, and obtain the value corresponding to the above-mentioned first subscript in the above-mentioned storage area second data, returning the processed second data to the read function;
在上述方法函数为设置方法函数的情况下,执行上述设置方法函数,通过上述方法函数的参数访问上述第二环境中的上述缓存对象的存储区,将上述第一数据写入上述第二下标对应的上述存储区,将写入成功结果返回给上述写入函数;When the above-mentioned method function is a setting method function, execute the above-mentioned setting method function, access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameters of the above-mentioned method function, and write the above-mentioned first data into the above-mentioned second subscript Corresponding to the above storage area, return the write success result to the above write function;
在上述方法函数为复制方法函数的情况下,执行上述复制方法函数调用上述第二环境中的第一复制函数,将上述第一复制函数执行结果返回给上述复制方法函数,获得上述复制方法函数执行结果并返回给上述复制函数。When the above-mentioned method function is a copy method function, execute the above-mentioned copy method function to call the first copy function in the above-mentioned second environment, return the execution result of the above-mentioned first copy function to the above-mentioned copy method function, and obtain the execution of the above-mentioned copy method function result and returned to the copy function above.
具体的,在上述方法函数为获得方法函数的情况下,请参见图2d,终端执行获得方法函数,通过获得方法函数中的数据记录指针访问第二环境中的上述缓存对象的存储区,再根据参数1找到需要读取的具***置,获取上述存储区中上述第一下标位置存储的第二数据,将处理后的上述第二数据返回给上述读取函数,如果没有成功获取上述第二数据,则获得方法函数会失败抛出异常。Specifically, in the case that the above-mentioned method function is an obtaining method function, please refer to FIG. 2d, the terminal executes the obtaining method function, and accesses the storage area of the above-mentioned cache object in the second environment through the data record pointer in the obtaining method function, and then according to Parameter 1 Find the specific location that needs to be read, obtain the second data stored in the first subscript position in the above storage area, and return the processed second data to the above read function, if the above second data is not successfully obtained , the get method function will fail and throw an exception.
在上述方法函数为设置方法函数的情况下,请参见图2e,终端执行设置方法函数,通过设置方法函数中的数据记录指针访问第二环境中的上述缓存对象的存储区,再根据参数 2找到需要读取的具***置,将上述第一数据写入上述第二下标对应的上述存储区,写入成功后设置方法函数将写入成功结果返回给上述写入函数,失败则抛出异常。In the case that the above method function is a setting method function, please refer to Figure 2e, the terminal executes the setting method function, accesses the storage area of the above cache object in the second environment through the data record pointer in the setting method function, and then finds according to parameter 2 For the specific location that needs to be read, write 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 will return the successful writing result to the above-mentioned writing function. If it fails, an exception will be thrown.
在上述方法函数为复制方法函数的情况下,请参见图2f,执行上述复制方法函数调用上述第二环境中的第一复制函数memcpy(),执行第一复制函数再将上述第一复制函数执行结果返回给上述复制方法函数,获得上述复制方法函数执行结果并返回给上述复制函数。图中第一复制函数的参数指针1来自复制方法函数的指针1,指针2来自复制方法函数的指针2,大小为复制方法函数的长度1和长度2中数值小的一个。In the case that the above-mentioned method function is a copy method function, please refer to Figure 2f, execute the above-mentioned copy method function to call the first copy function memcpy() in the above-mentioned second environment, execute the first copy function and then execute the above-mentioned first copy function The result is returned to the above-mentioned copy method function, and the execution result of the above-mentioned copy method function is obtained and returned to the above-mentioned 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 value among length 1 and length 2 of the copy method function.
不同的方法函数在第二环境中的缓存对象存储区执行不同操作,可以实现对缓存对象的多种处理,增加了缓存对象的利用方式。Different method functions perform different operations on the cache object storage area in the second environment, which can realize various processing on the cache object and increase the utilization mode of the cache object.
由上述所有内容可知缓存对象的生命周期如图3为本申请实施例提供的一种缓存对象生命周期流程图所示。From all the above contents, it can be seen that the life cycle of a cache object is shown in FIG. 3 , which is a flow chart of a life cycle of a cache object provided in an embodiment of the present application.
终端接收创建指令后,缓存对象生命周期开始,终端根据创建指令申请内存,如果内存申请成功,则用数据记录指针记录新创建的缓存对象,同时为缓存对象创建垃圾回收函数,在垃圾回收函数计数还没有归零时继续执行缓存对象相关业务,归零就调用垃圾回收函数释放第二环境中的缓存对象对应存储区,缓存对象生命周期结束;如果内存申请不成功,则缓存对象生命周期结束。After the terminal receives the creation command, the life cycle of the cached object begins, and the terminal applies for memory according to the creation command. If the memory application is successful, it uses the data record pointer to record the newly created cached object, and creates a garbage collection function for the cached object at the same time. If it has not been reset to zero, continue to execute the business related to the cache object, and call the garbage collection function to release the storage area corresponding to the cache object in the second environment when it is reset to zero, and the life cycle of the cache object ends; if the memory application is unsuccessful, the life cycle of the cache object ends.
为了便于更好地实施本申请实施例的上述方案,本申请还提供了用于实现实施上述方案的终端。In order to facilitate better implementation of the above-mentioned solutions in the embodiments of the present application, the present application further provides a terminal for implementing the above-mentioned solutions.
请参见图4,为本申请实施例提供的一种内存优化的装置的结构示意图,如图4所示的内存优化的装置40可包括:接收单元401、第一调用单元402、第一获取单元403、写入单元404。该内存优化的装置40可以用于执行上述方法实施例中的相关描述。其中,Please refer to FIG. 4, which is a schematic structural diagram of a memory-optimized device provided in the embodiment of the present application. The memory-optimized device 40 shown in FIG. 4 may include: a receiving unit 401, a first calling unit 402, and a first obtaining unit 403 . Write to the unit 404 . The memory optimization apparatus 40 may be used to execute related descriptions in the foregoing method embodiments. in,
接收单元401,用于接收第一环境中的创建指令;a receiving unit 401, configured to receive a creation instruction in the first environment;
第一调用单元402,用于依据上述创建指令使用上述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,在存储相同数据时,上述第一环境占用的内存大于上述第二环境占用的内存;The first calling unit 402 is used to use 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 above creation instruction. When storing the same data, the memory occupied by the first environment is larger than that of the second environment. The memory occupied by the second environment;
第一获取单元403,用于获取上述动态内存分配函数返回的针对上述缓存对象的指针信息;The first obtaining unit 403 is configured to obtain the pointer information for the above-mentioned cache object returned by the above-mentioned dynamic memory allocation function;
写入单元404,用于将上述指针信息写入上述第一环境中的数据记录指针。The writing unit 404 is configured to write the pointer information into the data record pointer in the first environment.
在一种可能的设计中,上述第一调用单元402,具体用于:In a possible design, the above-mentioned first calling unit 402 is specifically used for:
依据上述创建指令使用上述第一环境的底层接口调用第二环境中的动态内存分配函数;Call the dynamic memory allocation function in the second environment by using the underlying interface of the first environment according to the above creation instruction;
将上述第一内存参数传递给上述动态内存分配函数;passing the above-mentioned first memory parameter to the above-mentioned dynamic memory allocation function;
执行上述动态内存分配函数,上述动态内存分配函数用于创建上述缓存对象并且为上述缓存对象在上述第二环境中分配与上述第一内存参数对应的内存。Executing the above dynamic memory allocation function, the above dynamic memory allocation function is used to create the above cache object and allocate memory corresponding to the above first memory parameter in the above second environment for the above cache object.
在一种可能的设计中,上述装置还包括:In a possible design, the above device also includes:
第二调用单元405,用于获取上述第一环境中上述缓存对象调用的第一函数和对应参数;The second calling unit 405 is configured to obtain the first function and corresponding parameters called by the cache object in the first environment;
确定单元406,用于依据上述第一函数确定上述第一环境的底层接口使用的方法函数;A determining unit 406, configured to determine the method function used by the underlying interface of the first environment according to the first function;
第二获取单元407,用于依据上述对应参数和上述数据记录指针获取上述方法函数的参数;The second obtaining unit 407 is configured to obtain the parameters of the above-mentioned method function according to the above-mentioned corresponding parameters and the above-mentioned data record pointer;
执行单元408,用于依据上述方法函数和上述方法函数的参数访问上述第二环境中的上述缓存对象的存储区,在上述缓存对象的存储区执行上述方法函数对应操作。The execution unit 408 is configured 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 corresponding operations of the method function in the storage area of the cache object.
在一种可能的设计中,所述第一函数包括读取函数、写入函数、复制函数中的任一种;In a possible design, the first function includes any one of a read function, a write function, and a copy function;
在所述第一函数包括读取函数的情况下,所述对应参数包括待访问的所述缓存对象的 数组的第一下标;Where the first function includes a read function, the corresponding parameter includes a first subscript of an array of the cache object to be accessed;
在所述第一函数包括写入函数的情况下,所述对应参数包括待访问的所述缓存对象的数组的第二下标和待写入的第一数据;In the case where the first function includes a write function, the corresponding parameters include the second subscript of the array of the cache object to be accessed and the first data to be written;
在所述第一函数包括复制函数的情况下,所述对应参数包括被复制的目标缓存对象。Where the first function includes a copy function, the corresponding parameter includes a target cache object to be copied.
在一种可能的设计中,在所述第一函数包括读取函数的情况下,所述方法函数包括获得方法函数;In a possible design, when the first function includes a read function, the method function includes an obtain method function;
在所述第一函数包括写入函数的情况下,所述方法函数包括设置方法函数;Where the first function includes a write function, the method function includes a set method function;
在所述第一函数包括复制函数的情况下,所述方法函数包括复制方法函数。Where the first function includes a copy function, the method function includes a copy method function.
在一种可能的设计中,在所述方法函数包括获得方法函数的情况下,所述方法函数的参数包括所述第一下标和所述数据记录指针;In a possible design, when the method function includes an obtain method function, parameters of the method function include the first subscript and the data record pointer;
在所述方法函数包括设置方法函数的情况下,所述方法函数的参数包括所述第二下标、所述第一数据和所述数据记录指针;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;
在所述方法函数包括复制方法函数的情况下,所述方法函数的参数包括所述目标缓存对象数据记录指针、所述目标缓存对象长度、所述缓存对象长度和所述数据记录指针。When 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.
在一种可能的设计中,上述执行单元408,具体用于:In a possible design, the execution unit 408 is specifically used for:
在上述方法函数为获得方法函数的情况下,执行上述获得方法函数,通过上述方法函数的参数访问上述第二环境中的上述缓存对象的存储区,获取上述存储区中上述第一下标对应的第二数据,将处理后的上述第二数据返回给上述读取函数;In the case that the above-mentioned method function is an obtaining method function, execute the above-mentioned obtaining method function, access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameter of the above-mentioned method function, and obtain the value corresponding to the above-mentioned first subscript in the above-mentioned storage area second data, returning the processed second data to the read function;
在上述方法函数为设置方法函数的情况下,执行上述设置方法函数,通过上述方法函数的参数访问上述第二环境中的上述缓存对象的存储区,将上述第一数据写入上述第二下标对应的上述存储区,将写入成功结果返回给上述写入函数;When the above-mentioned method function is a setting method function, execute the above-mentioned setting method function, access the storage area of the above-mentioned cache object in the above-mentioned second environment through the parameters of the above-mentioned method function, and write the above-mentioned first data into the above-mentioned second subscript Corresponding to the above storage area, return the write success result to the above write function;
在上述方法函数为复制方法函数的情况下,执行上述复制方法函数调用上述第二环境中的第一复制函数,将上述第一复制函数执行结果返回给上述复制方法函数,获得上述复制方法函数执行结果并返回给上述复制函数。When the above-mentioned method function is a copy method function, execute the above-mentioned copy method function to call the first copy function in the above-mentioned second environment, return the execution result of the above-mentioned first copy function to the above-mentioned copy method function, and obtain the execution of the above-mentioned copy method function result and returned to the copy function above.
需要说明的是,本申请实施例所描述的内存优化的装置40中各功能单元的功能可根据上述方法实施例中的方法具体实现,此处不再赘述。It should be noted that the functions of each functional unit in the memory-optimized device 40 described in the embodiment of the present application can be specifically implemented according to the methods in the foregoing method embodiments, which will not be repeated here.
图5为本申请实施例提供的一种终端的结构示意图,该终端50可因配置或性能不同而产生比较大的差异,可以包括一个或一个以***处理器(central processing units,CPU)502(例如,一个或一个以上处理器)和存储器508,一个或一个以上存储应用程序506或数据505的存储介质507(例如一个或一个以上海量存储设备)。其中,存储器508和存储介质507可以是短暂存储或持久存储。存储在存储介质507的程序可以包括一个或一个以上模块(图示没标出),每个模块可以包括对电子设备中的一系列指令操作。更进一步地,中央处理器502可以设置为与存储介质507通信,在终端50上执行存储介质507中的一系列指令操作。终端50可以为本申请提供的软件运行设备。FIG. 5 is a schematic structural diagram of a terminal provided by 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 (central processing units, CPU) 502 ( For example, one or more processors) and memory 508, one or more storage media 507 (eg, one or more mass storage devices) for storing applications 506 or data 505 . Wherein, the memory 508 and the storage medium 507 may be temporary storage or persistent 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 on the electronic device. Furthermore, the central processing unit 502 may be configured to communicate with the storage medium 507 , and execute a series of instruction operations in the storage medium 507 on the terminal 50 . The terminal 50 may be a software running device provided in this application.
终端50还可以包括一个或一个以上电源503,一个或一个以上有线或无线网络接口509,一个或一个以上输入输出接口510,和/或,一个或一个以上操作***504,例如Windows ServerTM,Mac OS XTM,UnixTM,LinuxTM,FreeBSDTM等等。 Terminal 50 can 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™, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, etc.
上述实施例中由软件运行设备所执行的步骤可以基于该图5所示的终端结构。具体的,中央处理器502可实现图4中各单元的功能。The steps performed by the software running device in the foregoing embodiments may be based on the terminal structure shown in FIG. 5 . Specifically, the central processing unit 502 can realize the functions of each unit in FIG. 4 .
本申请实施例还提供了一种计算机可读存储介质。上述方法实施例中的全部或者部分流程可以由计算机程序来指令相关的硬件完成,该程序可存储于上述计算机存储介质中,该程序在执行时,可包括如上述各方法实施例的流程。该计算机可读存储介质包括:只读存储器(read-only memory,ROM)或随机存取存储器(random access memory,RAM)、磁碟或者光盘等各种可存储程序代码的介质。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 computer programs instructing related 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: a read-only memory (read-only memory, ROM) or a random access memory (random access memory, RAM), a magnetic disk or an optical disk, and other media that can store program codes.
在上述实施例中,可以全部或部分地通过软件、硬件、固件或者其任意组合来实现。当使用软件实现时,可以全部或部分地以计算机程序产品的形式实现。上述计算机程序产品包括一个或多个计算机指令。在计算机上加载和执行上述计算机程序指令时,全部或部分地产生按照本申请实施例上述的流程或功能。上述计算机可以是通用计算机、专用计算机、计算机网络、或者其他可编程装置。上述计算机指令可以存储在计算机可读存储介质中,或者通过上述计算机可读存储介质进行传输。上述计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含一个或多个可用介质集成的服务器、数据中心等数据存储设备。上述可用介质可以是磁性介质,(例如,软盘、硬盘、磁带)、光介质(例如,DVD)、或者半导体介质(例如,固态硬盘(solid state disk,SSD))等。In the above embodiments, all or part of them may be implemented by software, hardware, firmware or any combination thereof. When implemented using software, it may be implemented in whole or in part in the form of a computer program product. The computer program product described above comprises one or more computer instructions. When the above-mentioned computer program instructions are loaded and executed on the computer, all or part of the above-mentioned processes or functions according to the embodiments of the present application will be generated. The above-mentioned computers may be general-purpose computers, special-purpose computers, computer networks, or other programmable devices. The above-mentioned computer instructions may be stored in a computer-readable storage medium, or transmitted through the above-mentioned computer-readable storage medium. The above-mentioned computer-readable storage medium may be any available medium that can be accessed by a computer, or a data storage device such as a server or a data center integrated with one or more available media. The above available medium may be a magnetic medium (for example, a floppy disk, a hard disk, or a magnetic tape), an optical medium (for example, DVD), or a semiconductor medium (for example, a solid state disk (solid state disk, SSD)) and the like.
综上所述,通过实施本申请实施例,终端通过执行计算机程序,接收第一环境中的创建指令,再依据创建指令使用第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,缓存对象创建成功后获取动态内存分配函数返回的针对缓存对象的指针信息,将指针信息写入第一环境中的数据记录指针,通过改变缓存对象数据的存储环境,降低内存占用,实现了内存优化,提高了计算机语言脚本的兼容性和整体性能。To sum up, by implementing the embodiment of the present application, the terminal receives the creation instruction in the first environment by executing the computer program, and then uses the underlying interface of the first environment to call the dynamic memory allocation function in the second environment according to the creation instruction to create a cache object, after the cache object is successfully created, obtain the pointer information for the cache object returned by the dynamic memory allocation function, write the pointer information into the data record pointer in the first environment, and reduce the memory usage by changing the storage environment of the cache object data. Memory optimization, which improves the compatibility and overall performance of computer language scripts.
同时对于缓存对象的各种操作设置对应的函数,包括读取函数、写入函数、复制函数实现对缓存对象的基本使用,用户直接调用函数即可,无需重复书写,节约了用户使用时间。At the same time, set corresponding functions for various operations of the cache object, including read function, write function, and copy function to realize the basic use of the cache object. The user can directly call the function without repeated writing, which saves the user's time.
本申请实施例方法中的步骤可以根据实际需要进行顺序调整、合并和删减。The steps in the methods of the embodiments of the present application can be adjusted, combined and deleted according to actual needs.
本申请实施例装置中的模块可以根据实际需要进行合并、划分和删减。The modules in the device of the embodiment of the present application can be combined, divided and deleted according to actual needs.
综上所述,以上实施例仅用以说明本申请的技术方案,而非对其限制;尽管参照前述实施例对本申请进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本申请各实施例技术方案的范围。In summary, the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them; although the present application has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: it can still The technical solutions described in the foregoing embodiments are modified, or some of the technical features are replaced equivalently; and these modifications or replacements do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present application.

Claims (10)

  1. 一种内存优化方法,包括:A memory optimization method comprising:
    接收第一环境中的创建指令;receiving a creation instruction in the first environment;
    依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,在存储相同数据时,所述第一环境占用的内存大于所述第二环境占用的内存;According to the creation instruction, use the underlying interface of the first environment to call the dynamic memory allocation function in the second environment to create a cache object. When storing the same data, the memory occupied by the first environment is larger than that occupied by the second environment. Memory;
    获取所述动态内存分配函数返回的针对所述缓存对象的指针信息;Obtain 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.
  2. 根据权利要求1所述的方法,其特征在于,所述创建指令包括第一内存参数,所述依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象包括:The method according to claim 1, wherein the creation instruction includes a first memory parameter, and the creation instruction uses the underlying interface of the first environment to call a dynamic memory allocation function in the second environment to create Cache objects include:
    依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数;calling a dynamic memory allocation function in the second environment by using the underlying interface of the first environment according to the creation instruction;
    将所述第一内存参数传递给所述动态内存分配函数;passing the first memory parameter to the dynamic memory allocation function;
    执行所述动态内存分配函数,所述动态内存分配函数用于创建所述缓存对象并且为所述缓存对象在所述第二环境中分配与所述第一内存参数对应的内存。Executing the dynamic memory allocation function, 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.
  3. 根据权利要求1或2所述的方法,其特征在于,在所述将所述指针信息写入所述第一环境中的数据记录指针之后,该方法还包括:The method according to claim 1 or 2, wherein 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;
    依据所述第一函数确定所述第一环境的底层接口使用的方法函数;determining the method function used by the bottom layer interface of the first environment according to the first function;
    依据所述对应参数和所述数据记录指针获取所述方法函数的参数;Obtaining parameters of the method function according to the corresponding parameters and the data record pointer;
    依据所述方法函数和所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,在所述缓存对象的存储区执行所述方法函数对应操作。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 corresponding operation of the method function in the storage area of the cache object.
  4. 根据权利要求3所述的方法,其特征在于,所述第一函数包括读取函数、写入函数、复制函数中的任一种;The method according to claim 3, wherein the first function includes any one of a read function, a write function, and a copy function;
    在所述第一函数包括读取函数的情况下,所述对应参数包括待访问的所述缓存对象的数组的第一下标;In the case where the first function includes a read function, the corresponding parameter includes a first subscript of the array of cache objects to be accessed;
    在所述第一函数包括写入函数的情况下,所述对应参数包括待访问的所述缓存对象的数组的第二下标和待写入的第一数据;In the case where the first function includes a write function, the corresponding parameters include the second subscript of the array of the cache object to be accessed and the first data to be written;
    在所述第一函数包括复制函数的情况下,所述对应参数包括被复制的目标缓存对象。Where the first function includes a copy function, the corresponding parameter includes a target cache object to be copied.
  5. 根据权利要求4所述的方法,其特征在于,The method according to claim 4, characterized in that,
    在所述第一函数包括读取函数的情况下,所述方法函数包括获得方法函数;Where said first function comprises a read function, said method function comprises a get method function;
    在所述第一函数包括写入函数的情况下,所述方法函数包括设置方法函数;Where the first function includes a write function, the method function includes a set method function;
    在所述第一函数包括复制函数的情况下,所述方法函数包括复制方法函数。Where the first function includes a copy function, the method function includes a copy method function.
  6. 根据权利要求5所述的方法,其特征在于,The method according to claim 5, characterized in that,
    在所述方法函数包括获得方法函数的情况下,所述方法函数的参数包括所述第一下标和所述数据记录指针;Where the method function includes an obtain method function, the parameters of the method function include the first subscript and the data record pointer;
    在所述方法函数包括设置方法函数的情况下,所述方法函数的参数包括所述第二下标、 所述第一数据和所述数据记录指针;Where the method function includes a setting method function, parameters of the method function include the second subscript, the first data, and the data record pointer;
    在所述方法函数包括复制方法函数的情况下,所述方法函数的参数包括所述目标缓存对象数据记录指针、所述目标缓存对象长度、所述缓存对象长度和所述数据记录指针。When 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.
  7. 根据权利要求6所述的方法,其特征在于,所述依据所述方法函数和所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,在所述缓存对象的存储区执行所述方法函数对应操作包括:The method according to claim 6, characterized in that, accessing the storage area of the cache object in the second environment according to the method function and the parameters of the method function, in the storage area of the cache object The corresponding operations of performing the method function in the zone include:
    在所述方法函数包括获得方法函数的情况下,执行所述获得方法函数,通过所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,获取所述存储区中所述第一下标对应的第二数据,将处理后的所述第二数据返回给所述读取函数;In the case where the method function includes an obtain method function, execute the obtain method function, access the storage area of the cache object in the second environment through the parameters of the method function, and obtain all the information stored in the storage area The second data corresponding to the first subscript, returning the processed second data to the read function;
    在所述方法函数包括设置方法函数的情况下,执行所述设置方法函数,通过所述方法函数的参数访问所述第二环境中的所述缓存对象的存储区,将所述第一数据写入所述第二下标对应的所述存储区,将写入成功结果返回给所述写入函数;When the method function includes a setting method function, execute the setting method function, access the storage area of the cache object in the second environment through the parameter of the method function, and write the first data Enter the storage area corresponding to the second subscript, and return the write success result to the write function;
    在所述方法函数包括复制方法函数的情况下,执行所述复制方法函数调用所述第二环境中的第一复制函数,将所述第一复制函数执行结果返回给所述复制方法函数,获得所述复制方法函数执行结果并返回给所述复制函数。When the method function includes a copy method function, execute the copy method function to call the first copy function in the second environment, return the execution result of the first copy function to the copy method function, and obtain The execution result of the copy method function is returned to the copy function.
  8. 一种内存优化的装置,其特征在于,包括:A memory optimization device, characterized in that it comprises:
    接收单元,用于接收第一环境中的创建指令;a receiving unit, configured to receive a creation instruction in the first environment;
    第一调用单元,用于依据所述创建指令使用所述第一环境的底层接口调用第二环境中的动态内存分配函数创建缓存对象,在存储相同数据时,所述第一环境占用的内存大于所述第二环境占用的内存;The first calling unit is used to use 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. When storing the same data, the memory occupied by the first environment is larger than memory occupied by the second environment;
    第一获取单元,用于获取所述动态内存分配函数返回的针对所述缓存对象的指针信息;a first obtaining unit, configured to obtain pointer information for the cache object returned by the dynamic memory allocation function;
    写入单元,用于将所述指针信息写入所述第一环境中的数据记录指针。A writing unit, configured to write the pointer information into the data record pointer in the first environment.
  9. 一种终端,其特征在于,包括处理器和存储器,所述存储器存储有计算机程序,所述计算机程序被所述处理器执行时,所述处理器执行如权利要求1至7中任一项权利要求所述的方法。A terminal, characterized by comprising a processor and a memory, the memory stores a computer program, and when the computer program is executed by the processor, the processor executes any one of claims 1 to 7 method as described.
  10. 一种计算机可读存储介质,其特征在于,所述计算机存储介质存储有计算机程序,所述计算机程序被处理器执行时,使所述处理器执行如权利要求1至7中任一项权利要求所述的方法。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 any one of claims 1 to 7. the method described.
PCT/CN2022/094223 2021-10-26 2022-05-20 Memory optimization method and apparatus, terminal, and storage medium WO2023071158A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202111248990.7 2021-10-26
CN202111248990.7A CN113961485A (en) 2021-10-26 2021-10-26 Memory optimization method, device, terminal and storage medium

Publications (1)

Publication Number Publication Date
WO2023071158A1 true WO2023071158A1 (en) 2023-05-04

Family

ID=79467168

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/094223 WO2023071158A1 (en) 2021-10-26 2022-05-20 Memory optimization method and apparatus, terminal, and storage medium

Country Status (2)

Country Link
CN (1) CN113961485A (en)
WO (1) WO2023071158A1 (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113961485A (en) * 2021-10-26 2022-01-21 西安广和通无线通信有限公司 Memory optimization method, device, terminal and storage medium

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1527206A (en) * 2003-03-03 2004-09-08 华为技术有限公司 Memory pool managing method
US20130061007A1 (en) * 2011-09-02 2013-03-07 International Business Machines Corporation Generating code that calls functions based on types of memory
CN107102957A (en) * 2016-02-22 2017-08-29 深圳市知穹科技有限公司 The method and system that a kind of internal memory based between GPU and NIC is directly exchanged at a high speed
CN110022341A (en) * 2018-01-10 2019-07-16 腾讯科技(深圳)有限公司 A kind of data transmission method and relevant device
CN110245091A (en) * 2018-10-29 2019-09-17 浙江大华技术股份有限公司 A kind of method, apparatus and computer storage medium of memory management
CN113961485A (en) * 2021-10-26 2022-01-21 西安广和通无线通信有限公司 Memory optimization method, device, terminal and storage medium

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1527206A (en) * 2003-03-03 2004-09-08 华为技术有限公司 Memory pool managing method
US20130061007A1 (en) * 2011-09-02 2013-03-07 International Business Machines Corporation Generating code that calls functions based on types of memory
CN107102957A (en) * 2016-02-22 2017-08-29 深圳市知穹科技有限公司 The method and system that a kind of internal memory based between GPU and NIC is directly exchanged at a high speed
CN110022341A (en) * 2018-01-10 2019-07-16 腾讯科技(深圳)有限公司 A kind of data transmission method and relevant device
CN110245091A (en) * 2018-10-29 2019-09-17 浙江大华技术股份有限公司 A kind of method, apparatus and computer storage medium of memory management
CN113961485A (en) * 2021-10-26 2022-01-21 西安广和通无线通信有限公司 Memory optimization method, device, terminal and storage medium

Also Published As

Publication number Publication date
CN113961485A (en) 2022-01-21

Similar Documents

Publication Publication Date Title
US11126740B2 (en) Storage isolation for containers
KR101246982B1 (en) Using external memory devices to improve system performance
US20200057660A1 (en) Method and system for rendering user interfaces
US9213623B2 (en) Memory allocation with identification of requesting loadable kernel module
US9400667B2 (en) String cache file for optimizing memory usage in a Java virtual machine
US8041849B2 (en) Method for handling small computer system interface (SCSI) commands via a redundant array of inexpensive disks (RAID) device driver
US9715351B2 (en) Copy-offload on a device stack
US6665747B1 (en) Method and apparatus for interfacing with a secondary storage system
US20190004841A1 (en) Memory Sharing For Virtual Machines
US10936411B2 (en) Memory scrub system
US20150153958A1 (en) Electronic device and method for memory allocation in electronic device
TW201423596A (en) Pinning boot data for faster boot
WO2023071158A1 (en) Memory optimization method and apparatus, terminal, and storage medium
US10459662B1 (en) Write failure handling for a memory controller to non-volatile memory
US10642727B1 (en) Managing migration events performed by a memory controller
US8645667B2 (en) Operating system management of address-translation-related data structures and hardware lookasides
KR20220113372A (en) Unified reference and secondary object display
US20100005239A1 (en) Methods and apparatus for copying data
CN116126581B (en) Memory fault processing method, device, system, equipment and storage medium
US20070067358A1 (en) Method and apparatus for restoring versionable objects
US7958325B2 (en) Handling temporary files in a file system with snapshots
US10176112B2 (en) Information processing device, method, and non-transitory computer-readable recording medium storing information processing program for loading code into reconfigurable integrated circuit
JP2011008753A (en) Method and system for executing file stored in hidden storage area of storage device
WO2022088711A1 (en) Program execution method, program processing method, and related device
US20120185452A1 (en) System and method for file coordination

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22885071

Country of ref document: EP

Kind code of ref document: A1