Method of data sharing, method of memory management, and electronic device

CN122535883APending Publication Date: 2026-08-07HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
HUAWEI TECH CO LTD
Filing Date
2024-09-14
Publication Date
2026-08-07

AI Technical Summary

Technical Problem

In JavaScript, data sharing capabilities between multiple threads are insufficient, especially when processing complex data types or huge data volumes, the existing inter-thread communication methods are inefficient, affecting the processing efficiency of high concurrent tasks.

Method used

By saving the data that multiple threads need to use in the shared memory space, threads directly access the shared memory space, reducing data transmission, and using identification information to pass data addresses to realize data sharing, ensuring that data is transmitted in an unmodified state and avoiding data competition.

Benefits of technology

It improves data sharing efficiency between multiple threads, improves task processing capabilities in high concurrency scenarios, reduces the risk of multi-thread competition resources, and improves the stability and security of software development and operation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122535883A_ABST
    Figure CN122535883A_ABST
Patent Text Reader

Abstract

The application provides a data sharing method. Different threads of JavaScript can save and read data required in a cooperative processing process in a shared memory space. The data can no longer be transmitted through inter-thread communication, and different threads can use the data saved in the shared memory space through reference. The efficiency of data sharing between JavaScript threads is high, and the electronic device has high capability for task processing in a high concurrency scenario. In addition, the data sharing between threads is realized by setting a shared memory space. The scheme does not limit the data type and data volume shared between threads, and therefore, the implementation of the technical scheme is particularly suitable for concurrent processing of complex objects and / or large data objects.
Need to check novelty before this filing date? Find Prior Art

Description

Data sharing method, memory management method and electronic device

[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of China on December 29, 2023, with application number 202311861493.3 and invention name “Data sharing method, memory management method and electronic device”, the entire contents of which are incorporated by reference into this application. Technical Field

[0002] The present application relates to the field of terminal device software, and in particular, to a data sharing method, a memory management method, and an electronic device. Background Art

[0003] JavaScript (JS) is a high-level language that can be used for web and network development. It is widely used in servers, mobile applications, game development, and the Internet of Things.

[0004] In scenarios where multithreaded collaboration is required, different threads often need to share the same data. JavaScript threads can use serialized communication mechanisms to synchronize data and perform other concurrent tasks. However, this inter-thread communication-based data sharing capability is limited, especially when dealing with complex data types or large amounts of data. Therefore, improving data sharing capabilities between multiple threads in JavaScript applications is a worthy issue.

[0005] Summary of the Invention

[0006] This application provides a data sharing method, where different JS threads can read information in the same memory space. The data required by multiple JS threads does not need to be transmitted through inter-thread communication. The data sharing capability between threads is strong, and the efficiency of collaborative task processing between threads is higher.

[0007] In a first aspect, a data sharing method is provided, which is applied to an electronic device, including: a first thread saving a first object to a target memory space; a second thread reading the first object from the target memory space; wherein the first thread and the second thread are both JavaScript threads.

[0008] In some scenarios, the target memory space may also be referred to as a shared memory space, a first memory space, a shared memory, or a first memory, and objects stored in the target memory space may be referred to as shared objects.

[0009] In a possible implementation, the target memory space may be a heap space, or in other words, the data in the target memory space may be stored and managed in a heap data structure.

[0010] It should be noted that both the main thread and the worker thread of JavaScript can be referred to as JavaScript threads. In the above solution, one of the first thread or the second thread can be the main thread, and the other can be the worker thread. Alternatively, both the first thread and the second thread can be worker threads.

[0011] In this technical solution, the first thread can write data in the target memory space, and the second thread can read the data written by the first thread from the target memory space. In other words, both the first thread and the second thread can access the target memory space. In the case where the first thread and the second thread collaborate on processing tasks, the data that both the first thread and the second thread need to use can be directly saved in the target memory space, thereby facilitating reading by multiple threads. Compared with the method of transmitting the first object through inter-thread communication, the implementation of this technical solution is conducive to improving the speed at which the second thread reads the first object, and is conducive to improving the task processing capabilities of JavaScript applications in high-concurrency scenarios.

[0012] In addition, since this solution realizes data sharing between threads by establishing a shared memory space, the solution does not impose any restrictions on the type and amount of data shared between threads. Therefore, this technical solution is particularly suitable for concurrent processing involving complex objects and / or objects with large data volumes.

[0013] In combination with the first aspect, in some implementations of the first aspect, before the second thread reads the first object from the target memory space, the first thread sends identification information to the second thread, where the identification information is used to indicate the first object.

[0014] In a possible implementation, the identification information may be address information of the first object in the target memory space. In other words, the second thread may access the first object in the target memory space by reference.

[0015] Compared to the first object itself, the identification information of the first object is smaller in size, and the time it takes to transmit the identification information between the first and second threads is shorter. The second thread can obtain the relevant information of the first object more quickly, thereby obtaining the first object. The implementation of this technical solution is conducive to improving the efficiency of data sharing between the first and second threads, and is conducive to improving the ability of multiple JS threads to process tasks concurrently.

[0016] In combination with the first aspect, in some implementations of the first aspect, the second thread creates a second object based on the first object, the second object and the first object include attributes with the same key names, and / or the second object and the first object include the same methods.

[0017] In a possible implementation, the first object includes only methods, and the second object includes the same methods as the first object.

[0018] In one possible implementation, the first object only includes attributes. For example, the first object includes a first attribute and a second attribute. The second object also includes a first attribute and a second attribute. The value of the first attribute of the second object is the same as the value of the first attribute of the first object. The value of the second attribute of the second object is determined according to the second thread.

[0019] In one possible implementation, the first object includes properties and methods, the second object includes the same methods as the first object, the second object includes properties with the same key names as the first object, and the values ​​of the properties with the same key names in the first object and the second object may be different.

[0020] An independent new object is created based on the first object, and data processing operations are performed based on the newly created object. The implementation of this technical solution is conducive to reducing the competition problem caused by multiple threads when processing the same data, and is conducive to improving the stability of application data processing and the reliability of processing results. Software developers do not need to consider the locking problem introduced by multi-threaded competition for resources during the software development process, or in other words, the probability of locking problems introduced by multi-threaded competition for resources during the software development process is small, which is conducive to improving the efficiency of software development.

[0021] In combination with the first aspect, in some implementations of the first aspect, the second object is stored in the target memory space.

[0022] Storing the second object in the shared memory space is beneficial for other threads to perform subsequent processing operations based on the second object. The implementation of this technical solution is beneficial to improving the processing efficiency of concurrent tasks between threads.

[0023] In combination with the first aspect, in some implementations of the first aspect, before the first thread sends identification information to the second thread, the first thread performs target processing on the first object, and the first object after the target processing cannot be modified.

[0024] In some scenarios, the target process may also be referred to as an "unchanged" process or a "frozen" process.

[0025] In a possible implementation, the unmodifiable state of the first object is unchangeable or irreversible, or in other words, the first object cannot be modified to a modifiable state again after the target processing is performed.

[0026] Before sending the identification information of the first object, the first object is set to an unmodifiable state. In this way, even if the second thread can obtain the first object through the first identification information, it cannot modify the first object itself. Implementation of this technical solution helps avoid data races caused by the first and second threads reading and writing to the first object, and helps ensure that only one thread has the right to perform write operations on the first object at a time, thereby improving the security and reliability of software operation.

[0027] In combination with the first aspect, in some implementations of the first aspect, the first object includes a first field, and the first field is used to indicate that the first object is referenced.

[0028] In some scenarios, the first field may also be understood as being used to indicate whether the first object is referenced by the thread's heap, or in other words, the first field may be used to indicate whether the first object is referenced by an object stored on the thread's heap.

[0029] In some scenarios, the first field may also be called a first attribute, a first state variable, or a first domain.

[0030] In combination with the first aspect, in some implementations of the first aspect, the first field is further used to indicate the number of different threads that reference the first object.

[0031] In a possible implementation, when the first field is used to indicate the number of threads referencing the first object, the value of the first field may be dynamically changed according to the number of threads referencing the first object.

[0032] In some scenarios, the fact that the first object is referenced may also be understood as meaning that the memory resources occupied by the first object cannot be reclaimed.

[0033] Objects stored in a shared memory space can be provided with information indicating whether the object is referenced. The electronic device can determine whether the memory resources occupied by the first object need to be reclaimed based on the information. The implementation of this technical solution is conducive to improving the efficiency of electronic devices in recovering memory resources occupied by shared memory space, and is conducive to improving the memory resource utilization efficiency of electronic devices.

[0034] In combination with the first aspect, in certain implementations of the first aspect, the memory space of the first thread stores first information, the first information is used to indicate a first reference object, the first reference object is an object referenced by the first thread and stored in the target memory space, and the first reference object includes the first object; the memory space of the second thread stores second information, the second information is used to indicate a second reference object, the second reference object is an object referenced by the second thread and stored in the target memory space, and the second reference object includes the first object.

[0035] The memory spaces of the first thread and the second thread can both store their references to the data in the shared memory space. The electronic device can update the value of the first field of the aforementioned first object based on the information stored in these threads, so that the reference status of the first object can be obtained more accurately and quickly. The implementation of this technical solution is conducive to improving the utilization efficiency of memory resources in electronic devices.

[0036] In combination with the first aspect, in some implementations of the first aspect, the first object includes a second field, and the second field is used to indicate that the first object cannot be modified.

[0037] In some scenarios, the second field may also be referred to as a second attribute, a second state variable, or a second domain.

[0038] The information indicating whether the first object is in a modifiable state is saved in the first object. In this way, when multiple threads obtain the first object, this information can be used to determine whether the data of the first object is likely to be tampered with or erroneous, thereby determining whether to use the obtained first object to perform the corresponding operation. The implementation of this technical solution is conducive to improving the reliability of multi-threaded collaborative processing tasks and reducing the probability of errors during application operation.

[0039] In a second aspect, a memory management method is provided, which is applied to an electronic device, including: saving a first object in a target memory space, where the target memory space is a memory space accessible to multiple JavaScript threads; and in response to the first object no longer being referenced, reclaiming the memory resources occupied by the first object.

[0040] In some scenarios, the target memory space may also be referred to as a shared memory space, a first memory space, a shared memory, or a first memory, and objects stored in the target memory space may be referred to as shared objects.

[0041] The memory resources occupied by data in the shared memory space can be promptly recovered when not in use. The implementation of this technical solution is conducive to improving the utilization efficiency of the memory resources of the electronic device and reducing the ineffective occupation of the memory resources of the electronic device by the shared memory.

[0042] In combination with the second aspect, in some implementations of the second aspect, the first object includes a first field, the first field is used to indicate the number of different threads that reference the first object, and in response to the value of the first field being zero, the memory resources occupied by the first object are reclaimed.

[0043] In a possible implementation, when the first field is used to indicate the number of threads referencing the first object, the value of the first field may be dynamically changed according to the number of threads referencing the first object.

[0044] Objects stored in a shared memory space can be provided with information indicating whether the object is referenced. The electronic device can determine whether the memory resources occupied by the first object need to be reclaimed based on the information. The implementation of this technical solution is conducive to improving the efficiency of electronic devices in recovering memory resources occupied by shared memory space, and is conducive to improving the memory resource utilization efficiency of electronic devices.

[0045] In combination with the second aspect, in some implementations of the second aspect, the value of the first field is determined according to reference information of the target thread, the reference information is used to indicate the first object, and the multiple JavaScript threads include the target thread.

[0046] In combination with the second aspect, in certain implementations of the second aspect, the target memory space also includes a second object, the first object references the second object, the reference information includes first sub-information, second sub-information and third sub-information, the first sub-information is used to indicate the first object, the second sub-information is used to indicate the second object, and the third sub-information is used to indicate that the first object references the second object.

[0047] In a possible implementation, the reference information may be represented in the form of a linked list, the first sub-information and the second sub-information may be two nodes in the linked list, and the third sub-information may be a pointer in the linked list.

[0048] The memory spaces of the first thread and the second thread can both store their references to the data in the shared memory space. The electronic device can update the value of the first field of the aforementioned first object based on the information stored in these threads, so that the reference status of the first object can be obtained more accurately and quickly. The implementation of this technical solution is conducive to improving the utilization efficiency of memory resources in electronic devices.

[0049] For detailed explanations and beneficial effects of the following technical solutions, please refer to the relevant descriptions in the first aspect. For the sake of brevity, they will not be repeated below.

[0050] In a third aspect, a data sharing device is provided, which includes a first processing module and a second processing module, the first processing module is used to: save the first object to the target memory space; the second processing module is used to: read the first object from the target memory space; wherein the first processing module and the second processing module are both used to execute tasks executed by JavaScript threads.

[0051] In combination with the third aspect, in certain implementations of the third aspect, before the second processing module reads the first object from the target memory space, the first processing module is further used to: send identification information to the second processing module, where the identification information is used to indicate the first object.

[0052] In combination with the third aspect, in certain implementations of the third aspect, the second processing module is also used to: create a second object, the second object includes a first attribute and a second attribute, the value of the first attribute of the second object is the same as the value of the first attribute of the first object, and the value of the second attribute of the second object is determined according to the second processing module.

[0053] In combination with the third aspect, in certain implementations of the third aspect, the second object is stored in the target memory space.

[0054] In combination with the third aspect, in certain implementations of the third aspect, before the first processing module sends identification information to the second processing module, the first processing module is further used to: perform target processing on the first object, and the first object after the target processing cannot be modified.

[0055] In combination with the third aspect, in some implementations of the third aspect, the first object includes a first field, and the first field is used to indicate that the first object is referenced.

[0056] In combination with the third aspect, in some implementations of the third aspect, the first field is further used to indicate the number of different threads that reference the first object.

[0057] In combination with the third aspect, in certain implementations of the third aspect, the memory space of the first processing module stores first information, the first information is used to indicate a first reference object, the first reference object is an object referenced by the first processing module and stored in the target memory space, and the first reference object includes the first object, and the memory space of the second processing module stores second information, the second information is used to indicate a second reference object, the second reference object is an object referenced by the second processing module and stored in the target memory space, and the second reference object includes the first object.

[0058] In combination with the third aspect, in some implementations of the third aspect, the first object includes a second field, and the second field is used to indicate that the first object cannot be modified.

[0059] In a fourth aspect, an electronic device is provided, which includes a first processor, a second processor and a memory, the memory being used to store program instructions, the first processor being used to: save a first object to a target memory space; the second processor being used to: read the first object from the target memory space; wherein both the first processor and the second processor are used to execute tasks executed by JavaScript threads.

[0060] In combination with the fourth aspect, in certain implementations of the fourth aspect, before the second processor reads the first object from the target memory space, the first processor is further used to: send identification information to the second processor, where the identification information is used to indicate the first object.

[0061] In combination with the fourth aspect, in certain implementations of the fourth aspect, the second processor is further used to: create a second object, the second object including a first attribute and a second attribute, the value of the first attribute of the second object is the same as the value of the first attribute of the first object, and the value of the second attribute of the second object is determined according to the second processor.

[0062] In combination with the fourth aspect, in certain implementations of the fourth aspect, the second object is stored in the target memory space.

[0063] In combination with the fourth aspect, in certain implementations of the fourth aspect, before the first processor sends identification information to the second processor, the first processor is further used to: perform target processing on the first object, and the first object after the target processing cannot be modified.

[0064] In combination with the fourth aspect, in some implementations of the fourth aspect, the first object includes a first field, and the first field is used to indicate that the first object is referenced.

[0065] In combination with the fourth aspect, in some implementations of the fourth aspect, the first field is further used to indicate the number of different threads that reference the first object.

[0066] In combination with the fourth aspect, in certain implementations of the fourth aspect, the memory space of the first processor stores first information, the first information is used to indicate a first reference object, the first reference object is an object referenced by the first processor and stored in the target memory space, and the first reference object includes the first object; the memory space of the second processor stores second information, the second information is used to indicate a second reference object, the second reference object is an object referenced by the second processor and stored in the target memory space, and the second reference object includes the first object.

[0067] In combination with the fourth aspect, in some implementations of the fourth aspect, the first object includes a second field, and the second field is used to indicate that the first object cannot be modified.

[0068] In a fifth aspect, a memory management device is provided, which includes a processing module, and the processing module is used to: save a first object in a target memory space, where the target memory space is a memory space accessible to multiple JavaScript threads; and reclaim the memory resources occupied by the first object in response to the first object no longer being referenced.

[0069] In combination with the fifth aspect, in certain implementations of the fifth aspect, the first object includes a first field, which is used to indicate the number of different threads that reference the first object, and the processing module is specifically used to: in response to the value of the first field being zero, reclaim the memory resources occupied by the first object.

[0070] In combination with the fifth aspect, in certain implementations of the fifth aspect, the value of the first field is determined according to reference information of the target thread, the reference information is used to indicate the first object, and the multiple JavaScript threads include the target thread.

[0071] In combination with the fifth aspect, in certain implementations of the fifth aspect, the target memory space also includes a second object, the first object references the second object, the reference information includes first sub-information, second sub-information and third sub-information, the first sub-information is used to indicate the first object, the second sub-information is used to indicate the second object, and the third sub-information is used to indicate that the first object references the second object.

[0072] In a sixth aspect, an electronic device is provided, which includes a processor and a memory, the memory being used to store program instructions, and the processor being used to: save a first object in a target memory space, the target memory space being a memory space accessible to multiple JavaScript threads; and reclaim memory resources occupied by the first object in response to the first object no longer being referenced.

[0073] In combination with the sixth aspect, in certain implementations of the sixth aspect, the first object includes a first field, which is used to indicate the number of different threads that reference the first object, and the processor is specifically used to: in response to the value of the first field being zero, reclaim the memory resources occupied by the first object.

[0074] In combination with the sixth aspect, in certain implementations of the sixth aspect, the value of the first field is determined according to reference information of the target thread, the reference information is used to indicate the first object, and the multiple JavaScript threads include the target thread.

[0075] In combination with the sixth aspect, in certain implementations of the sixth aspect, the target memory space also includes a second object, the first object references the second object, the reference information includes first sub-information, second sub-information and third sub-information, the first sub-information is used to indicate the first object, the second sub-information is used to indicate the second object, and the third sub-information is used to indicate that the first object references the second object.

[0076] In a seventh aspect, a computer program product is provided, which includes a computer program code, and when the computer program code runs on a computer, the method in the first aspect and any possible implementation thereof or the method in the second aspect and any possible implementation thereof is executed.

[0077] In an eighth aspect, a computer-readable storage medium is provided, which stores a computer program code. When the computer program code runs on a computer, the method in the first aspect and any possible implementation thereof or the method in the second aspect and any possible implementation thereof is executed.

[0078] In the ninth aspect, a chip is provided, comprising a processor for reading instructions stored in a memory, wherein when the processor executes the instructions, the chip implements the method in the first aspect and any possible implementation thereof or the method in the second aspect and any possible implementation thereof. BRIEF DESCRIPTION OF THE DRAWINGS

[0079] FIG1 is a schematic diagram of an inter-thread data access method provided in an embodiment of the present application.

[0080] FIG2 is a schematic diagram of a data sharing method provided in an embodiment of the present application.

[0081] FIG3 is a schematic diagram of the composition structure of a shared object provided in an embodiment of the present application.

[0082] FIG4 is a schematic diagram of a memory management method provided in an embodiment of the present application.

[0083] FIG5 is a schematic diagram of another data sharing method provided in an embodiment of the present application.

[0084] FIG6 is a schematic diagram of another data sharing method provided in an embodiment of the present application.

[0085] FIG7 is a schematic diagram of a data sharing device provided in an embodiment of the present application.

[0086] FIG8 is a schematic diagram of a memory management device provided in an embodiment of the present application.

[0087] FIG9 is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0088] The technical solution in this application will be described below with reference to the accompanying drawings.

[0089] The terms used in the following embodiments are only for the purpose of describing specific embodiments and are not intended to limit the present application. As used in the specification of this application and the appended claims, the singular expressions "a", "an", "said", "above", "the" and "this" are intended to also include expressions such as "one or more", unless there is a clear contrary indication in the context. It should also be understood that in the following embodiments of the present application, "at least one", "one or more" refer to one, two or more. The term "and / or" is used to describe the association relationship of associated objects, indicating that three relationships can exist; for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and subsequent associated objects are in an "or" relationship.

[0090] In the description of the present application and the claims, the terms "first", "second" or "third" etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.

[0091] References to "one embodiment" or "some embodiments" in this specification mean that one or more embodiments of the present application include a particular feature, structure, or characteristic described in conjunction with that embodiment. Thus, phrases such as "in one embodiment," "in some embodiments," "in some other embodiments," and "in another embodiment" that appear in different places in this specification do not necessarily refer to the same embodiment, but rather mean "one or more but not all embodiments," unless otherwise specifically emphasized. The terms "including," "comprising," "having," and variations thereof all mean "including but not limited to," unless otherwise specifically emphasized.

[0092] Before formally introducing the embodiments provided in this application, some professional terms that may be used in the following content are first explained.

[0093] In JavaScript, an object is a data structure that can include an unordered set of related properties and methods. These properties and methods can be predefined or custom. Predefined objects include String, Number, Boolean, Array, Date, RegExp, Math, Error, Object, Function, and Global (also known as the Window object in browser environments).

[0094] The actor concurrency model is a message-passing programming model. The basic unit of computation is called an actor, and all computations in this model are performed within the actor. Actors are completely isolated from each other and do not share any variables. Interactions between actors are completed through asynchronous messaging. Each actor has its own mailbox to receive messages and processes the next message only after the previous one has been processed.

[0095] The JavaScript language uses the Actor concurrency model. Threads are memory-isolated and cannot access objects in each other's memory. Each thread has its own independent state and event loop. They asynchronously exchange information and modify each other's state through inter-thread communication. Inter-thread communication is accomplished through serialization.

[0096] Memory isolation means that the memory spaces of different processes or programs in a computer system are independent and protected from each other. The purpose of memory isolation is to prevent one program from mistakenly or maliciously accessing, modifying, or interfering with the memory contents of other programs, thereby improving system stability and security.

[0097] The structured clone algorithm is an algorithm for copying complex JavaScript objects. It can construct clones by recursively inputting objects while maintaining a map of previously visited references to avoid infinite traversal loops. The structured clone algorithm is implemented as a deep copy semantics.

[0098] In JavaScript, a deep copy is an operation that creates a new object or array with the same properties and values ​​as the original object or array, but occupies a different location in memory. This means that modifications to the new object or array will not affect the original object or array, and vice versa. The main purpose of a deep copy is to ensure that all properties and nested objects are independently copied when copying complex data structures (such as objects containing nested objects and arrays), rather than just copying references.

[0099] Garbage collection (GC) is a term used in computer programming to describe the process of finding and removing objects that are no longer referenced by other objects. In other words, garbage collection is the process of removing objects that are not being used by any other objects. Garbage collection is a fundamental component of the memory management system used in JavaScript.

[0100] The main thread is the primary execution thread of a JavaScript application and can also be called the JavaScript engine thread. The main thread is responsible for handling user interface updates and event responses, maintaining the responsiveness of the application. It typically handles tasks such as the event loop, document object model (DOM) operations, cascading style sheet (CSS) style calculations, network request callbacks, and timers.

[0101] A worker thread is an independent JavaScript thread that runs independently of the main thread. With its own execution environment and memory space, a worker thread can run code independently of the main thread and perform parallel computations. Worker threads can be used to handle time-consuming operations such as large data computations and file input / output. Worker threads can communicate with the main thread through message passing. For example, a worker thread can send messages using the postMessage() method, and the main thread can receive messages by listening for the message() event.

[0102] FIG1 is a schematic diagram of threads of a JavaScript-based application provided in an embodiment of the present application, wherein thread 1 and thread 2 may both be JavaScript threads, which may be referred to as JS threads for short.

[0103] Thread 1 and Thread 2 are two independent computing units, each with its own memory space. Thread 1 cannot directly access data stored in Thread 2's memory space, nor can Thread 2 directly access data stored in Thread 1's memory space. Here, the data stored in the memory space of Thread 1 or Thread 2 can be understood as objects in JS programming.

[0104] The memory space of a JS thread can include the heap and stack. The heap, also known as heap space, heap memory, or heap memory space, is the memory space in the JS thread used to store complex data structures and object instances. The stack, also known as stack space, stack memory, or stack memory space, is the memory space in the JS thread used to store and manage function call contexts and basic types of variables. When a function is called, a new stack frame is created and pushed onto the stack. This stack frame contains all the function's local variables, parameters, return address, and other information.

[0105] Because a JS thread can only process one task at a time, each JS thread can include a task queue. This task queue can be used to store multiple tasks waiting for the JS thread to process. In other words, multiple tasks waiting for the JS thread to process can be arranged in a certain order to form a task queue. When the tasks in the JS thread's stack are completed, JS will read a new task from its task queue and place it on the stack for execution. This task queue can also be thought of as a message queue. Tasks that other threads need to be processed by the current thread are sent to the current thread in the form of messages and stored in the message queue, waiting for the current thread to process them.

[0106] When multiple JS threads need to concurrently process the same task, or in other words, access the same object, different JS threads can collaboratively process the same data through asynchronous inter-thread communication. For example, in Figure 1, if task f2 in thread 1's stack and task f6 in thread 2 both need to modify object A in thread 1's memory space, a copy of object A, A1, can be transferred to thread 2's task queue, and thread 2 can complete task f6 based on copy A1. Thread 2's modification of copy A1 will not affect object A in thread 1's memory space.

[0107] The larger the amount of data in replica A1, the longer it takes to transfer replica A1 to the task queue of thread 2. In addition, the above-mentioned inter-thread communication method cannot complete the transmission of complex types of objects (such as user-defined types of objects). Therefore, in the scenario where multiple JS threads concurrently process tasks, if objects with large amounts of data or complex types are involved, the efficiency of multiple JS threads in processing the task is slow. For example, if the aforementioned concurrent processing tasks involve user interface updates, the delay in user interface updates is longer, or in other words, the average response time of applications developed based on JS to user operations is longer.

[0108] As shown in Figure 2, in order to improve the processing efficiency of applications developed based on the JS language for objects with large data volumes or complex types, this application provides a data sharing method. By setting a memory space that can be accessed by multiple JS threads, the data required by multiple JS threads can be stored in this memory space, and multiple JS threads can obtain the data they need by accessing this memory space. This solution reduces the frequency of large amounts of data transmitted between threads, thereby improving the processing efficiency of JS threads for objects with large data volumes or complex types to a certain extent.

[0109] The memory access method provided in the embodiments of the present application can be applied to portable electronic devices, wearable devices, vehicle-mounted devices, smart home devices or smart office devices, etc., wherein portable electronic devices may include mobile phones, tablets or laptops, etc., wearable devices may include bracelets, watches, augmented reality (AR) devices or virtual reality (VR) devices, etc., vehicle-mounted devices may include driving recorders or head-up display devices, etc., smart home devices may include televisions or speakers, etc., smart office equipment may include smart large screens, printers or scanners, etc. This application does not limit the specific types of the above-mentioned electronic devices.

[0110] S101: A first thread sends first identification information to a second thread.

[0111] The first thread and the second thread can be JS threads, the first thread or the second thread can be the main thread of JS, or the first thread and the second thread can be worker threads of JS. In other words, in the embodiment of the present application, the first identification information can be sent from the main thread of JS to the worker thread of JS, or from the worker thread of JS to the main thread of JS, or from one worker thread of JS to another worker thread of JS.

[0112] The first identification information can be used to indicate the first object. The first object can be an object stored in a shared memory. The shared memory can also be called a first memory or a first memory space, which refers to a memory space that can be accessed by multiple threads. The first object stored in the shared memory belongs to a shared object (Shared Object), and the first object can also be called a first shared object.

[0113] In some examples, the shared memory may be a heap space, for example, a heap space based on Java technology or C++ technology. In this case, the shared memory may also be referred to as a shared heap or shared heap space. The first thread and the second thread may both obtain the address of the shared memory, thereby enabling the first thread and the second thread to access the shared memory based on the address of the shared memory and read data information on the shared memory.

[0114] In some examples, the shared memory or shared heap space may correspond to an application process, or in other words, one application process may be provided with a shared memory or shared heap space.

[0115] By setting up shared memory that can be accessed by multiple threads, the data and information required by multiple threads in the process of executing tasks can be stored in the shared memory, and the operations performed on this data can also be completed on the shared memory, without the need to copy the data to their respective memory spaces for processing. The implementation of this technical solution is conducive to improving the processing efficiency of concurrent tasks (especially tasks involving large amounts of data or complex objects) between multiple JS threads.

[0116] In some examples, the first identification information may include address information of the first object in the shared memory. In other words, both the first thread and the second thread can access the first object stored in the shared memory by reference.

[0117] The first object stored in the shared memory may be a JavaScript predefined object, such as String, Number, Boolean, or Error, etc. Alternatively, the first object stored in the shared memory may be a user-defined object, such as an object created by a constructor, an object inherited from a prototype chain, or an object created by a factory function.

[0118] In some examples, the first object stored in the shared memory may be of other types, which is not limited in this application.

[0119] In some examples, before the first thread sends the first identification information to the second thread, the second thread may send a request message to the first thread, and the first thread may receive the request message in response. The request message may be used to request the first object stored in the shared memory, or the identification information or address information of the first object stored in the shared memory, i.e., the aforementioned first identification information.

[0120] Exemplarily, the request message may be stored in the task queue of the first thread. When the request message enters the stack of the first thread from the task queue of the first thread, the first thread sends the first identification information to the second thread according to the request message.

[0121] By sending identification information instead of directly transmitting the first object, the size or type of the first object does not affect the time taken to transmit the identification information between threads. Therefore, the implementation of this technical solution is conducive to improving the processing efficiency of the first thread and the second thread for objects containing a large amount of data or objects of complex types.

[0122] In some examples, before sending the first identification information, the first thread may also perform "unchanged" processing on the first object, where "unchanged" processing refers to changing the first object from a modifiable state to an unmodifiable state. In some examples, the unmodifiable state of the first object is unchangeable or irreversible, that is, the first object cannot be modified back to a modifiable state after performing the "unchanged" processing.

[0123] The above "no change" process may also be referred to as "freeze" process or other names, which are not limited in this application. In one possible implementation, the "no change" process may be implemented through the Immutable.convert function, and the first object may be converted into an immutable data structure.

[0124] Before sending the identification information of the first object, the first object is set to an unmodifiable state. In this way, even if the second thread can obtain the first object through the first identification information, it cannot modify the first object itself. Implementation of this technical solution helps avoid data races caused by the first and second threads reading and writing to the first object, and helps ensure that only one thread has the right to perform write operations on the first object at a time, thereby improving the security and reliability of software operation.

[0125] In the process of software development using the data sharing method provided by this application, developers do not need to consider the locking issues introduced by multi-threaded competition for resources, which is conducive to improving software development efficiency.

[0126] S102: The second thread reads the first object according to the first identification information.

[0127] Since the first object is stored in the shared memory, the second thread can obtain the address of the shared memory. Therefore, the second thread can read the first object from the shared memory according to the first identification information.

[0128] In some examples, the first identification information is stored in the task queue of the second thread. When all tasks in the stack of the second thread are completed, the second thread can read a new task from its task queue. When the first identification information enters the stack of the second thread, the second thread can read the first identification information.

[0129] In some examples, the first thread is the main thread of JS, and the second thread is the worker thread of JS.

[0130] Exemplarily, the second thread may be a worker thread responsible for processing a large amount of data or complex calculations. In this case, the first object may include a large amount of data to be processed by the second thread and / or data related to complex calculations.

[0131] Exemplarily, the second thread may be a worker thread responsible for executing asynchronous operations, for example, a worker thread responsible for executing network request tasks or file input / output tasks. In this case, the first object may include the corresponding address of the network request or file input / output.

[0132] Exemplarily, the second thread may be a worker thread responsible for processing real-time data (such as an audio stream or a video stream). In this case, the first object may include the real-time data that the second thread needs to process.

[0133] In some examples, the first thread and the second thread may both be JS worker threads.

[0134] Exemplarily, the first thread and the second thread may both be working threads responsible for processing complex computing tasks, which depend on the same input data or intermediate results. In this case, the first object may include the input data or intermediate results that the aforementioned complex computing tasks depend on.

[0135] For example, the first thread and the second thread can both be worker threads responsible for processing and updating real-time data, with the first thread and the second thread respectively responsible for performing different aspects of the real-time data processing and updating tasks. In this case, the first object can include real-time data required by both the first thread and the second thread. For example, the real-time data can be audio data, video data, or sensor data.

[0136] For example, the first thread and the second thread can both be worker threads responsible for executing game or animation rendering. The first thread and the second thread can each be responsible for different subtasks of the rendering task. For example, the first thread can be responsible for executing the scene management subtask, and the second thread can be responsible for executing the animation calculation subtask. In this case, the first object can include color, depth, and other scene information that needs to be processed during the execution of different subtasks.

[0137] For example, the first thread and the second thread can both be worker threads responsible for executing a database query task, and the first thread and the second thread can each be responsible for executing different subtasks of the database query task. For example, the first thread can be responsible for executing the subtask of loading data from a database, while the second thread can be responsible for executing the subtask of converting data. In this case, the first object can be information such as the aforementioned database connection or file handle.

[0138] S103: The second thread creates a second object based on the first object.

[0139] In some examples, the first object is in an unmodifiable state, and the second thread can read the information of the first object and create a second object based on the first object. The second object can be in a modifiable state. The second thread can process the content of the first object by modifying the second object.

[0140] For example, the second thread can process the first object through the Immutable.set interface to create the second object. The input of this interface may include the address information of the first object, and the input of this interface may also include the attribute A of the first object that the second thread needs to modify and the value of the modified attribute A.

[0141] In other words, the second object created based on the first object may include attribute A and attribute B, wherein the value of attribute A is the value modified by the second thread, and the value of attribute B is the same as that of attribute B of the first object.

[0142] The first object and the second object can be understood as two independent objects, or in other words, the processing operation on the second object will not affect the first object, and the processing operation on the first object will not affect the second object.

[0143] An independent new object is created based on the first object, and data processing operations are performed based on the newly created object. The implementation of this technical solution is conducive to reducing the competition problem caused by multiple threads when processing the same data, and is conducive to improving the stability of application data processing and the reliability of processing results. Software developers do not need to consider the locking problem introduced by multi-threaded competition for resources during the software development process, or in other words, the probability of locking problems introduced by multi-threaded competition for resources during the software development process is small, which is conducive to improving the efficiency of software development.

[0144] In some examples, the second object may also be stored in the aforementioned shared memory space. In other words, the second object may also serve as a shared object, referred to as a second shared object.

[0145] In some examples, the second thread can send or share the identification information of the second object with other threads that need to use the second object. Similar to the processing of the first object described above, before sharing the second object with multiple threads, the second thread will also perform an unchanged processing on the second object to avoid contention issues caused by multiple threads reading and writing the same object.

[0146] Storing the second object in the shared memory space is beneficial for other threads to perform subsequent processing operations based on the second object. The implementation of this technical solution is beneficial to improving the processing efficiency of concurrent tasks between threads.

[0147] Figure 3 shows a schematic diagram of the composition structure of the aforementioned shared object (e.g., the aforementioned first object) provided in an embodiment of the present application. A shared object may include one or more object members, which may be data items or function items. The object members of a data item may be called attributes, and the object members of a function item may be called methods. In other words, a shared object may include one or more attributes, and a shared object may also include one or more methods.

[0148] In some examples, a shared object may include first-category attributes and second-category attributes, where the first-category attributes may be attributes inherent to the shared object, or referred to as inherent attributes, and the second-category attributes may be attributes defined by a user, or referred to as defined attributes.

[0149] Exemplarily, the inherent attributes of the shared object may include a first attribute, and the first attribute may be used to indicate whether the shared object is in a modifiable state.

[0150] Exemplarily, the first attribute may also be referred to as a mutable field.

[0151] Exemplarily, when the value of the first attribute is zero, the first attribute can be used to indicate that the first object is in an unmodifiable state, or in other words, the first object has been processed as "unchanged"; when the value of the first attribute is 1, the first attribute can be used to indicate that the first object is in a modifiable state, or in other words, the first object has not been processed as "unchanged".

[0152] In some scenarios, the first attribute can also be understood as being capable of indicating whether the first object is in a state that can be shared between threads.

[0153] Exemplarily, when the value of the first attribute is zero, the first attribute can be used to indicate that the first object is in an unmodifiable state, and the first attribute can also be used to indicate that the first object can be shared between threads; when the value of the first attribute is 1, the first attribute can be used to indicate that the first object is in a modifiable state, and the first attribute can also be used to indicate that the first object cannot be shared between threads, or in other words, the first object needs to be "unchanged" before it can be shared between threads.

[0154] Exemplarily, the inherent properties of a shared object may include a second property, which may be used to indicate whether the first object is referenced by a thread, or in other words, the second property may be used to indicate whether the first object is referenced by the thread's heap, or in other words, the second property may be used to indicate whether the first object is referenced by an object stored on the thread's heap.

[0155] Exemplarily, the second attribute may also be referred to as an external reference count (ExtRef Count) field.

[0156] Exemplarily, when the value of the second attribute is zero, the second attribute can be used to indicate that the first object is not referenced by a thread; when the value of the second attribute is non-zero, the second attribute can be used to indicate that the first object is referenced by a thread.

[0157] In some examples, the second attribute may also be used to indicate the number of threads that reference the first object.

[0158] Exemplarily, when the value of the second attribute is zero, the second attribute can be used to indicate that the number of threads referencing the first object is zero; when the value of the second attribute is a positive integer N, the second attribute can be used to indicate that the number of threads referencing the first object is N.

[0159] When the second attribute is used to indicate the number of threads referencing the first object, the value of the second attribute can be dynamically changed based on the number of threads referencing the first object. For example, when a new thread references the first object, the value of the second attribute is incremented by one; when a new thread references the first object, the value of the second attribute is decremented by one, until no more threads reference the first object, i.e., the value of the second attribute is zero.

[0160] Exemplarily, the inherent attributes of the shared object may further include a third attribute, which may be used to indicate the thread occupying the first object. Exemplarily, the value of the third attribute may be an identifier of the thread occupying the first object, such as the name of the thread.

[0161] Exemplarily, the third attribute may also be referred to as an owner identification (owner identification or owner ID) field.

[0162] Exemplarily, the number of the aforementioned first thread may be 11. When the first thread occupies the first object, the value of the third attribute of the first object may be the number of the first thread, that is, 11.

[0163] According to the third attribute, the electronic device can determine the thread currently occupying the first object, and thus manage the first object according to the running status of the thread. The implementation of this technical solution is conducive to improving the utilization efficiency of the electronic device for the memory resources occupied by the shared memory space.

[0164] Exemplarily, the custom attributes of the shared object may include a fourth attribute, and the fourth attribute may be set according to user needs, which is not limited in this application.

[0165] In some examples, a shared object may further include a first category of methods and a second category of methods. The first category of methods may be methods inherent to the shared object, or referred to as inherent methods. For example, the inherent methods may include the first method. The second category of methods may be user-defined methods, or referred to as custom methods. For example, the custom methods may include the second method. This application does not limit the inherent methods and custom methods included in a shared object.

[0166] In order to improve the utilization efficiency of the memory resources of the electronic device and reduce the ineffective occupation of the memory resources of the electronic device by the aforementioned shared memory, based on the second attribute of the above-mentioned first object, as shown in Figure 4, the present application also provides a memory management method, which reclaims the memory space occupied by objects that can be accessed by multiple threads, thereby improving the utilization efficiency of memory resources to a certain extent.

[0167] S201 : In response to a change in a reference condition of a first object, adjust a value of a second attribute of the first object.

[0168] The first object may be an object stored in a shared memory, and the shared memory may be a memory space accessible to multiple JS threads (eg, the aforementioned first thread and the second thread).

[0169] The second attribute may be used to indicate the number of threads that reference the first object, or the second attribute may also be understood as indicating the number of memory spaces of the threads that reference the first object.

[0170] In some examples, the value of the second attribute can be a natural number, and the electronic device can adjust the value of the second attribute based on changes in the number of threads referencing the first object. For example, if a new thread references the first object, the value of the second attribute increases by one; if a new thread references the first object, the value of the second attribute decreases by one, until no more threads reference the first object, i.e., the value of the second attribute is zero.

[0171] Still taking the aforementioned first thread and second thread as an example, in some examples, the memory space of the first thread may store information about the object referenced by the first thread in the shared memory space.

[0172] Exemplarily, the memory space of the first thread may store second identification information, where the second identification information is used to indicate an object in the shared memory space referenced by the first thread. For example, if the first thread references the aforementioned first object, the memory space of the first thread may store second identification information indicating the first object. The second identification information may include the address of the first object, or the name of the first object.

[0173] In some examples, during garbage collection of the memory space of the first thread by the electronic device, the information stored in the memory space of the first thread may be cleared. During the process of reclaiming the memory space of the first thread, the second identification information stored in the memory space of the first thread may also be cleared. In response to the clearing of the second identification information, the electronic device may adjust the value of the second attribute of the object in the shared memory space indicated by the second identification information.

[0174] Exemplarily, the second identification information is used to indicate the first object. In response to the second identification information being cleared, the electronic device may reduce the value of the second attribute of the first object by one.

[0175] Exemplarily, the second identification information is used to indicate the first object and the second object. In response to the second identification information being cleared, the electronic device may reduce the value of the second attribute of the first object by one and reduce the value of the second attribute of the second object by one.

[0176] In some examples, the electronic device can create a third thread to collaborate with the existing first thread to process the same task. The task depends on the first object, which can be stored in the aforementioned shared memory space. When the newly created third thread references the first object, it saves the identification information of the first object in the memory space of the second thread to indicate that the third thread is referencing the first object. Accordingly, the electronic device can perform an increment operation on the value of the second attribute of the first object.

[0177] In some examples, the second identification information may include one or more sub-information, which may be used to indicate an object in the shared memory. The second identification information may also include information about the mutual reference relationships between the multiple sub-information, or in other words, the second identification information may also include information about the mutual reference relationships between the objects indicated by the multiple sub-information.

[0178] Exemplarily, the second identification information can be saved in the form of a linked list, the nodes in the linked list can be used to indicate objects in shared memory, and the pointers in the linked list can point from one node to another or more nodes, or in other words, the pointers in the linked list can be used to indicate the reference relationship between an object in shared memory and other objects.

[0179] The second identification information can also be implemented through other data structures, such as a tree, a graph, or a hash table, etc., and this application does not impose any restrictions on this.

[0180] In some examples, in order to effectively manage the memory resources occupied by shared memory, objects stored in shared memory cannot reference objects stored in non-shared memory (for example, called private memory space or second memory space), and objects stored in shared memory can reference objects stored in shared memory.

[0181] Since objects stored in shared memory can be referenced by objects stored in private memory, if objects stored in private memory can also be referenced by objects stored in shared memory, a circular reference problem will occur. In this case, the value of the second attribute of the object stored in shared memory may not be reset to zero, that is, garbage collection of the object stored in shared memory cannot be achieved. Therefore, the implementation of the above technical solution is conducive to achieving effective management of shared memory resources.

[0182] S202: When the value of the second attribute is zero, reclaim the memory space occupied by the first object.

[0183] The electronic device may read the value of the second attribute of the first object, and when the value of the second attribute is zero, perform a garbage collection operation on the memory space occupied by the first object.

[0184] In some examples, the electronic device may also determine whether to reclaim the memory space occupied by the first object based on the usage of memory resources and / or the code execution situation.

[0185] Exemplarily, when the value of the second attribute of the first object is zero, if the memory resources occupied by the application exceed the first threshold, the memory resources occupied by the first object are reclaimed; if the memory resources occupied by the application do not exceed the first threshold, the memory resources occupied by the first object are not reclaimed.

[0186] Exemplarily, when the value of the second attribute of the first object is zero, if the task queue in the main thread is cleared, the memory resources occupied by the first object are reclaimed; if the task queue in the main thread is not cleared, the memory resources occupied by the first object are not reclaimed.

[0187] The following describes the memory access method and memory management method provided by the present application in conjunction with the user interface update scenario. As shown in Figure 5, threads 101 and 102 in the electronic device collaborate to process the user interface update task. The data required by both threads 101 and 102 is stored in a shared memory space SM1, which is a memory space accessible to both threads 101 and 102.

[0188] In some examples, thread 101 may be a main thread or a worker thread.

[0189] For example, when thread 101 is the main thread, thread 101 can be responsible for monitoring and processing user input events, such as mouse clicks, keyboard input, touch events, etc. Thread 101 can also be responsible for modifying the file object model and recalculating the style of interface elements according to changes in the file object model, etc. Thread 101 can also be responsible for initiating network requests, responding to data interface updates, etc.

[0190] Exemplarily, when thread 101 is a working thread, thread 101 may be responsible for time-consuming computing tasks such as complex mathematical operations, big data processing, and image processing.

[0191] For example, thread 102 may be responsible for time-consuming computing tasks such as complex mathematical operations, big data processing, and image processing.

[0192] The memory space SM1 stores objects SO1, SO2, and SO3, which are all data needed during the user interface update process. Thread 101 needs to access objects SO1, SO2, and SO3, and thread 102 needs to access objects SO1 and SO2.

[0193] Referring to Figure 5 , object SO1 includes a field named MutableField with a value of 0, indicating that object SO1 is in an immutable state. Object SO1 also includes a field named extRefCount with a value of 2, indicating that object SO1 is referenced by two threads. Object SO1 also includes a field named owner-ID with a value of 101, indicating that object SO1 is owned by thread 101.

[0194] Similarly, object SO2 includes a field named MutableField with a value of 0, indicating that object SO2 is in an immutable state. Object SO2 also includes a field named extRefCount with a value of 2, indicating that object SO2 is referenced by two threads. Object SO2 may also include a field named owner-ID with a value of 101, indicating that object SO2 is owned by thread 101.

[0195] Continuing with Figure 5 , object SO1 references object SO2. The memory space of thread 101 may store a first linked list, which may include nodes 211, 212, and 213. Node 211 includes a first data field and a first pointer field. The first data field is used to indicate object SO1, and the first pointer field is used to indicate that object SO1 references object SO2. Node 212 includes a second data field and a second pointer field. The second data field is used to indicate object SO2, and the second pointer field may be empty to indicate that object SO2 does not reference other objects. Node 203 includes a third data field and a third pointer field. The third data field is used to indicate object SO3, and the third pointer field may be empty to indicate that object SO3 does not reference other objects.

[0196] The memory space of thread 102 may store a second linked list, which includes nodes 311 and 312. Node 311 includes a fourth data field and a fourth pointer field. The fourth data field is used to indicate object SO1, and the fourth pointer field is used to indicate that object SO1 references object SO2. Node 312 includes a fifth data field and a fifth pointer field. The fifth data field is used to indicate object SO2. The fifth pointer field may be empty, indicating that object SO2 does not reference any other objects.

[0197] The data and objects needed in the user interface update task can be saved in the shared memory space SM1. During the concurrent processing of threads 101 and 102, there is no need to transmit data through inter-thread communication, which is beneficial to improving the processing efficiency of the user interface update task and shortening the average response time of the electronic device to user operations.

[0198] In some examples, the electronic device may perform a garbage collection operation on thread 101 and thread 102. During this operation, information related to objects no longer referenced by thread 101 or thread 102 may be cleared. For example, if thread 101 no longer references object SO3, the electronic device may clear the information of node 213 in the first linked list. If thread 102 no longer references objects SO1 and SO2, the electronic device may clear the information of nodes 311 and 312 in the second linked list.

[0199] In response to thread 101 no longer referencing object SO3, the electronic device may decrement the value of the field named extRefCount in object SO3 by one. In response to thread 102 no longer referencing objects SO1 and SO2, the electronic device may decrement the value of the field named extRefCount in both objects SO1 and SO2 by one.

[0200] In some examples, in response to the value of the field named extRefCount in the object SO1, object SO2, or object SO3 being zero, the electronic device may reclaim the memory resources occupied by the object SO1, object SO2, or object SO3.

[0201] The electronic device can determine whether the memory resources occupied by the heap shared memory need to be reclaimed based on the properties of the shared object. The implementation of this technical solution is conducive to improving the utilization efficiency of the memory resources of the electronic device.

[0202] Taking thread 101 as the main thread and thread 102 as the worker thread as an example, the following code example shows how to pass a shared object a from thread 101 to thread 102 through a task pool to read and update the result and then return the result to thread 101.

[0203] Figure 6 shows a flowchart of the above code. Thread 101 creates shared object a and performs immutable processing (Immutable.convert()) on shared object a before transferring it to thread 102. This prevents thread 102 from modifying shared object a when acquiring it, thereby avoiding data contention issues caused by multi-threaded reading and writing.

[0204] After performing unchanged processing on the shared object a, the thread 101 may send information of the shared object a to the thread 102 . Here, the information of the shared object a may be address information of the shared object a in the shared memory.

[0205] After receiving the information about shared object a from thread 101, thread 102 can retrieve shared object a stored in shared memory based on this information. In some examples, thread 102 can use the Immutable.set interface to process shared object a, which generates a new shared object b. Shared object b includes the properties of shared object a and the properties modified by thread 102.

[0206] In some examples, thread 102 may send the processed information of shared object b to thread 101, so that thread 101 can perform subsequent processing operations using shared object b. Similarly, the information of shared object b here may be address information of shared object b in the shared memory.

[0207] The method embodiment of the present application is described in detail above in conjunction with Figures 1 to 6. The device embodiment of the present application is described below in conjunction with Figures 7 to 9. It should be understood that the description of the method embodiment corresponds to the description of the device embodiment. Therefore, for parts not described in detail, reference can be made to the above method embodiment.

[0208] Figure 7 shows a data sharing device 700 provided in an embodiment of the present application. This device 700 may have the functions of the electronic device in the above method embodiment and may be used to execute the steps performed by the functions of the electronic device in the above method embodiment. This function may be implemented in hardware, or in software or hardware executing corresponding software implementations. The hardware or software may include one or more modules corresponding to the above functions.

[0209] In a possible implementation, the data sharing apparatus 700 may include an acquisition module 710 and a processing module 720 , and the acquisition module 710 and the processing module 720 are coupled to each other.

[0210] The acquisition module 710 may be used to support the electronic device in acquiring user input, such as the aforementioned operation of the first thread or the second thread acquiring an object in the shared memory space.

[0211] The processing module 720 is used to support the electronic device in executing the processing actions in the above method embodiments, such as performing unchanged processing operations on shared objects.

[0212] Optionally, the device for storing data sharing 700 may further include a storage unit 730 for storing program codes and data of the device for storing data sharing 700 .

[0213] Figure 8 shows a memory management device 800 provided in an embodiment of the present application. This device 800 may have the functions of the electronic device in the above method embodiment and may be used to execute the steps performed by the functions of the electronic device in the above method embodiment. This function may be implemented in hardware, or in software or hardware executing corresponding software implementations. The hardware or software may include one or more modules corresponding to the above functions.

[0214] In a possible implementation, the memory management apparatus 800 may include an acquisition module 810 and a processing module 820 , and the acquisition module 810 and the processing module 820 are coupled to each other.

[0215] The acquisition module 810 may be used to support the electronic device in acquiring user input, such as acquiring reference information of a shared object in the memory space of the first thread.

[0216] The processing module 820 is used to support the electronic device in executing the processing actions in the above method embodiment, such as adjusting the value of the extRefCount field of the shared object.

[0217] Optionally, the memory management apparatus 800 may further include a storage unit 830 for storing program codes and data of the memory management apparatus 800 .

[0218] FIG9 illustrates an electronic device 900 provided in an embodiment of the present application. As shown in the figure, the electronic device 900 includes at least one processor 910 and a transceiver 920. The processor 910 is coupled to a memory and is configured to execute instructions stored in the memory to control the transceiver 920 to send and / or receive signals.

[0219] Optionally, the electronic device 900 further includes a memory 930 for storing instructions.

[0220] In some embodiments, the processor 910 and memory 930 may be combined into a processing device, and the processor 910 is configured to execute program codes stored in the memory 930 to implement the above functions. In specific implementations, the memory 930 may also be integrated into the processor 910 or independent of the processor 910.

[0221] In some embodiments, the transceiver 920 may include a receiver (or receiver) and a transmitter (or transmitter).

[0222] The transceiver 920 may further include an antenna, and the number of antennas may be one or more. The transceiver 920 may be a communication interface or an interface circuit.

[0223] When the electronic device 900 is a chip, the chip includes a transceiver module and a processing module. The transceiver module may be an input / output circuit or a communication interface; and the processing module may be a processor, microprocessor, or integrated circuit integrated on the chip.

[0224] This embodiment also provides a computer-readable storage medium, which stores computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the above-mentioned related method steps to implement the data sharing method and / or memory management method in the above-mentioned embodiment.

[0225] This embodiment further provides a computer program product. When the computer program product is run on a computer, it enables the computer to execute the above-mentioned related steps to implement the data sharing method and / or memory management method in the above-mentioned embodiment.

[0226] In addition, embodiments of the present application further provide a device, which may be a chip, component, or module, and may include a connected processor and memory. The memory is used to store computer-executable instructions. When the device is running, the processor may execute the computer-executable instructions stored in the memory to cause the chip to perform the data sharing method and / or memory management method described in the above-mentioned method embodiments.

[0227] Among them, the electronic device, computer-readable storage medium, computer program product or chip provided in this embodiment are all used to execute the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods provided above, and will not be repeated here.

[0228] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0229] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0230] In the several embodiments provided in this application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0231] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.

[0232] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0233] If the functions are implemented in the form of software functional units 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 the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0234] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.

Claims

1. A method for data sharing, applied to an electronic device, characterized in that, including: The first thread saves the first object to the target memory space; The second thread reads the first object from the target memory space; wherein, both the first thread and the second thread are threads of JavaScript.

2. The method according to claim 1, characterized in that, Before the second thread reads the first object from the target memory space, the method further includes: The first thread sends identification information to the second thread, and the identification information is used to indicate the first object.

3. The method according to claim 1 or 2, characterized in that, The method further includes: The second thread creates a second object according to the first object, and the second object includes attributes with the same key names as the first object, and / or, the second object includes the same methods as the first object.

4. The method according to claim 3, characterized in that, The second object is saved in the target memory space.

5. The method according to claim 2, characterized in that Before the first thread sends the identification information to the second thread, the method further includes: The first thread performs target processing on the first object, and the first object after the target processing is not modifiable.

6. The method according to any one of claims 1 to 5, characterized in that The first object includes a first field, and the first field is used to indicate that the first object is referenced.

7. The method according to claim 6, wherein The first field is further used to indicate the number of different threads that reference the first object.

8. The method according to any one of claims 1 to 7, wherein The memory space of the first thread stores first information, and the first information is used to indicate a first reference object, and the first reference object is an object saved in the target memory space and referenced by the first thread, and the first reference object includes the first object, The memory space of the second thread stores second information, and the second information is used to indicate a second reference object, and the second reference object is an object saved in the target memory space and referenced by the second thread, and the second reference object includes the first object.

9. The method according to any one of claims 1 to 8, characterized in that, The first object includes a second field, and the second field is used to indicate that the first object is not modifiable.

10. A method for memory management, applied to an electronic device, characterized in that, including: Save a first object in a target memory space, where the target memory space is a memory space that can be accessed by multiple JavaScript threads; In response to the first object no longer being referenced, recycle the memory resources occupied by the first object.

11. The method according to claim 10, characterized in that, The first object includes a first field, and the first field is used to indicate the number of different threads that reference the first object. The step of recycling the memory resources occupied by the first object in response to the first object no longer being referenced includes: In response to the value of the first field being zero, recycle the memory resources occupied by the first object.

12. The method according to claim 11, wherein The value of the first field is determined according to the reference information of the target thread, and the reference information is used to indicate the first object, and the multiple JavaScript threads include the target thread.

13. The method according to claim 12, wherein The target memory space further includes a second object, the first object references the second object, and the reference information includes first sub-information, second sub-information, and third sub-information. The first sub-information is used to indicate the first object, the second sub-information is used to indicate the second object, and the third sub-information is used to indicate that the first object references the second object.

14. An electronic device, characterized in that, It includes a processor and a memory, where the memory is used to store program instructions, and the processor is used to call the program instructions to execute the method described in any one of claims 1 to 9 or claims 10 to 13.

15. A device for data sharing, characterized in that, It includes a module for implementing the method described in any one of claims 1 to 9.

16. An apparatus for memory management, characterized in that, It includes a module for implementing the method described in any one of claims 10 to 13.

17. A computer-readable storage medium, characterized in that, A computer program is stored thereon, and when the computer program is executed by a computer, it enables the implementation of the method described in any one of claims 1 to 9 or claims 10 to 13.

18. A chip, characterized in that, It includes a processor and a memory, where the processor is used to read the instructions stored in the memory, and when the processor executes the instructions, it enables the chip to implement the method described in any one of claims 1 to 9 or claims 10 to 13.