Memory space monitoring method, processor, medium and program product
By performing static analysis based on the keywords marked by memory space when the application is not running, the problem of memory monitoring in the existing technology increases CPU usage, and more efficient memory space monitoring and optimization are achieved.
Patent Information
- Application Number
- CN202510084041.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-20
- Publication Date
- 2025-05-16
AI Technical Summary
The prior art monitors memory when the application is running, resulting in an increase in CPU usage and even affects the operation of the application.
When the application is not running, the usage of memory space is statically analyzed based on the keywords marked in the memory space, a monitoring result file is generated, and the usage of memory space is adjusted according to the results.
Reduces CPU usage, avoids the impact on application operation, and improves the efficiency of memory space monitoring.
Smart Images

Figure CN120011172A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a memory space monitoring method, a processor, a medium and a program product. Background Art
[0002] Whether it is integrated code or standard automotive-grade operating system, it is inseparable from the support of memory. In system applications, large structures, debug messages, communication caches and other large message bodies are often encountered. If they are not managed and monitored, the memory will be slowly consumed as the system runs for a long time, which will cause the system to hang. In addition, the continuous update of applications will increase memory usage and increase the difficulty of memory management. In this regard, the existing technology monitors the memory of the application while it is running. This monitoring method is dynamic and requires a larger CPU occupancy rate, and even affects the operation of the application. Summary of the invention
[0003] In view of this, one of the purposes of the embodiments of the present application is to provide a memory space monitoring method that can improve the problem that the prior art will generate greater CPU occupancy and even affect the operation of the application.
[0004] In order to achieve the above technical objectives, the technical solutions adopted in this application are as follows:
[0005] In a first aspect, an embodiment of the present application provides a memory space monitoring method, comprising:
[0006] When the application is not running, based on the keywords marked in the memory space, obtaining the usage of each memory segment in the memory space and the maximum usage of the memory space;
[0007] A monitoring result of the memory space is obtained according to the usage and the maximum usage, and the monitoring result includes a usage rate of the memory space.
[0008] Further, obtaining the usage of each memory segment in the memory space and the maximum usage of the memory space based on the keyword marked in the memory space includes:
[0009] Marking the keyword in the memory space, the keyword includes a first mark, a second mark and a third mark, the first mark is used to represent the start bit address of the memory space, the second mark is used to represent the end bit address of the memory space, and the third mark is used to represent the usage of each memory segment in the memory space;
[0010] The keyword is read to obtain the usage and the maximum usage.
[0011] Further, obtaining the monitoring result of the memory space according to the usage and the maximum usage includes:
[0012] Obtaining a usage rate of the memory space according to the usage and the maximum usage;
[0013] According to the correspondence between the name of the memory space and the usage rate of the memory space, a first file is generated, and a monitoring result containing the first file is obtained. The first file includes N first mapping relationship groups, where N represents a natural number greater than or equal to 1, and the first mapping relationship group includes a mapping relationship between the name of the memory space and the usage rate of the memory space.
[0014] Further, after generating the first file, the method further includes:
[0015] Based on the first file, adjusting the usage rate of the memory space, wherein adjusting the usage rate of the memory space includes: when the usage rate of the memory space is higher than a preset usage rate, reducing the usage rate of the memory space so that the reduced usage rate of the memory space is less than or equal to the preset usage rate.
[0016] Furthermore, the method further comprises:
[0017] After the application ends, the code file of the application is read to obtain the occupancy of the program component in the code file and the identification information of the program component, wherein the identification information includes the name of the memory space storing the program component, and the program component includes at least one of a code segment, a variable and a function, wherein the code segment represents the code text in the code file, the variable represents the data generated during the operation of the application, and the function is used to encapsulate the specified code so that the specified code is called during the operation of the application to execute the specified instruction.
[0018] Furthermore, after reading the code file of the application, the method further includes:
[0019] Based on the correspondence between the identification information of the program component and the occupancy of the program component, a second file is generated, and a monitoring result containing the second file is obtained. The second file includes a second mapping relationship group, and the second mapping relationship group includes a mapping relationship between the identification information of the program component and the occupancy of the program component.
[0020] Further, after generating the second file, the method further includes:
[0021] Based on the second file, the occupancy of the program component in the code file is adjusted, and the adjustment of the occupancy of the program component in the code file includes: when the occupancy of the program component exceeds a preset occupancy, reducing the occupancy of the program component until the occupancy of the program component is no more than the preset occupancy.
[0022] In a second aspect, an embodiment of the present application proposes a processor, which executes the above method by running program instructions.
[0023] In a third aspect, an embodiment of the present application proposes a computer-readable storage medium, in which a computer program is stored. When the computer program runs on a computer, the computer executes the above method.
[0024] In a fourth aspect, an embodiment of the present application proposes a computer program product, including a computer program, which implements the above method when executed by a processor.
[0025] The invention adopting the above technical solution has the following advantages:
[0026] In the technical solution provided in the present application, memory monitoring is implemented when the application is not running, and the memory space usage rate is statically analyzed based on the keywords marked in the memory space. Compared with the existing technology, it occupies less CPU resources and does not affect the operation of the application, reducing the probability of the application being stuck.
[0027] In the technical solution of the present application, by marking the keywords of the memory space and reading these keywords to obtain the monitoring results, the efficient calculation of the usage rate of the memory space is automatically realized, thereby improving the monitoring efficiency.
[0028] In the technical solution of the present application, a first file is generated based on the monitoring results, providing data support for adjusting and optimizing the remaining amount of memory space.
[0029] In the technical solution of the present application, a second file is generated to provide data support for adjusting and optimizing variables, code segments and functions. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] The present application may be further described by the non-limiting embodiments given in the accompanying drawings. It should be understood that the following drawings only illustrate certain embodiments of the present application and therefore should not be regarded as limiting the scope. For those of ordinary skill in the art, other relevant drawings may be obtained based on these drawings without creative effort.
[0031] Figure 1 An overall flow chart provided for an embodiment of the present application.
[0032] Figure 2 Flow chart of steps S121-S121 provided for the embodiment of the present application
[0033] Figure 3 This is an example of a keyword annotation provided in an embodiment of the present application.
[0034] Figure 4 This is an example of the first file provided in the embodiment of the present application.
[0035] Figure 5 This is an example of the second file provided in the embodiment of the present application.
[0036] Figure 6 An example of a method for optimizing memory space provided in an embodiment of the present application. DETAILED DESCRIPTION
[0037] The present application will be described in detail below in conjunction with the accompanying drawings and specific embodiments. It should be noted that in the drawings or descriptions, similar or identical parts use the same figure numbers, and the implementation methods not shown or described in the drawings are forms known to ordinary technicians in the relevant technical field. In the description of this application, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.
[0038] Please refer to Figure 1 The present application also provides a memory space monitoring method. The memory space monitoring method may include the following steps:
[0039] Step 110, when the application is not running, based on the keywords marked in the memory space, obtain the usage of each memory segment in the memory space and the maximum usage of the memory space;
[0040] Step 120, obtaining a monitoring result of the memory space according to the usage and the maximum usage, wherein the monitoring result includes a usage rate of the memory space.
[0041] In this embodiment, when the application described in step 110 is not running, it means that step 110 and step 120 are executed after the application is finished running or before the application is started. Therefore, the memory space is not monitored during the running of the application, but the memory space is monitored when the application is not running. Steps 110 and 120 provide a static memory monitoring method, which does not affect the running of the program itself in the application, and reduces the CPU occupancy compared to the prior art. At the same time, according to the monitoring results, the memory space can be optimized before the next running of the application.
[0042] The following will describe the steps of the memory space monitoring method in detail as follows:
[0043] Step 110 may include the following steps:
[0044] Step 111: marking a keyword in the memory space, the keyword including a first tag, a second tag and a third tag, the first tag is used to represent the start bit address of the memory space, the second tag is used to represent the end bit address of the memory space, and the third tag is used to represent the usage of each memory segment in the memory space;
[0045] Step 112: Read the keyword and obtain the monitoring result.
[0046] In step 110, a keyword is set on the memory space, and the keyword marks the starting bit address of the memory space, the ending bit address of the memory space, and the usage of each memory segment in the memory space. In this embodiment, the starting bit address of the memory space is the first mark, the ending bit address of the memory space is the second mark, and the usage of each memory segment in the memory space is the third mark.
[0047] For example, the annotation results are as follows: Figure 3 The above is an example of the annotation keywords for a certain memory space in this embodiment, wherein the keyword marked at 0×30000020 is ASW_STACK_START, indicating the first annotation, the keyword marked at 0×3000f02f is ASW_STACK_END, indicating the second annotation, and the middle part, for example, the keyword marked at 0×30004820 is MULTISTACKTRACE_STACK_70PCT, which is the third annotation, indicating that the memory occupancy rate of the memory segment between the memory segment address 0×30004820 and the memory segment address 0×30007820 reaches 70%, and the idle rate is 30%.
[0048] Similarly, other memory space annotation keywords can also be implemented in the above manner.
[0049] For example, when the application is not running, the compiler is started by running the script regularly. After the compiler is started, the compiler runs the relevant script to set the keyword of each memory segment address in the memory space. Then it is compiled into a link file by the compiler. The format can be map and ARXML.
[0050] Exemplarily, a compiler is used to set the range of memory in each memory space, mark the start and end addresses, and set keywords for each memory segment. After compilation, a link script is generated.
[0051] Exemplarily, according to the link script language method, different types of keywords can also be output, such as Trap keywords, memory out-of-bounds keywords, STACK usage keywords, etc.
[0052] In step 110, it can be understood that the size of the memory space can be determined by calculating the difference between the end bit address of the memory space and the start bit address of the memory space according to the first mark and the second mark. The usage of the memory space can be obtained according to the third mark, and then the maximum usage of the memory space can be obtained by combining the first mark, the second mark and the third mark, and the usage rate of the memory space can be obtained by the quotient of the usage and the maximum remaining amount.
[0053] In step 111, the first annotation, the second annotation and the third annotation can be obtained by setting a script and reading the above-mentioned link file. When the application is not running, the usage rate of each memory space is counted.
[0054] In step 111, illustratively, the running logic of the script may be:
[0055] 1. Get the compiled variable name or file name from the compiled file, get the variable / file memory location, and get the variable / file size (bytes).
[0056] 2. The script collects the above content and puts it into the memory statistics table.
[0057] 3. The script obtains the annotation keywords to determine the size, location, unused space size and other information of the memory segment.
[0058] 4. The script uses the memory statistics table to count the memory usage size of each file / variable, the used / free size of each memory segment, and the file size and variable size in each memory segment.
[0059] Exemplarily, the memory space usage can be monitored through step 111.
[0060] In step 112, all monitoring results are summarized to form a first file based on the correspondence between the name of the memory space and the usage rate of the memory space. The first file includes N first mapping relationship groups, and the first mapping relationship groups are used to characterize the mapping relationship between the name of the memory space and the usage rate of the memory space. N is a natural number greater than or equal to 1.
[0061] Exemplarily, the first file is a table file, and the generated table file is as follows: Figure 4 As shown, the names of the memory spaces are represented by RAM0, RAM1, etc., and different memory spaces are used to store different storage information; the "total amount" part is obtained through the first annotation and the second annotation, indicating the memory space storage capacity, and the "usage" column is obtained through the third annotation, wherein the stack usage and the bottom software + application usage can also be obtained by running a specific script, which will not be repeated here, and then through step 112, the parameters of the usage rate column are obtained.
[0062] Figure 4 RAM0 in the figure is mapped to 0.940108, and RAM1 is mapped to 0.874418.
[0063] Exemplarily, the free rate of the memory space can be adjusted according to the first file, and the adjustment method includes: based on the first file, adjusting the usage rate of the memory space, and adjusting the usage rate of the memory space includes: when the usage rate of the memory space is higher than the preset usage rate, reducing the usage rate of the memory space so that the reduced usage rate of the memory space is less than or equal to the preset usage rate.
[0064] For example, Figure 4 In the example, the usage rate of the memory space RAM1 is relatively high (87%), which has exceeded the preset idle rate value of 80%, so the invalid data of RAM1 is deleted to reduce its usage rate.
[0065] The above logic can be implemented by executing control instructions by the host machine, or by executing a specific script.
[0066] After the application ends, this embodiment runs the second script file, reads the code file of the application, obtains the occupancy of the program components in the code file, and the storage location of the program components. The program components in this embodiment include code segments, variables and functions.
[0067] In this embodiment, the code segment can represent the code text in the code file. Exemplarily, the code segment can include machine code or intermediate code generated by the compiler, which defines the behavior and logic of the application. When the application is running, the code segment is loaded into the memory and the instructions of the code segment are executed by the CPU.
[0068] The variables in this embodiment may represent data generated during the running of the application. For example, the data may be temporary storage data generated during the running of the application, including calculation results, user input, and program status, etc. It may also include caching pictures and videos uploaded or downloaded by users, caching user preferences, and storing intermediate results during the calculation process, etc.
[0069] In this embodiment, the function can be used to encapsulate the specified code so that the specified code is called during the running of the application to execute the specified instruction. For example, when the application needs to repeatedly execute a specified instruction, the specified instruction can represent a specified task, and the corresponding function can be called at multiple locations in the application to run the specified code in the function to complete the specified task.
[0070] Exemplarily, the code file is a file generated by the compiler of the application that can parse the variable / function size, such as an ELF file or a MAP file. The code file is also stored in the link file mentioned above, and the script can be used to extract the location and length of each code file's uninitialized variables, initialized variables, code segments, constants, etc. used in each memory segment. The code file also contains all the variable and function declarations of the application.
[0071] Exemplarily, by running the second script, the second script may be written in Python, Shell or other scripting languages, and the second script is used to read the link file.
[0072] The second script can also use regular expressions or parsers to extract information such as variable names, function names, and their data types from the code file. Based on the above information, the second script can determine which variables or functions are allocated to which memory spaces, as well as their specific usage (such as variable size, access frequency, etc.).
[0073] In this embodiment, after the second script parses the code file, a second file is generated, and the second file includes a plurality of second mapping relationship groups. The second mapping relationship groups are used to characterize the mapping relationship between the identification information of the program component and the occupancy of the program component. The identification information includes the name of the file storing the program component and / or the name of the memory space storing the program component.
[0074] Exemplarily, the second file is a file in a table format, such as Figure 5 As shown, one of the manifestations of the second file is exemplarily shown, wherein the first column is a serial number, the second column indicates the name of the file storing the program component, the third column indicates the name of the memory space storing the file, and the fourth column indicates the occupancy of the program component in the second column, in bytes. Among them, in serial number 139, Rte.O is the file name of the file storing a variable in the code file, and the file is stored in a memory space named ASW0_FLASH. The occupancy of the variable is 83106 bytes, then Rte.O, ASW0_FLASH and 83106 constitute a mapping relationship, and all the mapping relationships corresponding to serial numbers 139-160 constitute part of the second mapping relationship group.
[0075] In this embodiment, the purpose of adjusting the variables or code segments in the code file is achieved by generating a second file, and the adjustment method includes: when the occupancy of the program component exceeds the preset occupancy, reducing the occupancy of the program component until the occupancy of the program component is no more than the preset occupancy.
[0076] For example, when a code segment occupies too much space, it may affect the stack space during system operation, and its local variables and parameters can be optimized to reduce its occupancy. Or when some variables are too large to be moved to the memory segment, the characteristics of the variable can be considered, whether it is debugging data, and whether the internal members of the variable can be optimized.
[0077] In at least one embodiment, memory optimization can be achieved through the first file, the second file, and the annotation keyword, such as Figure 6 As shown. Exemplarily, by marking keywords, the maximum usage of memory space is obtained, and it plays a monitoring role. At the same time, after the application is completed, or before the application is started next time, the first script and the second script are run to monitor the usage of memory space and the memory usage of variables, code segments and other parts in the code file, and the first file and the second file are obtained to provide data support for the optimization of memory space.
[0078] Exemplarily, the above method can determine whether the usage rate of memory space and the size of code files are reasonable, and then make optimization strategies. When some variables are too large to be moved to the memory segment, the characteristics of the variable can be considered, whether it is debugging data, and whether the internal members of the variable can be optimized; when a code segment is too large, it may affect the stack space during system operation, and its local variables and parameters can be optimized to reduce its occupancy rate. These optimization measures do not require the system to be running, but are statistical analysis and optimization after compilation.
[0079] Based on this, optimization solutions may include:
[0080] 1. Move the location of variables and functions in the code file, such as Figure 5 As shown, if the memory margin of the ASW1_Flash memory space is small, the .O file of 140 is transferred to the ASW4_Flash memory space.
[0081] 2. Expand / reduce memory space, such as Figure 4 In the example, the usage rate of memory space RAM1 is higher, exceeding 87%, while the usage rate of RAM4 is lower, only 67%. Therefore, the remaining amount of RAM4 will be reduced and the remaining amount of RAM1 will be increased.
[0082] 3. Optimize variable / function logic, that is, optimize variables or functions to reduce the memory they occupy.
[0083] The memory space monitoring method proposed in this embodiment is suitable for embedded systems because embedded systems usually have the characteristics of limited resources, high real-time requirements, and strong specialization. In embedded systems, memory resources are precious and need to be carefully managed. Therefore, the memory space monitoring method of this embodiment is performed before and after the application is run, which occupies less CPU resources, thereby ensuring the operation of the application.
[0084] At the same time, since large structures, debug messages, communication caches and other large message bodies are often encountered in embedded system applications, if they are not managed and monitored, the memory will be slowly consumed as the system runs for a long time, which will cause the system to hang. In addition, the application is constantly updated, increasing memory usage, and memory management becomes more difficult. For memory monitoring and optimization, the memory usage rate of the application can be counted in advance, memory space can be reasonably allocated, and large messages can be optimized. Therefore, it is also necessary to monitor the memory of the embedded system through this method.
[0085] An embodiment of the present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer program runs on a computer, the computer executes the memory space monitoring method described in the above embodiment.
[0086] An embodiment of the present application also provides a computer program product, including a computer program, which implements the above-mentioned memory space monitoring method when executed by a processor.
[0087] Through the description of the above implementation methods, technical personnel in this field can clearly understand that the present application can be implemented by hardware, and can also be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.), including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each implementation scenario of the present application.
[0088] This embodiment also proposes a processor, which executes the above-mentioned memory space monitoring method by running program instructions. In this embodiment, the processor can be a central processing unit (CPU), a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, and can implement or execute the disclosed methods, steps and logic block diagrams in the embodiments of this application.
[0089] In the embodiments provided by the present application, it should be understood that the disclosed method can also be implemented in other ways. The method embodiments described above are merely schematic, for example, the flow charts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the systems, methods and computer program products according to the multiple embodiments of the present application. In this regard, each box in the flow chart or block diagram can represent a part of a module, a program segment or a code, and a part of the module, program segment or code includes one or more executable instructions for implementing the specified logical function. It should also be noted that each box in the block diagram and / or the flow chart, and the combination of the boxes in the block diagram and / or the flow chart can be implemented by a dedicated hardware-based system that performs the specified function or action, or can be implemented by a combination of dedicated hardware and computer instructions. In addition, each functional module in each embodiment of the present application can be integrated together to form an independent part, or each module can exist separately, or two or more modules can be integrated to form an independent part.
[0090] The above description is only an embodiment of the present application and is not intended to limit the protection scope of the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A memory space monitoring method, characterized in that: include: When the application is not running, based on the keywords marked in the memory space, obtaining the usage of each memory segment in the memory space and the maximum usage of the memory space; A monitoring result of the memory space is obtained according to the usage and the maximum usage, and the monitoring result includes a usage rate of the memory space.
2. The method according to claim 1, characterized in that The obtaining, based on the keyword marked in the memory space, the usage of each memory segment in the memory space and the maximum usage of the memory space includes: Marking the keyword in the memory space, the keyword includes a first mark, a second mark and a third mark, the first mark is used to represent the start bit address of the memory space, the second mark is used to represent the end bit address of the memory space, and the third mark is used to represent the usage of each memory segment in the memory space; The keyword is read to obtain the usage and the maximum usage.
3. The method according to claim 2, characterized in that The obtaining the monitoring result of the memory space according to the usage and the maximum usage includes: Obtaining a usage rate of the memory space according to the usage and the maximum usage; According to the correspondence between the name of the memory space and the usage rate of the memory space, a first file is generated, and a monitoring result containing the first file is obtained. The first file includes N first mapping relationship groups, where N represents a natural number greater than or equal to 1, and the first mapping relationship group includes a mapping relationship between the name of the memory space and the usage rate of the memory space.
4. The method according to claim 3, characterized in that After generating the first file, the method further includes: Based on the first file, adjusting the usage rate of the memory space, wherein adjusting the usage rate of the memory space includes: when the usage rate of the memory space is higher than a preset usage rate, reducing the usage rate of the memory space so that the reduced usage rate of the memory space is less than or equal to the preset usage rate.
5. The method according to claim 1, characterized in that: The method further comprises: After the application ends, the code file of the application is read to obtain the occupancy of the program component in the code file and the identification information of the program component, wherein the identification information includes the name of the memory space storing the program component, and the program component includes at least one of a code segment, a variable and a function, wherein the code segment represents the code text in the code file, the variable represents the data generated during the operation of the application, and the function is used to encapsulate the specified code so that the specified code is called during the operation of the application to execute the specified instruction.
6. The method according to claim 5, characterized in that After reading the code file of the application, the method further includes: Based on the correspondence between the identification information of the program component and the occupancy of the program component, a second file is generated, and a monitoring result containing the second file is obtained. The second file includes a second mapping relationship group, and the second mapping relationship group includes a mapping relationship between the identification information of the program component and the occupancy of the program component.
7. The method according to claim 6, characterized in that After generating the second file, the method further includes: Based on the second file, the occupancy of the program component in the code file is adjusted, and the adjustment of the occupancy of the program component in the code file includes: when the occupancy of the program component exceeds a preset occupancy, reducing the occupancy of the program component until the occupancy of the program component is no more than the preset occupancy.
8. A processor, characterized in that: The processor executes the method according to any one of claims 1 to 7 by running program instructions.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed on a computer, the computer is enabled to execute the method according to any one of claims 1 to 7.
10. A computer program product, characterized in that The invention comprises a computer program which, when executed by a processor, implements the method according to any one of claims 1 to 7.