Statistical method for memory usage and electronic equipment
By receiving two requests in an electronic device to record the object size created by the thread, the memory usage statistics in the thread dimension are realized, and the problem that the existing technology cannot accurately locate memory usage exceptions is solved, and the analysis and resolution efficiency of memory usage problems is improved.
Patent Information
- Application Number
- CN202510421915.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-07
- Publication Date
- 2025-05-06
- Estimated Expiration
- 2045-04-07
AI Technical Summary
The existing technology cannot accurately count and monitor the memory usage of electronic devices in the thread dimension, resulting in the inability to accurately locate the causes of abnormalities when there is too much memory usage or abnormal growth.
By receiving two requests, the object size created by the thread during this time period is recorded, thereby realizing the statistics of memory usage from the thread dimension. The specific method includes maintaining the first memory usage information in the electronic device, recording the object size created by the thread until the recording is stopped when the request is received again.
It realizes that when memory occupancy exceptions occur, the cause of the exception can be accurately located based on the statistical data of the thread dimension, and thus effectively analyze and solve the memory occupancy problem.
Smart Images

Figure CN119938453A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of terminal technology, and in particular to a method for counting memory occupancy and an electronic device. Background Art
[0002] Electronic devices can create and run processes. During the operation of electronic devices, some complex processes may frequently call memory, resulting in the problem of occupying a large amount of memory, or the memory occupied by some processes may increase abnormally. In order to ensure normal operation, electronic devices need to monitor the memory usage.
[0003] However, electronic devices can currently only count the memory usage of electronic devices in the process dimension, and cannot be broken down into specific threads. As a result, when problems such as excessive memory usage and abnormal memory growth occur, it is impossible to accurately locate the cause of the abnormality. Summary of the invention
[0004] The embodiments of the present application provide a memory occupancy statistics method and electronic device, which can count the memory occupancy from a thread dimension, so that when an exception occurs, the cause of the exception can be accurately located based on the data counted from the thread dimension.
[0005] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions: In a first aspect, the present application provides a method for counting memory usage, which is applied to an electronic device. Specifically, after receiving a first request to obtain the memory usage of a first thread, the electronic device, in response to the first thread creating an object, records the size of the object in the first memory usage information until the first request is received again, and stops recording the size of the object created by the first thread in the first memory usage information. The sizes of all objects recorded in the first memory usage information are used to indicate the memory usage of the first thread.
[0006] In the above scheme, the electronic device records the size of the object created by the thread between the two first requests by receiving two first requests, and realizes the statistics of memory occupancy from the thread dimension, so that when an exception occurs, the cause of the exception can be accurately located based on the data counted from the thread dimension, and effective analysis of the exception can be achieved. For example, after locating the thread that causes the memory exception problem based on the data counted from the thread dimension, the specific business code corresponding to the thread can be determined according to the business relationship to accurately locate the cause of the exception. Afterwards, the business code can be adjusted to accurately and efficiently solve the memory exception problem.
[0007] In a possible implementation of the first aspect, after receiving a first request for obtaining memory usage of a first thread, the electronic device sets a state value of the first thread to a first value. After receiving the first request again, the electronic device sets the state value of the first thread to a second value.
[0008] In the above scheme, since the electronic device needs to consume a lot of computing power in the process of recording the first memory usage information, the electronic device can use the first value and the second value to decide whether to start or stop recording the size of the object created by the thread in the first memory usage information, that is, it only starts recording when the first memory usage information is needed, thereby reducing the waste of computing power.
[0009] In another possible implementation of the first aspect, the first request is specifically used to request to obtain the memory usage of all objects created by the first thread, where all objects may include large objects and ordinary objects.
[0010] In the above solution, the electronic device can record the memory occupancy of all objects created by the thread in the first memory usage information through the first request, so as to accurately count the memory occupancy of the thread.
[0011] In another possible implementation of the first aspect, the electronic device includes an application layer and a virtual machine layer, and the first request is sent from the application layer to the virtual machine layer. The method also includes: after the virtual machine layer receives the first request to obtain the memory occupancy of the first thread, the virtual machine layer sets the information recorded in the first memory usage information to the initial value, and returns the initial value to the application layer. After the virtual machine layer receives the first request again, the virtual machine layer reads the size of all objects recorded in the first memory usage information to obtain a target value, and returns the target value to the application layer. The application layer determines the memory occupancy of the first thread based on the initial value and the target value. The target value can be the memory occupancy of each object created by the thread between the two first requests, or it can be the sum of the memory occupancy of all objects created by the thread between the two first requests.
[0012] In the above scheme, the virtual machine layer records the size of the object created by the thread between the two first requests by receiving the two first requests, and realizes the statistics of the memory occupancy from the thread dimension, so that when an exception occurs, the application layer can determine the memory occupancy of the thread based on the initial value and target value returned by the virtual machine layer, accurately locate the cause of the exception, and realize effective analysis of the exception.
[0013] In another possible implementation of the first aspect, after receiving a second request for obtaining memory occupancy of a first thread, the electronic device obtains a first occupancy value currently recorded in the second memory usage information, and a second occupancy value, the second occupancy value being the memory occupancy value of a common object stored in a first memory buffer for storing common objects created by the first thread when the second request is received. After receiving the second request again, the electronic device obtains a third occupancy value currently recorded in the second memory usage information, and a fourth occupancy value, the fourth occupancy value being the memory occupancy value of a common object stored in a first memory buffer for storing common objects created by the first thread when the second request is received again. The electronic device determines the memory occupancy of the first thread according to the first occupancy value, the second occupancy value, the third occupancy value, and the fourth occupancy value. The second memory usage information is used to record the memory occupancy value of an object stored in the first memory buffer for storing common objects created by the first thread that has been released and is historically used to store common objects created by the first thread.
[0014] In the above scheme, since most of the objects created by the thread are common objects, the electronic device determines the memory occupancy of the common objects created by the thread when the electronic device first receives the second request through the first occupancy value and the second occupancy value, and determines the memory occupancy of the common objects created by the thread when the electronic device receives the second request again through the third occupancy value and the fourth occupancy value. The electronic device can use this to determine the memory occupied by the common objects created by the thread between the two second requests, and realize the statistics of the memory occupancy from the thread dimension, so that when an abnormality occurs, it can accurately locate the cause of the abnormality based on the data counted from the thread dimension, and realize effective analysis of the abnormality.
[0015] In another possible implementation of the first aspect, the electronic device may record the second memory usage information under the following circumstances. Scenario 1: In response to the first thread creating a common object, the electronic device releases the first memory buffer currently used to store the common objects created by the first thread when there is insufficient remaining storage space in the first memory buffer currently used to store the common objects created by the first thread, and records the memory occupancy value of the stored objects in the second memory usage information. Scenario 2: In response to the first memory buffer currently used to store the common objects created by the first thread being recycled, the electronic device releases the first memory buffer currently used to store the common objects created by the first thread, and records the memory occupancy value of the stored objects in the second memory usage information.
[0016] In the above scheme, the electronic device can record the released first memory buffer in the second memory usage information when the first memory buffer is released, which can achieve lightweight recording of the second memory usage information without consuming more computing power.
[0017] In another possible implementation manner of the first aspect, the second request is specifically used to request to obtain the memory occupancy of the common objects created by the first thread.
[0018] In the above scheme, the computing power required for the electronic device to count the memory usage of common objects is relatively small, and will not cause much load on the central processing unit. Through the second request, the memory usage of common objects created by the thread can be quickly determined without affecting the operating efficiency of the electronic device.
[0019] In a second aspect, the present application provides an electronic device, the electronic device comprising: a memory and one or more processors, the memory being coupled to the processor. The memory stores computer program code, the computer program code comprising computer instructions, and when the computer instructions are executed by the processor, the electronic device executes the method in the first aspect and any possible implementation thereof.
[0020] In a third aspect, an embodiment of the present application provides a computer-readable storage medium, which includes computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the method of the first aspect and any possible implementation thereof.
[0021] In a fourth aspect, an embodiment of the present application provides a computer program product, which, when executed on a computer, enables the computer to execute the method of the first aspect and any possible implementation thereof. The computer may be an electronic device in the second aspect and any possible implementation thereof.
[0022] It can be understood that the beneficial effects that can be achieved by the electronic device of the second aspect, the computer-readable storage medium of the third aspect, and the computer program product of the fourth aspect provided above can be referred to the beneficial effects in the method of the first aspect and any possible implementation thereof, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 A schematic diagram of the structure of a heap memory provided in an embodiment of the present application; Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application; Figure 3 A schematic diagram of a software system architecture of an electronic device provided in an embodiment of the present application; Figure 4 A flowchart of a method for calculating memory usage statistics provided in an embodiment of the present application; Figure 5 A flowchart of another memory usage statistics method provided in an embodiment of the present application; Figure 6A schematic diagram of a fast path storage process provided by an embodiment of the present application; Figure 7 A schematic diagram of a slow path storage process provided by an embodiment of the present application; Figure 8 A schematic diagram of another slow path storage process provided by an embodiment of the present application; Fig. 9 A schematic diagram of a process of releasing a first memory buffer provided in an embodiment of the present application; Fig.10 A schematic diagram of a garbage collection process provided in an embodiment of the present application; Fig.11 A schematic diagram of interaction between modules provided in an embodiment of the present application. DETAILED DESCRIPTION
[0024] The following will describe the technical solutions in the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this application.
[0025] It should be noted that the following terms "first", "second", etc. are only used for descriptive purposes and should not be understood as indicating or implying relative importance or implicitly indicating the number of indicated technical features. Thus, features defined as "first", "second", etc. may explicitly or implicitly include one or more of the features. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units that are inherent to these processes, methods, products or devices.
[0026] References to "one embodiment" or "some embodiments" etc. described in this specification mean that a particular feature, structure or characteristic described in conjunction with the embodiment is included in one or more embodiments of the present application. Thus, the phrases "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. appearing in different places in this specification do not necessarily all refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways.
[0027] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
[0028] For ease of understanding, relevant terms are explained before introducing the method of the present application.
[0029] A process is an instance of an executed program in the operating system of an electronic device and is the basic unit for resource allocation and scheduling of the operating system. For example, after receiving an operation from a user to open application A, the operating system can create a process for application A. The process includes program code, system resources, other data or files, etc.
[0030] A thread is an execution unit in a process and is the basic unit for scheduling and dispatching by the central processing unit (CPU). A process can contain multiple threads, which share the resources of the process, such as memory resources, file descriptors, etc. The different threads included in a process are responsible for handling different business of the application. For example, the process created for application A includes three threads, one thread is responsible for rendering the page, another thread is responsible for processing user input operations, and another thread is responsible for communicating with the server to load the content of the page.
[0031] Heap memory is used to provide storage space for processes. As described above, multiple threads included in a process share the memory resources of the process. An electronic device can divide multiple memory areas in the heap memory for the threads included in the process to store. According to the size of the memory area, the divided memory area can be divided into: a common object storage area and other object storage area. The common object storage area is used to store common objects, and the other object storage area is used to store large objects, etc. The common object storage area may include: a thread local allocation buffer (Thread Local Allocation Buffer, TLAB) and a bump pointer buffer (or bump pointer space). Common objects can be stored in a thread local allocation buffer or a bump pointer buffer, wherein, when the electronic device uses a cross-correlation-based algorithm (Cross-Correlation, CC algorithm), TLAB will be used to store common objects. When the electronic device uses a concurrent mark compaction algorithm (ConcurrentMark Compact, CMC algorithm), the electronic device will use a bump pointer buffer to store common objects.
[0032] In the embodiments of the present application, the electronic device using TLAB to store common objects is mainly taken as an example, wherein one thread may correspond to one TLAB.
[0033] Other object storage areas may include a large object buffer and a non-moving buffer (or non-moving space), wherein each process corresponds to a large object buffer, and multiple threads included in the process may share the large object buffer.
[0034] Among them, the objects created by threads can be divided into large objects (or large object objects) and ordinary objects (or region space objects). Among them, large objects require a larger storage space, and ordinary objects require a smaller storage space. Large objects are generally stored in large object buffers.
[0035] In one example, when the electronic device detects that the object to be stored is a common object, the electronic device can store the object in the TLAB. Specifically, when the remaining storage space of the TLAB currently corresponding to the thread is sufficient to store the object to be stored, the electronic device can directly store the object in the TLAB. When the remaining storage space of the TLAB currently corresponding to the thread is insufficient to store the object to be stored, the electronic device will release the TLAB currently corresponding to the thread, and the electronic device will apply for a TLAB with a larger storage space in the heap memory for storing common objects.
[0036] In a second example, when the electronic device detects that the object to be stored is a large object, the electronic device may store the object in a large object buffer.
[0037] Figure 1 A schematic diagram of a heap memory structure provided in an embodiment of the present application. Figure 1 As shown, a process includes multiple threads, such as thread 1, thread 2 and thread 3, and one process corresponds to one heap memory. The heap memory includes a common object storage area, and the common object storage area includes multiple thread-local allocation buffers, such as thread-local allocation buffer 1, thread-local allocation buffer 2 and thread-local allocation buffer 3. The heap memory also includes a large object buffer. Each process corresponds to a large object buffer, and the multiple threads included in the process share the large object buffer. Specifically, one thread corresponds to a thread-local allocation buffer. For example, thread 1 corresponds to thread-local allocation buffer 1 and large object buffer, thread 2 corresponds to thread-local allocation buffer 2 and large object buffer, and thread 3 corresponds to thread-local allocation buffer 3 and large object buffer. These memory areas are used to store objects created by each thread respectively. The specific storage method and rules are described in the previous description, and will not be repeated here.
[0038] In an embodiment of the present application, TLAB may be referred to as a first memory buffer, and a large object buffer may be referred to as a second memory buffer.
[0039] The method of this application is described in detail below.
[0040] During the operation of electronic devices, processes need to frequently access memory. If the memory is occupied too much, the process may not be able to access the memory in time, causing the electronic device to freeze and degrade performance. Therefore, during the operation of electronic devices, it is necessary to monitor the memory usage and optimize the memory if it is detected that the memory usage is high.
[0041] However, in related technologies, electronic devices can only count memory usage in the process dimension, but cannot count in the thread dimension. For example, common memory statistics methods, such as the dumpsys function, eminfo function, and showmap function, can only count memory usage in the process dimension, but cannot be further broken down into the thread dimension. In other words, it is impossible to specifically distinguish the memory usage of each thread.
[0042] For example, when abnormal memory growth occurs, since the memory change can only be viewed in the process dimension, it is impossible to determine which specific thread caused the abnormal memory growth. As a result, R&D personnel are unable to locate the specific thread and determine the business code that caused the abnormal memory growth.
[0043] For another example, taking a complex process as an example, assuming that the complex process is a system service process (system_server process). The system service process of an electronic device can provide a variety of system services to the application of the electronic device, such as the service for managing the life cycle of the application (ActivityManagerService, AMS), the service for managing application updates (PackageManagerService, PMS), the service for managing window views (WindowManagerService, WMS), and the service for managing network status (NetWorkStatServices). The electronic device provides the corresponding system services to the application by executing the threads corresponding to these system services.
[0044] During the running of an application of an electronic device, the application may frequently call the system service of the system service process through an interface (such as a binder interface), which will cause the system service process to frequently access the memory.
[0045] In order to prevent the system service process from occupying too much memory, the electronic device needs to monitor and optimize the memory usage. However, in the related art, only the memory usage of the system service process can be determined, but the memory usage of each thread in the system service process cannot be determined.
[0046] As a result, when problems such as excessive memory usage and abnormal memory growth occur, it is impossible to accurately locate or effectively analyze the specific cause of the anomaly.
[0047] To this end, the present application provides an information acquisition method that can count the memory occupancy from the thread dimension, so that when an exception occurs, the cause of the exception can be accurately located based on the data counted from the thread dimension, and effective analysis of the exception can be achieved. For example, after locating the thread that causes the memory exception problem based on the data counted from the thread dimension, the specific business code corresponding to the thread can be determined according to the business relationship to accurately locate the cause of the exception. Afterwards, the business code can be adjusted to accurately and efficiently solve the memory exception problem.
[0048] Specifically, the electronic device may obtain the memory usage corresponding to the first thread at the first moment. After a preset time, the electronic device may again obtain the memory usage corresponding to the first thread at the second moment. The electronic device then calculates the difference between the memory usage at the first moment and the memory usage at the second moment to obtain the memory usage of the first thread within the preset time.
[0049] Among them, in the embodiment of the present application, the memory occupancy amount obtained by the electronic device may be the memory occupancy amount of at least one of the first memory buffer, the second memory buffer, the collision pointer buffer, and the non-movement buffer. In the embodiment of the present application, the memory occupancy amount obtained by the electronic device is the memory occupancy amount of the first memory buffer and / or the second memory buffer as an example for introduction.
[0050] It can be understood that through the above method, the electronic device obtains the memory occupancy of the first thread at the first moment and the memory occupancy of the first thread at the second moment to determine the memory occupancy of the memory corresponding to the first thread, thereby realizing the monitoring of the memory occupancy in the thread dimension.
[0051] Exemplarily, the electronic device may be a mobile phone, a tablet computer, a smart remote controller, a wearable device (such as a smart bracelet, a smart watch or smart glasses), a PDA, an augmented reality (AR) / virtual reality (VR) device. Alternatively, the electronic device may also be a portable multimedia player (PMP), a media player or other types of electronic devices. The embodiments of the present application do not impose any restrictions on the specific types of electronic devices.
[0052] Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0053] The electronic device 200 may include a processor 210, an external memory interface 220, an internal memory 221, a universal serial bus (USB) interface 230, a charging management module 240, a power management module 241, a battery 242, an antenna 1, an antenna 2, a mobile communication module 250, a wireless communication module 260, an audio module 270, a sensor module 280, a camera 1^N 293, a display screen 1^N 294, etc.
[0054] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 200. In other embodiments of the present application, the electronic device 200 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0055] The processor 210 may include one or more processing units, for example, the processor 210 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.
[0056] In the embodiment of the present application, the processor 210 is used to execute each function or step in the method embodiment of the present application.
[0057] The controller may be the nerve center and command center of the electronic device 200. The controller may generate an operation control signal according to the instruction operation code and the timing signal to complete the control of fetching and executing instructions.
[0058] The processor 210 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 210 is a cache memory. The memory may store instructions or data that the processor 210 has just used or cyclically used. If the processor 210 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 210, and thus improves the efficiency of the system.
[0059] In some embodiments, the processor 210 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0060] The I2C interface is a bidirectional synchronous serial bus, including a serial data line (SDA) and a serial clock line (SCL). In some embodiments, the processor 210 may include multiple groups of I2C buses. The processor 210 may be coupled to the touch sensor 280K, the charger, the flash, the camera 1^N 293, etc. through different I2C bus interfaces. For example: the processor 210 may be coupled to the touch sensor 280K through the I2C interface, so that the processor 210 communicates with the touch sensor 280K through the I2C bus interface to realize the touch function of the electronic device 200.
[0061] The wireless communication function of the electronic device 200 can be implemented through the antenna 1, the antenna 2, the mobile communication module 250, the wireless communication module 260, the modem processor and the baseband processor.
[0062] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in the electronic device 200 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of the antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.
[0063] The display screen 1^N 294 is used to display images, videos, etc. The display screen 1^N 294 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light emitting diode (QLED), etc. In some embodiments, the electronic device 200 may include 1 or N display screens 294, where N is a positive integer greater than 1.
[0064] Video codecs are used to compress or decompress digital videos. The electronic device 200 may support one or more video codecs. Thus, the electronic device 200 may play or record videos in a variety of coding formats, such as moving picture experts group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.
[0065] NPU is a neural network (NN) computing processor. By drawing on the structure of biological neural networks, such as the transmission mode between neurons in the human brain, it can quickly process input information and can also continuously self-learn. Through NPU, applications such as intelligent cognition of the electronic device 200 can be realized, such as image recognition, face recognition, voice recognition, text understanding, etc.
[0066] The external memory interface 220 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 200. The external memory card communicates with the processor 210 through the external memory interface 220 to implement a data storage function, such as storing music, video and other files in the external memory card.
[0067] The internal memory 221 can be used to store computer executable program codes, and the executable program codes include instructions. The processor 210 executes various functional applications and data processing of the electronic device 200 by running the instructions stored in the internal memory 221. The internal memory 221 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc. The data storage area may store data created during the use of the electronic device 200 (such as audio data, a phone book, etc.), etc. In addition, the internal memory 221 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0068] In an embodiment of the present application, the internal memory 221 includes memory resources allocated to each process, such as the internal memory 221 includes a heap memory, which is used to provide corresponding storage space for the process. The heap memory can be used to store objects created by each thread included in the process. The heap memory can also be used to store the first memory usage information and the second memory usage information corresponding to each thread. The first memory usage information and the second memory usage information corresponding to the thread are used to determine the memory usage of the thread. For a detailed introduction to the first memory usage information and the second memory usage information, please refer to the corresponding content below, which will not be repeated here.
[0069] Figure 3 A schematic diagram of a software system architecture of an electronic device provided in an embodiment of the present application.
[0070] The software system of the electronic device may adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. The embodiment of the present invention takes the Android system of the layered architecture as an example to exemplify the software structure of the electronic device.
[0071] The layered architecture divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into three layers, from top to bottom: application layer, application framework layer and virtual machine layer.
[0072] The application layer may include a series of application packages.
[0073] like Figure 3 As shown, the application layer may include a thread memory monitoring module. The application layer may also include: system applications, third-party applications, etc. It should be understood that system applications may include: camera, gallery, calendar, call, map, navigation, Bluetooth, music, video, short message and other application programs, which are not shown one by one in the figure.
[0074] The thread memory monitoring module is used to send requests to the virtual machine layer to monitor the memory usage of the thread. For example, according to the memory usage information at different times returned by the virtual machine layer, the thread memory usage is determined to realize the monitoring of the memory usage in the thread dimension.
[0075] System applications or third-party applications in the application layer can call any manager, any provider, or any system in the application framework layer to implement the functions of the application.
[0076] The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer includes some predefined functions.
[0077] like Figure 3 As shown, the application framework layer may include: window manager, content provider, view system, phone manager, resource manager, notification manager, etc., which are not shown one by one in the figure. These modules are system modules, which are used to provide corresponding functions or services to the applications in the application layer.
[0078] The window manager is used to manage window programs. The content provider is used to store and obtain data and make the data accessible to applications. The view system can be used to build applications. The phone manager is used to provide communication functions for electronic devices. The resource manager provides various resources for applications. The notification manager enables applications to display notification information in the status bar, which can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction.
[0079] The virtual machine layer includes: a memory usage information acquisition module, a thread running module, a memory storage module, a garbage collection module, a thread list management module and a thread management module.
[0080] The memory usage information acquisition module is used to receive a request from the thread memory monitoring module and acquire the memory usage information of the thread at the current moment, such as the first memory usage information described below, and the second memory usage information described below.
[0081] The thread running module is used to run the thread. The thread running module is also used to store the object in the corresponding memory buffer through the memory storage module when the thread creates a new object. The thread running module can also be used to record the memory usage in the process of storing the object, such as the actual memory usage of the released TLAB, and the memory usage of ordinary objects or large objects. This information can be recorded in the above-mentioned memory usage information.
[0082] The memory storage module is used to store the objects created by the thread in the corresponding buffer.
[0083] The garbage collection module is used to automatically recycle objects in the heap memory whose usage frequency is less than or equal to a preset frequency threshold. The thread running module is also used to record the actual memory usage of the TLAB released during the garbage collection process.
[0084] The thread list management module is used to manage the threads in the thread list to support the garbage collection module to perform recycling operations.
[0085] The thread management module is used to execute the registered flip function of the thread and resume the suspended thread to support the garbage collection module to perform the recycling operation.
[0086] The information acquisition method of the present application is described in detail below in conjunction with the accompanying drawings.
[0087] In an embodiment of the present application, the electronic device may maintain first memory usage information and second memory usage information for each created thread. In some other embodiments, the electronic device may also maintain first memory usage information and second memory usage information for a specific thread. The specific thread may be pre-set. For example, the developer may combine the test data of the electronic device to set the thread that is prone to memory anomalies as the thread that needs to maintain memory information.
[0088] The first memory usage information and the second memory usage information maintained for different threads are used to monitor the memory occupancy of the thread.
[0089] It is understandable that a thread may constantly occupy or release memory during its operation. Therefore, the first memory usage information and the second memory usage information maintained at different times for the same thread are generally different. The electronic device can obtain the first memory usage information and / or the second memory usage information maintained at different times during the operation of any of the created threads. Based on the obtained memory usage information, the memory usage of the thread is determined.
[0090] In this embodiment, the second memory usage information may be represented by RegionAllocateBytes, and the first memory usage information may be represented by AllAllocateBytes.
[0091] As can be seen from the above description, each thread has a corresponding first memory buffer, such as TLAB. When a thread has a large object to store, the thread can also store it in the corresponding second memory buffer.
[0092] The first memory usage information maintained for the thread is used to record the memory occupancy of common objects in the first memory buffer corresponding to the thread and the memory occupancy of large objects in the second memory buffer corresponding to the thread. The second memory usage information maintained for the thread is used to record the memory occupancy of common objects in the first memory buffer corresponding to the thread.
[0093] The following is a detailed description using a specific thread as an example.
[0094] The following first describes in detail how the electronic device maintains the first memory usage information for a thread using an example.
[0095] As described above, the first memory usage information maintained for a thread is used to record the memory occupancy of common objects in the first memory buffer corresponding to the thread and the memory occupancy of large objects in the second memory buffer corresponding to the thread.
[0096] That is, the first memory usage information is used to count the actual memory usage of common objects and large objects. Since large objects require a large storage space, the electronic device needs to spend more computing power in the process of determining the storage space required by large objects. Therefore, in order to save computing power, the electronic device can count the actual memory usage of common objects and large objects within a certain period of time, and not count at other times.
[0097] Based on this, the first memory usage information maintained for the thread is specifically used to count the memory usage of the ordinary object created by the thread when storing the ordinary object, and the memory usage of the large object created by the thread when storing the large object. The "first memory buffer corresponding to the thread" described here refers to: the first memory buffer currently used by the thread.
[0098] For example, the electronic device may store the common object created by the thread in the first memory buffer currently used by the thread. When the electronic device stores the common object, if it is within the aforementioned time period, the electronic device may record the memory usage of the common object in the first memory usage information.
[0099] Similarly, the electronic device can store the large object created by the thread in the second memory buffer corresponding to the thread. When the electronic device stores the large object, if it is within the aforementioned time period, the electronic device can record the memory usage of the large object in the first memory usage information.
[0100] Wherein, whether it is in the aforementioned time period can be judged by the status value maintained by the electronic device. Wherein, the specific value of the status value may include a first value and a second value. For example, take the first value is true and the second value is false as an example. When the status value is true, it can be considered to be in the aforementioned time period. When the status value is false, it can be considered not to be in the aforementioned time period. After the status value of the electronic device is set to the second value such as false, the electronic device can clear the maintained first memory usage information. Alternatively, when the status value of the electronic device is set to the first value such as true, the electronic device can clear the maintained first memory usage information. Therefore, before maintaining the first memory usage information, the initial value of the first memory usage information is 0.
[0101] The following specifically describes how the electronic device maintains the second memory usage information for a thread using an example.
[0102] As described above, the second memory usage information maintained for the thread is used to record the memory usage of the first memory buffer corresponding to the thread. The "first memory buffer corresponding to the thread" described here includes: the first memory buffer historically allocated to the thread but released. The memory usage of the first memory buffer may refer to the actual usage of the first memory buffer by the objects created by the thread.
[0103] The second memory usage information may specifically record the memory usage of each first memory buffer in the released first memory buffer, or the second memory usage information may specifically record the sum of the memory usage of all first memory buffers in the released first memory buffer.
[0104] As an example, the memory occupancy of the first memory buffer that was historically allocated to the thread but has been released can be recorded in the second memory usage information when the first memory buffer is released. The first memory buffer allocated to the thread can be released when the remaining storage space of the first memory buffer is insufficient. The first memory buffer allocated to the thread can also be released when the first memory buffer is recycled.
[0105] Take the example that the second memory usage information records the sum of the memory occupancy of all the first memory buffers in the above-mentioned released first memory buffer, and the first memory buffer allocated for the thread can be released when the remaining storage space in the first memory buffer is insufficient. For example, in the initial case, the memory occupancy recorded by the second memory usage information is 0. In the case of the thread's need to store ordinary objects, the electronic device allocates a first memory buffer to the thread, such as called the first memory buffer 1, which has a size of 10 bytes. Afterwards, the electronic device stores the ordinary objects created by the thread in the first memory buffer 1. For example, after a certain period of time, such as at time 1, the first memory buffer 1 stores 2 ordinary objects, such as object 1 and object 2, and occupies 7 bytes.
[0106] Afterwards, at time 2, the thread creates a new common object, such as object 3, which requires 7 bytes of storage space. Since the remaining space in the first memory buffer 1 cannot store object 3, the electronic device can create a new first memory buffer with a memory size of 20 bytes, such as first memory buffer 2. In this case, the electronic device releases the first memory buffer 1 and stores the two common objects in the first memory buffer 1 and the newly created common object in the first memory buffer 2, that is, storing object 1, object 2 and object 3 in the first memory buffer 2.
[0107] In this embodiment, when releasing the first memory buffer 1, the electronic device can obtain the memory occupancy of the released first memory buffer 1, such as the actual occupancy of the first memory buffer 1 by object 1 and object 2, that is, 7 bytes. According to the acquired memory occupancy of the released first memory buffer 1 of 7 bytes, the memory occupancy of the released first memory buffer is determined to be 7 bytes and recorded. Similarly, if there are ordinary objects of the thread that need to be stored later, but the first memory buffer currently used by the thread, such as the first memory buffer 2, has insufficient remaining storage space, the electronic device can create a new first memory buffer with larger memory, such as the first memory buffer 3, for storing the ordinary objects of the thread, and release the first memory buffer 2. Then, when releasing the first memory buffer 2, the electronic device will also record the memory occupancy of the released first memory buffer. By analogy, the electronic device can record the memory occupancy of all allocated but released first memory buffers during the thread running process in the second memory usage information.
[0108] As described above, the electronic device can obtain the first memory usage information and / or second memory usage information maintained at different times during the running process of any of the created threads. Based on the acquired memory usage information, the memory occupancy of the thread is determined. For example, during the running of the thread, the electronic device can obtain the first memory usage information at two different times, and determine the memory occupancy of the thread within a preset time by comparing the first memory usage information obtained twice. Similarly, the electronic device can obtain the second memory usage information at two different times and the memory occupancy of the first memory buffer currently used by the thread at the corresponding time, and determine the memory occupancy of the thread within a preset time by comparing the two acquired data.
[0109] As for whether the electronic device determines the memory usage corresponding to the thread based on the first memory usage information, the second memory usage information, or the first memory usage information and the second memory usage information, it can be configured according to the needs of the actual application scenario. The embodiments of the present application are not specifically limited here.
[0110] In some embodiments, after receiving a first request to obtain memory usage of a first thread, the electronic device records the size of the object in the first memory usage information in response to the first thread creating an object, until the first request is received again, and stops recording the size of the object created by the first thread in the first memory usage information. The sizes of all objects recorded in the first memory usage information are used to indicate the memory usage of the first thread.
[0111] It can be understood that after the electronic device receives the first request for the first time, it can obtain the first memory usage information when the first request is first received. After the electronic device receives the first request again, it can obtain the first memory usage information when the first request is received again. By comparing the first memory usage information at two different times, the memory occupancy of the thread between the two first requests received can be determined.
[0112] In other embodiments, after receiving a second request for obtaining the memory occupancy of the first thread, the electronic device obtains the first occupancy value currently recorded in the second memory usage information, and the second occupancy value, the second occupancy value being the memory occupancy value of the ordinary object stored in the first memory buffer used to store the ordinary object created by the first thread when the second request is received. After receiving the second request again, the electronic device obtains the third occupancy value currently recorded in the second memory usage information, and the fourth occupancy value, the fourth occupancy value being the memory occupancy value of the ordinary object stored in the first memory buffer used to store the ordinary object created by the first thread when the second request is received again. The electronic device determines the memory occupancy of the first thread based on the first occupancy value, the second occupancy value, the third occupancy value and the fourth occupancy value. Among them, the second memory usage information is used to record the memory occupancy value of the object stored in the first memory buffer that has been released and historically used to store the ordinary object created by the first thread.
[0113] Among them, the first occupancy value is the second memory usage information recorded when the electronic device receives the second request for the first time, and the second occupancy value is the memory occupancy of the first memory buffer currently used by the thread when the electronic device receives the second request for the first time. Similarly, the third occupancy value is the second memory usage information recorded when the electronic device receives the second request again, and the fourth occupancy value is the memory occupancy of the first memory buffer currently used by the thread when the electronic device receives the second request again. The electronic device can determine the memory occupancy of the thread between the two received second requests by comparing the first occupancy value and the second occupancy value when the second request is received for the first time, and the third occupancy value and the fourth occupancy value when the second request is received for the second time.
[0114] The following combination Figure 4 and Figure 5 Describe in detail.
[0115] like Figure 4 As shown, the specific steps for the electronic device to determine the memory occupancy corresponding to the thread based on the first memory usage information or the second memory usage information are as follows.
[0116] Step 401: The electronic device obtains memory statistics requirements for threads.
[0117] In an embodiment of the present application, the electronic device can obtain the memory statistics result of the thread corresponding to the requirement at the current moment through the memory statistics requirement. The electronic device can obtain the memory statistics requirement through the received first request or the second request.
[0118] The electronic device can also determine whether to count the memory occupancy of common objects corresponding to the thread, or to count the memory occupancy of common objects and large objects corresponding to the thread according to the memory statistics requirements. The electronic device can determine the memory occupancy of common objects corresponding to the thread through the second memory usage information. The electronic device can also determine the memory occupancy of common objects and large objects corresponding to the thread through the first memory usage information.
[0119] Step 402: The electronic device determines whether to count only the memory occupancy of common objects corresponding to the thread based on the memory statistics requirement.
[0120] In an embodiment of the present application, when only the memory occupancy of common objects corresponding to threads is counted, the electronic device executes step 403 , and when not only the memory occupancy of common objects corresponding to threads is counted, the electronic device executes step 406 .
[0121] Step 403: The electronic device obtains second memory usage information.
[0122] Step 404: The electronic device obtains the actual memory occupancy of the first memory buffer currently used by the thread.
[0123] Step 405: The electronic device takes the sum of the actual memory occupancy of the first memory buffer currently used by the thread and the second memory usage information as the memory statistics result.
[0124] In the embodiment of the present application, after the electronic device executes step 405, in order to respond to the memory statistics demand, step 409 may be executed. The electronic device determines the memory occupancy of the thread by obtaining the memory statistics results at two moments before and after. After the electronic device obtains the memory occupancy of the common object for the first time, after a preset time, the electronic device may continue to execute step 401 and obtain the memory occupancy of the common object again, so as to determine the memory occupancy of the thread within the preset time.
[0125] Step 406: The electronic device determines whether it is the first time to apply for obtaining the memory usage of common objects and large objects.
[0126] In the embodiment of the present application, the electronic device determines the memory occupancy of the thread by obtaining the memory statistics results at two moments before and after, that is, the electronic device needs to obtain the memory occupancy of the common objects and large objects of the thread twice.
[0127] In an embodiment of the present application, when applying for obtaining memory usage of common objects and large objects for the first time, the electronic device executes step 407 , and when applying for obtaining memory usage of common objects and large objects for not the first time, the electronic device executes step 408 .
[0128] Step 407: The electronic device sets the state value to the first value, sets the first memory usage information to zero, and uses the first memory usage information as the memory statistics result.
[0129] In an embodiment of the present application, after the electronic device executes step 407, in order to respond to the memory statistics requirement, step 409 may be executed, and the memory statistics result returned at this time is 0.
[0130] The electronic device determines the memory occupancy of the thread by obtaining the memory statistics results at two moments before and after. After the electronic device obtains the memory occupancy of ordinary objects and large objects for the first time, after a preset time, the electronic device can continue to execute step 401 and obtain the memory occupancy of ordinary objects and large objects again to determine the memory occupancy of the thread within the preset time, and after executing to step 406, it will execute steps 408 and 409.
[0131] Step 408: The electronic device sets the state value to the second value, and uses the first memory usage information as the memory statistics result.
[0132] In an embodiment of the present application, the first memory usage information counted in step 408 is the memory occupancy of common objects and large objects stored in the thread counted when the electronic device is at the first value.
[0133] Step 409: Return the memory statistics result.
[0134] like Figure 5 As shown, combined with Figure 3 The software module of the electronic device in the embodiment describes in detail the process of obtaining the memory statistics results of the thread.
[0135] Step 501: The thread memory monitoring module generates memory statistics requirements for the thread.
[0136] The thread memory monitoring module generates thread memory statistics requirements for certain specific threads or when abnormal memory growth is detected. The memory statistics requirements are used to indicate the memory usage of the acquired thread.
[0137] Step 502: The memory usage information acquisition module determines whether the thread object for which memory statistics are required is a common object according to the memory statistics requirement.
[0138] When the thread object is a common object, step 503 is executed.
[0139] When the thread object is a common object or a large object, step 505 is executed.
[0140] The threads to be counted as indicated by the statistical requirements may include ordinary objects or ordinary objects and large objects. In response to the memory statistical requirements of different threads, the memory usage information acquisition module performs different acquisition operations and feeds back the acquired memory statistical results to the thread memory monitoring module. The following will describe these two situations separately.
[0141] On the one hand, for the case where the thread object is a common object, the main processes include the following: Step 503: The memory usage information acquisition module acquires the second memory usage information at the current moment, and acquires the current memory occupancy of the first memory buffer currently used by the thread.
[0142] Specifically, the memory usage information acquisition module can call related functions through a local interface to obtain the second memory usage information at the current moment and the current memory occupancy of the first memory buffer currently in use. The local interface can be a Java Native Interface, also known as a Java native interface. The calling function can include a VMRuntime_getBytesAllocated() function, or other functions, without limitation.
[0143] Step 504: the memory usage information acquisition module sends the current memory statistics result to the thread memory monitoring module; the current memory statistics result includes the second memory usage information at the current moment and the current memory occupancy of the first memory buffer currently used by the thread.
[0144] The memory usage information acquisition module can directly feed back the second memory usage information at the current moment and the current memory occupancy of the first memory buffer currently used by the thread as the current memory statistics result to the thread memory monitoring module.
[0145] Alternatively, the memory usage information acquisition module may also calculate the sum of the second memory usage information at the current moment and the current memory occupancy of the first memory buffer currently used by the thread, and feed the sum as the current memory statistics result back to the thread memory monitoring module.
[0146] On the other hand, when the thread object is a common object or a large object, the following process is mainly executed: Step 505: The memory usage information acquisition module determines whether the memory statistics demand is the first statistics demand for common objects and large objects.
[0147] When the memory statistics demand is the first statistics demand for common objects and large objects, step 506 is executed.
[0148] If the memory statistics requirement is not the first statistics requirement for common objects and large objects, step 508 is executed.
[0149] The module for obtaining memory usage information will perform different operations depending on whether it is the first statistical request for common objects or large objects. The following will describe these two situations separately.
[0150] First, when the memory statistics requirement is the first statistical requirement for common objects and large objects, the following process is mainly performed: Step 506: The module for obtaining memory usage information sets the state value to the first value, and sets the first memory usage information to zero.
[0151] When the memory statistics requirement is the first statistical requirement for common objects and large objects, the memory usage information acquisition module first sets the state value to a first value, so that the thread running module records the memory usage in the process of storing objects under the state indicated by the first value.
[0152] Also, when the memory statistics requirement is the first statistical requirement for common objects and large objects, the thread running module has not started to record the memory usage. In this case, the module for obtaining memory usage information can first set an initial value for the first memory usage information. For example, the initial value is zero.
[0153] Step 507: the memory usage information acquisition module sends the current memory statistics result to the thread memory monitoring module; the current memory statistics result includes the current first memory usage information.
[0154] In response to the memory statistics demand initiated by the thread memory monitoring module, the first memory usage information that is set to zero by the memory usage information acquisition module is fed back to the thread memory monitoring module as the current memory statistics result.
[0155] Second, if the memory statistics requirement is not the first statistical requirement for common objects and large objects, the following process is mainly performed: Step 508: The memory usage information acquisition module sets the state value to the second value, and acquires the first memory usage information.
[0156] Step 507: the memory usage information acquisition module sends the current memory statistics result to the thread memory monitoring module; the current memory statistics result includes the current first memory usage information.
[0157] In the case of no first statistical requirement, the memory operation module has recorded the memory usage of the thread, that is, the first memory usage information. The memory usage information acquisition module can feed back the current first memory usage information as the current memory statistical result to the thread memory monitoring module.
[0158] It should be noted that after the thread memory monitoring module generates a memory statistics requirement, it will not execute all the actions from step 502 to step 508. The thread memory monitoring module selects to execute the memory usage information acquisition scheme for common objects, or selects to execute the memory usage information acquisition scheme for common objects and large objects, according to the thread object indicated by the memory statistics requirement. That is, after the thread memory monitoring module generates a memory statistics requirement, the memory usage information acquisition module will feedback a memory statistics result that matches the memory statistics requirement.
[0159] After obtaining the current memory statistics, the thread memory monitoring module will also perform the following steps: Step 509: The thread memory monitoring module determines whether there is abnormal memory usage based on the difference between the current memory statistics result and the previous content statistics result.
[0160] Step 510: When there is abnormal memory usage, report an abnormal event; the abnormal event is used to indicate the thread with abnormal memory usage.
[0161] The thread memory monitoring module generates a memory statistics requirement each time, and obtains the current content statistics result by sending the memory statistics requirement to the memory usage information acquisition module. The thread memory monitoring module subtracts the current memory statistics result from the previous memory statistics result to obtain the memory usage situation from the last statistical moment to the current statistical moment.
[0162] The critical condition of abnormal memory usage is associated with the memory usage threshold. If the difference between the current memory statistics and the previous statistics is greater than the usage threshold, it is a critical condition for abnormal memory usage. In this case, the thread memory monitoring module reports the abnormal event. Other modules of the electronic device can output the abnormal event to prompt the maintenance personnel of the electronic device to deal with the abnormal memory usage.
[0163] In an example, when the memory statistics requirement indicates that the statistics thread is a common object, the thread memory monitoring module executes steps 509 and 510.
[0164] In another example, the memory statistics requirement indicates the common objects and large objects of a certain thread to be counted. In this case, the thread memory monitoring module usually generates only two memory statistics requirements, namely the first and second time. The first time, the first memory usage information is set to zero, and the second time, the first memory usage information is the real-time recorded memory usage.
[0165] Even if the thread memory monitoring module makes a difference between the second and first times, the difference obtained is actually the first memory usage information of the second time. Based on this, after generating the second memory statistics requirement for the thread, the thread memory monitoring module can use the memory statistics result of the second memory statistics requirement to compare with the preset usage threshold. If the memory statistics result of the second memory statistics requirement is greater than the preset usage threshold, the thread memory monitoring module reports an abnormal event.
[0166] In some embodiments, the memory storage module can store objects included in the thread, and the thread execution module records the memory usage of the objects included in the thread. That is, the electronic device records the memory usage of the object based on the stored thread object. The following mainly explains the specific implementation method of the electronic device storing objects and recording memory usage from the perspective of the electronic device.
[0167] In one example, the electronic device stores common objects, and records the memory usage of the common objects.
[0168] Specifically, the electronic device first verifies the class of the object to be stored to determine the type of the object to be stored. If the object to be stored is a common object, and the remaining storage space of the first memory buffer currently corresponding to the thread is greater than the memory required to be occupied by the common object, the electronic device can directly store the common object to be stored in the first memory buffer. If the state value of the thread is the first value, the electronic device records the memory usage occupied by the common object in the first memory usage information. This process can also be called fast path storage.
[0169] When the object to be stored is a common object, but the remaining storage space of the current first memory buffer of the thread is less than or equal to the memory required to be occupied by the common object, the electronic device releases the current first memory buffer of the thread, recreates a new first memory buffer, and stores the common object in the new first memory buffer. When the state value of the thread is the first value, the electronic device records the memory occupancy occupied by the common object in the first memory usage information. This process can be called slow path storage. In addition, the electronic device records the actual occupancy of the released first memory buffer in the second memory usage information.
[0170] In one example, the electronic device stores a large object, and records the memory usage of the large object. When the state value of the thread is the first value, the actual memory usage of the large object is recorded in the first memory usage information. This process can also be called slow path storage.
[0171] Below through Figures 6 to 8 Explain in detail the storage and recording schemes implemented by the electronic device. Figure 6As shown, the specific steps are as follows: Step 601: The electronic device generates an object creation requirement; the object creation requirement is used to instruct to create an object for a thread.
[0172] In some embodiments, the electronic device generates an object creation requirement during the process of running a thread to instruct the creation of an object for the thread. The object that the electronic device needs to create for the thread can be a common object or a large object. Step 602: Whether the electronic device performs an object creation operation through an interpreter.
[0173] If it is determined that the object creation operation is not performed through the interpreter, step 603 is executed.
[0174] When it is determined that the object creation operation is performed through the interpreter, step 605 is executed.
[0175] An interpreter is a computer program that reads and executes source code.
[0176] Step 603: The electronic device obtains the machine language corresponding to the source code used to create the object.
[0177] Step 604: The electronic device obtains a function for storing an object in a machine language.
[0178] The electronic device can obtain the machine language by calling visitnewinstance(). When the electronic device creates an object, it stores the created object in the corresponding buffer.
[0179] During specific implementation, the electronic device may determine the function used to store the object through the GetEntrypoint() function.
[0180] Step 605: The electronic device compiles the source code for creating the object through an interpreter.
[0181] The electronic device can compile the source code, create the object and store the created object through NterpAllocateObject().
[0182] Step 606: The electronic device determines the class of the object to be created and stored.
[0183] In an embodiment of the present application, the class of an object is used to determine the type of the object. For example, the type of the object includes: basic data types, reference types, binary types, and the like.
[0184] Step 607: The electronic device determines whether the required memory usage is fixed according to the class of the object.
[0185] When the required memory usage is fixed, step 608 is executed to use the fast path storage.
[0186] When the required memory usage is not fixed, the slow path is used for storage. Figure 7 Related examples.
[0187] In the embodiments of the present application, different types of objects require different amounts of memory.
[0188] In one case, for common objects, the memory usage required is fixed. In other words, when objects of basic data types and objects of reference types are common objects, the memory usage required for the objects of basic data types and objects of reference types is fixed.
[0189] In another case, for a large object, the memory usage required is not fixed. In other words, when a binary type object is a large object, the memory usage required for the binary type large object is not fixed.
[0190] If the required memory usage is fixed, the object is a common object, and step 608 is executed. If the required memory usage is not fixed, the object is a large object, and the following Figure 7 , electronic devices take the slow path for storage.
[0191] Step 608: The electronic device determines whether the amount of memory required by the object is less than or equal to the remaining memory in the first memory buffer.
[0192] When the memory usage required by the object is less than or equal to the remaining memory of the first memory buffer, step 609 is executed. In this case, the electronic device uses a fast path storage to directly store the object in the first memory buffer.
[0193] When the memory usage required by the object is greater than the remaining memory in the first memory buffer, the electronic device may also use the slow path for storage. Figure 7 Related examples.
[0194] Step 609: The electronic device stores the object in the first memory buffer.
[0195] Step 610: The electronic device initializes object information of the object.
[0196] Object information includes object status, attribute values, construction methods, etc. When creating an object, the electronic device initializes the object information of the object, that is, the electronic device adds relevant parameters of the object information to the object, or assigns relevant parameters of the object information to common initial values, etc., without limitation.
[0197] Step 611: The electronic device determines whether the current state value of the thread is the first value.
[0198] In an embodiment of the present application, when the current state value of the thread is the first value, step 612 is executed, and when the current state of the electronic device is not the first value, step 613 is executed.
[0199] Step 612: The electronic device records the actual memory usage of the object in the first memory usage information.
[0200] When the current state value is the first value, the electronic device may perform a memory usage recording operation. For example, a memory operation module of the electronic device records the memory usage.
[0201] Step 613: The electronic device returns the object storage result.
[0202] In an embodiment of the present application, the electronic device returns an object storage result, wherein the storage result includes two types: storage success and storage failure.
[0203] The above steps 609 to 613 correspond to the process of the electronic device executing fast path storage. Figure 7 The process of implementing slow path storage for electronic devices is as follows.
[0204] Step 701: The electronic device selects a memory storage device that matches the type of the object.
[0205] Different memory storages can be used to store different types of objects. For example, when the object is a large object, the electronic device can select a memory storage for large objects to store the large object. When the object is a common object, the electronic device can select a memory storage for common objects to store the common object.
[0206] In an embodiment of the present application, the electronic device may call the object memory storage execution function AllocObjectWthAllocator() and select different memory storage devices according to the type of the object. Step 702: The electronic device determines whether the object is a large object or a common object.
[0207] In the embodiment of the present application, when the electronic device determines that the object is a large object or a common object, the electronic device executes step 705. When the electronic device does not determine whether the object is a large object or a common object, the electronic device executes step 703.
[0208] Step 703: The electronic device determines whether the object is a large object.
[0209] In the embodiment of the present application, when the electronic device determines that the object is a large object, step 704 is executed, and the electronic device determines that the object is a large object. When the electronic device determines that the object is not a large object, the object is a common object, and step 705 is executed. The electronic device can determine whether to determine whether the object is a large object through the isLargeObject() function.
[0210] Step 704: The electronic device determines that the object is a large object.
[0211] In an embodiment of the present application, the electronic device may set a value through the AllocLargeObject() function, indicating that the electronic device determines that the current object is a large object. The value may be true, and the process returns to step 701, so that the electronic device selects the memory storage of the large object to store the large object.
[0212] Step 705: The electronic device determines whether the memory storage of the common object can be directly used to store the object into the first memory buffer.
[0213] In an embodiment of the present application, when the remaining storage space of the first memory buffer corresponding to the thread is greater than or equal to the memory required to be occupied by the common object, the electronic device can directly use the memory storage of the common object to store the object in the first memory buffer, and then execute step 706. When the remaining storage space of the first memory buffer corresponding to the thread is less than the memory required to be occupied by the common object, the electronic device cannot directly use the memory storage of the common object to store the object in the first memory buffer, and executes step 708. Step 708 can refer to Figure 8 Alternatively, when the object is a large object, the electronic device uses the memory storage of the large object to store the object, and cannot use the memory storage of the common object to store the large object in the first memory buffer. The electronic device may also perform step 708, which may refer to Figure 8 .
[0214] Step 706: The electronic device stores the object in a first memory buffer.
[0215] Step 707: Whether the electronic device successfully stores the object.
[0216] In the embodiment of the present application, if the electronic device successfully stores the object, step 709 is executed. If the electronic device fails to successfully store the object, step 708 is executed.
[0217] Step 708: The electronic device attempts to store the object.
[0218] Step 709: The electronic device initializes the object information of the object.
[0219] Step 710: The electronic device determines whether the current state value of the thread is the first value.
[0220] In an embodiment of the present application, when the current state value of the thread is the first value, step 711 is executed, and when the current state of the electronic device is not the first value, step 712 is executed.
[0221] Step 711: The electronic device records the actual memory usage of the object in the first memory usage information.
[0222] When the current state value is the first value, the electronic device may perform a memory usage recording operation. For example, a memory operation module of the electronic device records the memory usage.
[0223] Step 712: The electronic device returns the object storage result.
[0224] When the remaining storage space of the first memory buffer corresponding to the current thread of the electronic device is smaller than the memory required by the common object, or when the object is a large object and the electronic device cannot use the memory storage of the common object to store the object in the first memory buffer, such as Figure 8 As shown, perform the following steps.
[0225] Step 801: The electronic device determines whether the memory storage required to store the object is a memory storage matching the common object.
[0226] In the case where the memory storage required for storing the object is the memory storage of a common object, step 802 is executed.
[0227] Step 802: The electronic device creates a new first memory buffer and stores the object to be stored in the new first memory buffer.
[0228] In the case that the memory storage required for storing the object is not the memory storage of a common object, step 803 is executed.
[0229] For a specific implementation scheme of the electronic device storing the object to be stored in the new first memory buffer, see Fig. 9 Related examples.
[0230] Step 803: The electronic device uses the memory storage of the large object to store the large object in the second memory buffer. It should be understood that after the electronic device stores the common object in the corresponding memory cache area, it can also execute the steps of setting the status value and recording the object information, which will not be elaborated herein.
[0231] like Fig. 9 As shown, the specific process of the electronic device storing the object to be stored into the new memory buffer can refer to the following steps.
[0232] Step 901: The electronic device expands the space of the first memory buffer and determines whether the available memory of the expanded first memory buffer is greater than or equal to the memory usage required by the object.
[0233] In an embodiment of the present application, the available memory allocated by the electronic device to the first memory buffer may be much larger than the actual memory used by the first memory buffer. In order to prevent the size of the unoccupied memory of the first memory buffer from being too large, the electronic device may only grant permission to a portion of the memory in the first memory buffer, and this portion of the memory with permission can be used for storage.
[0234] In the case that the memory area opened by the first memory buffer does not meet the memory occupancy required by the thread to be stored, the electronic device can open the allocated first memory buffer with remaining unopened permissions to store new objects.
[0235] For example, the electronic device actually allocates 20 bytes of storage space to the first memory buffer, and the electronic device may only open 10 bytes of memory space to the first memory buffer for object storage. After the first memory buffer has used 9 bytes of space, the electronic device responds to the need to store a 3-byte object in the first memory buffer. Since the memory size of the first memory buffer opened by the electronic device cannot meet the current storage, the electronic device can expand the space of the first memory buffer, that is, the electronic device opens another 10 bytes of storage space to make the storage space of the first memory buffer 20 bytes. In this case, the storage space of the expanded first memory buffer is sufficient to store the object. But assuming that the memory size occupied by the object to be stored is 15 bytes, in this case, the electronic device cannot store the object even if it expands the storage space of the first memory buffer.
[0236] In an embodiment of the present application, if the available memory of the expanded first memory buffer is greater than or equal to the memory footprint required by the object, step 909 is executed. If the available memory of the expanded first memory buffer is less than the memory footprint required by the object, step 902 is executed.
[0237] Step 902: The electronic device determines whether the type of the memory buffer to be created is a first memory buffer.
[0238] In an embodiment of the present application, in addition to the first memory buffer and the second memory buffer, a third memory buffer and a fourth memory buffer may also be provided in the heap memory of the electronic device. Different types of memory buffers have different usage conditions. In an embodiment of the present application, the third memory buffer may be a collision pointer buffer. The fourth memory buffer may be a non-moving buffer.
[0239] As described above, when the electronic device uses a cross-correlation algorithm (CC algorithm), the first memory buffer will be used to store the object. When the electronic device uses a concurrent mark compaction algorithm (CMC algorithm), the electronic device will use a third memory buffer to store the object.
[0240] When the memory usage required by the object is greater than a preset memory threshold, or when the memory required by the object exceeds the available memory of the electronic device, the fourth memory buffer is used. When the memory usage required by the object is less than or equal to the preset memory threshold, and the memory required by the object does not exceed the available memory of the electronic device, the electronic device stores the object in the first memory buffer.
[0241] In the embodiment of the present application, when the type of the memory buffer to be created by the electronic device is the first memory buffer, step 903 is executed. When the type of the memory buffer to be created by the electronic device is not the first memory buffer, step 910 is executed.
[0242] Step 903: The electronic device determines whether the memory usage required by the object is greater than a preset memory threshold.
[0243] In an embodiment of the present application, when the memory usage required by the object is greater than a preset memory threshold, step 912 is executed; when the memory usage required by the object is less than or equal to the preset memory threshold, step 904 is executed.
[0244] Step 904: The electronic device determines whether the memory required by the object exceeds the available memory of the electronic device.
[0245] In the embodiment of the present application, whether the available memory of the first buffer is expanded or a new buffer is created, it should be within the available memory of the electronic device. If the memory required by the object exceeds the available memory of the electronic device, step 912 is executed, and if the memory required by the object does not exceed the available memory of the electronic device, step 905 is executed.
[0246] Step 905: The electronic device calculates the available memory of the first memory buffer to be created.
[0247] In an embodiment of the present application, the electronic device determines the available memory of the required first memory buffer. For example, the first memory buffer is 20 bytes, of which 17 bytes of storage space are occupied, and the size of the object to be stored is 10 bytes. In this case, the first memory buffer newly created by the electronic device can be 30 bytes.
[0248] Step 906: The electronic device determines whether the new first memory buffer is successfully created.
[0249] In an embodiment of the present application, if the electronic device successfully creates a new first memory buffer, step 907 is executed, and if the electronic device fails to successfully create a new first memory buffer, step 912 is executed.
[0250] Step 907: The electronic device stores the object in a memory buffer.
[0251] Step 908: The electronic device sets the object information of the stored object.
[0252] After storing the object in the new first memory buffer, the electronic device may also perform operations such as setting object information and setting status values, which will not be described in detail.
[0253] Step 909: The electronic device expands the memory space of the first memory buffer.
[0254] In this embodiment, since the expanded first memory buffer can directly store the object, the electronic device can directly store the object in the expanded first memory buffer when the available memory of the expanded first memory buffer is greater than or equal to the memory occupancy required by the object.
[0255] Step 910: The electronic device calculates the available memory of the third memory buffer to be created.
[0256] In this embodiment, when the algorithm executed by the electronic device is the CMC algorithm, the object of the electronic device is stored in the third memory buffer. When the object is stored in the third memory buffer, the electronic device will create the third memory buffer and determine the available memory of the third memory buffer according to the amount of occupancy required by the object.
[0257] Step 911: The electronic device determines whether a new third memory buffer is successfully created.
[0258] In an embodiment of the present application, if the electronic device successfully creates a new third memory buffer, step 907 is executed. If the electronic device fails to successfully create a new third memory buffer, the electronic device returns a result of object storage failure.
[0259] Step 912: The electronic device stores the object in a fourth memory buffer.
[0260] In an embodiment of the present application, when the memory usage required by the object is greater than a preset memory threshold, or when the memory required by the object exceeds the available memory of the electronic device, or when the electronic device fails to successfully create a new first memory buffer, the electronic device may create a fourth memory buffer to store the object, and store the object in the fourth memory buffer. If the storage is successful, step 907 is executed, and if the storage fails, the electronic device will return a result that the object storage failed.
[0261] It is understandable that in the embodiment of the present application, the electronic device can not only count the first memory buffer and the second memory buffer, but also count the third memory buffer and the fourth memory buffer. The method for the electronic device to count the memory occupancy of the third memory buffer and the fourth memory buffer is the same as the method for the electronic device to count the memory occupancy of the first memory buffer and the second memory buffer, and will not be described in detail here.
[0262] The following is a specific description of how the electronic device collects statistics of the second memory usage information using an example.
[0263] In some embodiments, during the running of the thread, regardless of whether the electronic device is the first value, the electronic device always counts the second memory usage information. Among them, when the remaining storage space of the current first memory buffer of the thread cannot store new objects, the electronic device needs to release the current first memory buffer and create a new first memory buffer, the electronic device can record the actual memory occupancy of the released first memory buffer to the second memory usage information. And, when the electronic device executes a garbage collection mechanism, the electronic device can record the actual memory occupancy of the first memory buffer released during the garbage collection process to the second memory usage information.
[0264] For example, when the storage space of the first memory buffer is insufficient, the process of storing the object in the electronic device can refer to the above Figure 8 and Fig. 9 After the electronic device creates a new first memory buffer, stores the object in the new first memory buffer, and releases the old first memory buffer, the electronic device does not need to determine whether the current state value of the thread is the first value, and the electronic device directly records the actual amount of memory occupied in the released first memory buffer.
[0265] In some embodiments, the electronic device may also record the actual amount of memory occupied in the released first memory buffer during the garbage collection process.
[0266] For details, please refer to Fig.10 The specific process is as follows.
[0267] Step 1001: The garbage collection module starts the garbage collection mechanism.
[0268] In an embodiment of the present application, the electronic device may start a garbage collection mechanism through a Heap::CollectGarbage() function.
[0269] Step 1002: The garbage collection module prepares to suspend all threads in the thread list management module.
[0270] In an embodiment of the present application, the electronic device will pause all threads before performing garbage collection, so as to correctly identify the garbage objects to be recycled in all threads. The garbage objects are objects whose usage frequency is less than or equal to a preset frequency threshold. The electronic device can be initialized through the FlipThreadRoots() function and prepare to pause all current threads.
[0271] Step 1003: The garbage collection module determines the status of each thread.
[0272] Thread flipping can also be called thread priority flipping. Thread priority flipping usually occurs in a multi-threaded environment. When a high-priority thread is blocked by a low-priority thread, and this low-priority thread delays the release of resources because it is preempted by other medium-priority threads, the real-time performance of the high-priority thread cannot be guaranteed.
[0273] In an embodiment of the present application, the electronic device prevents thread flipping during garbage collection by determining the state of each thread. For example, the thread scheduling can be optimized by the thread state, so as to control the behavior of the thread during thread flipping, which can be pausing the thread, resuming the thread, etc. The electronic device can determine the state of each thread through the newThreadFlipVision() function to control the behavior of the thread during thread flipping.
[0274] Step 1004: The thread list management module suspends all current threads.
[0275] In an embodiment of the present application, the electronic device can pause all threads in the current list through the FlipThreadRoots() function.
[0276] Step 1005: The thread list management module registers the function to be called when the thread is flipped.
[0277] In an embodiment of the present application, the electronic device can register the function to be called when the thread flips through the SetFlipFunction() function.
[0278] Step 1006: The thread list management module ensures that all registered flip functions have been initialized.
[0279] In an embodiment of the present application, the electronic device may ensure that all registered flip functions have been initialized through the EnsureFlipFunctionStarted() function.
[0280] Step 1007: The thread management module executes the registered flip function.
[0281] In an embodiment of the present application, the electronic device may execute a registered flip function through the RunFlipFunction() function.
[0282] Step 1008: The thread management module resumes the suspended thread and ends the garbage collection mechanism.
[0283] In an embodiment of the present application, the electronic device can resume the suspended thread through the ThreadFlipVision Run() function.
[0284] Step 1009: The thread management module cleans up thread local resources.
[0285] In an embodiment of the present application, thread-local resources refer to resources that are closely related to threads and used in the process of garbage collection by electronic devices. These resources may include memory, file handles, database connections, etc. When the thread ends, if these resources are not recycled in time, resource leakage may occur, thereby affecting the stability and performance of the system. Therefore, the garbage collection mechanism cleans up thread-local resources after the end in order to ensure that these resources can be effectively managed and recycled to avoid resource leakage problems. Electronic devices can clean up thread-local resources through the RevokeThreadLocableBuffers() function.
[0286] Step 1010: The memory usage information acquisition module updates the second memory usage information.
[0287] In an embodiment of the present application, the electronic device may record the actual amount of memory occupied in the first memory buffer that has been released during the garbage collection process through the RevokeThreadLocalBuffersLocked() function.
[0288] like Fig.11 As shown, the following is combined with the above Figure 3 The software module of the embodiment of the present application is described in detail.
[0289] Step 1101: The thread memory monitoring module sends a memory statistics request to the memory usage information acquisition module.
[0290] Step 1102: When the thread object counted by the memory statistics requirement is a common object, the module for obtaining memory usage information adds the second memory usage information to the actual amount of memory occupied in the first memory buffer currently allocated to the thread by the electronic device, and uses the result of the memory statistics. When the thread object counted by the memory statistics requirement is a common object or a large object, the module for obtaining memory usage information sets the state value of the thread to the first value, sets the first memory usage information to the initial value, and uses the first memory usage information as the result of the memory statistics.
[0291] Step 1103: The thread runs normally, and when it is necessary to store a common object or a large object, the common object or the large object is stored through the memory storage module.
[0292] Step 1104: The memory storage module stores the common object or the large object into the corresponding memory buffer.
[0293] Step 1105: The memory usage information acquisition module updates the first memory usage information or the second memory usage information according to the amount of memory occupied by the stored common objects or large objects.
[0294] Step 1106: The thread memory monitoring module sends a memory statistics request to the memory usage information acquisition module again.
[0295] Step 1107: When the thread object counted by the memory statistics requirement is a common object, the module for obtaining memory usage information adds the second memory usage information to the actual amount of memory occupied in the first memory buffer currently allocated to the thread by the electronic device, and uses the sum as the memory statistics result. When the thread object counted by the memory statistics requirement is a common object or a large object, the module for obtaining memory usage information sets the state value to the second value, obtains the first memory usage information for statistics, and uses the first memory usage information as the memory statistics result.
[0296] In some cases, when the electronic device obtains the first memory usage information or the second memory usage information of the thread, the electronic device will record the acquisition log in the source code of the electronic device. Also, the electronic device can apply for a memory of a fixed storage size for the thread, and the electronic device obtains the memory usage information of the thread before applying for the memory. After the thread successfully applies for the memory of the fixed storage size, the electronic device obtains the memory usage information of the thread again. The electronic device compares the memory usage information obtained twice. If the two memory usage information indicate that the memory added by the thread is close to or approximately equal to the applied memory size, it means that the method provided in this embodiment is effective.
[0297] Some other embodiments of the present application provide an electronic device, which may include: a memory and one or more processors. The memory is coupled to the processor. The memory is used to store computer program code, and the computer program code includes computer instructions. When the computer instructions are executed by the processor, the electronic device performs each function or step in the above method embodiment.
[0298] An embodiment of the present application also provides a computer-readable storage medium, which includes computer instructions. When the computer instructions are executed on the above-mentioned electronic device, the electronic device executes each function or step executed by the electronic device in the above-mentioned method embodiment.
[0299] The embodiment of the present application also provides a computer program product. When the computer program product is run on a computer, the computer executes each function or step executed by the electronic device in the above method embodiment.
[0300] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0301] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another device, 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.
[0302] The units described as separate components may or may not be physically separated, and the components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0303] 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. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0304] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions to enable a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to execute all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.
[0305] The above contents are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application shall be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.
Claims
1. A memory occupancy statistics method, applied to electronic equipment, characterized in that: The method comprises: After receiving a first request for obtaining memory usage information of a first thread, in response to the first thread creating an object, recording a size of the object in first memory usage information until the first request is received again, stopping recording the size of the object created by the first thread in the first memory usage information; The sizes of all objects recorded in the first memory usage information are used to indicate the memory usage of the first thread.
2. The method according to claim 1, characterized in that The method further comprises: After receiving the first request for obtaining the memory occupancy of the first thread, setting the state value of the first thread to a first value; After receiving the first request again, the state value of the first thread is set to a second value.
3. The method according to claim 1 or 2, characterized in that: The first request is specifically used to request to obtain the memory occupancy of all objects created by the first thread.
4. The method according to claim 1, characterized in that: The electronic device includes an application layer and a virtual machine layer; the first request is sent from the application layer to the virtual machine layer; The method further comprises: After the virtual machine layer receives the first request for obtaining the memory usage status of the first thread, the virtual machine layer sets the information recorded in the first memory usage information as an initial value, and returns the initial value to the application layer; After the virtual machine layer receives the first request again, the virtual machine layer reads the sizes of all objects recorded in the first memory usage information to obtain a target value, and returns the target value to the application layer; The application layer determines the memory occupancy of the first thread according to the initial value and the target value.
5. The method according to claim 1, characterized in that The method further comprises: After receiving a second request for obtaining memory occupancy of the first thread, obtaining a first occupancy value currently recorded in the second memory usage information and a second occupancy value, wherein the second occupancy value is a memory occupancy value of a common object stored in a first memory buffer for storing common objects created by the first thread when the second request is received; After receiving the second request again, obtaining a third occupancy value and a fourth occupancy value currently recorded in the second memory usage information, wherein the fourth occupancy value is a memory occupancy value of a common object stored in a first memory buffer for storing common objects created by the first thread when the second request is received again; Determine the memory occupation status of the first thread according to the first occupation value, the second occupation value, the third occupation value and the fourth occupation value; The second memory usage information is used to record the memory occupancy value of the object stored in the first memory buffer that is released and historically used to store common objects created by the first thread.
6. The method according to claim 5, characterized in that The method further comprises: In response to the first thread creating a common object, if the remaining storage space of a first memory buffer currently used to store the common objects created by the first thread is insufficient, releasing the first memory buffer currently used to store the common objects created by the first thread, and recording the memory occupancy value of the stored objects in the second memory usage information; and / or, In response to the first memory buffer currently used to store common objects created by the first thread being recycled, the first memory buffer currently used to store common objects created by the first thread is released, and the memory occupancy value of the stored objects is recorded in the second memory usage information.
7. The method according to claim 5 or 6, characterized in that: The second request is specifically used to request to obtain the memory occupancy of common objects created by the first thread.
8. An electronic device, characterized in that: The electronic device comprises at least: a memory and one or more processors; the memory is used to store computer instructions, and when the one or more processors execute the computer instructions, the electronic device executes the method as described in any one of claims 1-7.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes computer instructions, and when the computer instructions are executed on an electronic device, the electronic device executes the method according to any one of claims 1 to 7.
10. A computer program product, characterized in that When the computer program product is run on a computer, the computer is caused to execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Open services gateway initiative (OSGi)-based software component monitoring method and system
CN101980175A
Memory leak detection method and device and electronic equipment
CN115454696A
Memory resource management method and electronic equipment
CN117112192A
Memory detection method and device, equipment and storage medium
CN117992204A
Memory leak detection method and device, equipment and medium
CN118939460A