Memory occupation statistical method and electronic device

By receiving two requests in the electronic device to record the size of the thread-created object and counting the memory usage from the thread dimension, the problem of the inability to accurately locate memory anomalies in the existing technology is solved, and the accurate location and rapid resolution of memory anomalies are achieved.

CN119938453BActive Publication Date: 2025-10-17HONOR DEVICE CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510421915.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-04-07
Publication Date
2025-10-17
Estimated Expiration
2045-04-07

AI Technical Summary

Technical Problem

Existing technologies cannot accurately count memory usage at the thread level, resulting in the inability to accurately locate the cause of the anomaly when the memory usage is high or increases abnormally.

Method used

By receiving two requests in the electronic device and recording the size of the object created by the thread, the memory usage is counted from the thread dimension, and the cause of the exception is determined in combination with the business relationship.

Benefits of technology

It achieves precise positioning and effective analysis of memory anomalies, and can quickly adjust business codes to resolve memory anomaly problems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119938453B_ABST
    Figure CN119938453B_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a memory occupation statistical method and an electronic device, and relate to the technical field of terminals. The method can statistically analyze the memory occupation from the thread dimension. After receiving a first request for obtaining the memory occupation of a first thread, the electronic device records the size of an object created by the first thread in first memory usage information in response to the object creation, and stops recording the size of the object created by the first thread in the first memory usage information until the first request is received again. The size of all the objects recorded in the first memory usage information is used to indicate the memory occupation of the first thread.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present application relate to the technical field of terminal, and in particular, to a memory occupation statistical method and an electronic device. BACKGROUND

[0002] An electronic device can create and run a process. In the process of running the electronic device, some complex processes can call memory very frequently, resulting in a problem of occupying a large amount of memory, or the memory occupied by some processes can abnormally increase. In order to ensure normal operation, the electronic device needs to monitor the occupation of the memory.

[0003] However, the electronic device can only currently statistically analyze the occupation of the memory of the electronic device in the process dimension, and cannot be disassembled to a specific thread. Therefore, when problems such as a large amount of memory occupation and abnormal memory increase occur, the abnormal cause cannot be accurately located. SUMMARY

[0004] Embodiments of the present application provide a memory occupation statistical method and an electronic device, which can statistically analyze the occupation of the memory from the thread dimension, so that when an abnormality occurs, the abnormal cause can be accurately located based on the data statistically analyzed from the thread dimension.

[0005] To achieve the above object, embodiments of the present application adopt the following technical solutions:

[0006] In a first aspect, the present application provides a memory occupation statistical method, which is applied to an electronic device. The specific method is that the electronic device, after receiving a first request for obtaining the memory occupation of a first thread, records the size of an object created by the first thread in first memory usage information in response to the creation of the object, and stops recording the size of the object created by the first thread in the first memory usage information until the first request is received again. The size of all objects recorded in the first memory usage information is used to indicate the occupation of the memory by the first thread.

[0007] In the above scheme, the electronic device records the size of the object created by the thread between two first requests by receiving the two first requests, so as to statistically analyze the occupation of the memory from the thread dimension. When an abnormality occurs, the abnormal cause can be accurately located based on the data statistically analyzed from the thread dimension, and the abnormality can be effectively analyzed. For example, after the thread causing the memory abnormality is located based on the data statistically analyzed from the thread dimension, the specific business code corresponding to the thread can be determined according to the business relationship, so as to accurately locate the abnormal cause. Then, the business code can be adjusted, so as to accurately and efficiently solve the memory abnormality problem.

[0008] In a possible implementation of the first aspect, the electronic device sets the state value of the first thread to a first value after receiving the first request for obtaining the memory usage of the first thread. The electronic device sets the state value of the first thread to a second value after receiving the first request again.

[0009] In the above solution, the electronic device needs to consume a large amount of computing power in recording the first memory usage information. Therefore, the electronic device can determine whether to start or stop recording the size of the object created by the thread in the first memory usage information by using the first value and the second value, that is, the recording is started only when the first memory usage information is needed, thereby reducing the waste of computing power.

[0010] In another possible implementation of the first aspect, the first request is specifically used to request the memory usage of all objects created by the first thread. The all objects can include large objects and normal objects.

[0011] In the above solution, the electronic device can record the memory usage of all objects created by the thread in the first memory usage information by using the first request, so as to accurately count the memory usage of the thread.

[0012] 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 issued by the application layer to the virtual machine layer. The method further includes: after the virtual machine layer receives the first request for obtaining the memory usage of the first thread, the virtual machine layer sets the information recorded in the first memory usage information to 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 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 usage of the first thread according to the initial value and the target value. The target value can be the memory usage of each object created by the thread between the two first requests, or the sum of the memory usage of all objects created by the thread between the two first requests.

[0013] In the above solution, 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, so as to count the memory usage from the thread dimension, so that the application layer can determine the memory usage of the thread based on the initial value and the target value returned by the virtual machine layer when an exception occurs, accurately locate the cause of the exception, and effectively analyze the exception.

[0014] In a possible implementation of the first aspect, the electronic device, after receiving the second request for obtaining the memory occupation of the first thread, obtains a first occupation value currently recorded in the second memory usage information, and a second occupation value, the second occupation value being an occupation value of the memory by the normal object stored in the first memory buffer for storing the normal object created by the first thread at the time of receiving the second request. The electronic device, after receiving the second request again, obtains a third occupation value currently recorded in the second memory usage information, and a fourth occupation value, the fourth occupation value being an occupation value of the memory by the normal object stored in the first memory buffer for storing the normal object created by the first thread at the time of receiving the second request again. The electronic device determines the memory occupation 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 occupation value of the memory by the object stored in the first memory buffer for storing the normal object created by the first thread, which is released.

[0015] In the above solution, since the objects created by the thread are mostly normal objects, the electronic device determines the memory occupation of the normal objects created by the thread at the time of receiving the second request for the first time according to the first occupation value and the second occupation value, and determines the memory occupation of the normal objects created by the thread at the time of receiving the second request again according to the third occupation value and the fourth occupation value. The electronic device can determine the memory occupied by the normal objects created by the thread between the two times of receiving the second request, and implement the statistics of the memory occupation from the thread dimension, so as to accurately locate the cause of the exception based on the data from the thread dimension when the exception occurs, and implement the effective analysis of the exception.

[0016] In a possible implementation of the first aspect, the electronic device can record the second memory usage information in the following cases. Case one: the electronic device, in response to the first thread creating a normal object, releases the first memory buffer currently used for storing the normal object created by the first thread in a case where the first memory buffer currently used for storing the normal object created by the first thread has insufficient remaining storage space, and records the occupation value of the memory by the object stored in the first memory buffer in the second memory usage information. Case two: the electronic device, in response to the first memory buffer currently used for storing the normal object created by the first thread being recycled, releases the first memory buffer currently used for storing the normal object created by the first thread, and records the occupation value of the memory by the object stored in the first memory buffer in the second memory usage information.

[0017] In the above solution, 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 implement the lightweight recording of the second memory usage information without consuming too much computing power.

[0018] In a possible implementation form of the first aspect, the second request is specifically used for requesting to obtain the memory occupation of the normal object created by the first thread.

[0019] In the above solution, the electronic device needs less computing power to count the memory occupation of the normal object, and the central processing unit is not loaded too much. The memory occupation of the normal object created by the thread can be quickly determined without affecting the running efficiency of the electronic device through the second request.

[0020] In a second aspect, the present application provides an electronic device, comprising a memory and one or more processors, wherein the memory is coupled with the processor. The memory stores computer program codes, and the computer program codes include computer instructions. When the computer instructions are executed by the processor, the electronic device executes the method in the first aspect and any possible implementation form thereof.

[0021] In a third aspect, the present application provides a computer readable storage medium, which includes computer instructions. When the computer instructions are run on an electronic device, the electronic device executes the method in the first aspect and any possible implementation form thereof.

[0022] In a fourth aspect, the present application provides a computer program product. When the computer program product is run on a computer, the computer executes the method in the first aspect and any possible implementation form thereof. The computer can be the electronic device in the second aspect and any possible implementation form thereof.

[0023] It can be understood that 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 can achieve the beneficial effects as described in the method of the first aspect and any possible implementation form thereof, which will not be described here. BRIEF DESCRIPTION OF DRAWINGS

[0024] Figure 1 A structure diagram of a heap memory is provided for the embodiments of the present application.

[0025] Figure 2 A structure diagram of an electronic device is provided for the embodiments of the present application.

[0026] Figure 3 A software system architecture diagram of an electronic device is provided for the embodiments of the present application.

[0027] Figure 4 A flow diagram of a memory occupation counting method is provided for the embodiments of the present application.

[0028] Figure 5A flowchart of another memory occupation statistical method provided by the embodiment of the present application is shown in FIG. 6;

[0029] Figure 6 A flowchart of a fast path storage provided by the embodiment of the present application is shown in FIG. 7;

[0030] Figure 7 A flowchart of a slow path storage provided by the embodiment of the present application is shown in FIG. 8;

[0031] Figure 8 A flowchart of another slow path storage provided by the embodiment of the present application is shown in FIG. 9;

[0032] Figure 9 A flowchart of a first memory buffer releasing provided by the embodiment of the present application is shown in FIG. 10;

[0033] Figure 10 A flowchart of a garbage collection provided by the embodiment of the present application is shown in FIG. 11;

[0034] Figure 11 An interaction diagram between modules provided by the embodiment of the present application is shown in FIG. 12. DETAILED DESCRIPTION

[0035] The technical solutions in the embodiments of the present application will be described below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work should fall within the scope of protection of the present application.

[0036] It should be noted that the following terms "first", "second", etc. are only used for description purpose, and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features limited by "first", "second", etc. can 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 inclusion. For example, the processes, methods, systems, products or devices including a series of steps or units are not limited to the listed steps or units, but can optionally include steps or units not listed or can optionally include other steps or units inherent to the processes, methods, products or devices.

[0037] Reference within this specification to "one embodiment" or "an embodiment" or "embodiments" means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment of the application. The appearances of the phrase "in one embodiment" or "in some embodiments" within this specification do not necessarily refer to the same embodiment, although it can. Thus, use of the phrase "in one embodiment" or "in some embodiments" within the specification does not necessarily refer to the same embodiment, although it can. Such phrases as "in one embodiment" or "in some embodiments" within this specification do not necessarily refer to the same embodiment, although it can. Such phrases as "in one embodiment" or "in some embodiments" within this specification do not necessarily refer to the same embodiment, although it can. Such phrases as "in one embodiment" or "in some embodiments" within this specification do not necessarily refer to the same embodiment, although it can.

[0038] In this application, the word "exemplary" or "for example" is used to mean serving as an example, instance, or illustration. Any implementation or design scheme described in this application as "exemplary" or "for example" should not be construed as being preferred or advantageous over other implementations or design schemes. Rather, the use of the word "exemplary" or "for example" is intended to present concepts in a concrete manner.

[0039] For ease of understanding, before introducing the method of the present application, the related terms are explained.

[0040] A process is an instance of executing a program in an operating system of an electronic device, and is a basic unit of resource allocation and scheduling by the operating system. For example, after receiving a user's operation of opening an application A, the operating system can create a process for the application A. The process includes program code, system resources, other data or files, etc.

[0041] A thread is an execution unit in a process, and is a basic unit of scheduling and dispatching by a Central Processing Unit (CPU). A process can contain multiple threads, and the threads share resources of the process, such as memory resources, file descriptors, etc. Different threads included in a process are responsible for processing different businesses of an application. For example, a process created for the application A includes three threads, one thread is responsible for rendering a page, another thread is responsible for processing a user's input operation, and another thread is responsible for communicating with a server to load content of the page.

[0042] The heap memory is used to provide storage space for a process. As described above, multiple threads included in the process share the memory resource of the process. The electronic device can divide multiple memory regions in the heap memory for storage by threads included in the process. According to the size of the memory region, the divided memory regions can be divided into: a normal object storage area and other object storage areas. The normal object storage area is used to store normal objects, and the other object storage areas are used to store large objects, etc. The normal object storage area can include: a thread local allocation buffer (TLAB) and a bump pointer buffer area (or referred to as a bump pointer space). Normal objects can be stored in the thread local allocation buffer or the bump pointer buffer area, wherein, in the case that the electronic device uses a cross-correlation-based algorithm (CC algorithm), the TLAB will be used to store normal objects. In the case that the electronic device uses a concurrent mark compact algorithm (CMC algorithm), the electronic device will use the bump pointer buffer area to store normal objects.

[0043] In the embodiments of the present application, the electronic device mainly uses the TLAB to store normal objects as an example. Wherein, one thread can correspond to one TLAB.

[0044] The other object storage area can include a large object buffer area and a non-moving buffer area (or referred to as a non-moving space). Wherein, each process corresponds to one large object buffer area, and multiple threads included in the process can share the large object buffer area.

[0045] Wherein, the objects created by the thread can be divided into large objects (or referred to as large object objects) and normal objects (or referred to as region space objects). Wherein, the storage space required by the large object is larger, and the storage space required by the normal object is smaller. The large object is generally stored in the large object buffer area.

[0046] An example, in the case that the electronic device detects that the object to be stored is a normal object, the electronic device can store the object in the TLAB. Specifically, in the case that 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. In the case that 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 a TLAB with larger storage space in the heap memory for storing normal objects.

[0047] 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 the large object buffer.

[0048] Figure 1 A schematic diagram of the structure of a heap memory 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, which in turn includes multiple thread-locally allocated buffers, such as thread-locally allocated buffer 1, thread-locally allocated buffer 2, and thread-locally allocated 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 one thread-locally allocated buffer. For example, thread 1 corresponds to thread-locally allocated buffer 1 and a large object buffer, thread 2 corresponds to thread-locally allocated buffer 2 and a large object buffer, and thread 3 corresponds to thread-locally allocated buffer 3 and a large object buffer. These memory areas are used to store objects created by each thread. The specific storage method and rules are described above and will not be repeated here.

[0049] 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.

[0050] The method of this application is described in detail below.

[0051] When electronic devices are running, processes frequently access memory. If excessive memory usage occurs, processes may not be able to access memory in a timely manner, causing the electronic device to experience lags and performance degradation. Therefore, electronic devices need to monitor memory usage during operation and optimize memory usage if high memory usage is detected.

[0052] However, in related technologies, electronic devices can only count memory usage at the process level, not at the thread level. For example, common memory statistics methods such as dumpsys, eminfo, and showmap only count memory usage at the process level, and cannot further break it down to the thread level. In other words, it's impossible to specifically distinguish the memory usage of each thread.

[0053] For example, when abnormal memory growth occurs, since the memory changes 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 cannot locate the specific thread and determine the business code that caused the abnormal memory growth.

[0054] For 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 the electronic device can provide various system services for the applications of the electronic device, such as a service for managing the life cycle of an application program (ActivityManagerService, AMS), a service for managing the update of an application program (PackageManagerService, PMS), a service for managing a window view (WindowManagerService, WMS), and a service for managing a network state (NetWorkStatServices), and the like. The electronic device provides the corresponding system service for the application by executing the thread corresponding to the system service.

[0055] During the running of the application program of the electronic device, the application program can frequently call the system service of the system service process through an interface (such as a binder interface), which can cause the system service process to frequently access the memory.

[0056] In order to prevent the system service process from occupying a large amount of memory, the electronic device needs to monitor the occupation of the memory and optimize it. However, in the related art, only the occupation of the memory by the system service process can be determined, and the occupation of the memory by each thread in the system service process cannot be determined.

[0057] Therefore, in the case of a problem such as a large amount of memory occupation or abnormal memory growth, the specific cause of the abnormality cannot be accurately located or effectively analyzed.

[0058] Therefore, the present application provides an information acquisition method, which can statistically determine the occupation of the memory from the thread dimension, so as to accurately locate the cause of the abnormality based on the data statistically determined from the thread dimension when an abnormality occurs, and effectively analyze the abnormality. For example, after locating the thread causing the memory abnormality problem based on the data statistically determined from the thread dimension, the specific business code corresponding to the thread can be determined according to the business relationship, and the cause of the abnormality can be accurately located. Then, the business code can be adjusted to accurately and efficiently solve the memory abnormality problem.

[0059] Specifically, the electronic device can obtain the memory occupation amount corresponding to the first thread at a first time. After a preset time period, the electronic device can again obtain the memory occupation amount corresponding to the first thread at a second time. The electronic device calculates the difference between the memory occupation amount at the first time and the memory occupation amount at the second time, and obtains the memory occupation amount of the first thread within the preset time period.

[0060] In the embodiments of the present application, the memory usage obtained by the electronic device can be the memory usage of at least one of the first memory buffer, the second memory buffer, the collision pointer buffer, and the non-moving buffer. In the embodiments of the present application, the memory usage obtained by the electronic device is taken as the memory usage of the first memory buffer and / or the second memory buffer as an example for description.

[0061] It can be understood that, by the above method, the electronic device determines the memory usage of the memory corresponding to the first thread by obtaining the memory usage of the first thread at the first time and the memory usage of the first thread at the second time, thereby monitoring the memory usage in the thread dimension.

[0062] For example, the electronic device can 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, etc.), a palm computer, an augmented reality (AR) / virtual reality (VR) device. Alternatively, the electronic device can also be a portable multimedia player (PMP), a media player, or other types of electronic devices. The embodiments of the present application do not limit the specific type of the electronic device.

[0063] Figure 2 A structural schematic diagram of an electronic device provided in the embodiments of the present application.

[0064] The electronic device 200 can include a processor 210, an external memory interface 220, an internal memory 221, a universal serial bus (USB) interface 230, a charge 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, and the like.

[0065] It can be understood that the structure illustrated in the embodiments 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 can include more or fewer components than illustrated, or combine certain components, or split certain components, or different arrangement of components. The illustrated components can be implemented in hardware, software, or a combination of software and hardware.

[0066] The processor 210 can include one or more processing units, for example: the processor 210 can include an application processor (AP), a modem processor, a graphics processing unit (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. Among them, different processing units can be independent devices, or can be integrated in one or more processors.

[0067] In the embodiments of the present application, the processor 210 is configured to perform various functions or steps in the method embodiments of the present application.

[0068] The controller can be the nerve center and command center of the electronic device 200. The controller can generate operation control signals according to instruction operation codes and timing signals, and complete the control of fetching instructions and executing instructions.

[0069] The processor 210 can 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 can save instructions or data that the processor 210 has just used or repeatedly uses. If the processor 210 needs to use the instructions or data again, it can directly call from the memory. Avoiding repeated access, reducing the waiting time of the processor 210, thus improving the efficiency of the system.

[0070] In some embodiments, the processor 210 can include one or more interfaces. The interfaces can 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.

[0071] 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 can include multiple sets of I2C buses. The processor 210 can be coupled to the touch sensor 280K, the charger, the flash, the camera 1-N 293, etc. through different I2C bus interfaces, respectively. For example, the processor 210 can be coupled to the touch sensor 280K through an I2C interface, so that the processor 210 and the touch sensor 280K communicate through the I2C bus interface, and the touch function of the electronic device 200 is realized.

[0072] The wireless communication function of the electronic device 200 can be realized through the antenna 1, the antenna 2, the mobile communication module 250, the wireless communication module 260, the modem processor, and the baseband processor, etc.

[0073] The antenna 1 and the 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 multiplexed to improve the utilization rate of the antennas. For example, the antenna 1 can be multiplexed as a diversity antenna of a wireless local area network. In some other embodiments, the antennas can be used in combination with tuning switches.

[0074] The display screen 1^N 294 is configured to display images, videos, and the like. 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 (AMOLED), a flex light-emitting diode (FLED), a Miniled, a MicroLed, a Micro-oLed, a quantum dot light emitting diodes (QLED), or the like. In some embodiments, the electronic device 200 can include one or N display screens 294, where N is a positive integer greater than 1.

[0075] A video codec is configured to compress or decompress digital videos. The electronic device 200 can support one or more video codecs. In this way, the electronic device 200 can play or record videos in multiple encoding formats, such as moving picture experts group (MPEG) 1, MPEG 2, MPEG 3, MPEG 4, and the like.

[0076] An NPU is a neural-network (NN) computing processor. By drawing on the structure of a biological neural network, such as the transmission mode between human brain neurons, the NPU can quickly process input information and can also constantly self-learn. Through the NPU, the electronic device 200 can implement intelligent cognitive applications, such as image recognition, face recognition, voice recognition, text understanding, and the like.

[0077] The external memory interface 220 can be configured 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. For example, music, video, and the like files can be saved in the external memory card.

[0078] 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 can 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 can 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.

[0079] In an embodiment of the present application, the internal memory 221 includes memory resources allocated to each process, such as a heap memory included in the internal memory 221, 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 first memory usage information and second memory usage information corresponding to each thread. The first memory usage information and 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 and will not be repeated here.

[0080] Figure 3 A schematic diagram of the software system architecture of an electronic device provided in an embodiment of the present application.

[0081] The software system of the electronic device can adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a micro-service architecture, or a cloud architecture. In the embodiment of the present invention, the Android system with a layered architecture is used as an example to illustrate the software structure of the electronic device.

[0082] A layered architecture divides software into several layers, each with distinct roles and responsibilities. Layers communicate with each other via software interfaces. In some embodiments, the Android system is divided into three layers: the application layer, the application framework layer, and the virtual machine layer, from top to bottom.

[0083] The application layer can include a series of application packages.

[0084] like Figure 3 As shown, the application layer may include a thread memory monitoring module. The application layer may also include system applications and third-party applications. It should be understood that system applications may include camera, gallery, calendar, call, map, navigation, Bluetooth, music, video, short message, and other applications, which are not shown in the figure.

[0085] The thread memory monitoring module is configured to send a request to the virtual machine layer to monitor the memory occupation of the thread. The memory occupation of the thread is determined according to the memory usage information returned by the virtual machine layer at different times, so as to realize the monitoring of the memory occupation of the thread.

[0086] The system application or the third-party application in the application layer can call any manager or any provider or any system in the application program framework layer to realize the function of the application.

[0087] The application program framework layer provides an application programming interface (API) and a programming framework for the application program of the application layer. The application program framework layer includes some pre-defined functions.

[0088] As shown in FIG. 1, the application program framework layer can include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like, which are not all shown in the figure. These modules are system modules, and are configured to provide corresponding functions or services for the application program in the application layer. Figure 3

[0089] The window manager is configured to manage the window program. The content provider is configured to store and obtain data, and enable the data to be accessed by the application program. The view system is configured to build the application program. The phone manager is configured to provide the communication function of the electronic device. The resource manager is configured to provide various resources for the application program. The notification manager is configured to enable the application program to display notification information in the status bar, and can be used to convey the message of the notification type, which can automatically disappear after a short stay without user interaction.

[0090] The virtual machine layer includes an obtaining memory usage information module, a thread running module, a memory storage module, a garbage collection module, a thread list management module, and a thread management module.

[0091] The obtaining memory usage information module is configured to receive the request of the thread memory monitoring module, and obtain the memory usage information of the thread at the current time, such as the first memory usage information described below, or the second memory usage information described below.

[0092] The thread running module is configured to run the thread. The thread running module is further configured to, in the case that the thread creates a new object, store the object into the corresponding memory buffer through the memory storage module. The thread running module can also be configured to record the memory occupation in the process of storing the object, such as the actual memory occupation of the released TLAB, or the memory occupation of the normal object or the large object. These information can be recorded in the memory usage information described above.

[0093] ​The memory storage module is configured to store the objects created by the threads into corresponding buffer areas.

[0094] The garbage collection module is configured to automatically collect the objects in the heap memory whose usage frequency is less than or equal to a preset frequency threshold. The thread running module is further configured to record the actual memory usage of the TLABs released in the garbage collection process.

[0095] The thread list management module is configured to manage the threads belonging to the thread list, so as to support the garbage collection module to perform the collection operation.

[0096] The thread management module is configured to execute the registered flip functions of the threads and resume the suspended threads, so as to support the garbage collection module to perform the collection operation.

[0097] The information acquisition method of the present application will be described in detail below with reference to the accompanying drawings.

[0098] In the embodiments of the present application, the electronic device can maintain the first memory usage information and the second memory usage information corresponding to each thread created. In some other embodiments, the electronic device can also maintain the first memory usage information and the second memory usage information corresponding to specific threads. The specific threads can be preset. For example, the developer can set the threads that are likely to cause memory exceptions as the threads for which the memory information needs to be maintained, in combination with the test data of the electronic device.

[0099] The first memory usage information and the second memory usage information maintained for different threads are used to monitor the memory usage of the threads.

[0100] It can be understood that the threads may occupy or release memory continuously during the running process. Therefore, the first memory usage information and the second memory usage information maintained for the same thread at different time points are generally different. The electronic device can acquire the first memory usage information and / or the second memory usage information maintained for any one of the created threads at different time points during the running process. Based on the acquired memory usage information, the memory usage of the thread is determined.

[0101] In the present embodiment, the second memory usage information can be represented by RegionAllocateBytes. The first memory usage information can be represented by AllAllocateBytes.

[0102] As described above, each thread has a corresponding first memory buffer area, such as TLAB. In the case where there is a large object to be stored in the thread, the thread can also be stored in the corresponding second memory buffer area.

[0103] The first memory usage information maintained for the thread is used to record the memory occupancy of the normal objects in the first memory buffer corresponding to the thread and the memory occupancy of the 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 the normal objects in the first memory buffer corresponding to the thread.

[0104] The following will be described in detail with an example of a specific thread.

[0105] The following will be described in detail with an example of a specific thread.

[0106] As described above, the first memory usage information maintained for the thread is used to record the memory occupancy of the normal objects in the first memory buffer corresponding to the thread and the memory occupancy of the large objects in the second memory buffer corresponding to the thread.

[0107] That is, the first memory usage information is used to count the actual memory occupancy of the normal objects and the large objects. Since the large objects require a large storage space, more computing power is required in the process of determining the storage space required by the large objects. Therefore, in order to save computing power, the electronic device can count the actual memory occupancy of the normal objects and the large objects in a certain period of time, and not count in other time.

[0108] Based on this, the first memory usage information maintained for the thread is specifically used to count the memory occupancy of the normal objects when storing the normal objects created by the thread and the memory occupancy of the large objects when storing the large objects created by the thread in a period of time. Here, the "first memory buffer corresponding to the thread" refers to the first memory buffer currently used by the thread.

[0109] For example, the electronic device can store the normal objects created by the thread into the first memory buffer currently used by the thread. When the electronic device stores the normal objects, if it is within the aforementioned period of time, the electronic device can record the memory occupancy of the normal objects in the first memory usage information.

[0110] Similarly, the electronic device can store the large objects created by the thread into the second memory buffer corresponding to the thread. When the electronic device stores the large objects, if it is within the aforementioned period of time, the electronic device can record the memory occupancy of the large objects in the first memory usage information.

[0111] The state value can have a first value and a second value. For example, the first value can be true and the second value can be false. When the state value is true, the electronic device can be considered to be in the time period. When the state value is false, the electronic device can be considered not to be in the time period. The electronic device can clear the maintained first memory usage information when the state value is set to the second value, for example, false. Alternatively, the electronic device can clear the maintained first memory usage information when the state value is set to the first value, for example, true. Therefore, the initial value of the first memory usage information before the first memory usage information is maintained can be 0.

[0112] The second memory usage information maintained by the electronic device for the thread is described in detail below with an example.

[0113] 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 herein includes a first memory buffer that has been allocated to the thread historically but has been released. The memory usage of the first memory buffer can refer to the actual usage of the first memory buffer by an object created by the thread.

[0114] The second memory usage information can specifically record the memory usage of each of the released first memory buffers. Alternatively, the second memory usage information can specifically record the sum of the memory usage of all the released first memory buffers.

[0115] As an example, the memory usage of the first memory buffer that has been allocated to the thread historically 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.

[0116] For example, in the initial case, the second memory usage information records a memory occupancy of 0. In the case that the thread needs to store a normal object, the electronic device allocates a first memory buffer for the thread, for example, a first memory buffer 1, with a size of 10 bytes. Then, the electronic device stores the normal object created by the thread in the first memory buffer 1. For example, after a certain period of time, for example, at time 1, the first memory buffer 1 stores 2 normal objects, for example, object 1 and object 2, and is occupied by 7 bytes.

[0117] Then, at time 2, the thread creates a new normal object, for example, object 3, which requires a storage space of 7 bytes. Since the remaining space of the first memory buffer 1 cannot store the object 3, the electronic device can create a new first memory buffer, for example, a first memory buffer 2, with a memory size of 20 bytes. In this case, the electronic device releases the first memory buffer 1 and stores the two normal objects in the first memory buffer 1 and the newly created normal object in the first memory buffer 2, that is, stores object 1, object 2 and object 3 in the first memory buffer 2.

[0118] In the embodiment, the electronic device can obtain the memory occupancy of the released first memory buffer 1, for example, the actual occupancy of the first memory buffer 1 by object 1 and object 2, that is, 7 bytes, when the first memory buffer 1 is released. According to the obtained memory occupancy 7 bytes of the released first memory buffer 1, the electronic device determines that the memory occupancy of the released first memory buffer is 7 bytes and records it. Similarly, if there is a normal object of the thread that needs to be stored, but the remaining storage space of the first memory buffer currently used by the thread, for example, the first memory buffer 2, is insufficient, the electronic device can create a new first memory buffer, for example, a first memory buffer 3, with a larger memory, to store the normal object of the thread and release the first memory buffer 2. Then, the electronic device records the memory occupancy of the released first memory buffer when the first memory buffer 2 is released. In turn, the electronic device can record the memory occupancy of all allocated but released first memory buffers in the second memory usage information during the running of the thread.

[0119] As described above, the electronic device can obtain the first memory usage information and / or the second memory usage information maintained at different time instants during the running of any one of the created threads. Based on the obtained memory usage information, the memory occupation 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 time instants, and determine the memory occupation of the thread in a preset time period by comparing the first memory usage information obtained at the two time instants. Similarly, the electronic device can obtain the second memory usage information at two different time instants and the memory occupation of the first memory buffer currently used by the thread at the corresponding time instants, and determine the memory occupation of the thread in a preset time period by comparing the data obtained at the two time instants.

[0120] As to whether the electronic device determines the memory occupation of 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 requirements of the actual application scenario. The present embodiment does not make specific limitations here.

[0121] In some embodiments, after the electronic device receives the first request for obtaining the memory occupation of the first thread, the size of the object created by the first thread is recorded in the first memory usage information in response to the first thread creating the object, until the first request is received again, and the recording of the size of the object created by the first thread in the first memory usage information is stopped. The size of all the objects recorded in the first memory usage information is used to indicate the memory occupation of the first thread.

[0122] It can be understood that after the electronic device receives the first request for the first time, the electronic device can obtain the first memory usage information at the time when the first request is received for the first time, and after the electronic device receives the first request again, the electronic device can obtain the first memory usage information at the time when the first request is received again. By comparing the first memory usage information at the two different time instants, the memory occupation of the thread between the two received first requests is determined.

[0123] In some embodiments, the electronic device receives a second request for obtaining the memory usage of the first thread, and obtains a first usage value recorded in the second memory usage information and a second usage value, the second usage value being the memory usage of the normal objects stored in the first memory buffer for storing the normal objects created by the first thread at the time when the second request is received. The electronic device receives the second request again, and obtains a third usage value recorded in the second memory usage information and a fourth usage value, the fourth usage value being the memory usage of the normal objects stored in the first memory buffer for storing the normal objects created by the first thread at the time when the second request is received again. The electronic device determines the memory usage of the first thread according to the first usage value, the second usage value, the third usage value and the fourth usage value. The second memory usage information is used to record the memory usage of the objects stored in the first memory buffer for storing the normal objects created by the first thread which are released.

[0124] The first usage value is the second memory usage information recorded by the electronic device at the time when the second request is received for the first time. The second usage value is the memory usage of the first memory buffer currently used by the thread at the time when the second request is received for the first time. Similarly, the third usage value is the second memory usage information recorded by the electronic device at the time when the second request is received for the second time. The fourth usage value is the memory usage of the first memory buffer currently used by the thread at the time when the second request is received for the second time. The electronic device can determine the memory usage of the thread between the two times when the second request is received by comparing the first usage value and the second usage value at the time when the second request is received for the first time, and the third usage value and the fourth usage value at the time when the second request is received for the second time.

[0125] The embodiments are described in detail below with reference to the drawings. Figure 4 and Figure 5 The embodiments are described in detail below with reference to the drawings.

[0126] As shown in Figure 4 , the specific steps of determining the memory usage of the thread by the electronic device based on the first memory usage information or the second memory usage information are as follows.

[0127] Step 401: The electronic device obtains the memory statistical requirement for the thread.

[0128] In the embodiments of the present application, the electronic device can obtain the memory statistical result of the thread corresponding to the requirement at the current time through the memory statistical requirement. The electronic device can obtain the memory statistical requirement through the received first request or second request.

[0129] The electronic device can also determine whether to count the memory usage of the normal objects corresponding to the thread or the memory usage of the normal objects and the large objects corresponding to the thread according to the memory counting requirement. The electronic device can determine the memory usage of the normal objects corresponding to the thread through the second memory usage information. The electronic device can also determine the memory usage of the normal objects and the large objects corresponding to the thread through the first memory usage information.

[0130] Step 402: The electronic device determines whether to count only the memory usage of the normal objects corresponding to the thread based on the memory counting requirement.

[0131] In the embodiments of the present application, the electronic device performs step 403 in the case of counting only the memory usage of the normal objects corresponding to the thread, and performs step 406 in the case of not counting only the memory usage of the normal objects corresponding to the thread.

[0132] Step 403: The electronic device obtains second memory usage information.

[0133] Step 404: The electronic device obtains the actual memory usage of the first memory buffer currently used by the thread.

[0134] Step 405: The electronic device takes the sum of the actual memory usage of the first memory buffer currently used by the thread and the second memory usage information as the memory counting result.

[0135] In the embodiments of the present application, after the electronic device performs step 405, step 409 can be performed in response to the memory counting requirement. The electronic device determines the memory usage of the thread by obtaining the memory counting results of two time points. After the electronic device obtains the memory usage of the normal objects for the first time, the electronic device can continue to perform step 401 to obtain the memory usage of the normal objects again to determine the memory usage of the thread within a preset time period.

[0136] Step 406: The electronic device determines whether to apply for obtaining the memory usage of the normal objects and the large objects for the first time.

[0137] In the embodiments of the present application, the electronic device determines the memory usage of the thread by obtaining the memory counting results of two time points. That is, the electronic device needs to obtain the memory usage of the normal objects and the large objects of the thread twice.

[0138] In the embodiments of the present application, the electronic device performs step 407 in the case of applying for obtaining the memory usage of the normal objects and the large objects for the first time, and performs step 408 in the case of not applying for obtaining the memory usage of the normal objects and the large objects for the first time.

[0139] 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.

[0140] 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.

[0141] 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 step 406, it will execute steps 408 and 409.

[0142] 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.

[0143] In an embodiment of the present application, the first memory usage information counted in step 408 is the memory usage of common objects and large objects stored in the thread counted when the electronic device is at the first value.

[0144] Step 409: Return the memory statistics result.

[0145] like Figure 5 As shown, combined with Figure 3 The software module of the electronic device in the present invention describes in detail the process of obtaining the memory statistics results of the thread.

[0146] Step 501: The thread memory monitoring module generates memory statistics requirements for the thread.

[0147] The thread memory monitoring module generates thread memory statistics for specific threads or when abnormal memory growth is detected. The memory statistics are used to obtain the memory usage of the thread.

[0148] Step 502: The memory usage information acquisition module determines whether the thread object requiring memory statistics is a common object according to the memory statistics requirement.

[0149] If the thread object is a common object, step 503 is executed.

[0150] When the thread object is a common object or a large object, step 505 is executed.

[0151] The thread indicated by the memory statistical requirement can include a normal object or a normal object and a large object. The memory usage information obtaining module performs different obtaining operations according to the memory statistical requirement of different threads, and feeds back the obtained memory statistical result to the thread memory monitoring module. The following will describe the two cases respectively.

[0152] In one aspect, for the case that the thread object is a normal object, the following process is mainly included:

[0153] Step 503: The memory usage information obtaining module obtains the second memory usage information at the current time and the current memory occupation of the first memory buffer currently used by the thread.

[0154] Specifically, the memory usage information obtaining module can call a related function through a local interface to obtain the second memory usage information at the current time and the current memory occupation of the first memory buffer currently used. The local interface can be Java Native Interface, also known as Java Native Interface. The called function can include the VMRuntime_getBytesAllocated() function or other functions, which are not limited.

[0155] Step 504: The memory usage information obtaining module sends the current memory statistical result to the thread memory monitoring module; the current memory statistical result includes the second memory usage information at the current time and the current memory occupation of the first memory buffer currently used by the thread.

[0156] The memory usage information obtaining module can directly feed back the second memory usage information at the current time and the current memory occupation of the first memory buffer currently used by the thread to the thread memory monitoring module as the current memory statistical result.

[0157] Alternatively, the memory usage information obtaining module can also calculate the sum of the second memory usage information at the current time and the current memory occupation of the first memory buffer currently used by the thread, and feed back the sum to the thread memory monitoring module as the current memory statistical result.

[0158] On the other hand, for the case that the thread object is a normal object and a large object, the following process is mainly executed:

[0159] Step 505: The memory usage information obtaining module judges whether the memory statistical requirement is the first statistical requirement for the normal object and the large object.

[0160] In the case that the memory statistical requirement is the first statistical requirement for the normal object and the large object, step 506 is executed.

[0161] If the memory statistics requirement is not the first statistics requirement for the normal objects and the large objects, step 508 is performed.

[0162] The memory usage information obtaining module performs different operations according to whether the memory statistics requirement is the first statistics requirement for the normal objects and the large objects. The following will describe the two cases respectively.

[0163] First, if the memory statistics requirement is the first statistics requirement for the normal objects and the large objects, the following process is mainly performed:

[0164] Step 506: The memory usage information obtaining module sets the state value to a first value, and sets the first memory usage information to zero.

[0165] If the memory statistics requirement is the first statistics requirement for the normal objects and the large objects, the memory usage information obtaining module first sets the state value to the first value, so that the thread running module records the memory occupation in the process of storing the objects in the state indicated by the first value.

[0166] In addition, if the memory statistics requirement is the first statistics requirement for the normal objects and the large objects, the thread running module has not started to record the memory occupation. In this case, the memory usage information obtaining module can first set an initial value to the first memory usage information. For example, the initial value is zero.

[0167] Step 507: The memory usage information obtaining 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.

[0168] For the memory statistics requirement initiated by the thread memory monitoring module, the memory usage information obtaining module feeds back the first memory usage information set to zero as the current memory statistics result to the thread memory monitoring module.

[0169] Second, if the memory statistics requirement is not the first statistics requirement for the normal objects and the large objects, the following process is mainly performed:

[0170] Step 508: The memory usage information obtaining module sets the state value to a second value, and obtains the first memory usage information.

[0171] Step 507: The memory usage information obtaining 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.

[0172] In the case of not being the first time to count the demand, the memory running module has recorded the memory occupation of the thread, that is, the first memory usage information. The memory usage information obtaining module can feed back the current first memory usage information as the current memory counting result to the thread memory monitoring module.

[0173] It should be noted that the thread memory monitoring module does not execute the actions of steps 502 to 508 again after generating the memory counting demand. The thread memory monitoring module selects to execute the memory usage information obtaining scheme for the normal object or selects to execute the memory usage information obtaining scheme for the normal object and the large object according to the thread object indicated by the memory counting demand. That is, after the thread memory monitoring module generates a memory counting demand, the memory usage information obtaining module feeds back a memory counting result matched with the memory counting demand.

[0174] After obtaining the current memory counting result, the thread memory monitoring module further executes the following steps:

[0175] Step 509: The thread memory monitoring module determines whether there is an abnormal memory occupation according to the difference between the current memory counting result and the last memory counting result.

[0176] Step 510: In the case of the abnormal memory occupation, an abnormal event is reported. The abnormal event is used to indicate the thread with the abnormal occupation.

[0177] The thread memory monitoring module generates a memory counting demand each time and obtains the current memory counting result by sending the memory counting demand to the memory usage information obtaining module. The thread memory monitoring module obtains the memory occupation in the period from the last counting time to the current counting time by subtracting the current memory counting result from the last memory counting result.

[0178] The critical condition of the abnormal memory occupation is associated with a memory usage threshold. If the difference between the current memory counting result and the last memory counting result is greater than the usage threshold, that is, the critical condition of the abnormal memory occupation occurs. In this case, the thread memory monitoring module reports an abnormal event. Other modules of the electronic device can output the abnormal event to prompt the maintenance personnel of the electronic device to handle the abnormal memory occupation.

[0179] In an example, in the case of the memory counting demand indicating to count the thread as a normal object, the thread memory monitoring module executes steps 509-510.

[0180] In another example, the memory statistics requirement indicates the normal objects and the large objects of a thread of the statistics. For this case, the thread memory monitoring module usually generates memory statistics requirement twice, i.e., the first time and the second time. The first time, the first memory usage information is zero, and the second time, the first memory usage information is the real-time recorded memory occupation.

[0181] Even if the thread memory monitoring module makes a difference between the second time and the first time, the difference is actually the first memory usage information of the second time. Based on this, the thread memory monitoring module can compare the memory statistics result of the second time memory statistics requirement with the preset usage threshold after generating the second time memory statistics requirement for the thread. The thread memory monitoring module reports an abnormal event in the case that the memory statistics result of the second time memory statistics requirement is greater than the preset usage threshold.

[0182] In some embodiments, the memory storage module can store the objects included in the thread, and the thread running module records the memory occupation of the objects included in the thread. That is, the electronic device records the memory occupation of the objects according to the stored objects of the thread. The following mainly explains the specific implementation of the electronic device storing the objects and recording the memory occupation from the perspective of the electronic device.

[0183] In an example, the electronic device stores the normal objects, and the electronic device records the memory occupation of the normal objects.

[0184] Specifically, the electronic device first verifies the class of the to-be-stored object to determine the type of the to-be-stored object. In the case that the to-be-stored object is a normal 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 normal object, the electronic device can directly store the to-be-stored normal object into the first memory buffer. In the case that the state value of the thread is the first value, the electronic device records the memory occupation of the normal object to the first memory usage information. This process can also be called fast path storage.

[0185] In the case that the to-be-stored object is a normal object, but the remaining storage space of the first memory buffer currently corresponding to the thread is less than or equal to the memory required to be occupied by the normal object, the electronic device releases the first memory buffer currently corresponding to the thread and re-creates a new first memory buffer, and stores the normal object into the new first memory buffer. In the case that the state value of the thread is the first value, the electronic device records the memory occupation of the normal object to the first memory usage information. This process can be called slow path storage. Moreover, the electronic device records the actual occupation of the released first memory buffer to the second memory usage information.

[0186] In an example, the electronic device stores a large object, and records the memory usage of the large object. In a case where the state value of the thread is the first value, the actual memory usage of the large object is recorded into the first memory usage information. This process can also be referred to as a slow path storage.

[0187] The storage scheme and the recording scheme performed by the electronic device are explained in detail below. Figures 6 to 8 The specific steps are as follows: Figure 6

[0188] Step 601: The electronic device generates an object creation requirement, which is used to indicate the creation of an object for a thread.

[0189] In some embodiments, the electronic device generates an object creation requirement to indicate the creation of an object for a thread during the running of the thread. The object that the electronic device needs to create for the thread can be a normal object or a large object.

[0190] Step 602: The electronic device determines whether to execute the object creation operation through an interpreter.

[0191] In a case where it is determined not to execute the object creation operation through the interpreter, step 603 is performed.

[0192] In a case where it is determined to execute the object creation operation through the interpreter, step 605 is performed.

[0193] The interpreter is a computer program used to read and execute source code.

[0194] Step 603: The electronic device obtains the machine language corresponding to the source code used to create the object.

[0195] Step 604: The electronic device obtains the function used to store the object in the machine language.

[0196] The electronic device can obtain the machine language by calling visitnewinstance(). During the process of creating the object, the electronic device stores the created object in the corresponding buffer.

[0197] In a specific implementation, the electronic device can determine the function used to store the object by using the GetEntrypoint() function.

[0198] Step 605: The electronic device compiles the source code used to create the object through the interpreter.

[0199] The electronic device can compile the source code by using NterpAllocateObject() to create the object and store the created object.

[0200] ​Step 606: The electronic device determines the class of the to-be-created to-be-stored object.

[0201] In embodiments of the present application, the class of an object is used to determine the type of the object, such as the type of the object including a basic data type, a reference type, and a binary type, etc.

[0202] Step 607: The electronic device determines whether the required memory occupancy is fixed according to the class of the object.

[0203] In the case where the required memory occupancy is fixed, step 608 is executed, and the fast path storage is taken.

[0204] In the case where the required memory occupancy is not fixed, the slow path storage is taken. The slow path storage is described in the following Figure 7 related examples.

[0205] In embodiments of the present application, the required memory occupancy of different types of objects is different.

[0206] In one case, for a normal object, the required memory occupancy is fixed. Or, in the case where the object is a basic data type object or a reference type object, the required memory occupancy of the basic data type object or the reference type object is fixed.

[0207] In another case, for a large object, the required memory occupancy is not fixed. Or, in the case where the object is a binary type object, the required memory occupancy of the binary type object is not fixed.

[0208] In the case where the required memory occupancy is fixed, the object is a normal object, and step 608 is executed. In the case where the required memory occupancy is not fixed, the object is a large object, and the following Figure 7 , the electronic device takes the slow path storage.

[0209] Step 608: The electronic device determines whether the required memory occupancy of the object is less than or equal to the remaining memory of the first memory buffer.

[0210] In the case where the required memory occupancy of 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 takes the fast path storage and directly stores the object into the first memory buffer.

[0211] In the case where the required memory occupancy of the object is greater than the remaining memory of the first memory buffer, the electronic device can also take the slow path storage. The slow path storage is described in the following Figure 7 related examples.

[0212] Step 609: The electronic device stores the object into the first memory buffer.

[0213] Step 610: The electronic device initializes object information of the object.

[0214] The object information includes object status, attribute value, construction method, etc. When creating the object, the electronic device initializes the object information of the object, i.e., the electronic device newly adds relevant parameters of the object information to the object, or assigns the relevant parameters of the object information to a general initial value, etc., without limitation.

[0215] Step 611: The electronic device determines whether the current state value of the thread is the first value.

[0216] In the embodiments of the present application, step 612 is performed when the current state value of the thread is the first value, and step 613 is performed when the current state of the electronic device is not the first value.

[0217] Step 612: The electronic device records the actual memory occupancy of the object in the first memory usage information.

[0218] When the current state value is the first value, the electronic device can perform the recording operation of the memory occupancy. For example, the memory running module of the electronic device records the memory occupancy.

[0219] Step 613: The electronic device returns the object storage result.

[0220] In the embodiments of the present application, the electronic device returns the object storage result. The storage result includes two kinds of storage success and storage failure.

[0221] The above steps 609 to 613 correspond to the flow of the electronic device performing the fast path storage. Figure 7 The flow of the electronic device performing the slow path storage is as follows.

[0222] Step 701: The electronic device selects a memory storage matching the type of the object.

[0223] 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 large object memory storage to store the large object. When the object is a normal object, the electronic device can select a normal object memory storage to store the normal object.

[0224] In the embodiments of the present application, the electronic device can call the object memory storage execution function AllocObjectWthAllocator() to select different memory storages according to the type of the object.

[0225] Step 702: The electronic device determines whether the object is a large object or a normal object.

[0226] In embodiments of the present application, if the electronic device determines that the object is a large object or a normal object, the electronic device performs step 705. If the electronic device does not determine whether the object is a large object or a normal object, the electronic device performs step 703.

[0227] Step 703: The electronic device determines whether the object is a large object.

[0228] In embodiments of the present application, if the electronic device determines that the object is a large object, step 704 is performed, and the electronic device determines that the object is a large object. If the electronic device determines that the object is not a large object, the object is a normal object, and step 705 is performed. The electronic device can determine whether the object should be determined to be a large object by using the isLargeObject() function.

[0229] Step 704: The electronic device determines that the object is a large object.

[0230] In embodiments of the present application, the electronic device can set a value by using the AllocLargeObject() function to indicate that the electronic device determines that the current object is a large object, and the value can be true. The electronic device returns to perform step 701 to enable the electronic device to select a memory storage for large objects to store the large object.

[0231] Step 705: The electronic device determines whether the object can be directly stored in the first memory buffer by using the memory storage for normal objects.

[0232] In embodiments of the present application, if the remaining storage space of the first memory buffer currently corresponding to the thread is greater than or equal to the memory required by the normal object, the electronic device can directly store the object in the first memory buffer by using the memory storage for normal objects, and then perform step 706. If the remaining storage space of the first memory buffer currently corresponding to the thread is less than the memory required by the normal object, the electronic device cannot directly store the object in the first memory buffer by using the memory storage for normal objects, and performs step 708, which can be referred to in Figure 8 . Alternatively, if the object is a large object, the electronic device stores the object by using the memory storage for large objects, and cannot store the large object in the first memory buffer by using the memory storage for normal objects. The electronic device can also perform step 708, which can be referred to in Figure 8 .

[0233] Step 706: The electronic device stores the object in the first memory buffer.

[0234] Step 707: The electronic device determines whether the object is successfully stored.

[0235] In the embodiments of the present application, step 709 is performed when the electronic device successfully stores the object. Step 708 is performed when the electronic device fails to store the object.

[0236] Step 708: The electronic device attempts to store the object.

[0237] Step 709: The electronic device initializes the object information of the object.

[0238] Step 710: The electronic device determines whether the current state value of the thread is the first value.

[0239] In the embodiments of the present application, step 711 is performed when the current state value of the thread is the first value, and step 712 is performed when the current state of the electronic device is not the first value.

[0240] Step 711: The electronic device records the actual memory usage of the object in the first memory usage information.

[0241] When the current state value is the first value, the electronic device can perform the recording operation of the memory usage. For example, the memory running module of the electronic device records the memory usage.

[0242] Step 712: The electronic device returns the object storage result.

[0243] When the remaining storage space of the first memory buffer currently corresponding to the thread is less than the memory required by the normal object, or when the object is a large object and the electronic device cannot store the object into the first memory buffer using the memory storage of the normal object, the following steps are performed as shown in Figure 8

[0244] Step 801: The electronic device determines whether the memory storage required for storing the object is the memory storage matched with the normal object.

[0245] When the memory storage required for storing the object is the memory storage of the normal object, step 802 is performed.

[0246] Step 802: The electronic device creates a new first memory buffer and stores the object to be stored in the new first memory buffer.

[0247] When the memory storage required for storing the object is not the memory storage of the normal object, step 803 is performed.

[0248] The specific implementation scheme of the electronic device storing the object to be stored in the new first memory buffer is described in the related examples. Figure 9

[0249] ​​Step 803: The electronic device stores the large object into the second memory buffer using the memory of the large object.

[0250] It should be understood that after the electronic device stores the normal object into the corresponding memory buffer, the electronic device can also perform the steps of setting the state value and recording the object information, which will not be described herein.

[0251] As shown in FIG. 8, the specific process that the electronic device stores the object to be stored into the new memory buffer can refer to the following steps. Figure 9

[0252] Step 901: The electronic device extends the space of the first memory buffer, and judges whether the available memory of the extended first memory buffer is greater than or equal to the memory occupancy required by the object.

[0253] In the embodiment of the present application, the available memory allocated to the first memory buffer can be much greater than the actual used memory of 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 can only open a part of the memory of the first memory buffer to the right to use, and this part of the opened memory can be used for storage.

[0254] In the case that the opened memory region of the first memory buffer does not satisfy the memory occupancy required by the thread to be stored, the electronic device can open the remaining first memory buffer which has been allocated but not opened to the right to use, to store the new object.

[0255] For example, the electronic device actually allocates a storage space of 20 bytes to the first memory buffer, and the electronic device can only open 10 bytes of memory space of the first memory buffer to store the object. After the first memory buffer has used 9 bytes of space, the electronic device responds to the need to store an object of 3 bytes into the first memory buffer. Since the size of the memory of the first memory buffer opened by the electronic device cannot satisfy the current storage, the electronic device can extend the space of the first memory buffer, i.e., the electronic device opens another 10 bytes of storage space, so that the storage space of the first memory buffer becomes 20 bytes. In this case, the storage space of the extended first memory buffer is sufficient to store the object. But suppose the object to be stored occupies a memory size of 15 bytes, in which case the electronic device cannot store the object even if it extends the storage space of the first memory buffer.

[0256] In the embodiment of the present application, in the case that the available memory of the extended first memory buffer is greater than or equal to the memory occupancy required by the object, step 909 is performed. In the case that the available memory of the extended first memory buffer is less than the memory occupancy required by the object, step 902 is performed.

[0257] ​Step 902: The electronic device determines whether the type of the memory buffer to be created is the first memory buffer.

[0258] In embodiments of the present application, in addition to the first memory buffer and the second memory buffer, the heap memory of the electronic device can also be provided with a third memory buffer and a fourth memory buffer. The use conditions of different types of memory buffers are different. In embodiments of the present application, the third memory buffer can be a collision pointer buffer. The fourth memory buffer can be a non-moving buffer.

[0259] As described above, in the case where the electronic device uses a cross-correlation-based algorithm (CC algorithm), the first memory buffer is used to store the object. In the case where the electronic device uses a concurrent mark compact algorithm (CMC algorithm), the electronic device uses the third memory buffer to store the object.

[0260] In the case where the memory occupancy required by the object is greater than the preset memory threshold, or in the case where the memory required by the object exceeds the available memory of the electronic device, the fourth memory buffer is used. In the case where the memory occupancy 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.

[0261] In embodiments of the present application, in the case where the type of the memory buffer to be created by the electronic device is the first memory buffer, step 903 is performed. In the case where the type of the memory buffer to be created by the electronic device is not the first memory buffer, step 910 is performed.

[0262] Step 903: The electronic device determines whether the memory occupancy required by the object is greater than the preset memory threshold.

[0263] In embodiments of the present application, in the case where the memory occupancy required by the object is greater than the preset memory threshold, step 912 is performed, and in the case where the memory occupancy required by the object is less than or equal to the preset memory threshold, step 904 is performed.

[0264] Step 904: The electronic device determines whether the memory required by the object exceeds the available memory of the electronic device.

[0265] In embodiments 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. In the case where the memory required by the object exceeds the available memory of the electronic device, step 912 is performed, and in the case where the memory required by the object does not exceed the available memory of the electronic device, step 905 is performed.

[0266] Step 905: The electronic device calculates the available memory of the first memory buffer to be created.

[0267] In an embodiment of the present application, the electronic device determines the available memory of the first memory buffer required. For example, the first memory buffer is 20 bytes, in which 17 bytes of storage space are occupied, and the size of the object to be stored is 10 bytes. In this case, the newly created first memory buffer of the electronic device can be 30 bytes.

[0268] Step 906: The electronic device determines whether the new first memory buffer is successfully created.

[0269] In an embodiment of the present application, in the case where the electronic device successfully creates the new first memory buffer, step 907 is performed, and in the case where the electronic device does not successfully create the new first memory buffer, step 912 is performed.

[0270] Step 907: The electronic device stores the object in the memory buffer.

[0271] Step 908: The electronic device sets the object information of the stored object.

[0272] After the electronic device stores the object in the new first memory buffer, the electronic device can also perform operations such as setting the object information and setting the state value, which are not described herein.

[0273] Step 909: The electronic device extends the memory space of the first memory buffer.

[0274] In this embodiment, since the extended first memory buffer can directly store the object, the electronic device can directly store the object in the extended first memory buffer in the case where the available memory of the extended first memory buffer is greater than or equal to the memory occupancy required by the object.

[0275] Step 910: The electronic device calculates the available memory of the third memory buffer to be created.

[0276] In this embodiment, in the case where the algorithm executed by the electronic device is the CMC algorithm, the electronic device stores the object in the third memory buffer. In the case where the object is stored in the third memory buffer, the electronic device will create the third memory buffer, and according to the occupancy required by the object, determine the available memory of the third memory buffer according to the occupancy.

[0277] Step 911: The electronic device determines whether the new third memory buffer is successfully created.

[0278] In the embodiments of the present application, in the case that the electronic device successfully creates the new third memory buffer, step 907 is performed, and in the case that the electronic device fails to successfully create the new third memory buffer, the electronic device returns a result of object storage failure.

[0279] Step 912: The electronic device stores the object in the fourth memory buffer.

[0280] In the embodiments of the present application, in the case that the memory occupancy required by the object is greater than the preset memory threshold, or in the case that the memory required by the object exceeds the available memory of the electronic device, or in the case that the electronic device fails to successfully create the new first memory buffer, the electronic device can create the fourth memory buffer to store the object, and store the object in the fourth memory buffer. In the case of successful storage, step 907 is performed, and in the case of storage failure, the electronic device returns a result of object storage failure.

[0281] It can be understood that, in the embodiments 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.

[0282] The following will take an example to specifically describe how the electronic device counts the second memory usage information.

[0283] In some embodiments, during the running of the thread, the electronic device always counts the second memory usage information regardless of whether the electronic device is the first value. Among them, in the case that the current first memory buffer of the thread cannot store a new object, 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 in the case that the electronic device performs the garbage collection mechanism, the electronic device can record the actual memory occupancy of the first memory buffer released in the garbage collection process to the second memory usage information.

[0284] For example, in the case that the storage space of the first memory buffer is not enough, the process in which the electronic device stores the object can refer to the above Figure 8 and Figure 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 judge whether the current state value of the thread is the first value, and the electronic device directly records the actual occupied memory occupancy in the released first memory buffer.

[0285] In some embodiments, the electronic device can also record the actual memory usage of the first memory buffer that has been released in the process of garbage collection.

[0286] For details, please refer to Figure 10 The specific process is shown as follows.

[0287] Step 1001: The garbage collection module starts the garbage collection mechanism.

[0288] In the embodiments of the present application, the electronic device can start the garbage collection mechanism by the Heap::CollectGarbage() function.

[0289] Step 1002: The garbage collection module prepares to suspend all threads in the thread list management module.

[0290] In the embodiments of the present application, the electronic device suspends all threads before performing garbage collection, so as to correctly identify the garbage objects in all threads that need to be recycled. The garbage object is an object whose usage frequency is less than or equal to a preset frequency threshold. The electronic device can be initialized by the FlipThreadRoots() function and prepare to suspend all threads at present.

[0291] Step 1003: The garbage collection module determines the state of each thread.

[0292] Thread flipping can also be referred to as thread priority flipping. Thread priority flipping usually occurs in a multi-thread environment. When a high-priority thread is blocked by a low-priority thread, and the low-priority thread is delayed in releasing resources because it is preempted by other medium-priority threads, the real-time performance of the high-priority thread cannot be guaranteed.

[0293] In the embodiments of the present application, the electronic device determines the state of each thread to prevent thread flipping problems in the process of garbage collection. For example, the state of the thread can be used to optimize the scheduling of the thread, so as to control the behavior of the thread in the process of thread flipping. The behavior can be suspending the thread, resuming the thread, and the like. The electronic device can determine the state of each thread by the newThreadFlipVision() function, so as to control the behavior of the thread in the process of thread flipping.

[0294] Step 1004: The thread list management module suspends all threads at present.

[0295] In the embodiments of the present application, the electronic device can suspend all threads in the current list by the FlipThreadRoots() function.

[0296] Step 1005: The thread list management module registers the function to be called when the thread flips.

[0297] 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.

[0298] Step 1006: The thread list management module ensures that all registered flip functions have been initialized.

[0299] In an embodiment of the present application, the electronic device can ensure that all registered flip functions have been initialized through the EnsureFlipFunctionStarted() function.

[0300] Step 1007: The thread management module executes the registered flip function.

[0301] In an embodiment of the present application, the electronic device can execute the registered flip function through the RunFlipFunction() function.

[0302] Step 1008: The thread management module resumes the suspended thread and ends the garbage collection mechanism.

[0303] In an embodiment of the present application, the electronic device can resume the suspended thread through the ThreadFlipVision Run() function.

[0304] Step 1009: The thread management module cleans up the thread local resources.

[0305] In an embodiment of the present application, thread local resources refer to resources used by the electronic device during garbage collection that are closely related to the thread. These resources may include memory, file handles, database connections, etc. If these resources are not reclaimed in time when the thread ends, it may cause resource leakage, which in turn affects the stability and performance of the system. Therefore, the garbage collection mechanism cleans up the thread local resources after it ends, in order to ensure that these resources can be effectively managed and reclaimed, avoiding resource leakage problems. The electronic device can clean up the thread local resources through the RevokeThreadLocalBuffers() function.

[0306] Step 1010: The memory usage information acquisition module updates the second memory usage information.

[0307] In an embodiment of the present application, the electronic device can record the actual memory usage of the first memory buffer that has been released during garbage collection through the RevokeThreadLocalBuffersLocked() function.

[0308] AsFigure 11 The following will be described in detail in combination with the above Figure 3 software modules.

[0309] Step 1101: The thread memory monitoring module sends a memory statistics requirement to the memory usage information acquisition module.

[0310] Step 1102: In the case that the thread object to be counted in the memory statistics requirement is a normal object, the memory usage information acquisition module adds the second memory usage information to the actual occupied memory usage in the first memory buffer currently allocated to the thread by the electronic device, and takes the result as the memory statistics result. In the case that the thread object to be counted in the memory statistics requirement is a normal object and a large object, the memory usage information acquisition module sets the state value of the thread to a first value, sets the first memory usage information to an initial value, and takes the first memory usage information as the memory statistics result.

[0311] Step 1103: The thread normally runs, and in the case that a normal object or a large object needs to be stored, the normal object or the large object is stored through the memory storage module.

[0312] Step 1104: The memory storage module stores the normal object or the large object to the corresponding memory buffer.

[0313] Step 1105: The memory usage information acquisition module updates the first memory usage information or the second memory usage information according to the memory usage occupied by the stored normal object or large object.

[0314] Step 1106: The thread memory monitoring module sends a memory statistics requirement to the memory usage information acquisition module again.

[0315] Step 1107: In the case that the thread object to be counted in the memory statistics requirement is a normal object, the memory usage information acquisition module adds the second memory usage information to the actual occupied memory usage in the first memory buffer currently allocated to the thread by the electronic device, and takes the result as the memory statistics result. In the case that the thread object to be counted in the memory statistics requirement is a normal object and a large object, the memory usage information acquisition module sets the state value to a second value, and acquires the first memory usage information, and takes the first memory usage information as the memory statistics result.

[0316] In some cases, when an electronic device obtains the first memory usage information or the second memory usage information of a thread, the electronic device's source code will record the acquisition log. In addition, the electronic device can apply for a fixed storage size of memory 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 fixed storage size of memory, 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 by this embodiment is effective.

[0317] Other embodiments of the present application provide an electronic device that may include a memory and one or more processors. The memory and processors are coupled. The memory is configured to store computer program code, which includes computer instructions. When the computer instructions are executed by the processor, the electronic device performs the functions or steps described in the aforementioned method embodiments.

[0318] 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 the various functions or steps executed by the electronic device in the above-mentioned method embodiment.

[0319] An embodiment of the present application further provides a computer program product. When the computer program product is run on a computer, the computer is enabled to execute the functions or steps executed by the electronic device in the above method embodiment.

[0320] 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.

[0321] In the several embodiments provided in this 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 merely schematic. For example, the division of modules or units is only a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another 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.

[0322] The units described as separate components may or may not be physically separate, and the components displayed as units may be a physical unit or multiple physical units, that is, may be located in one place, or also can be distributed to multiple different places. Part or all of the units can be selected according to actual needs to achieve the purpose of the embodiment of the present application.

[0323] In addition, each functional unit in each embodiment of the present application can be integrated in one processing unit, or each unit can be physically present alone, or two or more units can be integrated in one unit. The integrated unit can be realized in the form of hardware or in the form of a software functional unit.

[0324] The integrated unit, if realized in the form of a software functional unit and sold or used as an independent product, can be stored in a readable storage medium. Based on such understanding, the technical solutions of the embodiments of the present application essentially or the part of the prior art that contributes to the technical solutions or the whole or part of the technical solutions can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions to make a device (which can be a single-chip microcomputer, a chip, etc.) or a processor execute all or part of the steps of the method of each embodiment of the present application. The foregoing storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various program code storage media.

[0325] The above is only a specific embodiment of the present application, but the protection scope of the present application is not limited thereto, any change or replacement within the technical scope disclosed in the present application should be covered in the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.

Claims

1. A memory usage statistics method, applied to electronic devices, 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 created by the first thread in first memory usage information until the first request is received again, and then ceasing to record the size of the object created by the first thread in the first memory usage information; Wherein, in a time period between two receipts of the first request, the sizes of all objects recorded in the first memory usage information are used to indicate the memory usage of the first thread; 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 information of the first thread, the virtual machine layer sets the information recorded in the first memory usage information to 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 difference between the initial value and the target value.

2. The method according to claim 1, characterized in that The method further comprises: After receiving the first request for obtaining the memory usage 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, wherein The method further comprises: After receiving a second request for obtaining memory usage information of the first thread, obtaining a first occupancy value and a second occupancy value currently recorded in the second memory usage information, wherein the second request is used to request obtaining memory usage information of common objects created by the first thread, the first occupancy value is the second memory usage information recorded when the second request is first received, and the second occupancy value is the memory occupancy value of common objects stored in a first memory buffer used for storing common objects created by the first thread when the second request is first 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 third occupancy value is the second memory usage information recorded when the second request is received again; and the fourth occupancy value is the memory occupancy value of ordinary objects stored in a first memory buffer for storing ordinary objects created by the first thread when the second request is received again; determining 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; Among them, the first memory usage information is used to record the memory occupancy value of ordinary objects in the first memory buffer corresponding to the first thread, and the memory occupancy value of large objects in the second memory buffer corresponding to the first thread; the second memory usage information is used to record the memory occupancy value of objects stored in the first memory buffer that has been released and historically used to store ordinary objects created by the first thread.

5. The method according to claim 4, characterized in that The method further comprises: In response to the first thread creating a common object, if remaining storage space in a first memory buffer currently used for storing the common object created by the first thread is insufficient, releasing the first memory buffer currently used for storing the common object created by the first thread, and recording a memory usage value of the stored object 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.

6. 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 according to any one of claims 1 to 5.

7. 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 is caused to execute the method according to any one of claims 1 to 5.

8. A computer program product, characterized in that When the computer program product is run on a computer, the computer is caused to perform the method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Memory leak detection method and device and electronic equipment

    CN115454696A