Memory detection method, device, equipment, storage medium and product
Patent Information
- Application Number
- CN202510519508.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-23
- Publication Date
- 2026-10-23
AI Technical Summary
然而,针对每个内存事件都申请存储空间并进行全量的数据记录不仅耗时且会占用较多的存储空间,对应用程序的性能损耗也较大,因此,可以针对内存大小较大的内存事件进行记录,然而,该方案无法全面检测内存使用情况
[0021]本公开实施例提供的内存检测方案,响应于检测到内存申请事件,获取内存申请事件的内存申请信息,从预设数组中的空闲链表中确定第一链表节点,将所获取的内存申请信息存入所述第一链表节点,并将第一链表节点加入预设数组中的忙碌链表中,响应于检测到内存释放事件,获取内存释放事件的内存释放信息,在忙碌链表中查找到用于存储所获取的内存释放信息对应的内存申请信息的第二链表节点,将第二链表节点中存储的内存申请信息删除,并将第二链表节点加入所述空闲链表中,其中,相对应的内存申请信息和内存释放信息关联于相同的内存块。通过采用上述技术方案,可以更加全面地检测内存使用情况。
Smart Images

Figure CN122963839A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and more particularly to memory detection methods, apparatus, devices, storage media, and products. Background Technology
[0002] With the rapid development of computer technology, applications are becoming increasingly feature-rich, bringing numerous conveniences to users' work and lives. Application performance directly impacts the user experience; therefore, performance optimization is frequently necessary.
[0003] Memory is one of the most critical resources for application operation, and its improper use can negatively impact application performance and stability. Current detection methods for unreasonable memory usage, such as memory leaks, typically involve detecting memory events (including memory allocation and deallocation events) and allocating separate storage space to record event-related data, followed by anomaly analysis based on these records. However, allocating storage space and recording full data for every memory event is time-consuming, consumes significant storage space, and significantly impacts application performance. Therefore, while recording large-scale memory events is possible, this approach cannot comprehensively detect all memory usage patterns. Summary of the Invention
[0004] This disclosure provides a memory detection method, apparatus, device, storage medium, and product, which can optimize existing memory detection solutions.
[0005] In a first aspect, embodiments of this disclosure provide a memory detection method, including:
[0006] In response to detecting a memory request event, obtain the memory request information of the memory request event;
[0007] The first linked list node is determined from the free linked list in the preset array, the obtained memory allocation information is stored in the first linked list node, and the first linked list node is added to the busy linked list in the preset array.
[0008] In response to detecting a memory release event, obtain the memory release information of the memory release event;
[0009] The second linked list node used to store the memory request information corresponding to the acquired memory release information is found in the busy linked list. The memory request information stored in the second linked list node is deleted, and the second linked list node is added to the free linked list. The corresponding memory request information and memory release information are associated with the same memory block.
[0010] Secondly, embodiments of this disclosure also provide a memory detection device, including:
[0011] The application information acquisition module is used to acquire the memory application information of the memory application event in response to the detection of the memory application event;
[0012] The information storage module is used to determine the first linked list node from the free linked list in the preset array, store the obtained memory request information into the first linked list node, and add the first linked list node to the busy linked list in the preset array.
[0013] The memory release information acquisition module is used to acquire memory release information of the memory release event in response to the detection of the memory release event;
[0014] The data deletion module is used to find the second linked list node in the busy linked list that stores the memory request information corresponding to the acquired memory release information, delete the memory request information stored in the second linked list node, and add the second linked list node to the free linked list, wherein the corresponding memory request information and memory release information are associated with the same memory block.
[0015] Thirdly, embodiments of this disclosure also provide an electronic device, the electronic device comprising:
[0016] One or more processors;
[0017] Storage device for storing one or more programs.
[0018] When the one or more programs are executed by the one or more processors, the one or more processors implement the memory detection method provided in the embodiments of this disclosure.
[0019] Fourthly, embodiments of this disclosure also provide a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to perform the memory detection method provided in embodiments of this disclosure.
[0020] Fifthly, embodiments of this disclosure also provide a computer program product, including a computer program that, when executed by a processor, implements the memory detection method provided in embodiments of this disclosure.
[0021] The memory detection scheme provided in this disclosure, in response to the detection of a memory request event, acquires the memory request information of the memory request event, determines a first linked list node from the free linked list in a preset array, stores the acquired memory request information into the first linked list node, and adds the first linked list node to the busy linked list in the preset array. In response to the detection of a memory release event, acquires the memory release information of the memory release event, finds a second linked list node in the busy linked list to store the memory request information corresponding to the acquired memory release information, deletes the memory request information stored in the second linked list node, and adds the second linked list node to the free linked list. The corresponding memory request information and memory release information are associated with the same memory block. By adopting the above technical solution, memory usage can be detected more comprehensively. Attached Figure Description
[0022] The above and other features, advantages, and aspects of the embodiments of this disclosure will become more apparent from the accompanying drawings and the following detailed description. Throughout the drawings, the same or similar reference numerals denote the same or similar elements. It should be understood that the drawings are schematic, and the originals and elements are not necessarily drawn to scale.
[0023] Figure 1 This is a schematic flowchart of a memory detection method provided in an embodiment of the present disclosure;
[0024] Figure 2 This is a schematic diagram of the structure of a preset array provided in an embodiment of the present disclosure;
[0025] Figure 3 This is a flowchart illustrating another memory detection method provided in an embodiment of the present disclosure;
[0026] Figure 4 A schematic diagram of the structure of a memory block with a target memory size provided in an embodiment of this disclosure;
[0027] Figure 5 A schematic diagram of the structure of a memory block with another target memory size provided in an embodiment of this disclosure;
[0028] Figure 6 This is a flowchart illustrating yet another memory detection method provided in an embodiment of the present disclosure;
[0029] Figure 7 This is a schematic diagram illustrating the sorting result of a function call chain provided in an embodiment of this disclosure;
[0030] Figure 8 This is a schematic diagram of a preset sampling event triggering process provided in an embodiment of the present disclosure;
[0031] Figure 9This is a schematic diagram of the structure of a memory detection device provided in an embodiment of the present disclosure;
[0032] Figure 10 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure. Detailed Implementation
[0033] Embodiments of this disclosure will now be described in more detail with reference to the accompanying drawings. While some embodiments of this disclosure are shown in the drawings, it should be understood that this disclosure can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of this disclosure. It should be understood that the accompanying drawings and embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of protection of this disclosure.
[0034] It should be understood that the steps described in the method embodiments of this disclosure may be performed in different orders and / or in parallel. Furthermore, the method embodiments may include additional steps and / or omit the steps shown. The scope of this disclosure is not limited in this respect.
[0035] The term "comprising" and its variations as used herein are open-ended inclusions, meaning "including but not limited to". The term "based on" means "at least partially based on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". Definitions of other terms will be given in the description below.
[0036] It should be noted that the concepts of "first" and "second" mentioned in this disclosure are used only to distinguish different devices, modules or units, and are not used to limit the order of functions performed by these devices, modules or units or their interdependencies.
[0037] It should be noted that the terms "a" and "a plurality of" used in this disclosure are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise expressly indicated in the context, they should be understood as "one or more".
[0038] Figure 1 This is a schematic diagram of a memory detection process provided by an embodiment of the present disclosure. The embodiments of the present disclosure are applicable to memory detection scenarios. The method can be executed by a memory detection device, which can be implemented in the form of software and / or hardware. Optionally, it can be implemented by an electronic device, such as a mobile terminal such as a mobile phone, smartwatch, tablet computer, or personal digital assistant, or a device such as a personal computer (PC) or server.
[0039] like Figure 1 As shown, the method includes:
[0040] Step 101: In response to the detection of a memory request event, obtain the memory request information of the memory request event.
[0041] In this embodiment of the disclosure, a memory allocation event can be an event in which an application requests the operating system to allocate a certain amount of memory space (or memory block), which can be achieved by calling a memory allocation function, such as the malloc function. In this embodiment of the disclosure, memory detection can be performed on a preset application. In this step, memory allocation events can be detected by detecting calls to the memory allocation function. After a memory allocation event is detected, memory allocation information of the memory allocation event can be obtained. The memory allocation information includes information related to this memory allocation event, such as one or more of the following: thread identifier, allocation timestamp, allocation size, and allocation pointer. Among them, the thread identifier is the identifier of the thread that requests memory, such as thread ID; the allocation timestamp is the time when the memory allocation is made, also known as the memory allocation timestamp; the allocation size can be the size of memory that the thread needs to allocate, also known as the expected memory allocation size; the allocation pointer can be used to indicate the starting address of the allocated memory.
[0042] Step 102: Determine the first linked list node from the free linked list in the preset array, store the obtained memory allocation information into the first linked list node, and add the first linked list node to the busy linked list in the preset array.
[0043] In this embodiment, the preset array can be an array structure created within a pre-allocated storage space (the specific size is set according to actual conditions, such as 3M). This data structure is specifically a linked list data structure based on an array. The preset array can include multiple elements, the number of which can be determined according to the preset size. Each element in the preset array can be considered a linked list node. Two linked lists can exist simultaneously in the preset array, denoted as the free linked list and the busy linked list. Nodes in the free linked list are in an idle state, storing empty content (i.e., no data). Nodes in the busy linked list are in a busy state, storing non-empty content (i.e., data). Each element in the preset array can be a node in either the free or busy linked list, dynamically determined by the memory allocation information to be stored or deleted as needed. In the initial state of the preset array, all elements are nodes in the free linked list. Nodes in the linked list are connected by linked list pointers. The first node at the head of the linked list has a pointer to the second node, and so on.
[0044] Figure 2This is a schematic diagram of a preset array provided in an embodiment of the present disclosure. For ease of explanation, an array containing 9 elements is used as an example. However, the actual preset array can contain much more than 9 elements. Figure 2 In the preset array shown in the topmost diagram, there are pointers to linked lists in the busy linked list (represented by solid arrows), which includes nodes 1, 2, 3, 4, and 5 connected in sequence. There are also pointers to linked lists in the free linked list (represented by dashed arrows), which includes nodes a, b, c, and d connected in sequence. It should be noted that initially, all nine elements in the preset array are nodes in the free linked list. The state shown in the topmost diagram is reached as memory allocation information is stored and deleted.
[0045] In this step, after obtaining the memory allocation information of the current memory allocation event, the information needs to be stored in a preset array. A preset determination method can be used to determine the first linked list node from the free list in the preset array. For example, the first linked list node (i.e., the head linked list node) in the free list can be selected as the first linked list node. After determining the first linked list node, the obtained memory allocation information of the current memory allocation event is stored in the first linked list node, and the first linked list node is added as a new busy linked list node in the preset array. Specifically, a linked list pointer pointing to the first linked list node can be added to the last linked list node in the current busy linked list. Furthermore, the linked list pointer from the first linked list node to the original second linked list node in the free list is removed.
[0046] like Figure 2 For example, assuming that in the initial state of the preset array, all 9 elements are nodes in the free list, then the first element (i.e., the first node in the free list) is determined as the first node in the first list. The memory allocation information is stored in the first node. Since there is no busy list before, the first node will become the first node in the busy list of the preset array. Figure 2 In the state shown in the top diagram, node a can be designated as the first linked list node. The memory allocation information is stored in node a, a linked list pointer from node 5 to node a is incremented, and the linked list pointer from node a to node b is removed. Then, node b becomes the first linked list node in the free list, achieving... Figure 2 The state shown in the middle of the diagram.
[0047] Step 103: In response to the detection of a memory release event, obtain the memory release information of the memory release event.
[0048] In this embodiment, a memory release event can be an event in which an application returns previously allocated memory space to the operating system, which can be achieved by calling a memory release function, such as the `free` function. In this step, memory release events can be detected by detecting calls to the memory release function. After detecting a memory release event, memory release information can be obtained. This memory release information includes information related to the current memory release event, such as one or more of the following: release size, release timestamp, and release pointer. The release timestamp is the time when the memory was released; the release size can be the actual amount of memory released by the thread; and the release pointer can be used to indicate the starting address of the released memory.
[0049] Step 104: Locate the second linked list node in the busy linked list to store the memory request information corresponding to the acquired memory release information, delete the memory request information stored in the second linked list node, and add the second linked list node to the free linked list. The corresponding memory request information and memory release information are associated with the same memory block.
[0050] In related technologies, both memory allocation and deallocation information are recorded without deletion. Each record requires additional storage space, which is inefficient and consumes a lot of storage space, resulting in significant performance degradation for applications. To avoid excessive storage consumption, a recording threshold is typically set, meaning only memory allocation and deallocation information exceeding the threshold is recorded. However, the number of single allocations exceeding the threshold may be less than 10% of the total number of allocations, failing to cover all memory allocation events, such as high-frequency small memory allocations. For temporary memory allocations, such as tens of thousands of allocations and deallocations in a function within a loop, memory fragmentation is easily caused, and this situation cannot be detected. Therefore, the solutions in related technologies are difficult to comprehensively detect memory usage, hindering efficient and accurate application performance optimization.
[0051] In this embodiment of the disclosure, for the currently detected memory release event, the released memory is requested by a historical memory request event. This historical memory request event can be the memory request event in step 101, or it can be a different memory request event from the one in step 101; the specific event is not limited and is determined based on the actual detection situation. For ease of explanation, it is assumed that the historical memory request event is not the memory request event in step 101. The memory request information of the historical memory request event is generally stored in a busy linked list of a preset array (except in cases where the historical memory request event is not detected; for example, before the code initialization corresponding to the memory detection method provided in this embodiment of the disclosure is completed, there may be memory request events of system functions, in which case the memory request events will not be detected). Since the memory has been released, to avoid occupying elements in the preset array, the corresponding memory request information can be found and deleted to ensure efficient full detection of memory usage while saving storage space.
[0052] For example, corresponding memory allocation and memory release information are associated with the same memory block; that is, the memory allocated by the memory allocation information and the memory released by the memory release information are the same memory block. The specific method for determining that memory allocation and memory release information are associated with the same memory block is not limited. The second linked list node used to store the memory allocation information corresponding to the acquired memory release information is found in the current busy linked list in the preset array. The specific search method is not limited; for example, each linked list node in the busy linked list can be traversed one by one to determine whether the current linked list node stores the memory allocation information corresponding to the memory release information. If it does, the current linked list node is determined to be the second linked list node.
[0053] For example, after finding the second linked list node, the memory allocation information stored in the second linked list node is deleted, that is, the second linked list node is cleared, and the second linked list node is returned to the free linked list, that is, the second linked list node is removed from the busy linked list (for example, by adding a linked list pointer to the next linked list node of the second linked list node in the busy linked list to the previous linked list node of the second linked list node in the busy linked list), and the second linked list node is added to the free linked list (for example, by adding a linked list pointer to the second linked list node to the last linked list node in the free linked list).
[0054] like Figure 2 As shown, assuming node 2 is the found second linked list node, removing node 2 from the busy linked list changes node 1's pointer from pointing to node 2 to pointing to node 3. A linked list pointer pointing to node 2 is then added to node d, achieving... Figure 2 The state shown in the bottom diagram is as follows.
[0055] The memory detection method provided in this disclosure, in response to the detection of a memory request event, acquires the memory request information of the memory request event, determines a first linked list node from the free linked list in a preset array, stores the acquired memory request information into the first linked list node, and adds the first linked list node to the busy linked list in the preset array. In response to the detection of a memory release event, acquires the memory release information of the memory release event, finds a second linked list node in the busy linked list to store the memory request information corresponding to the acquired memory release information, deletes the memory request information stored in the second linked list node, and adds the second linked list node to the free linked list. The corresponding memory request information and memory release information are associated with the same memory block. By adopting the above technical solution, using a preset array to dynamically store and delete memory request information in a linked list format avoids allocating storage space every time a memory event is detected. The unique data storage structure in this disclosure allows for timely deletion of memory request information that is being released, reducing the amount of data that needs to be recorded, saving storage space, and enabling more comprehensive recording of memory request-related data, including high-frequency small memory requests and temporary memory requests, thereby enabling more comprehensive detection of memory usage.
[0056] In some embodiments, the memory allocation information includes an allocation pointer and the memory release information includes a release pointer; wherein, finding a second linked list node in the busy linked list for storing the memory allocation information corresponding to the acquired memory release information includes: determining the target release pointer contained in the acquired memory release information; finding a second linked list node in the busy linked list for storing memory allocation information containing the target allocation pointer, wherein the target allocation pointer is the same as the target release pointer. Therefore, by comparing the allocation pointer and the release pointer, the recorded memory allocation information that needs to be deleted can be found quickly and accurately.
[0057] For example, each node in the busy linked list can be traversed one by one, the request pointer stored in the current linked list node can be read, and it can be determined whether the read request pointer is the same as the target release pointer. If they are the same, the current request pointer is the target request pointer and the current linked list node is the second linked list node. If they are not the same, the next linked list node can be traversed according to the order of the elements in the preset array.
[0058] In this embodiment of the disclosure, considering that the busy linked list may be long, it is time-consuming to search for the second linked list node by traversing it one by one. In order to solve this problem, the solution can be further optimized.
[0059] Figure 3This is a flowchart illustrating another memory detection method provided in this embodiment. This embodiment optimizes the various optional solutions described above, improving the search efficiency of the second linked list node. Specifically, the method includes the following steps:
[0060] Step 301: In response to the detection of a memory allocation event, obtain the expected memory size to be allocated for the memory allocation event.
[0061] For example, the expected memory size, as described above, is the actual amount of memory the thread wants to request.
[0062] Step 302: Add a preset memory size to the expected memory size of the memory request event to obtain the target memory size, request memory of the target memory size, and obtain the request pointer of the memory request event to obtain the memory request information of the memory request event.
[0063] For example, memory allocation events can be detected using a proxy memory allocation function. After obtaining the expected memory allocation size, additional memory can be allocated to store extra information. The preset memory size can be set according to actual needs, such as 8 bytes or 4 bytes. The preset memory size is added to the expected memory allocation size to obtain the actual memory size required for the memory allocation event, denoted as the target memory size. The system is then requested for the target memory size, and the starting address of the memory block allocated by the system for the memory allocation event is obtained, which is the allocation pointer of the memory allocation event.
[0064] Step 303: Determine the first linked list node from the free linked list in the preset array.
[0065] Step 304: Store the first position information of the first linked list node in the preset array in the memory corresponding to the preset memory size.
[0066] For example, after allocating memory of the target memory size, the first position information of the first linked list node in the preset array can be stored in a preset memory space at the end of that memory. Specifically, the position information can be the element index of the linked list node in the preset array, such as... Figure 2 As shown, the position information of node a can be 1, the position information of node b can be 2, and so on. Assuming node a is determined to be the first linked list node, its first position information can be 8.
[0067] Figure 4 This is a schematic diagram of the structure of a memory block of a target memory size provided in an embodiment of this disclosure, as shown below. Figure 4As shown, the starting position of the memory block of the target memory size requested corresponds to the request pointer. The first part is the expected request size, and the second part is the preset memory size. The location information is stored in the memory space corresponding to the preset memory size.
[0068] For example, the request result (which may include the request pointer and the target memory size) can be returned to the requester corresponding to the memory request event, such as the thread that requested the memory, so that when the requester releases the memory, the size of the memory released (release size) is equal to the target memory size in the request result.
[0069] Step 305: Store the obtained memory allocation information into the first linked list node, and add the first linked list node to the busy linked list in the preset array.
[0070] Step 306: In response to detecting a memory release event, obtain the memory release information of the memory release event, wherein the memory release information includes the release pointer of the associated memory block and the release size of the memory release event.
[0071] For example, the target release pointer can be obtained by detecting the memory release function, and the size of the memory release event can be obtained by a preset function (such as malloc_usable_size) based on the target release pointer.
[0072] Step 307: Determine the second location information stored in memory corresponding to the release size based on the target release pointer, release size, and preset memory size.
[0073] For example, by adding the release size to the target release pointer, we can obtain the end position corresponding to the target memory size when the memory was requested. By subtracting the preset memory size from the end position, we can obtain the start position of the memory space corresponding to the preset memory size. We can read the stored position information from the memory space corresponding to the preset memory size and record it as the second position information.
[0074] Step 308: Find the target linked list node corresponding to the second position information from the preset array, and obtain the request pointer stored in the target linked list node.
[0075] For example, such as Figure 2 As shown, assuming the second position information is 3, the linked list node corresponding to the second position information is node 2, and the request pointer stored in node 2 is obtained.
[0076] Step 309: In response to the fact that the obtained request pointer is the same as the target release pointer, the target linked list node is determined to be the second linked list node.
[0077] For example, if the request pointer obtained from node 2 is the same as the target release pointer, then node 2 can be determined to be the second linked list node.
[0078] Step 310: Delete the memory allocation information stored in the second linked list node and add the second linked list node to the free linked list.
[0079] The memory detection method provided in this embodiment, after detecting a memory request event, allocates an additional preset memory size on top of the expected request size to store the first position information of the determined first linked list node. After detecting a memory release event, it determines the second position information stored in the memory corresponding to the release size based on the target release pointer, the release size, and the preset memory size. Based on the second position information, the linked list node storing the corresponding memory request information can be quickly found from the busy linked list in the preset array, and subsequent related operations can be performed. This can improve the efficiency of deleting memory request information, improve memory detection efficiency, and reduce the impact on application performance.
[0080] In some embodiments, considering that there may be undetected memory request events before the memory detection method provided in this disclosure is successfully initialized, there is no corresponding memory request information in the preset array for undetected memory request events, so there is no need to delete them. In order to quickly determine whether deletion is necessary, further optimization can be performed.
[0081] Optionally, storing the first position information of the first linked list node in the preset array in the memory corresponding to the preset memory size includes: storing preset data and the first position information of the first linked list node in the preset array in the memory corresponding to the preset memory size. The step of determining the second position information stored in the memory corresponding to the release size based on the target release pointer, the release size, and the preset memory size includes: determining the target data currently stored in the storage location for storing the preset data in the memory corresponding to the release size based on the target release pointer, the release size, and the preset memory size; and determining the second position information stored in the memory corresponding to the release size in response to the target data being the same as the preset data. Therefore, by first quickly determining whether there is corresponding memory request information to be deleted in the preset array through matching preset data, the memory detection efficiency is further improved.
[0082] For example, the preset data could be the magic number, and the specific value can be set according to actual needs, generally set to uncommon data. Figure 5 This is a schematic diagram of the structure of a memory block with another target memory size provided in an embodiment of this disclosure, as shown below. Figure 5As shown, the memory space corresponding to the preset memory size stores preset data and location information. The preset memory size includes a first memory size and a second memory size. The first memory size is used to store the preset data, and the second memory size is used to store the location information. For example, if the preset memory size is 8 bytes, the first 4 bytes are used to store the preset data, and the last 4 bytes are used to store the location information. For example, by adding the release size to the target release pointer, the end position corresponding to the target memory size when requesting memory can be obtained. By subtracting the preset memory size from the end position, the start position of the memory space corresponding to the preset memory size can be obtained. The data stored in the memory space corresponding to the preset memory size is read and recorded as the target data. It is determined whether the target data is the same as the preset data. If they are the same, it can be basically determined that the memory was requested using the memory detection scheme provided in this embodiment of the disclosure. Although it is not ruled out that the actual stored data is the same as the preset data, it can quickly filter out most cases where there is no stored memory request information by matching the preset data. Then, the location information stored therein, that is, the second location information, is read, and then the subsequent determination based on the location information is performed. Optionally, in response to the target data being different from the preset data, the system memory release function is invoked to release the memory to be released, without needing to search for a second linked list node.
[0083] In some embodiments, the method may further include: in response to a preset abnormal event being triggered, obtaining data stored in the linked list nodes of the busy linked list to obtain memory usage data. Thus, when an abnormal event occurs, such as frame dropping, application not responding (ANR), or other abnormal situations, obtaining the data stored in the linked list nodes of the current busy linked list allows for the determination of all unreleased memory and the acquisition of memory usage data.
[0084] In some embodiments, further optimizations can be made to determine which function specifically allocated the unreleased memory, in order to obtain more accurate analysis results.
[0085] Figure 6 This is a flowchart illustrating yet another memory detection method provided in this disclosure, which is an optimization based on the various optional solutions in the above embodiments. Specifically, the method includes the following steps:
[0086] Step 601: In response to the triggering of a preset sampling event, perform a stack sampling operation on the currently running thread to obtain stack sampling data and record the triggering time of the preset sampling event. The stack sampling data includes a function call chain, and the function call chain includes the function to be detected.
[0087] In this embodiment of the disclosure, the execution order of steps 601, 602 and 603 is not limited and can be executed concurrently.
[0088] The currently running thread can be any thread in the preset application or a pre-defined thread. The triggering of the preset sampling event can be independent for different threads. That is, when different threads are regarded as the current thread, they can correspond to different preset sampling events. Accordingly, step 601 can be executed independently for different threads.
[0089] In this embodiment, the triggering condition for the preset sampling event is not limited. For example, it can be triggered at a set time or when a preset condition is met. To ensure the accuracy of data processing, the triggering frequency of the preset sampling event corresponding to the current thread can be higher than a preset frequency threshold, thereby achieving a higher sampling frequency. Optionally, the preset sampling event can be triggered when a memory allocation event is detected and when a memory release event is detected.
[0090] For example, stack sampling for the currently running thread can specifically involve performing stack sampling (also known as stack grabbing) on the thread's stack. The thread stack is a memory area used by the thread to store temporary data and function call information during runtime. Stack sampling is the process of obtaining the current call stack information from the thread stack. The stack records the execution path of the currently running thread, i.e., the call chain from the thread's entry function to the currently executing function. The stack sampling data includes the function call chain, which includes the function to be detected. In other words, the function call chain involves the call relationships between functions, and any function within this call relationship can be the function to be detected. Stack sampling for the currently running thread can be performed by the currently running thread itself, i.e., the stack sampling data is obtained by the currently running thread performing stack sampling on its own stack; stack sampling for the currently running thread can also be performed by a thread other than the currently running thread.
[0091] Step 602: In response to the detection of a memory request event, obtain the memory request information of the memory request event, determine the first linked list node from the free linked list in the preset array, store the obtained memory request information into the first linked list node, and add the first linked list node to the busy linked list in the preset array.
[0092] The memory allocation information includes the thread identifier of the thread requesting the memory, the expected size of the memory to be allocated, and the memory allocation timestamp.
[0093] Step 603: In response to the detection of a memory release event, obtain the memory release information of the memory release event, find the second linked list node in the busy linked list that is used to store the memory request information corresponding to the obtained memory release information, delete the memory request information stored in the second linked list node, and add the second linked list node to the free linked list.
[0094] Step 604: In response to the triggering of a preset exception event, retrieve the data stored in the linked list nodes of the busy linked list to obtain memory usage data.
[0095] Step 605: For each thread, sort the function call chain in the stack sampling data corresponding to the multiple preset sampling events triggered by the current thread according to the trigger time of the preset sampling events triggered multiple times, and obtain the sorting result. For each function to be detected that appears at least once consecutively in the sorting result, determine the function start time according to the trigger time corresponding to the first appearance of the current function to be detected, and determine the function end time according to the trigger time corresponding to the last appearance of the current function to be detected. Find the expected memory size of the memory request event whose thread identifier is the current thread and whose memory request timestamp is between the function start time and the function end time from the memory usage data. Sum the found expected memory sizes to obtain the unreleased size of the current function to be detected.
[0096] For example, step 601 can be executed multiple times. Optionally, the preset sampling event is triggered at a preset frequency, for example, once every specified time interval (e.g., 3 milliseconds). Optionally, if the preset sampling event is triggered when a memory allocation event is detected and when a memory release event is detected, it can be determined whether the time interval between the current time and the time of the last trigger of the preset sampling event is greater than or equal to the preset time interval (e.g., 3 milliseconds) when a memory allocation event or a memory release event is detected, the preset sampling event is triggered if it is greater than or equal to the preset time interval (e.g., 3 milliseconds). If it is greater, the preset sampling event is triggered; if it is less than, the preset sampling event is not triggered, thus avoiding the preset sampling event being triggered too frequently and affecting application performance.
[0097] For example, the function call chains in the stack sampling data corresponding to the multiple triggered preset sampling events are sorted in order from earliest to latest according to the trigger time of the preset sampling events.
[0098] Figure 7 This is a schematic diagram illustrating the sorting result of a function call chain provided in an embodiment of this disclosure, as shown below. Figure 7As shown, the trigger times of the preset sampling events that are triggered multiple times are T0, T1, T2, T3, T4, and T5, respectively. The function call chain corresponding to T0 is A, B, C, and X, that is, function A calls function B, function B calls function C, and function C calls function X; the function call chain corresponding to T1 is A, B, C, E, F, and G; the function call chain corresponding to T2 is A, B, C, D, and H; the function call chain corresponding to T3 is A, B, C, D, I, and J; the function call chain corresponding to T4 is A, B, and H; and the function call chain corresponding to T5 is A, B, H, D, E, and F.
[0099] The trigger time corresponding to the first occurrence of the function to be detected is the time of its first occurrence in a series of consecutive occurrences, and the trigger time corresponding to the last occurrence of the function to be detected is the time of its last occurrence in a series of consecutive occurrences. For example... Figure 7 As shown, functions A and B both appear 6 times consecutively, function C appears 4 times consecutively, and function D appears 2 times and 1 time consecutively, respectively. For function D, execution has already ended once in between, so the start and end times of the function need to be determined separately. The same logic applies to other functions. Taking function C as an example, if the trigger time for the first appearance of function C is T0, then the start time of function C is T0. If the trigger time for the last appearance of function C is T3, then the end time of the function is T3. For a function to be detected that appears only once, such as function G, the previous trigger time when function G appears can be determined as the start time of function G, and the trigger time when function G appears again can be determined as the end time of function G. Alternatively, the trigger time when function G appears can be determined as the start time of function G, and the next trigger time when function G appears again can be determined as the end time of function G.
[0100] For example, since the memory usage data includes thread identifiers and memory allocation timestamps in the information stored in each linked list node, when the thread identifier matches the thread corresponding to the current sorting result, and the memory allocation timestamp is within the function start time and function end time of a function to be tested, it can be considered as the unreleased memory allocated by the function to be tested. If multiple expected memory allocation sizes are found, the sum of the multiple expected memory allocation sizes can be used to obtain the cumulative unreleased memory size of the function to be tested, referred to as the unreleased size.
[0101] The memory detection method provided in this disclosure uses a pre-defined array to dynamically store and delete memory request information in a linked list format. This avoids requesting storage space every time a memory event is detected. The unique data storage structure of this disclosure allows for timely deletion of memory request information related to memory release, reducing the amount of data that needs to be recorded and saving storage space. This enables more comprehensive recording of memory request-related data. Furthermore, while recording memory request information, stack sampling is also performed. In case of an anomaly, the data stored in the nodes of the busy linked list is obtained as memory usage data. Based on the function call chain sorting results of multiple stack sampling data, the start and end times of each function to be detected can be determined. By analyzing the relationship between the memory request timestamp and the function start and end times in the memory usage data, it is determined which function requested the unreleased memory and the cumulative unreleased size of the current function to be detected can be determined. This facilitates more accurate analysis of the cause of the anomaly and thus efficiently and accurately optimizes application performance.
[0102] In some embodiments, the method may further include: in response to a preset sampling event being triggered, performing a numerical sampling operation on the cumulative memory allocation size of the currently running thread to obtain numerical sampling data; after obtaining the sorting result, the method further includes: for each function to be detected that appears at least once consecutively in the sorting result, determining the first numerical sampling data and the second numerical sampling data corresponding to the current function to be detected, and determining the function memory allocation size of the current function to be detected based on the difference between the second numerical sampling data and the first numerical sampling data; wherein, the first numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the first occurrence in the consecutive occurrences of the current function to be detected, and the second numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the last occurrence in the consecutive occurrences of the current function to be detected. Therefore, while performing stack sampling, the cumulative memory allocation size is also numerically sampled, thereby determining the allocated memory size of each function to be detected, which is beneficial for more accurate analysis of the cause of the anomaly, and thus for efficient and accurate optimization of application performance.
[0103] Optionally, after determining the memory allocation size of the function to be tested, the proportion of the unreleased size in the total memory allocation size can be determined by combining the unreleased size of the function to be tested. This provides richer information for tracing the cause of the exception, which is conducive to further accurate analysis of the cause of the exception, and thus optimizes the application performance efficiently and accurately.
[0104] For example, for the currently running thread, the cumulative memory allocation size of the currently running thread can be dynamically updated and stored to ensure that when a preset sampling event is triggered, the accurate value of the cumulative memory allocation size at the sampling moment can be obtained through numerical sampling operations. The cumulative memory allocation size of the currently running thread can be the amount of memory allocated to created objects from the time the currently running thread starts until the current moment. During the numerical sampling operation, the current value of the cumulative memory allocation size of the currently running thread stored at the sampling moment is obtained to obtain the numerical sampling data. The sampling moment can be the trigger time of the preset sampling event.
[0105] For example, stack sampling operations and numerical sampling operations can be executed in parallel to ensure that the stack sampled data and numerical sampled data correspond to the same sampling time. Optionally, in response to a preset sampling event being triggered, the trigger time is recorded, and the stack sampled data and numerical sampled data corresponding to the same trigger time are associated and stored to obtain the stack sampled data and numerical sampled data corresponding to the trigger time of the preset sampling event. Figure 8 This is a schematic diagram of a preset sampling event triggering process provided in an embodiment of the present disclosure, as shown below. Figure 8 As shown, the cumulative memory allocation size of the current thread can be dynamically updated and maintained, forming a data axis with the cumulative memory allocation size as a parameter. After each preset sampling event is triggered, stack sampling operation and numerical sampling operation are performed. The function call chain is obtained by performing stack sampling operation, and the current value of the data axis is recorded by performing numerical sampling operation on the data axis. The stack sampling data and numerical sampling data are stored together to obtain the stack sampling data and numerical sampling data corresponding to the trigger time of each preset sampling event.
[0106] Figure 9 This is a schematic diagram of the structure of a memory detection device provided in an embodiment of this disclosure, as shown below. Figure 9 As shown, the device includes:
[0107] The application information acquisition module 901 is used to acquire the memory application information of the memory application event in response to the detection of the memory application event;
[0108] The information storage module 902 is used to determine the first linked list node from the free linked list in the preset array, store the obtained memory request information into the first linked list node, and add the first linked list node to the busy linked list in the preset array.
[0109] The memory release information acquisition module 903 is used to acquire memory release information of the memory release event in response to the detection of the memory release event;
[0110] The data deletion module 904 is used to find the second linked list node in the busy linked list that stores the memory request information corresponding to the acquired memory release information, delete the memory request information stored in the second linked list node, and add the second linked list node to the free linked list, wherein the corresponding memory request information and memory release information are associated with the same memory block.
[0111] The memory detection device provided in this disclosure uses a pre-set array to dynamically store and delete memory request information in the form of a linked list. This avoids requesting storage space every time a memory event is detected. The unique data storage structure in this disclosure allows for the timely deletion of memory request information that is releasing memory, which reduces the amount of data that needs to be recorded and saves storage space. This enables a more comprehensive recording of memory request-related data, including high-frequency small memory requests and temporary memory requests, thereby enabling a more comprehensive detection of memory usage.
[0112] Optionally, the memory allocation information includes an allocation pointer, and the memory release information includes a release pointer. Specifically, finding a second linked list node in the busy linked list to store the memory allocation information corresponding to the acquired memory release information includes: determining the target release pointer contained in the acquired memory release information; and finding a second linked list node in the busy linked list to store the memory allocation information containing the target allocation pointer, wherein the target allocation pointer is the same as the target release pointer.
[0113] Optionally, the memory allocation information may also include the expected memory allocation size of the memory allocation event, and the memory release information may also include the memory release size of the memory release event;
[0114] The application information acquisition module includes:
[0115] The expected allocation size acquisition unit is used to acquire the expected memory allocation size of the memory allocation event in response to the detection of the memory allocation event;
[0116] The memory allocation unit is used to add a preset memory size to the expected memory size of the memory allocation event to obtain a target memory size, allocate memory of the target memory size, and obtain the allocation pointer of the memory allocation event to obtain the memory allocation information of the memory allocation event;
[0117] The device further includes:
[0118] The location storage module is used to store the first position information of the first linked list node in the preset array in the memory corresponding to the preset memory size;
[0119] The step of finding a second linked list node in the busy linked list for storing memory allocation information containing a target allocation pointer includes: determining the second location information stored in the memory corresponding to the release size based on the target release pointer, the release size, and the preset memory size; finding the target linked list node corresponding to the second location information from the preset array and obtaining the allocation pointer stored in the target linked list node; and determining the target linked list node as the second linked list node in response to the obtained allocation pointer being the same as the target release pointer.
[0120] Optionally, the location storage module is used to: store preset data and the first position information of the first linked list node in the preset array in the memory corresponding to the preset memory size;
[0121] The step of determining the second location information stored in the memory corresponding to the release size based on the target release pointer, the release size, and the preset memory size includes: determining the target data currently stored in the storage location in the memory corresponding to the release size for storing the preset data based on the target release pointer, the release size, and the preset memory size; and determining the second location information stored in the memory corresponding to the release size in response to the target data being the same as the preset data.
[0122] Optionally, the memory request information includes the thread identifier of the thread requesting memory, the expected memory size, and the memory request timestamp;
[0123] The device further includes:
[0124] The stack sampling module is used to perform stack sampling operations on the currently running thread in response to the triggering of a preset sampling event, obtain stack sampling data, and record the triggering time of the preset sampling event. The stack sampling data includes a function call chain, and the function call chain includes the function to be detected.
[0125] The data acquisition module is used to obtain the data stored in the linked list nodes of the busy linked list in response to the triggering of a preset abnormal event, and to obtain the memory usage data.
[0126] The unreleased size determination module is used to sort the function call chain in the stack sampling data corresponding to the multiple preset sampling events triggered by the current thread for each thread, according to the trigger time of the preset sampling events triggered multiple times, and obtain a sorting result. For each function to be detected that appears at least once consecutively in the sorting result, the function start time is determined according to the trigger time corresponding to the first appearance of the current function to be detected, and the function end time is determined according to the trigger time corresponding to the last appearance of the current function to be detected. The module searches the memory usage data for the expected memory size of memory allocation events whose thread identifier is the current thread and whose memory allocation timestamp is between the function start time and the function end time. The module sums the found expected memory allocation sizes to obtain the unreleased size of the current function to be detected.
[0127] Optionally, the device may also include:
[0128] The numerical sampling module is used to perform a numerical sampling operation on the cumulative memory allocation size of the currently running thread in response to the triggering of a preset sampling event, and to obtain numerical sampling data.
[0129] The memory allocation size determination module is used, after obtaining the sorting results, to determine, for each function to be detected that appears at least once consecutively in the sorting results, the first numerical sampling data and the second numerical sampling data corresponding to the current function to be detected, and to determine the function memory allocation size of the current function to be detected based on the difference between the second numerical sampling data and the first numerical sampling data; wherein, the first numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the first occurrence in the consecutive occurrences of the current function to be detected, and the second numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the last occurrence in the consecutive occurrences of the current function to be detected.
[0130] The memory detection device provided in this disclosure can execute the memory detection method provided in any embodiment of this disclosure, and has the corresponding functional modules and beneficial effects of executing the method.
[0131] It is worth noting that the various units and modules included in the above-mentioned device are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be realized; in addition, the specific names of each functional unit are only for easy differentiation and are not used to limit the protection scope of the embodiments of this disclosure.
[0132] Figure 10 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure. Reference is made below. Figure 10 It illustrates an electronic device suitable for implementing embodiments of the present disclosure (e.g., Figure 10 The diagram below shows the structure of the terminal device or server 1000. The terminal device in this embodiment may include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), and vehicle terminals (e.g., vehicle navigation terminals), as well as fixed terminals such as digital TVs and desktop computers. Figure 10 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments disclosed herein.
[0133] like Figure 10 As shown, the electronic device 1000 may include a processing unit (e.g., a central processing unit, a graphics processing unit, etc.) 1001, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage device 1008 into a random access memory (RAM) 1003. The RAM 1003 also stores various programs and data required for the operation of the electronic device 1000. The processing unit 1001, ROM 1002, and RAM 1003 are interconnected via a bus 1004. An edit / output (I / O) interface 1005 is also connected to the bus 1004.
[0134] Typically, the following devices can be connected to the I / O interface 1005: input devices 1006 including, for example, a touchscreen, touchpad, keyboard, mouse, camera, microphone, accelerometer, gyroscope, etc.; output devices 1007 including, for example, a liquid crystal display (LCD), speaker, vibrator, etc.; storage devices 1008 including, for example, magnetic tape, hard disk, etc.; and communication devices 1009. Communication device 1009 allows electronic device 1000 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 10 An electronic device 1000 with various devices is shown; however, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.
[0135] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication device 1009, or installed from storage device 1008, or installed from ROM 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of embodiments of this disclosure.
[0136] The names of messages or information exchanged between multiple devices in the embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of such messages or information.
[0137] The electronic device provided in this embodiment and the memory detection method provided in the above embodiments belong to the same inventive concept. Technical details not described in detail in this embodiment can be found in the above embodiments, and this embodiment has the same beneficial effects as the above embodiments.
[0138] This disclosure provides a computer storage medium storing a computer program that, when executed by a processor, implements the memory detection method provided in the above embodiments.
[0139] This disclosure provides a computer program product, including a computer program that, when executed by a processor, implements the memory detection method provided in the above embodiments.
[0140] It should be noted that the computer-readable medium described in this disclosure can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in connection with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.
[0141] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.
[0142] The aforementioned computer-readable medium carries one or more programs. When the electronic device executes the one or more programs, the electronic device causes the following actions: In response to detecting a memory request event, it acquires memory request information of the memory request event; determines a first linked list node from a free linked list in a preset array, stores the acquired memory request information into the first linked list node, and adds the first linked list node to a busy linked list in the preset array; In response to detecting a memory release event, it acquires memory release information of the memory release event; finds a second linked list node in the busy linked list for storing memory request information corresponding to the acquired memory release information, deletes the memory request information stored in the second linked list node, and adds the second linked list node to the free linked list, wherein the corresponding memory request information and memory release information are associated with the same memory block.
[0143] Computer program code for performing the operations of this disclosure can be written in one or more programming languages or a combination thereof, including but not limited to object-oriented programming languages such as Java, Smalltalk, and C++, as well as conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0144] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0145] The units described in the embodiments of this disclosure can be implemented in software or in hardware. The names of modules do not necessarily limit the module itself; for example, a module for obtaining application information can also be described as "a module that, in response to detecting a memory application event, obtains memory application information of the memory application event."
[0146] The functions described above in this document can be performed, at least in part, by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: Field Programmable Gate Arrays (FPGAs), Application-Specific Integrated Circuits (ASICs), Application Standard Products (ASSPs), System-on-Chip (SoCs), Complex Programmable Logic Devices (CPLDs), and so on.
[0147] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0148] According to one or more embodiments of this disclosure, a memory detection method is provided, comprising:
[0149] In response to detecting a memory request event, obtain the memory request information of the memory request event;
[0150] The first linked list node is determined from the free linked list in the preset array, the obtained memory allocation information is stored in the first linked list node, and the first linked list node is added to the busy linked list in the preset array.
[0151] In response to detecting a memory release event, obtain the memory release information of the memory release event;
[0152] The second linked list node used to store the memory request information corresponding to the acquired memory release information is found in the busy linked list. The memory request information stored in the second linked list node is deleted, and the second linked list node is added to the free linked list. The corresponding memory request information and memory release information are associated with the same memory block.
[0153] According to one or more embodiments of this disclosure, the memory allocation information includes an allocation pointer and the memory release information includes a release pointer;
[0154] The second linked list node used to store the memory allocation information corresponding to the acquired memory release information is located in the busy linked list, including:
[0155] Determine the target release pointer contained in the acquired memory release information;
[0156] The second linked list node for storing memory allocation information containing the target allocation pointer is found in the busy linked list, wherein the target allocation pointer is the same as the target release pointer.
[0157] According to one or more embodiments of this disclosure, the memory request information further includes the expected memory size requested for the memory request event, and the memory release information further includes the size released for the memory release event;
[0158] Wherein, the step of obtaining the memory request information of the memory request event in response to detecting a memory request event includes:
[0159] In response to detecting a memory request event, the expected memory size to be requested for the memory request event is obtained;
[0160] Based on the expected memory size requested in the memory request event, a preset memory size is added to obtain the target memory size. Memory of the target memory size is requested, and the request pointer of the memory request event is obtained to obtain the memory request information of the memory request event.
[0161] The method further includes:
[0162] The first position information of the first linked list node in the preset array is stored in the memory corresponding to the preset memory size;
[0163] The step of finding a second linked list node in the busy linked list to store memory allocation information containing the target allocation pointer includes:
[0164] Based on the target release pointer, the release size, and the preset memory size, determine the second location information stored in memory corresponding to the release size;
[0165] Find the target linked list node corresponding to the second position information from the preset array, and obtain the request pointer stored in the target linked list node;
[0166] In response to the fact that the obtained request pointer is the same as the target release pointer, the target linked list node is determined to be the second linked list node.
[0167] According to one or more embodiments of this disclosure, storing the first position information of the first linked list node in the preset array in memory corresponding to the preset memory size includes:
[0168] Preset data and the first position information of the first linked list node in the preset array are stored in the memory corresponding to the preset memory size.
[0169] The step of determining the second location information stored in memory corresponding to the release size based on the target release pointer, the release size, and the preset memory size includes:
[0170] Based on the target release pointer, the release size, and the preset memory size, determine the target data currently stored in the storage location in memory corresponding to the release size that is used to store the preset data;
[0171] In response to the target data being the same as the preset data, the second location information stored in memory corresponding to the release size is determined.
[0172] According to one or more embodiments of this disclosure, the memory request information includes the thread identifier of the thread requesting memory, the expected memory size, and the memory request timestamp;
[0173] The method further includes:
[0174] In response to the triggering of a preset sampling event, a stack sampling operation is performed on the currently running thread to obtain stack sampling data, and the triggering time of the preset sampling event is recorded. The stack sampling data includes a function call chain, and the function call chain includes the function to be detected.
[0175] In response to the triggering of a preset abnormal event, the data stored in the linked list nodes of the busy linked list is obtained to obtain memory usage data;
[0176] For each thread, the function call chain in the stack sampling data corresponding to the multiple triggered preset sampling events of the current thread is sorted according to the trigger time of the preset sampling events, and a sorting result is obtained. For each function to be detected that appears at least once consecutively in the sorting result, the function start time is determined according to the trigger time corresponding to the first appearance of the current function to be detected, and the function end time is determined according to the trigger time corresponding to the last appearance of the current function to be detected. The expected memory allocation size of the memory allocation event with the thread identifier of the current thread and the memory allocation timestamp between the function start time and the function end time is found from the memory usage data. The expected memory allocation sizes found are summed to obtain the unreleased size of the current function to be detected.
[0177] According to one or more embodiments of this disclosure, it further includes:
[0178] In response to the triggering of a preset sampling event, a numerical sampling operation is performed on the cumulative memory allocation size of the currently running thread to obtain numerical sampling data;
[0179] After obtaining the sorting results, the following is also included:
[0180] For each function to be detected that appears at least once consecutively in the sorting results, the first numerical sampling data and the second numerical sampling data corresponding to the current function to be detected are determined, and the memory allocation size of the current function to be detected is determined based on the difference between the second numerical sampling data and the first numerical sampling data; wherein, the first numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the first occurrence in the consecutive occurrences of the current function to be detected, and the second numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the last occurrence in the consecutive occurrences of the current function to be detected.
[0181] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.
[0182] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.
[0183] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative examples of implementing the claims.
Claims
1. A memory detection method, characterized in that, include: In response to detecting a memory request event, obtain the memory request information of the memory request event; The first linked list node is determined from the free linked list in the preset array, the obtained memory allocation information is stored in the first linked list node, and the first linked list node is added to the busy linked list in the preset array. In response to detecting a memory release event, obtain the memory release information of the memory release event; The second linked list node used to store the memory request information corresponding to the acquired memory release information is found in the busy linked list. The memory request information stored in the second linked list node is deleted, and the second linked list node is added to the free linked list. The corresponding memory request information and memory release information are associated with the same memory block.
2. The method according to claim 1, characterized in that, The memory allocation information includes an allocation pointer, and the memory release information includes a release pointer. The second linked list node used to store the memory allocation information corresponding to the acquired memory release information is located in the busy linked list, including: Determine the target release pointer contained in the acquired memory release information; The second linked list node for storing memory allocation information containing the target allocation pointer is found in the busy linked list, wherein the target allocation pointer is the same as the target release pointer.
3. The method according to claim 2, characterized in that, The memory allocation information also includes the expected memory allocation size of the memory allocation event, and the memory release information also includes the memory release size of the memory release event; Wherein, the step of obtaining the memory request information of the memory request event in response to detecting a memory request event includes: In response to detecting a memory request event, the expected memory size to be requested for the memory request event is obtained; Based on the expected memory size requested in the memory request event, a preset memory size is added to obtain the target memory size. Memory of the target memory size is requested, and the request pointer of the memory request event is obtained to obtain the memory request information of the memory request event. The method further includes: The first position information of the first linked list node in the preset array is stored in the memory corresponding to the preset memory size; The step of finding a second linked list node in the busy linked list to store memory allocation information containing the target allocation pointer includes: Based on the target release pointer, the release size, and the preset memory size, determine the second location information stored in memory corresponding to the release size; Find the target linked list node corresponding to the second position information from the preset array, and obtain the request pointer stored in the target linked list node; In response to the fact that the obtained request pointer is the same as the target release pointer, the target linked list node is determined to be the second linked list node.
4. The method according to claim 3, characterized in that, The step of storing the first position information of the first linked list node in the preset array in the memory corresponding to the preset memory size includes: Preset data and the first position information of the first linked list node in the preset array are stored in the memory corresponding to the preset memory size. The step of determining the second location information stored in memory corresponding to the release size based on the target release pointer, the release size, and the preset memory size includes: Based on the target release pointer, the release size, and the preset memory size, determine the target data currently stored in the storage location in memory corresponding to the release size that is used to store the preset data; In response to the target data being the same as the preset data, the second location information stored in memory corresponding to the release size is determined.
5. The method according to claim 1, characterized in that, The memory request information includes the thread identifier of the thread requesting memory, the expected memory size, and the memory request timestamp; The method further includes: In response to the triggering of a preset sampling event, a stack sampling operation is performed on the currently running thread to obtain stack sampling data, and the triggering time of the preset sampling event is recorded. The stack sampling data includes a function call chain, and the function call chain includes the function to be detected. In response to the triggering of a preset abnormal event, the data stored in the linked list nodes of the busy linked list is obtained to obtain memory usage data; For each thread, the function call chain in the stack sampling data corresponding to the multiple triggered preset sampling events of the current thread is sorted according to the trigger time of the preset sampling events, and a sorting result is obtained. For each function to be detected that appears at least once consecutively in the sorting result, the function start time is determined according to the trigger time corresponding to the first appearance of the current function to be detected, and the function end time is determined according to the trigger time corresponding to the last appearance of the current function to be detected. The expected memory allocation size of the memory allocation event with the thread identifier of the current thread and the memory allocation timestamp between the function start time and the function end time is found from the memory usage data. The expected memory allocation sizes found are summed to obtain the unreleased size of the current function to be detected.
6. The method according to claim 5, characterized in that, Also includes: In response to the triggering of a preset sampling event, a numerical sampling operation is performed on the cumulative memory allocation size of the currently running thread to obtain numerical sampling data; After obtaining the sorting results, the following is also included: For each function to be detected that appears at least once consecutively in the sorting results, the first numerical sampling data and the second numerical sampling data corresponding to the current function to be detected are determined, and the memory allocation size of the current function to be detected is determined based on the difference between the second numerical sampling data and the first numerical sampling data; wherein, the first numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the first occurrence in the consecutive occurrences of the current function to be detected, and the second numerical sampling data corresponding to the current function to be detected is determined based on the trigger time of the last occurrence in the consecutive occurrences of the current function to be detected.
7. A memory detection device, characterized in that, include: The application information acquisition module is used to acquire the memory application information of the memory application event in response to the detection of the memory application event; The information storage module is used to determine the first linked list node from the free linked list in the preset array, store the obtained memory request information into the first linked list node, and add the first linked list node to the busy linked list in the preset array. The memory release information acquisition module is used to acquire memory release information of the memory release event in response to the detection of the memory release event; The information deletion module is used to find the second linked list node in the busy linked list that stores the memory request information corresponding to the acquired memory release information, delete the memory request information stored in the second linked list node, and add the second linked list node to the free linked list. The corresponding memory request information and memory release information are associated with the same memory block.
8. An electronic device, characterized in that, The electronic device includes: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the memory detection method as described in any one of claims 1-6.
9. A storage medium containing computer-executable instructions, characterized in that, The computer-executable instructions, when executed by a computer processor, are used to perform the memory detection method as described in any one of claims 1-6.
10. A computer program product, characterized in that, It includes a computer program that, when executed by a processor, implements the memory detection method as described in any one of claims 1-6.