Program running method, device and equipment
By dynamically adjusting the pointer length and memory block allocation, the problem of pointer compression failing in a large memory environment in the prior art is solved, and efficient compression and memory saving of object pointers are achieved.
Patent Information
- Application Number
- CN202110153118.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-02-03
- Publication Date
- 2025-08-19
- Estimated Expiration
- 2041-02-03
AI Technical Summary
Under the X86-64 architecture, the pointer compression method in the prior art fails when the JVM heap memory exceeds 32GB, resulting in excessive memory pointers that cannot be effectively compressed.
According to the preset number of objects managed by the program, the pointer length is dynamically adjusted, and memory blocks are divided from continuous memory space to generate object pointers, and the memory address of the object is obtained for access through calculation.
It effectively compresses the pointer length of the object pointer, reduces memory usage, avoids pointer compression failure, is suitable for different memory environments, and improves applicability.
Smart Images

Figure CN112988379B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a program running method, apparatus, and device. Background Art
[0002] Currently, on the x86-64 architecture, the pointer size (i.e., pointer length) is 8 bytes. Programs access memory objects through pointers. In enterprise storage and big data, the number of memory objects is often very large, and the memory occupied by pointers to these objects is also very large. For example, if there are 1 billion memory objects, the memory occupied by pointers can reach as much as 8GB.
[0003] In the prior art, the Java Virtual Machine (JVM) uses a fixed-byte alignment compression method to compress pointers. When pointer compression is enabled, the JVM allocates memory using 8-byte alignment, addresses it in 8-byte units, and uses a pointer length of 4 bytes, allowing it to address a maximum of 32GB of memory. However, this pointer compression method has limitations. When the JVM's heap memory exceeds 32GB, pointer compression fails. Summary of the Invention
[0004] In view of the above problems, the present application is proposed to provide a program running method, device and equipment that solve the above problems or at least partially solve the above problems.
[0005] Therefore, in one embodiment of the present application, a program running method is provided. The method includes:
[0006] determining a pointer length based on a predetermined number of objects managed by the program;
[0007] A first memory block for allocating to the object is divided from the continuous memory space applied for by the program;
[0008] generating an object pointer of the pointer length for the object based on the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block; the second memory blocks are used to be allocated to other objects; the size of the first memory block and each of the second memory blocks is a preset object size; the addresses of any two adjacent memory blocks that have been divided in the continuous memory space are continuous;
[0009] When the object needs to be accessed, the memory address of the object is obtained according to the starting address of the continuous memory space, the preset object size and the object pointer, so as to access the object.
[0010] In another embodiment of the present application, a program execution device is provided. The program execution device includes:
[0011] a determination module, configured to determine a pointer length based on a preset number of objects managed by the program;
[0012] a partitioning module, configured to partition a first memory block for allocation to an object from the continuous memory space applied for by the program;
[0013] a pointer generation module, configured to generate an object pointer of the specified pointer length for the object based on the number of second memory blocks that have been allocated in the continuous memory space and are located before the first memory block; the second memory blocks are used to be allocated to other objects; the size of the first memory block and each of the second memory blocks is a preset object size; and the addresses of any two adjacent memory blocks that have been allocated in the continuous memory space are continuous;
[0014] The acquisition module is used to obtain the memory address of the object according to the starting address of the continuous memory space, the preset object size and the object pointer when the object needs to be accessed, so as to access the object.
[0015] In another embodiment of the present application, an electronic device is provided. The electronic device includes: a memory and a processor, wherein:
[0016] The memory is used to store programs;
[0017] The processor is coupled to the memory and is configured to execute the program stored in the memory to:
[0018] determining a pointer length based on a predetermined number of objects managed by the program;
[0019] A first memory block for allocating to the object is divided from the continuous memory space applied for by the program;
[0020] generating an object pointer of the pointer length for the object based on the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block; the second memory blocks are used to be allocated to other objects; the size of the first memory block and each of the second memory blocks is a preset object size; the addresses of any two adjacent memory blocks that have been divided in the continuous memory space are continuous;
[0021] When the object needs to be accessed, the memory address of the object is obtained according to the starting address of the continuous memory space, the preset object size and the object pointer, so as to access the object.
[0022] In the technical solution provided by the embodiment of the present application, the object pointer can obtain the memory address of the object after calculation, and the object can be normally accessed according to the memory address of the object. The pointer length of the object pointer is determined by the preset number of objects managed by the program, rather than by the number of addressing bits of the central processing unit (CPU), which effectively compresses the pointer length of the object pointer and reduces the amount of memory occupied by the object pointer. Moreover, the pointer length of the object pointer is determined by the preset number of objects managed by the program, so that all objects managed by the program can correspond to different object pointers, effectively avoiding the situation in which pointer compression fails due to excessive memory settings in existing pointer compression solutions, and has good applicability. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0024] Figure 1 A flowchart of a program running method provided in one embodiment of the present application;
[0025] Figure 2 This is an example diagram of the structure of an object pointer provided in one embodiment of the present application;
[0026] Figure 3 This is a diagram of an example of memory partitioning provided in one embodiment of the present application;
[0027] Figure 4 A structural block diagram of a program execution device provided in one embodiment of the present application;
[0028] Figure 5 This is a structural block diagram of an electronic device provided in another embodiment of the present application. DETAILED DESCRIPTION
[0029] In order to enable those skilled in the art to better understand the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0030] In addition, some of the processes described in the specification, claims and the above-mentioned figures of this application include multiple operations that appear in a specific order. These operations may not be executed in the order in which they appear in this document or may be executed in parallel. The serial numbers of the operations, such as 101, 102, etc., are only used to distinguish between different operations, and the serial numbers themselves do not represent any order of execution. In addition, these processes may include more or fewer operations, and these operations may be executed in sequence or in parallel. It should be noted that the descriptions of "first", "second", etc. in this document are used to distinguish different messages, devices, modules, etc., and do not represent the order of precedence, nor do they limit "first" and "second" to be different types.
[0031] Figure 1 FIG. 1 shows a flow chart of a program running method provided by an embodiment of the present application. Figure 1 As shown, the method includes:
[0032] 101. Determine a pointer length according to a preset number of objects managed by the program.
[0033] 102. A first memory block for allocation to the object is divided from the continuous memory space applied for by the program.
[0034] 103. Generate an object pointer of the pointer length for the object according to the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block.
[0035] The second memory block is used to be allocated to other objects; the sizes of the first memory block and each of the second memory blocks are preset object sizes; and the addresses of any two adjacent memory blocks divided in the continuous memory space are continuous.
[0036] 104. When the object needs to be accessed, the memory address of the object is obtained according to the starting address of the continuous memory space, the preset object size, and the object pointer, so as to access the object.
[0037] In the above 101, the program may specifically include a driver or an application. The preset number of objects managed by the program can be understood as the maximum number of objects managed by the program. In actual application, the preset number of objects can be configured in advance by the user; or it can be calculated based on the memory capacity allocated to the program by the computer and the preset object size. The memory capacity can be understood as the maximum memory size that the program can use. The preset object size is also the size of the objects managed by the program. Usually, an object includes an object header and instance data in memory, that is, the object size is the sum of the size of its object header and instance data. In scenarios where performance is required, the object may also include alignment padding bytes in memory, that is, the object size is the sum of the size of its object header, instance data, and alignment padding bytes.
[0038] For example, the object managed by the program is a routing table entry. Typically, a routing device stores a routing table; each entry in the routing table can be considered an object in this application.
[0039] In this application, pointer length refers to the number of bits of the pointer. The longer the pointer length, the more bits the pointer has. The pointer length depends on the number of preset objects managed by the program. In this way, different objects managed by the program can correspond to different object pointers, and there is no compression failure. Note: The different object pointers here refer to the different contents of the object pointers. Taking the pointer length of 3 as an example, object pointer P1 = 001; object pointer P2 = 010; 001 is the content of object pointer P1; 010 is the content of object pointer P2, and the two contents are different.
[0040] Specifically, the number of preset objects is greater than 2 n-1 and less than or equal to 2 n When the pointer length is set to be greater than or equal to n bits, compression failure can be avoided. Since the longer the pointer length is, the more memory the object pointer occupies. To reduce memory consumption, in one feasible solution, the pointer length can be set to n bits. In this embodiment, all n bits of the object pointer are used to represent the number of the second memory block.
[0041] For example: the default number of objects is 2 32 (about 2.15 billion), the object pointer length can be set to 32 bits, that is, 4 bytes, which is half the size of the 8-byte pointer in the prior art; for example: the preset number of objects is 2 16 , the object pointer length can be set to 16 bits, that is, 2 bytes, which is 6 bytes less than the 8-byte pointer in the prior art.
[0042] In step 102 above, the contiguous memory space may be obtained by the program from the operating system during initialization. In one feasible solution, the total memory size to be requested can be determined based on the number of preset objects managed by the program and the size of the preset objects; and a request is made from the operating system for the contiguous memory space equal to the total memory size. After the contiguous memory space is requested, the starting address of the contiguous memory space is recorded.
[0043] Based on the object size of the object, a first memory block is allocated from the continuous memory space for allocation to the object. That is, the size of the first memory block is equal to the object size of the object. In an example application, the object size of the object may also be the preset object size.
[0044] In the above 103, the size of the first memory block and each of the second memory blocks is a preset object size. The addresses of any two adjacent memory blocks divided in the continuous memory space are continuous. The addresses of two adjacent memory blocks are continuous when the end address of the first memory block and the start address of the second memory block are continuous.
[0045] In one example, the starting address of the second memory block at the front of the continuous memory space may be the starting address of the continuous memory space. It should be noted that if the number of second memory blocks is 0, it indicates that the first memory block is the first memory block divided from the continuous memory. In this case, the starting address of the first memory block may be the starting address of the continuous memory space.
[0046] An object pointer of the pointer length is generated for the object based on the number of second memory blocks that have been divided out of the continuous memory space and are located before the first memory block. In one example, the number of the second memory blocks can be converted into a binary number of the pointer length to serve as the object pointer for the object.
[0047] In actual application, after an object pointer of the pointer length is generated for an object, it needs to be saved in memory for subsequent use.
[0048] 104. When accessing the object is required, the memory address of the object is calculated based on the starting address of the continuous memory space, the preset object size, and the object pointer. The object can then be accessed based on the memory address of the object. The memory address of the object is also the first address of the first memory block.
[0049] In the technical solution provided by the embodiment of the present application, the object pointer can obtain the memory address of the object after calculation, and the object can be normally accessed according to the memory address of the object. The pointer length of the object pointer is determined by the preset number of objects managed by the program, rather than by the addressing bit number of the central processing unit (CPU), which effectively compresses the pointer length of the object pointer and reduces the amount of memory occupied by the object pointer. Moreover, since the pointer length of the object pointer is determined by the preset number of objects managed by the program, all objects managed by the program can correspond to different object pointers, effectively avoiding the situation in which pointer compression fails due to excessive memory settings in existing pointer compression solutions, and has good applicability.
[0050] In actual application, the above-mentioned continuous memory space can be a continuous virtual memory space; the memory address of the above-mentioned object can be the virtual memory address of the object; the physical memory address of the object can be obtained by mapping according to the virtual memory address of the object, and then access can be performed. The specific mapping principle and process can be referred to the prior art and will not be described here. In addition, in this embodiment, the use of virtual memory technology can also reduce the waste of physical memory. This is because: only when the virtual memory address is accessed for the first time (which can be understood as the first time data is written to the applied memory block) will the operating system allocate actual physical memory space for it.
[0051] Furthermore, compared to prior art solutions that require 8-byte alignment of object memory addresses, the object pointers provided by the embodiments of the present application do not require fixed alignment of object memory addresses, thus avoiding memory waste. Furthermore, compared to prior art pointer compression solutions, the object pointers provided by the embodiments of the present application are not limited by the size of the computer's memory; compressed pointers remain valid even when the computer's memory exceeds 32GB. Furthermore, prior art pointer compression solutions are only applicable to the JVM runtime environment, while the pointer compression solution provided by the embodiments of the present application is adaptable to any runtime environment, demonstrating strong applicability.
[0052] In one feasible solution, the step of “obtaining the memory address of the object according to the starting address of the continuous memory space, the preset object size, and the object pointer” in step 104 can be implemented by the following steps:
[0053] 1041. Determine, based on the object pointer, the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block.
[0054] 1042. Determine an address offset of the object in the continuous memory space according to the number of the second memory blocks and the preset object size.
[0055] 1043. Obtain the memory address of the object according to the address offset and the starting address of the continuous memory space.
[0056] In step 1041 above, when accessing an object is required, the program can obtain an object pointer from the memory. Since the object pointer is generated based on the number of second memory blocks that have been allocated in the continuous memory space and are located before the first memory block, the number of second memory blocks that have been allocated in the continuous memory space and are located before the first memory block can be determined based on the object pointer.
[0057] In the above 1042, the product of the number of the second memory blocks and the preset object size is the address offset of the object in the continuous memory space.
[0058] In the above 1043, the address offset is added to the starting address of the continuous memory space to obtain the memory address of the object, that is, the first address of the first memory block.
[0059] In some scenarios, after calculating the memory address of an object, the program will delete the object pointer stored in the memory to complete the corresponding task processing. After the corresponding task processing is completed, the object pointer needs to be restored. Therefore, the above method also includes:
[0060] 105. When the object pointer of the object needs to be restored after being deleted, generate the object pointer of the object according to the memory address of the object, the starting address of the continuous memory space, and the preset object size.
[0061] Specifically, the object's memory address can be subtracted from the starting address of the continuous memory space to obtain the object's address offset in the continuous memory space. The address offset is divided by the preset object size to obtain the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block. Based on the number of second memory blocks, an object pointer is generated and stored. In this embodiment, the object pointer of the object is obtained through reverse calculation.
[0062] Typically, a null pointer is required when a program is running. When an object pointer contains a null pointer value, it indicates that it is idle and does not point to anything meaningful. The function of a null pointer can be referenced in existing techniques and will not be discussed in detail here. For example, when defining an object pointer, if it is not used immediately, it should be assigned a null pointer value. Otherwise, it may point to an arbitrary memory object, causing the program to crash.
[0063] Therefore, the above method further includes:
[0064] 106. Define a null pointer.
[0065] The pointer length of the null pointer is the same as the pointer length of the object pointer. The value of the highest bit in the null pointer is different from the value of the highest bit in the object pointer. And the bits in the object pointer other than the highest bit are used to represent the number of the second memory blocks.
[0066] In one example, the value of the most significant bit in the object pointer is non-zero; the value of the most significant bit in the null pointer is zero.
[0067] In another feasible solution, the number of preset objects is greater than 2 n-1 and less than or equal to 2 n In this embodiment, the lower n bits of the object pointer are used to indicate the number of the second memory blocks; wherein the highest bit is used to indicate whether it is a null pointer.
[0068] For example: the default number of objects is 2 31 (about 2.15 billion), the object pointer length can be set to 32 bits. In actual application, during the initialization phase of program operation, memory allocation for all objects can be completed, and object pointers for all objects can be saved in physical memory. Of course, the embodiment of the present application is not limited to completing memory allocation for all objects during the initialization phase. Memory allocation can also be performed for some objects during the initialization phase. Subsequently, memory allocation from continuous memory space is only required when and only when there are no free memory blocks. In this way, it is sufficient to store some object pointers during the initialization phase, without the need to store a large number of object pointers, which can reduce the waste of physical memory and make the present solution more applicable. Specifically, the above method may also include:
[0069] 107. Determine whether there is any free memory block that has been allocated and is not currently in use in the current continuous memory space.
[0070] 108. When there is no free memory block, execute the step of dividing the first memory block for allocation to the object from the continuous memory space applied for by the program.
[0071] In the above 107, the unused free memory block can be understood as the space memory block not being accessed or the object corresponding to the space memory block not being accessed.
[0072] In the above 108, when there is no free memory block, it means that all the divided memory blocks have been accessed, and a new memory block needs to be divided from the continuous memory space applied for by the program to be allocated to the new object.
[0073] In performance-critical scenarios, such as when the CPU needs to align object accesses to cache lines to improve performance, a reserved field in the object can be padded to align the object size to the desired size, ensuring that the memory addresses of the first and second memory blocks are aligned to a specified byte. A typical cache line size is 64 bytes, so the specified byte can be 64 bytes.
[0074] In one example, the object pointer can be applied to a data structure containing pointer-type data, such as a linked list.
[0075] It should be noted that this solution is not limited to 64-bit operating systems. The compression effect will be more obvious under 64-bit systems. Pointer compression can also be performed according to the technical solution provided in this application under 32-bit systems. In addition, object pointers are suitable for scenarios where object pre-allocation is required, that is, scenarios where memory for objects needs to be allocated continuously from a continuous block of memory.
[0076] The following is an example of the program running method provided in the embodiment of the present application:
[0077] Assume that the number of preset objects managed by the program is 2 31 , the size of each object is the preset object size, which is 64 bytes.
[0078] Step 300: According to the preset number of objects managed by the program 2 31 The pointer length of the object pointer is determined to be 32 bits, that is, 4 bytes.
[0079] Step 301: According to the preset number of objects 2 31 And the preset object size is 64 bytes, and a 128GB continuous virtual address space is applied for allocating objects.
[0080] Step 302: Continuously divide the continuous virtual address space according to the preset object size to obtain multiple memory blocks; and generate and save a 4-byte object pointer for the object corresponding to each memory block.
[0081] For convenience, multiple memory blocks can be numbered consecutively starting from 0, and the number of each memory block is used to indicate the number of memory blocks that precede it in the continuous virtual memory space. Figure 3 The continuous memory space shown is divided into a plurality of memory blocks respectively allocated to a plurality of objects. Arrow 401 indicates the starting address of the continuous virtual memory space.
[0082] The structure of the object pointer is as follows Figure 2As shown in the figure, the object pointer length is 32 bits, or 4 bytes, which is half the length of the 8-byte memory address. When the highest bit is 0, it indicates a null pointer. When the highest bit is non-zero, the lower 31 bits store the memory block number (because each object corresponds to a memory block, it can also be called the object number).
[0083] Step 303, when the object needs to be accessed, the memory block number in the object pointer of the object is multiplied by the preset object size to obtain the address offset of the object's memory block in the continuous virtual memory space, and the address offset is added to the starting address 401 to obtain the memory address of the object.
[0084] Step 304: When reverse calculation is required, the memory address of the object is subtracted from the starting address pointed to by arrow 401 to obtain the address offset of the object in the continuous virtual memory space. The address offset is divided by the preset object size to obtain the memory block number of the object, thereby generating the object pointer of the object.
[0085] Figure 4 FIG. 1 shows a structural block diagram of a program running device provided by another embodiment of the present application. Figure 4 As shown, the device includes:
[0086] Determining module 501, for determining the pointer length according to the preset number of objects managed by the program;
[0087] A partitioning module 502 is configured to partition a first memory block for allocation to an object from the continuous memory space applied for by the program;
[0088] a pointer generation module 503 configured to generate an object pointer of the specified pointer length for the object based on the number of second memory blocks that have been allocated in the continuous memory space and are located before the first memory block; the second memory blocks are used to be allocated to other objects; the size of the first memory block and each of the second memory blocks is a preset object size; and the addresses of any two adjacent memory blocks that have been allocated in the continuous memory space are continuous;
[0089] The acquisition module 504 is configured to obtain the memory address of the object according to the starting address of the continuous memory space, the preset object size, and the object pointer when the object needs to be accessed, so as to access the object.
[0090] Optionally, the acquisition module 504 is specifically configured to:
[0091] Determine, according to the object pointer, the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block;
[0092] Determining an address offset of the object in the continuous memory space according to the number of the second memory blocks and the preset object size;
[0093] The memory address of the object is obtained according to the address offset and the starting address of the continuous memory space.
[0094] Optionally, the pointer generation module 503 is further configured to:
[0095] When the object pointer of the object needs to be restored after being deleted, the object pointer of the object is generated according to the memory address of the object, the starting address of the continuous memory space and the preset object size.
[0096] Optionally, bits other than the highest bit in the object pointer are used to indicate the number of the second memory blocks;
[0097] The above device further includes:
[0098] A definition module is used to define a null pointer; wherein the value of the highest bit in the null pointer is different from the value of the highest bit in the object pointer.
[0099] Optionally, the value of the highest bit in the object pointer is non-zero; the value of the highest bit in the null pointer is zero.
[0100] Optionally, the above device further includes:
[0101] A determination module, configured to determine whether there is a free memory block that has been allocated and is not currently in use in the current continuous memory space;
[0102] The execution module is used to execute the step of dividing the first memory block for allocation to the object from the continuous memory space applied for by the program when there is no free memory block.
[0103] Optionally, the memory addresses of the first memory block and the second memory block are aligned according to specified bytes.
[0104] Optionally, the determination module 501 is further configured to determine the total memory size to be applied for based on the number of preset objects managed by the program and the sizes of the preset objects;
[0105] The above device further includes:
[0106] The application module is used to apply to the operating system for the continuous memory space with a size equal to the total memory size.
[0107] It should be noted here that the program running device provided in the above embodiments can implement the technical solutions described in the above method embodiments. The specific implementation principles and technical effects of the above modules or units can be found in the corresponding contents of the above method embodiments, which will not be repeated here.
[0108] Figure 5 FIG. 1 shows a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. Figure 5 As shown, the electronic device includes a memory 1101 and a processor 1102. The memory 1101 can be configured to store various other data to support operations on the electronic device. Examples of such data include instructions for any application or method operating on the electronic device. The memory 1101 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0109] The memory 1101 is used to store programs;
[0110] The processor 1102 is coupled to the memory 1101 and is configured to execute the program stored in the memory 1101 to implement the program running methods provided in the above-mentioned method embodiments.
[0111] Further, if Figure 5 As shown, the electronic device also includes: a communication component 1103, a display 1104, a power component 1105, an audio component 1106 and other components. Figure 5 Only some components are shown schematically, which does not mean that the electronic device only includes Figure 5 Components shown.
[0112] Accordingly, an embodiment of the present application further provides a computer-readable storage medium storing a computer program, which, when executed by a computer, can implement the steps or functions of the program running method provided in the above-mentioned method embodiments.
[0113] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0114] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, or of course, by hardware. Based on this understanding, the essence of the above technical solution or the part that contributes to the existing technology can be embodied in the form of a software product. The computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes 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 embodiment or certain parts of the embodiments.
[0115] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A program running method, characterized in that: include: determining a pointer length based on a predetermined number of objects managed by the program; A first memory block for allocating to the object is divided from the continuous memory space applied for by the program; generating an object pointer of the pointer length for the object based on the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block; the second memory blocks are used to be allocated to other objects; the size of the first memory block and each of the second memory blocks is a preset object size; the addresses of any two adjacent memory blocks that have been divided in the continuous memory space are continuous; When the object needs to be accessed, the memory address of the object is obtained according to the starting address of the continuous memory space, the preset object size and the object pointer, so as to access the object.
2. The method according to claim 1, characterized in that Obtaining a memory address of the object according to a starting address of the continuous memory space, the preset object size, and the object pointer includes: Determine, according to the object pointer, the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block; Determining an address offset of the object in the continuous memory space according to the number of the second memory blocks and the preset object size; The memory address of the object is obtained according to the address offset and the starting address of the continuous memory space.
3. The method according to claim 2, characterized in that Also includes: When the object pointer of the object needs to be restored after being deleted, the object pointer of the object is generated according to the memory address of the object, the starting address of the continuous memory space and the preset object size.
4. The method according to any one of claims 1 to 3, characterized in that The bits other than the highest bit in the object pointer are used to indicate the number of the second memory blocks; The above method further includes: A null pointer is defined; wherein the value of the highest bit in the null pointer is different from the value of the highest bit in the object pointer.
5. The method according to claim 4, characterized in that The value of the highest bit in the object pointer is non-zero; the value of the highest bit in the null pointer is zero.
6. The method according to any one of claims 1 to 3, characterized in that Also includes: Determine whether there is a free memory block that has been allocated and is not currently in use in the current continuous memory space; When there is no free memory block, the step of dividing the first memory block for allocating to the object from the continuous memory space applied for by the program is performed.
7. The method according to any one of claims 1 to 3, characterized in that Memory addresses of the first memory block and the second memory block are aligned according to specified bytes.
8. The method according to any one of claims 1 to 3, characterized in that Also includes: Determining the total memory size to be applied for based on the number of preset objects managed by the program and the size of the preset objects; Apply to the operating system for the continuous memory space with a size equal to the total memory size.
9. A program running device, characterized in that: include: a determination module, configured to determine a pointer length based on a preset number of objects managed by the program; a partitioning module, configured to partition a first memory block for allocation to an object from the continuous memory space applied for by the program; a pointer generation module, configured to generate an object pointer of the specified pointer length for the object based on the number of second memory blocks that have been allocated in the continuous memory space and are located before the first memory block; the second memory blocks are used to be allocated to other objects; the size of the first memory block and each of the second memory blocks is a preset object size; and the addresses of any two adjacent memory blocks that have been allocated in the continuous memory space are continuous; The acquisition module is used to obtain the memory address of the object according to the starting address of the continuous memory space, the preset object size and the object pointer when the object needs to be accessed, so as to access the object.
10. An electronic device, characterized in that: include: memory and a processor, wherein The memory is used to store programs; The processor is coupled to the memory and is configured to execute the program stored in the memory to: determining a pointer length based on a predetermined number of objects managed by the program; A first memory block for allocating to the object is divided from the continuous memory space applied for by the program; generating an object pointer of the pointer length for the object based on the number of second memory blocks that have been divided in the continuous memory space and are located before the first memory block; the second memory blocks are used to be allocated to other objects; the size of the first memory block and each of the second memory blocks is a preset object size; the addresses of any two adjacent memory blocks that have been divided in the continuous memory space are continuous; When the object needs to be accessed, the memory address of the object is obtained according to the starting address of the continuous memory space, the preset object size and the object pointer, so as to access the object.
Citation Information
Patent Citations
Storage space using method and device, storage medium and electronic device
CN112035379A