A shared memory management method and device, electronic equipment and storage medium
By setting up shared memory and performing binary data transfer between native applications and mobile web pages, the data transfer efficiency problem between JavaScript and native applications is solved, a memory expansion mechanism is implemented, and CPU running speed and data processing efficiency are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ZHUHAI FANTAI GEEK TECH CO LTD
- Filing Date
- 2026-02-10
- Publication Date
- 2026-05-05
AI Technical Summary
In existing technologies, data transmission between JavaScript and native applications requires frequent string serialization and deserialization, resulting in high CPU utilization, reduced overall CPU speed, and impacted data processing efficiency.
Set up shared memory between native applications and mobile web pages, read and write data in binary form, expand memory when memory is insufficient, avoid string serialization and deserialization, and use preset rules and memory usage calculation formulas to determine whether to expand memory.
It reduces CPU usage, improves CPU speed and data processing efficiency, and ensures stable data transmission between native applications and mobile web pages.
Smart Images

Figure CN121681180B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of mobile application development technology, and more specifically, to a shared memory management method, apparatus, electronic device, and storage medium. Background Technology
[0002] JSBridge (JavaScript Bridge) is a bridge connecting different technology platforms and languages. It allows JavaScript code to interact with native applications (such as Android and iOS apps). Through JSBridge, developers can implement cross-platform functionality and improve development efficiency. JSBridge needs to pass data between JavaScript (the core scripting language of HTML5) and different native application environments (such as iOS WebView or Android WebView), which may support different data types.
[0003] In existing technologies, the large amount of data transfer between JavaScript and native applications requires frequent string serialization and deserialization, which leads to high CPU utilization, reduced overall CPU speed, and consequently affects overall data processing efficiency. Summary of the Invention
[0004] The present invention aims to, for example, provide a shared memory management method, apparatus, electronic device, and storage medium that can at least partially solve the aforementioned technical problems.
[0005] The embodiments of the present invention can be implemented as follows:
[0006] In a first aspect, embodiments of the present invention provide a shared memory management method for communication between a native application and a mobile webpage, wherein the native application has shared memory configured, and the mobile webpage and the native application read / write data in the shared memory in binary data form; the method includes:
[0007] When shared memory expansion is possible, the actual expansion memory is determined based on the relationship between the target memory and the maximum preset memory, and the shared memory is expanded according to the actual expansion memory; wherein, the target memory is the size of the shared memory after expansion according to preset rules;
[0008] The actual expanded memory is used as the expanded shared memory, and the listeners on the mobile webpage and the native application are notified to update the memory usage statistics.
[0009] Optionally, the shared memory includes a data storage area, and the method further includes:
[0010] Based on a preset memory utilization calculation formula, the memory utilization rate of the shared memory is obtained according to the first pointer, the second pointer, and the size of the shared memory; wherein, the first pointer is a pointer pointing to the current writable data position in the data storage area, and the second pointer is a pointer pointing to the current readable data position in the data storage area;
[0011] If the memory usage rate is greater than a preset usage rate threshold, the target memory is determined, and based on the maximum preset memory, the shared memory, and the target memory, it is determined whether the shared memory can be expanded.
[0012] If shared memory can be expanded, the shared memory is expanded according to the actual expansion memory.
[0013] Optionally, the method further includes:
[0014] Receive a write request, determine the memory required for the write data, and obtain the remaining memory in the data storage area of the shared memory;
[0015] If the memory for writing data is greater than the remaining memory, determine the target memory, and based on the maximum preset memory, the shared memory, and the target memory, determine whether the shared memory can be expanded.
[0016] If shared memory can be expanded, the shared memory is expanded according to the actual expansion memory, and after the expansion is completed, data is written into the data storage area of the expanded shared memory in chronological order.
[0017] Optionally, the method further includes:
[0018] If shared memory cannot be expanded, an error signal is generated, the number of consecutive errors is recorded, and the shared memory expansion is reassessed after a first interval. If shared memory expansion still cannot be expanded, an error signal is generated, the number of consecutive errors is recorded, and the shared memory expansion is reassessed after the first interval until it is determined that shared memory expansion can be expanded, at which point the number of consecutive errors is cleared to zero.
[0019] If the number of consecutive errors exceeds a preset consecutive error threshold, then after a second interval longer than the first interval, it is re-evaluated whether shared memory expansion can be performed. If shared memory expansion cannot be performed, the re-evaluation interval is increased until it is determined that shared memory expansion can be performed, and then the number of consecutive errors is reset to zero.
[0020] Optionally, the step of determining the actual expansion memory based on the size relationship between the target memory and the maximum preset memory, and expanding the shared memory according to the actual expansion memory, includes:
[0021] If the target memory is less than or equal to the maximum preset memory, the target memory is determined to be the actual expanded memory;
[0022] If the target memory is greater than the maximum preset memory, the maximum preset memory is determined to be the actual expansion memory;
[0023] Pause writing data to the shared memory and create the actual expanded memory;
[0024] The data in the shared memory is migrated to the actual expanded memory;
[0025] Release the shared memory, update the first pointer and the second pointer, and complete the expansion of the shared memory.
[0026] Optionally, both the shared memory and the actual expanded memory's data storage area are circular buffers, and the step of migrating data from the shared memory to the actual expanded memory includes:
[0027] Determine the current data size in the data storage area of the shared memory, and determine the relative positions of the first pointer and the second pointer;
[0028] If the second pointer is smaller than the first pointer, the data in the shared memory data storage area is directly migrated to the data storage area in the actual expanded memory.
[0029] If the second pointer is greater than or equal to the first pointer, the first part of the data is determined according to the current data size and the second pointer. The first part of the data is the data from the second pointer to the end of the data storage area in the shared memory.
[0030] According to the second pointer, the first part of the data is migrated to the beginning position of the data storage area in the actual expanded memory;
[0031] After migrating the second part of the data to the first part of the data according to the first pointer, the data migration is completed; wherein, the second part of the data is the data in the data storage area of the shared memory other than the first part of the data.
[0032] Optionally, the method further includes:
[0033] After the mobile webpage reads data from the shared memory, garbage collection is performed on the shared memory.
[0034] The memory utilization rate is calculated based on the preset memory utilization rate calculation formula;
[0035] If the memory usage rate remains below the preset memory usage rate lower limit for a preset period of time, or if a system memory alarm signal from the native application is received, the shared memory is reduced to obtain the actual memory reduction.
[0036] The actual reduced memory is used as the reduced shared memory.
[0037] Secondly, embodiments of the present invention provide a shared memory management device for communication between a native application and a mobile webpage, wherein the native application has shared memory, and the mobile webpage and the native application read / write data on the shared memory in binary data form; the shared memory management device includes:
[0038] A memory expansion unit is used to determine the actual expansion memory based on the size relationship between the target memory and the maximum preset memory when shared memory expansion is possible, and to expand the shared memory according to the actual expansion memory; wherein, the target memory is the size of the shared memory after expansion according to preset rules;
[0039] The update notification unit is used to treat the actual expanded memory as the expanded shared memory and to notify the listeners on the mobile webpage and the native application to update the memory usage statistics.
[0040] Thirdly, embodiments of the present invention provide an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps of any of the methods described above.
[0041] Fourthly, embodiments of the present invention provide a computer-readable storage medium comprising a computer program that, when executed, implements the steps of any of the methods described above.
[0042] The beneficial effects of the embodiments of the present invention include, for example:
[0043] By setting up shared memory on the native application, mobile web pages and native applications can read and write data to the shared memory in binary form, avoiding the overhead of string serialization and deserialization. When the amount of data increases and the shared memory becomes insufficient, the shared memory can be expanded if possible, ensuring the stability of data transmission between the native application and the mobile web page. This reduces CPU utilization and improves CPU speed and data processing efficiency. Attached Figure Description
[0044] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as a limitation on the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0045] Figure 1 A block diagram illustrating an electronic device according to an embodiment of the present invention;
[0046] Figure 2 A flowchart illustrating the steps of a shared memory management method provided in an embodiment of the present invention;
[0047] Figure 3 A flowchart illustrating a shared memory management method provided in an embodiment of the present invention;
[0048] Figure 4 This is an architectural diagram of a shared memory management device provided in an embodiment of the present invention.
[0049] Icons: 100 - Electronic device; 110 - Memory; 120 - Processor; 130 - Communication module; 300 - Shared memory management device; 301 - Memory expansion unit; 302 - Update notification unit. Detailed Implementation
[0050] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.
[0051] Therefore, the following detailed description of the embodiments of the invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the invention without inventive effort are within the scope of protection of the invention.
[0052] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures.
[0053] Furthermore, the terms "first" and "second" are used only to distinguish descriptions and should not be interpreted as indicating or implying relative importance.
[0054] It should be noted that, where there is no conflict, the features in the embodiments of the present invention can be combined with each other.
[0055] Please refer to Figure 1 This is a block diagram of an electronic device 100 provided in this application. The electronic device 100 can be a device capable of data processing, such as a terminal device containing native applications (mobile phone, tablet computer, etc.). This embodiment does not limit this. The electronic device 100 includes a memory 110, a processor 120, and a communication module 130. The memory 110, processor 120, and communication module 130 are electrically connected directly or indirectly to achieve data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses or signal lines.
[0056] The memory 110 is used to store programs or data. The memory 110 may be, but is not limited to, random access memory (RAM), read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), etc.
[0057] The processor 120 is used to read / write data or programs stored in memory and to perform corresponding functions.
[0058] The communication module 130 is used to establish a communication connection between the server and other communication terminals through the network, and to send and receive data through the network.
[0059] It should be understood that, Figure 1 The structure shown is only a schematic diagram of the electronic device 100. The electronic device 100 may also include components that are larger than... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown. Figure 1 The components shown can be implemented using hardware, software, or a combination thereof. This electronic device 100 can be integrated into other devices or configured as a standalone device.
[0060] Corresponding to electronic device 100, this embodiment of the invention provides a shared memory management method for communication between a native application and a mobile webpage. The native application has shared memory configured, and the mobile webpage and the native application read / write data in the shared memory in binary data format. The method includes, for example: Figure 2 The following steps are shown:
[0061] Step S110: If shared memory expansion is possible, determine the actual expansion memory based on the relationship between the target memory and the maximum preset memory, and expand the shared memory according to the actual expansion memory. The target memory is the size of the shared memory after expansion according to preset rules.
[0062] Step S120: Use the actual expanded memory as the expanded shared memory, and notify the listener on the mobile webpage and the native application to update the memory usage statistics.
[0063] In step S110, if shared memory expansion is possible, the actual expansion memory is determined based on the relationship between the target memory and the maximum preset memory, and the shared memory is expanded according to the actual expansion memory. The target memory is the size of the shared memory after expansion according to preset rules.
[0064] The native application creates a shared memory space, allowing mobile web pages and native applications to directly exchange binary data, thus avoiding the overhead of traditional string serialization and deserialization. When the data volume increases and memory expansion is required, and the system determines that the conditions for shared memory expansion are met during operation, the system calculates the theoretical "target memory" according to preset rules. This target memory represents the size after expansion according to a predetermined growth strategy. For example, the preset rule is to double the current shared memory size to the target memory; or, for example: target memory = current shared memory size × preset expansion multiple (preset expansion multiple ≥ 1.2 and ≤ 2.0), or target memory = current shared memory size + the difference between the memory used for writing data and the remaining memory.
[0065] The target memory is compared with a pre-set maximum preset memory (i.e., the upper limit of memory after the shared memory is expanded), and the specific value of the actual expanded memory (i.e., the actual size of the shared memory after expansion) is finally determined based on the relationship between the two.
[0066] For example, if the target memory does not exceed the maximum preset memory, the target memory is directly used as the actual expansion memory; if the target memory exceeds the maximum preset memory, the maximum preset memory is used as the actual expansion memory. After determining the actual expansion memory, the physical expansion operation of the shared memory is performed based on this.
[0067] Optionally, the shared memory includes a data storage area, and the method further includes:
[0068] Based on a preset memory utilization calculation formula, the memory utilization rate of the shared memory is obtained according to the first pointer, the second pointer, and the size of the shared memory; wherein, the first pointer is a pointer pointing to the current writable data position in the data storage area, and the second pointer is a pointer pointing to the current readable data position in the data storage area;
[0069] If the memory usage rate is greater than a preset usage rate threshold, the target memory is determined, and based on the maximum preset memory, the shared memory, and the target memory, it is determined whether the shared memory can be expanded.
[0070] If shared memory can be expanded, the shared memory is expanded according to the actual expansion memory.
[0071] To determine the specific conditions under which shared memory can be expanded, the current memory utilization rate can be calculated using a preset memory utilization rate calculation formula, combined with the first pointer pointing to the writable location of the data storage area, the second pointer pointing to the readable location, and the total size of the shared memory.
[0072] For example, the default formula for calculating memory usage is:
[0073] usedSpace=(writePointer-readPointer+totalSize)%totalSize
[0074] In this calculation, `writePointer` is the first pointer, `readPointer` is the second pointer, and `totalSize` is the total size of the shared memory. The offset or distance between the first and second pointers is calculated by subtracting `readPointer` from `writePointer`, representing the amount of data that has been written but not yet read. Since the result of `writePointer - readPointer` may be negative, it is added to the total size of the shared memory to ensure that the result is positive even if the first pointer is smaller than the second pointer. Finally, a modulo operation is performed on the result of `(writePointer - readPointer + totalSize)` to obtain the memory utilization rate and ensure that the location within the data storage area is correctly mapped back to the valid range.
[0075] When memory usage exceeds a preset threshold, such as 80%, it is determined that expansion is needed, and the target memory is calculated according to preset rules. The system comprehensively judges whether expansion is possible based on the maximum preset memory, the shared memory size, and the target memory. For example, it checks whether the target memory exceeds the system's maximum preset memory limit, or whether the shared memory is equal to the maximum preset memory. If expansion is possible, the expansion operation is then executed based on the determined actual expansion memory, thus clarifying the trigger condition for expansion as the memory usage reaching a certain critical state, making memory management more predictable.
[0076] Optionally, the method further includes:
[0077] Receive a write request, determine the memory required for the write data, and obtain the remaining memory in the data storage area of the shared memory;
[0078] If the memory for writing data is greater than the remaining memory, determine the target memory, and based on the maximum preset memory, the shared memory, and the target memory, determine whether the shared memory can be expanded.
[0079] If shared memory can be expanded, the shared memory is expanded according to the actual expansion memory, and after the expansion is completed, data is written into the data storage area of the expanded shared memory in chronological order.
[0080] In another optional implementation, the feasibility of shared memory expansion can be determined based on write requests. Upon receiving a write request, the size of the write data memory required by the data carried in the request is determined, and the remaining memory size of the data storage area in the current shared memory is obtained according to a preset memory utilization calculation formula. By comparing these two values, if the write data memory is greater than the remaining memory, meaning the current free space is insufficient to accommodate the data to be written, expansion is deemed necessary, and the target memory is calculated according to preset rules. Subsequently, as described above, the feasibility of expansion is determined based on the maximum preset memory, the current shared memory, and the target memory. If feasible, the expansion is completed according to the determined actual expansion memory.
[0081] Since new write requests may be received during memory expansion, in order to ensure that the write order is not incorrect, after the expansion operation is completed, the data can be written to the data storage area of the expanded shared memory in the order in which the requests arrived. This ensures that memory resources can be expanded in time to meet the demand when faced with specific write pressure.
[0082] Optionally, the method further includes:
[0083] If shared memory cannot be expanded, an error signal is generated, the number of consecutive errors is recorded, and the shared memory expansion is reassessed after a first interval. If shared memory expansion still cannot be expanded, an error signal is generated, the number of consecutive errors is recorded, and the shared memory expansion is reassessed after the first interval until it is determined that shared memory expansion can be expanded, at which point the number of consecutive errors is cleared to zero.
[0084] If the number of consecutive errors exceeds a preset consecutive error threshold, then after a second interval longer than the first interval, it is re-evaluated whether shared memory expansion can be performed. If shared memory expansion cannot be performed, the re-evaluation interval is increased until it is determined that shared memory expansion can be performed, and then the number of consecutive errors is reset to zero.
[0085] When the system determines that shared memory cannot be expanded, it will generate an error signal and record the error, while accumulating the number of consecutive errors. For example, if the current shared memory has reached the maximum limit or the remaining memory in the total physical memory is insufficient to support memory growth, the number of consecutive errors will be accumulated to 1.
[0086] The system waits for a first interval, after which it re-initiates a check to determine whether shared memory expansion is possible. If expansion is still not possible after the re-check, the process repeats: the corresponding error signal is generated again and the number of consecutive errors is accumulated, the system waits for the first interval, and then retryes. This loop continues until a check determines that expansion is possible, at which point the accumulated number of consecutive errors is cleared to zero.
[0087] To address persistent failures, this method also incorporates threshold management. If the number of consecutive errors exceeds a preset threshold (e.g., three consecutive errors), the system extends the waiting time, re-evaluating after a longer second interval. If expansion still fails in subsequent evaluations, the system further increases the interval between each re-evaluation (e.g., increasing the first interval by 50% each time), and so on, until successful expansion is achieved and the error count is cleared. This progressively extending retry interval mechanism effectively avoids the additional load caused by frequent expansion attempts when memory resources are scarce or the system is busy.
[0088] Optionally, the step of determining the actual expansion memory based on the size relationship between the target memory and the maximum preset memory, and expanding the shared memory according to the actual expansion memory, includes:
[0089] If the target memory is less than or equal to the maximum preset memory, the target memory is determined to be the actual expanded memory;
[0090] If the target memory is greater than the maximum preset memory, the maximum preset memory is determined to be the actual expansion memory;
[0091] Pause writing data to the shared memory and create the actual expanded memory;
[0092] The data in the shared memory is migrated to the actual expanded memory;
[0093] Release the shared memory, update the first pointer and the second pointer, and complete the expansion of the shared memory.
[0094] As an optional implementation, the target memory can be compared with the maximum preset memory size to determine the specific value of the actual memory expansion. That is, if the target memory is less than or equal to the maximum preset memory, the target memory is determined to be the actual expansion memory; if the target memory is greater than the maximum preset memory, the maximum preset memory is determined to be the actual expansion memory.
[0095] Once confirmed, writing data to the shared memory is first suspended to ensure data consistency during the expansion process. Then, the system creates a new memory region in the physical memory of the device hosting the native application, with a size equal to the actual expanded memory. All valid data stored in the original shared memory is migrated to the newly created expanded memory region. After the data migration is complete, the original shared memory is released, and its occupied resources are reclaimed. Simultaneously, the first and second pointers pointing to the read / write locations of the data storage area are updated to reflect the correct location of the data in the new memory region. With the shared memory expansion complete, the system will switch to using the new, larger-capacity shared memory.
[0096] Optionally, both the shared memory and the actual expanded memory's data storage area are circular buffers, and the step of migrating data from the shared memory to the actual expanded memory includes:
[0097] Determine the current data size in the data storage area of the shared memory, and determine the relative positions of the first pointer and the second pointer;
[0098] If the second pointer is smaller than the first pointer, the data in the shared memory data storage area is directly migrated to the data storage area in the actual expanded memory.
[0099] If the second pointer is greater than or equal to the first pointer, the first part of the data is determined according to the current data size and the second pointer. The first part of the data is the data from the second pointer to the end of the data storage area in the shared memory.
[0100] According to the second pointer, the first part of the data is migrated to the beginning position of the data storage area in the actual expanded memory;
[0101] After migrating the second part of the data to the first part of the data according to the first pointer, the data migration is completed; wherein, the second part of the data is the data in the data storage area of the shared memory other than the first part of the data.
[0102] As an optional implementation, in order to make data reading and writing more efficient, allow data to be written and read continuously, and reduce the latency caused by reading and writing data in shared memory, both the shared memory and the data storage area of the actual expanded memory can be set as a circular buffer.
[0103] For a circular buffer, first determine the total amount of data currently stored in the original shared memory area, and then determine the relative positions of the first and second pointers. If the second pointer is less than the first pointer, it indicates that the data is stored contiguously in the circular buffer and there is no "circling". In this case, the entire block of data can be directly copied from the original memory area to the starting position of the new memory area.
[0104] If the second pointer is greater than or equal to the first pointer, it indicates that data has been written from the end of the buffer and wrapped around to the beginning, meaning the data is physically discontinuous. In this case, the data from the second pointer to the end of the original memory area can be defined as the first part of the data, and this part can be moved to the beginning of the new memory area. After the move, the second pointer will point to the beginning of the actual expanded memory area. Then, the remaining data from the beginning of the original memory area to the first pointer can be defined as the second part of the data, and this part can be moved to the new memory area, immediately following the first part. After the move, the first pointer will point to the end of the second part. This reorganizes the data into a contiguous storage layout in the new area, completing the entire data migration process.
[0105] After data migration is complete, when writing data, the system first checks if there are any cases where the data corresponding to an ID (a unique identifier for each data record in the circular buffer, used to locate and manage data in the circular buffer) is empty (during data cleanup, only the data corresponding to the ID is cleaned up, but the ID itself is not). If it exists, the data to be written, carried in the write request, is stored in the location corresponding to that ID in the circular buffer. If it does not exist (i.e., each ID corresponds to data), a new ID is created for the data to be written, carried in the write request, and the data is stored in the location corresponding to the new ID. This achieves an ID reuse mechanism, effectively utilizing ID space.
[0106] Optionally, the method further includes:
[0107] After the mobile webpage reads data from the shared memory, garbage collection is performed on the shared memory.
[0108] The memory utilization rate is calculated based on the preset memory utilization rate calculation formula;
[0109] If the memory usage rate remains below the preset memory usage rate lower limit for a preset period of time, or if a system memory alarm signal from the native application is received, the shared memory is reduced to obtain the actual memory reduction.
[0110] The actual reduced memory is used as the reduced shared memory.
[0111] After a mobile webpage reads data from shared memory, the system performs a garbage collection operation on the shared memory, reclaiming the space occupied by the read data. In one optional implementation, the system can recalculate the current memory usage using a preset memory usage calculation formula and continuously monitor this memory usage. If, within a preset time period, the memory usage remains below a lower preset memory usage limit, or if the system receives a system memory alarm signal from the native application's operating system (such as insufficient system memory), a memory reduction decision is triggered.
[0112] When any of the above conditions are met, the system performs a reduction operation on the current shared memory. Similar to shared memory expansion, a smaller actual reduction in memory is calculated (for example, the reduced memory is specified to be no less than the data size currently occupied in the shared memory and no less than the minimum preset memory). This actual reduction in memory is then used as new, reduced shared memory, thereby proactively releasing excess memory resources and improving memory utilization efficiency when memory utilization is consistently low or the overall system memory is strained.
[0113] In step S120, the actual expanded memory is used as the expanded shared memory, and the listeners on the mobile webpage and the native application are notified to update the memory usage statistics.
[0114] After the expansion is completed, the system will establish the newly created and expanded memory as the currently effective shared memory, and simultaneously notify the listeners registered on both the mobile webpage and the native application, so that they can update the memory usage statistics accordingly, thereby completing a complete memory adaptive adjustment cycle.
[0115] For example, the native application's listener has a memory statistics module responsible for monitoring and reporting memory usage, and a cache manager that manages data caching strategies. Upon receiving a notification, the memory statistics module updates memory usage statistics, such as currently used memory and total memory size, and the cache manager adjusts the caching strategy (such as the LRU eviction threshold) based on the new memory size.
[0116] Understandably, after the memory reduction is completed, the listeners on mobile web pages and native applications can still update memory usage statistics in the same way as the above notification.
[0117] To better explain the solutions of the embodiments of the present invention, the embodiments of the present invention also provide, as follows: Figure 3 The flowchart shown illustrates the solution of the present invention.
[0118] Please see Figure 3 To determine whether memory expansion is needed, we consider two factors: First, the memory utilization rate of the shared memory. If the utilization rate is greater than the preset utilization rate threshold, then memory expansion is required. Second, when a write request is received, if the memory for the write data corresponding to the write request is greater than the remaining memory, then memory expansion is required.
[0119] When memory expansion is required, the target memory is determined, and the maximum preset memory, shared memory, and target memory are used to determine whether shared memory expansion is possible.
[0120] If memory expansion cannot be performed, an error signal is generated, the number of consecutive errors is accumulated, and a new assessment is made after the first interval. If shared memory expansion can be performed, the accumulated number of consecutive errors is cleared; if it cannot be performed, the above steps are repeated. If the number of consecutive errors exceeds a preset consecutive error threshold, the interval is extended after each subsequent assessment until shared memory expansion can be performed.
[0121] If memory expansion is possible, first determine the actual expansion memory based on the relationship between the target memory and the maximum preset memory. Pause writing data to shared memory and migrate the data stored in shared memory to the newly created expanded memory. Release the shared memory, update the first and second pointers, and complete the shared memory expansion.
[0122] After the expansion is complete, notify the listeners on the mobile web page and native application to update the memory usage statistics.
[0123] After garbage collection, if the calculated memory usage rate is consistently lower than the preset memory usage rate limit within a preset time period, the memory will be reduced in a manner similar to memory expansion. The data will be migrated to the actual reduced memory, the original shared memory will be released, the pointers will be updated, and the listener will be notified to update the memory usage statistics.
[0124] Based on the same inventive concept, such as Figure 4As shown in the figure, an embodiment of the present invention provides a shared memory management device 300 for communication between a native application and a mobile webpage. The native application has shared memory, and the mobile webpage and the native application read / write data on the shared memory in binary data form. The shared memory management device 300 includes:
[0125] The memory expansion unit 301 is used to determine the actual expansion memory based on the size relationship between the target memory and the maximum preset memory when shared memory expansion is possible, and to expand the shared memory according to the actual expansion memory; wherein, the target memory is the size of the shared memory after expansion according to preset rules;
[0126] The update notification unit 302 is used to treat the actual expanded memory as the expanded shared memory and notify the listeners on the mobile webpage and the native application to update the memory usage statistics.
[0127] Regarding the shared memory management device 300 described above, the specific functions of each unit have been described in detail in the embodiments of the shared memory management method provided in this specification, and will not be elaborated here.
[0128] Based on the same inventive concept, embodiments of this specification provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of any of the methods of the aforementioned shared memory management method.
[0129] The present invention has at least the following beneficial effects:
[0130] 1. By setting up shared memory on the native application, mobile web pages and native applications can read and write data to shared memory in binary form, reducing or even eliminating the overhead of string serialization and deserialization. When the amount of data increases and shared memory becomes insufficient, it can be expanded if possible, ensuring the stability of data transmission between the native application and the mobile web page. This reduces CPU utilization and improves CPU speed and data processing efficiency.
[0131] 2. Two mechanisms are set up to determine whether to expand memory, so that memory can be expanded in a timely manner when shared memory is insufficient, avoiding situations where the amount of data is too large and there is insufficient memory and the shared memory cannot be expanded in time.
[0132] 3. Setting the shared memory data storage area as a circular buffer makes data reading and writing more efficient, allows data to be written and read continuously, and reduces the latency caused by reading and writing data in shared memory.
[0133] 4. When cleaning up data, only the data corresponding to the ID is cleaned up, and the ID itself is not cleaned up. This implements an ID reuse mechanism and effectively utilizes the ID space.
[0134] 5. Reduce memory usage when system memory is insufficient or shared memory is underutilized for an extended period, proactively releasing excess memory resources and improving memory utilization efficiency.
[0135] In the several embodiments provided by this invention, it should be understood that the disclosed apparatus and methods can also be implemented in other ways. The apparatus embodiments described above are merely illustrative; for example, the flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of the invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram and / or flowchart, and combinations of blocks in block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
[0136] In addition, the functional modules in the various embodiments of the present invention can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0137] If the aforementioned functions are implemented as software functional modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0138] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A shared memory management method, characterized in that, For communication between a native application and a mobile webpage, the native application has shared memory, which includes a data storage area. The mobile webpage and the native application read / write data in the shared memory in binary data format. The method includes: Based on a preset memory utilization calculation formula, the memory utilization rate of the shared memory is obtained according to the first pointer, the second pointer, and the size of the shared memory; wherein, the first pointer is a pointer to the current writable data position in the data storage area, and the second pointer is a pointer to the current readable data position in the data storage area; if the memory utilization rate is greater than a preset utilization rate threshold, a target memory is determined, and it is determined whether the shared memory can be expanded based on the maximum preset memory, the shared memory, and the target memory; When shared memory expansion is possible, the actual expansion memory is determined based on the relationship between the target memory and the maximum preset memory, and the shared memory is expanded according to the actual expansion memory, wherein the target memory is the size of the shared memory after expansion according to preset rules; The actual expanded memory is used as the expanded shared memory, and the listeners on the mobile webpage and the native application are notified to update the memory usage statistics; If shared memory expansion cannot be performed, an error signal is generated, the number of consecutive errors is recorded, and a new assessment is conducted after a first interval to determine whether shared memory expansion can be performed. If shared memory expansion still cannot be performed, an error signal is generated, the number of consecutive errors is recorded, and a new assessment is conducted after the first interval to determine whether shared memory expansion can be performed, until it is determined that shared memory expansion can be performed, at which point the number of consecutive errors is reset to zero. If the number of consecutive errors exceeds a preset consecutive error threshold, a new assessment is conducted after a second interval longer than the first interval to determine whether shared memory expansion can be performed, and if shared memory expansion cannot be performed, the interval for reassessment is increased until it is determined that shared memory expansion can be performed, at which point the number of consecutive errors is reset to zero.
2. The shared memory management method as described in claim 1, characterized in that, The method further includes: Receive a write request, determine the memory required for the write data, and obtain the remaining memory in the data storage area of the shared memory; If the memory for writing data is greater than the remaining memory, determine the target memory, and based on the maximum preset memory, the shared memory, and the target memory, determine whether the shared memory can be expanded. If shared memory can be expanded, the shared memory is expanded according to the actual expansion memory, and after the expansion is completed, data is written into the data storage area of the expanded shared memory in chronological order.
3. The shared memory management method as described in claim 1, characterized in that, The step of determining the actual expansion memory based on the size relationship between the target memory and the maximum preset memory, and expanding the shared memory according to the actual expansion memory, includes: If the target memory is less than or equal to the maximum preset memory, the target memory is determined to be the actual expanded memory; If the target memory is greater than the maximum preset memory, the maximum preset memory is determined to be the actual expansion memory; Pause writing data to the shared memory and create the actual expanded memory; The data in the shared memory is migrated to the actual expanded memory; Release the shared memory, update the first pointer and the second pointer, and complete the expansion of the shared memory.
4. The shared memory management method as described in claim 3, characterized in that, Both the shared memory and the actual expanded memory serve as circular buffers for data storage. The process of migrating data from the shared memory to the actual expanded memory includes: Determine the current data size in the data storage area of the shared memory, and determine the relative positions of the first pointer and the second pointer; If the second pointer is smaller than the first pointer, the data in the shared memory data storage area is directly migrated to the data storage area in the actual expanded memory. If the second pointer is greater than or equal to the first pointer, the first part of the data is determined according to the current data size and the second pointer. The first part of the data is the data from the second pointer to the end of the data storage area in the shared memory. According to the second pointer, the first part of the data is migrated to the beginning position of the data storage area in the actual expanded memory; After migrating the second part of the data to the first part of the data according to the first pointer, the data migration is completed; wherein, the second part of the data is the data in the data storage area of the shared memory other than the first part of the data.
5. The shared memory management method as described in claim 1, characterized in that, The method further includes: After the mobile webpage reads data from the shared memory, garbage collection is performed on the shared memory. The memory utilization rate is calculated based on the preset memory utilization rate calculation formula; If the memory usage rate remains below the preset memory usage rate lower limit for a preset period of time, or if a system memory alarm signal from the native application is received, the shared memory is reduced to obtain the actual memory reduction. The actual reduced memory is used as the reduced shared memory.
6. A shared memory management device, characterized in that, This is used for communication between native applications and mobile web pages. The native application has a shared memory, which includes a data storage area. The mobile web page and the native application read / write data in the shared memory in the form of binary data. The shared memory management device includes: A memory expansion unit is used to determine the actual expansion memory based on the relationship between the target memory and the maximum preset memory when shared memory expansion is possible, and to expand the shared memory according to the actual expansion memory; wherein, the target memory is the size of the shared memory after expansion according to preset rules; based on a preset memory utilization rate calculation formula, the memory utilization rate of the shared memory is obtained according to a first pointer, a second pointer, and the size of the shared memory; wherein, the first pointer is a pointer pointing to the current writable data position in the data storage area, and the second pointer is a pointer pointing to the current readable data position in the data storage area; if the memory utilization rate is greater than a preset utilization rate threshold, a target memory is determined, and based on the maximum preset memory, the shared memory, and the target memory size, the memory utilization rate is calculated. The system determines whether shared memory expansion is possible. If it is not possible, an error signal is generated, the number of consecutive errors is recorded, and the system re-evaluates whether shared memory expansion is possible after a first interval. If it is still not possible, an error signal is generated, the number of consecutive errors is recorded, and the system re-evaluates whether shared memory expansion is possible after the first interval, until it is determined that shared memory expansion is possible, at which point the number of consecutive errors is reset to zero. If the number of consecutive errors exceeds a preset consecutive error threshold, the system re-evaluates whether shared memory expansion is possible after a second interval longer than the first interval, and if shared memory expansion is not possible, the re-evaluation interval is increased until it is determined that shared memory expansion is possible, at which point the number of consecutive errors is reset to zero. The update notification unit is used to treat the actual expanded memory as the expanded shared memory and to notify the listeners on the mobile webpage and the native application to update the memory usage statistics.
7. An electronic device, characterized in that, include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the program, performs the steps of the method according to any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a computer program, which, when executed, controls the server where the computer-readable storage medium is located to implement the steps of the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Data caching method and device, electronic equipment and storage medium
CN114048152A
JsBridge communication method and device during running of small swan gap program and storage medium
CN118331765A