Dynamic memory management method based on one-way queue, electronic equipment and medium
By introducing a management method for the main memory and auxiliary memory of the queue into a unidirectional queue, the problem of high frequency of queue element copying and release is solved, and memory utilization is improved.
Patent Information
- Application Number
- CN202511962510.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-24
- Publication Date
- 2026-01-23
- Estimated Expiration
- 2045-12-24
AI Technical Summary
In existing technologies, queues without a specified container capacity undergo frequent copying and memory release of queue elements during dynamic changes, resulting in low memory utilization.
A dynamic memory management method using a unidirectional queue is adopted. By initializing the main memory and auxiliary memory of the queue, the main memory of the queue is first used to store elements. When it is insufficient, the auxiliary memory of the queue is used to expand the memory capacity and reduce the frequency of copying and releasing queue elements.
It improves the memory utilization of unidirectional queues and reduces the frequency of copying and releasing queue elements during dynamic queue changes.
Smart Images

Figure CN121387570A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of memory management, in particular to a dynamic memory management method based on a one-way queue, an electronic device and a medium. BACKGROUND
[0002] A queue includes a queue with a specified capacity and a queue without a specified container capacity. For the queue without a specified container capacity, unlimited queue elements can be added or deleted. In the prior art, an initial memory is allocated for the queue without a specified container capacity. When the initial memory is insufficient to meet the memory requirement, a larger capacity memory needs to be applied, the queue elements in the original memory are copied to the newly applied memory, and the original memory is released. In general, the newly applied memory is an integer multiple of the original memory, which is easy to cause long-term idling of the allocated memory and low memory utilization. Moreover, each time a larger capacity memory is applied, all queue elements need to be copied to the newly applied memory, and the original memory is released, which is high in frequency of queue element copying and memory releasing. The deletion of data of the queue without a specified container capacity can further cause the memory idling problem. When the memory idling occurs, a smaller memory can be applied to copy all queue elements in the original memory to the smaller memory and release the original memory, but this further increases the frequency of queue element copying and memory releasing. Therefore, how to reduce the frequency of queue element copying and memory releasing in the dynamic change process of the queue and improve the queue memory utilization has become a technical problem to be solved. SUMMARY
[0003] The present application aims to provide a dynamic memory management method based on a one-way queue, an electronic device and a medium, which reduces the frequency of queue element copying and memory releasing in the dynamic change process of the one-way queue and improves the queue memory utilization of the one-way queue.
[0004] According to a first aspect of the present application, a dynamic memory management method based on a one-way queue is provided, comprising: Step S1, initializing a queue main memory with a memory size of Ca, and initially setting a current queue element number Ba=0, wherein the queue is a one-way queue without a specified container capacity; Step S2, obtaining a queue element to be inserted into the queue, and if BaCa, executing step S3, otherwise, executing step S4; Step S3, inserting the queue element to be inserted into the queue main memory, and setting Ba=Ba+1, and returning to step S2; Step S4, if Ba=Ca, executing step S6, otherwise, executing step S5; Step S5, if Ba=2×Ca, executing step S8, otherwise, executing step S7, Step S6, initialize the queue auxiliary memory with size Ca, set the queue auxiliary memory and the queue main memory as logically continuous queue memory, and execute step S7; Step S7, insert the queue element to be inserted into the queue auxiliary memory, set Ba=Ba+1, and return to step S2; Step S8, set Ca=2xCa, initialize the new queue main memory with size Ca, re-write the queue elements in the original queue main memory and the queue auxiliary memory into the new queue main memory, release the original queue main memory and the queue auxiliary memory, initialize the new queue auxiliary memory with size Ca, insert the queue element to be inserted into the new queue auxiliary memory, and return to step S2.
[0005] According to the second aspect of the present application, an electronic device is provided, comprising at least one processor, and a memory connected with the at least one processor in communication; wherein the memory stores instructions executed by the at least one processor, and the instructions are configured to execute the method according to the first aspect of the present application.
[0006] According to the third aspect of the present application, a computer readable storage medium is provided, which stores computer executable instructions, and the computer executable instructions are configured to execute the method according to the first aspect of the present application.
[0007] Compared with the prior art, the present application has obvious advantages and beneficial effects. By means of the above technical scheme, the dynamic memory management method based on the one-way queue, the electronic device and the medium provided by the present application can achieve considerable technical progress and practicality, and have wide industrial utilization value. At least the following beneficial effects are achieved: The present application stores the queue elements by setting the queue main memory and the queue auxiliary memory, enables the queue main memory first, then enables the queue auxiliary memory, and when the queue main memory and the queue auxiliary memory are insufficient to meet the queue element storage requirement, updates the size of the queue main memory and the queue auxiliary memory to expand, thereby reducing the frequency of copying and releasing the memory of the queue elements in the dynamic change process of the one-way queue, and improving the queue memory utilization rate of the one-way queue. BRIEF DESCRIPTION OF DRAWINGS
[0008] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0009] Figure 1 The flowchart of the dynamic memory management method based on the one-way queue provided by the embodiments of the present application is shown. DETAILED DESCRIPTION
[0010] The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by a person of ordinary skill in the art without creative effort belong to the scope of protection of the present application.
[0011] The embodiment of the present application provides a dynamic memory management method based on a single-direction queue, as shown in the figure, comprising: Figure 1 Step S1, initializing a queue main memory with a memory size of Ca, and initially setting a current queue element number Ba=0, wherein the queue is a single-direction queue without a specified container capacity.
[0012] The single-direction queue refers to a queue in which queue elements can only be inserted or deleted at the head or tail of the queue. The queue can be generated based on SystemVerilog, C++, Java, Go, Swift and the like.
[0013] Step S2, obtaining a to-be-inserted queue element of the queue, and if Ba
[0014] It can be understood that if Ba
[0015] Step S3, inserting the to-be-inserted queue element into the queue main memory, setting Ba=Ba+1, and returning to step S2.
[0016] It should be noted that since it is a single-direction queue, the physical order of the queue elements in the queue main memory is the same as that of a double-direction queue, and therefore the to-be-inserted queue element can be directly inserted into the queue main memory in order.
[0017] Step S4, if Ba=Ca, executing step S6, otherwise, executing step S5.
[0018] It should be noted that if Ba=Ca, the queue main memory is full at this time, and at this time, the queue auxiliary memory is enabled through step S6. It can be understood that the queue auxiliary memory is not started before the queue main memory meets the queue element storage requirement, thereby reducing idle memory and improving queue memory utilization.
[0019] Step S5, if Ba=2×Ca, executing step S8, otherwise, executing step S7.
[0020] It should be noted that in step S5, when Ba≠2×Ca, at this time, Ca<Ba<2×Ca, the queue element to be inserted is inserted into the queue auxiliary memory.
[0021] In step S6, the queue auxiliary memory with the size of Ca is initialized, the queue auxiliary memory and the queue main memory are set as the logically continuous queue memory, and step S7 is executed.
[0022] It should be noted that the queue auxiliary memory is a physically continuous memory, the queue auxiliary memory is also a physically continuous memory, the queue auxiliary memory and the queue main memory can be physically continuous or discontinuous, but the queue auxiliary memory and the queue main memory are set as the logically continuous queue memory, which can be logically understood as that the last memory unit of the queue auxiliary memory is connected with the first memory unit of the queue auxiliary memory.
[0023] In step S7, the queue element to be inserted is inserted into the queue auxiliary memory, Ba is set as Ba+1, and step S2 is returned.
[0024] In step S8, Ca is set as 2×Ca, the new queue main memory with the size of Ca is initialized, the queue elements in the original queue main memory and the queue auxiliary memory are written into the new queue main memory, the original queue main memory and the queue auxiliary memory are released, the new queue auxiliary memory with the size of Ca is initialized, the queue element to be inserted is inserted into the new queue auxiliary memory, and step S2 is returned.
[0025] It should be noted that since it is a single queue, the physical order of the queue elements in the queue memory composed of the queue main memory and the queue auxiliary memory is the same as that of the double queue, and therefore the queue elements in the original queue main memory and the queue auxiliary memory can be directly written into the new queue main memory.
[0026] In the embodiment of the application, the queue main memory is first used, the queue elements are stored into the queue main memory, when the queue main memory is insufficient to meet the queue element storage requirement, the queue auxiliary memory is then used, at this time, the queue elements in the queue main memory do not need to be copied again, and the queue main memory does not need to be released, thereby reducing the frequency of the queue element copying and the memory releasing in the queue dynamic change process. When the queue auxiliary memory is also insufficient to meet the memory requirement, the new queue main memory and the new auxiliary memory with larger sizes are set, the original queue main memory and the queue auxiliary memory are released, and the queue main memory and the queue auxiliary memory are used in cooperation, which can reduce the memory idle condition and improve the queue memory utilization rate.
[0027] As an embodiment, the queue supports inserting or deleting queue elements at the tail, and in step S8, queue elements in the original queue main memory and the queue auxiliary memory are written into the new queue main memory, including: In step S81, queue elements in the original queue main memory and the queue auxiliary memory are directly spliced and stored into the new queue main memory.
[0028] It should be noted that, since the queue is a single-direction queue, in the present example, only insertion of queue elements at the tail is supported, and the queue auxiliary memory and the queue main memory are set as logically continuous queue memory, thus the physical storage order of queue elements in the original queue main memory and the queue auxiliary memory is consistent with the storage order of queue elements in the queue, and thus queue elements in the original queue main memory and the queue auxiliary memory can be directly spliced and stored into the new queue main memory.
[0029] It should be noted that, the queue can also support insertion or deletion of queue elements at the head, which is similar to the queue supporting insertion or deletion of queue elements at the tail, and thus will not be described again.
[0030] As an embodiment, the queue main memory includes Ca memory units, the queue auxiliary memory includes Ca memory units, and queue elements in the queue, memory units in the queue main memory and memory units in the queue auxiliary memory are all numbered in sequence from x, specifically, all are numbered in sequence from x with the same length, x can take values of 0, 1, 2, 3,..., and preferably, x takes values of 0 or 1. The method further includes: In step S10, a queue element to be accessed is obtained, and the queue element to be accessed is denoted as Q, Q being the corresponding serial number of the queue element to be accessed in the queue.
[0031] In step S20, if the serial number of the queue element to be accessed is less than or equal to x+Ca-1, step S30 is executed, otherwise, step S40 is executed.
[0032] It should be noted that, if the serial number of the queue element to be accessed is less than or equal to x+Ca-1, it indicates that the queue element to be accessed is located in the queue main memory, and if the serial number of the queue element to be accessed is greater than x+Ca-1, it indicates that the queue element to be accessed is located in the queue auxiliary memory.
[0033] In step S30, the queue element in the memory unit with serial number Q in the queue main memory is determined as the queue element to be accessed, and the process is returned and ended.
[0034] In step S40, the queue element in the memory unit with serial number Q-Ca in the queue auxiliary memory is determined as the queue element to be accessed, and the process is returned and ended.
[0035] It should be noted that the queue element can be accessed in the queue memory quickly and accurately based on the corresponding sequence number of the queue element to be accessed in the queue through steps S10-S40.
[0036] The queue element can be not only inserted into the queue but also deleted. As an embodiment, the method further comprises: Step C00, if the instruction of deleting the queue element is received, the first queue element at the tail of the queue is deleted from the queue memory, and Ba=Ba-1 is set.
[0037] It should be noted that if the number of the queue elements to be deleted is present in the instruction of deleting the queue element, the queue elements at the tail of the queue can also be deleted in batches.
[0038] With the increase of the number of the deleted queue elements, the number of the idle queue memory also increases. In order to increase the utilization rate of the queue memory, when the number of the queue elements in the queue memory is less than a certain value, the queue memory can be shrunk. As an embodiment, the method further comprises: Step C10, if Ba<the first threshold value, the first threshold value is greater than Ca / 2 and less than Ca.
[0039] It should be noted that if the first threshold value is less than Ca, it indicates that the queue elements in the current queue memory are only stored in the queue main memory. At this time, if the queue auxiliary memory exists, the queue auxiliary memory is in an idle state. At this time, the queue auxiliary memory can be released to improve the utilization rate of the memory.
[0040] Step C20, it is judged whether the queue auxiliary memory currently exists. If it exists, step C30 is executed, otherwise, step C40 is executed.
[0041] It should be noted that under the premise of Ba<the first threshold value, if the queue auxiliary memory currently does not exist, it is further judged whether Ca needs to be reduced.
[0042] Step C30, the queue auxiliary memory is released.
[0043] Step C40, if Ba<the second threshold value, the second threshold value is less than or equal to Ca / 2, step C50 is executed.
[0044] Step C50, Ca=Ca / 2 is set, a new queue main memory with the size of Ca is initialized, the data in the original queue main memory is written into the new queue main memory again, and the original queue main memory is released.
[0045] As a preferred embodiment, the first threshold value is set to 2Ca / 3, and the second threshold value is set to Ca / 3.
[0046] It can be understood that Ca is dynamically adjusted according to the change of Ba, and steps C10-step C50 are a process of reducing the size of the queue memory, and if there are new elements to be inserted into the queue subsequently, the process jumps to step S2 for execution.
[0047] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used in the description herein is for describing specific embodiments only and is not intended to be limiting. As used herein, the term "and / or" includes any and all combinations of one or more of the associated listed items.
[0048] It is to be understood that some of the example embodiments are described in terms of a process or method being depicted as a flowchart. Although a flowchart can describe a process as a sequential process, many of the steps can be performed in parallel, concurrently or simultaneously. In addition, the order of the steps can be re-arranged. A process can be terminated when its operations are completed, but could also occur under some other condition or event. The processes might correspond to methods, functions, procedures, subroutines, subprograms, etc. When a process corresponds to a function, its termination can correspond to a return of the function to the calling function or the main function.
[0049] The embodiments of the present application further provide an electronic device, comprising: at least one processor; and a memory connected with the at least one processor in communication; wherein the memory stores instructions executed by the at least one processor, and the instructions are arranged to execute the method according to the embodiments of the present application.
[0050] The embodiments of the present application further provide a computer readable storage medium, which stores computer executable instructions, and the computer executable instructions are used for executing the method according to the embodiments of the present application.
[0051] The embodiments of the present application store queue elements by setting a queue main memory and a queue auxiliary memory, enable the queue main memory first, then enable the queue auxiliary memory, and update the size of the queue main memory and the queue auxiliary memory to expand when the queue main memory and the queue auxiliary memory are insufficient to meet the queue element storage requirement, thereby reducing the frequency of copying and releasing memory of queue elements in the dynamic change process of the single queue, and improving the queue memory utilization rate of the single queue.
[0052] The above merely describes preferred embodiments of the present application, and is not intended to limit the present application in any form. Although the present application has been disclosed with the preferred embodiments as above, it is not intended to limit the present application, and any person skilled in the art can make some changes or modifications to the above disclosed technical contents to obtain equivalent embodiments with equivalent changes, as long as the changes or modifications do not depart from the technical solution of the present application. Any simple modification, equivalent change and modification made to the above embodiments according to the technical essence of the present application still belong to the scope of the technical solution of the present application.
Claims
1. A dynamic memory management method based on a unidirectional queue, characterized in that, Comprising: Step S1: Initialize the main memory of the queue with a size of Ca, initially set the current number of queue elements Ba = 0, and the queue is a single-direction queue without a specified container capacity; Step S2: Obtain the queue element to be inserted into the queue. If Ba < Ca, execute Step S3; otherwise, execute Step S4; Step S3: Insert the queue element to be inserted into the main memory of the queue, set Ba = Ba + 1, and return to Step S2; Step S4: If Ba = Ca, execute Step S6; otherwise, execute Step S5; Step S5: If Ba = 2×Ca, then execute Step S8; otherwise, execute Step S7; Step S6: Initialize the auxiliary memory of the queue with a size of Ca, set the auxiliary memory of the queue and the main memory of the queue as logically continuous queue memories, and execute Step S7; Step S7: Insert the queue element to be inserted into the auxiliary memory of the queue, set Ba = Ba + 1, and return to Step S2; Step S8: Set Ca = 2×Ca, initialize a new main memory of the queue with a size of Ca, rewrite the queue elements in the original main memory of the queue and the auxiliary memory of the queue into the new main memory of the queue, release the original main memory of the queue and the auxiliary memory of the queue, initialize a new auxiliary memory of the queue with a size of Ca, insert the queue element to be inserted into the new auxiliary memory of the queue, and return to Step S2.
2. The method according to claim 1, wherein: The queue supports inserting or deleting queue elements at the tail of the queue. In Step S8, rewriting the queue elements in the original main memory of the queue and the auxiliary memory of the queue into the new main memory of the queue includes: Step S81: Directly splice the queue elements in the original main memory of the queue and the auxiliary memory of the queue and store them in the new main memory of the queue.
3. The method according to claim 1, wherein: The main memory of the queue includes Ca memory units, the auxiliary memory of the queue includes Ca memory units, the queue elements in the queue, the memory units in the main memory of the queue, and the memory units in the auxiliary memory of the queue are numbered in sequence starting from x. The method further includes: Step S10: Obtain the label Q of the queue element to be accessed in the queue, where Q is the serial number corresponding to the queue element to be accessed in the queue; Step S20: If the label of the queue element to be accessed is less than or equal to x + Ca - 1, then execute Step S30; otherwise, execute Step S40; Step S30: Determine the queue element in the memory unit with the label Q in the main memory of the queue as the queue element to be accessed, and return to end the access process; Step S40: Determine the queue element in the memory unit with the label Q - Ca in the auxiliary memory of the queue as the queue element to be accessed, and return to end the access process.
4. The method according to claim 1, wherein: The method further includes: Step C00: If a queue element deletion instruction is received, delete the first queue element at the tail from the queue memory, and set Ba = Ba - 1.
5. The method according to claim 4, wherein: The method further includes: Step C10: If Ba < the first threshold, where the first threshold is greater than Ca / 2 and less than Ca; Step C20: Determine if a queue auxiliary memory exists. If it exists, proceed to step C30; otherwise, proceed to step C40. Step C30: Release the queue auxiliary memory; Step C40: If Ba < the second threshold, and the second threshold is less than or equal to Ca / 2, proceed to step C50; Step C50: Set Ca = Ca / 2, initialize a new queue main memory of size Ca, rewrite the data in the original queue main memory into the new queue main memory, and release the original queue main memory.
6. The method according to claim 5, characterized in that, The first threshold is set to 2Ca / 3.
7. The method according to claim 5, characterized in that, The second threshold is set to Ca / 3.
8. The method according to claim 1, characterized in that, The queues are generated using SystemVerilog, C++, Java, Go, or Swift.
9. An electronic device, characterized in that, include: At least one processor; And, a memory communicatively connected to the at least one processor; The memory stores instructions that are executed by the at least one processor, the instructions being configured to perform the method of any one of claims 1-8.
10. A computer-readable storage medium, characterized in that, The device stores computer-executable instructions for performing the method of any one of claims 1-8.
Citation Information
Patent Citations
Method and device for data storage
CN104008067A
Queue processing method and device, equipment and storage medium
CN118245241A
A message processing method, device, electronic device and storage medium
CN119759549A
Elastic scaling method and apparatus for number of hosts in distributed system, and computer device
WO2020211253A1
