Memory compression method and device
By recording the correspondence between memory pages to be compressed and target compression algorithms in the memory compression module and dynamically switching compression algorithms, the inefficiency caused by resetting the memory compression module in existing technologies is solved, and more efficient memory resource management is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-23
- Publication Date
- 2026-03-27
AI Technical Summary
In existing technologies, switching memory compression algorithms requires resetting the memory compression module, resulting in low efficiency.
The memory compression module records the correspondence between memory pages to be compressed and target compression algorithms, dynamically switches compression algorithms without resetting, and adjusts the compression algorithm based on system load information and application access frequency.
It improves the efficiency of switching memory compression algorithms, adapts to the resource usage of electronic devices, and improves resource utilization.
Smart Images

Figure CN121743052A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of memory compression, in particular to a memory compression method and device. BACKGROUND
[0002] In order to save memory space, an electronic device can use a memory compression module to compress a memory page. In related technologies, replacing a used memory compression algorithm requires resetting the memory compression module, so the efficiency of switching memory compression algorithms in related technologies is low. SUMMARY
[0003] To this end, the present application discloses the following technical solutions:
[0004] The first aspect of the present application provides a memory compression method, comprising:
[0005] determining a target compression algorithm corresponding to a to-be-compressed memory page from a plurality of compression algorithms;
[0006] recording a correspondence between the to-be-compressed memory page and the target compression algorithm in a memory compression module;
[0007] when the memory compression module is in a first mode, obtaining the target compression algorithm according to the correspondence, and performing compression processing on the to-be-compressed memory page based on the target compression algorithm.
[0008] Optionally, the method further comprises:
[0009] when the memory compression module is in a second mode, performing compression processing on the to-be-compressed memory page according to a preset primary compression algorithm, and obtaining a compressed memory page;
[0010] in response to obtaining a re-compression instruction, decompressing the compressed memory page according to the primary compression algorithm, and obtaining the compressed memory page;
[0011] performing compression processing on the to-be-compressed memory page according to a preset re-compression algorithm, and obtaining a re-compressed memory page.
[0012] Optionally, the method further comprises at least one of the following:
[0013] switching the memory compression module from the second mode to the first mode according to system load information;
[0014] in response to obtaining a switching instruction, switching the memory compression module from the second mode to the first mode.
[0015] Optionally, the recording of the correspondence between the to-be-compressed memory page and the target compression algorithm in the memory compression module comprises:
[0016] According to a device management interface of a system kernel, an algorithm identifier of a target compression algorithm is recorded for a memory page to be compressed in an algorithm configuration structure of a memory compression module, so as to record a correspondence between the memory page to be compressed and the target compression algorithm.
[0017] Optionally, the method further comprises:
[0018] In response to a read request for the memory page to be compressed, a compressed memory page obtained by performing compression processing on the memory page to be compressed is obtained.
[0019] According to the correspondence, a target compression algorithm corresponding to the memory page to be compressed is obtained, so as to decompress the compressed memory page according to the target compression algorithm, and obtain the memory page to be compressed.
[0020] Optionally, the method further comprises:
[0021] A compressed memory page obtained by performing compression processing based on the target compression algorithm is stored, and an algorithm identifier of the target compression algorithm is recorded in an index table used for recording an index of the compressed memory page, so as to call the target compression algorithm according to the algorithm identifier when decompressing the compressed memory page.
[0022] Optionally, the determining of the target compression algorithm corresponding to the memory page to be compressed from the plurality of compression algorithms comprises:
[0023] According to a running state interface of a system kernel, system load information is obtained, the system load information comprising any one or more of processor usage, processor stress information, memory usage, and memory stress information;
[0024] According to the system load information, the target compression algorithm corresponding to the memory page to be compressed is determined from the plurality of compression algorithms.
[0025] Optionally, the plurality of compression algorithms comprises a first compression algorithm and a second compression algorithm, and a compression rate of the first compression algorithm is smaller than a compression rate of the second compression algorithm.
[0026] The determining of the target compression algorithm corresponding to the memory page to be compressed from the plurality of compression algorithms according to the system load information comprises:
[0027] When the system load information indicates that the processor is in a high load state, the first compression algorithm is determined as the target compression algorithm corresponding to the memory page to be compressed.
[0028] When the system load information indicates that the memory is in a high load state, the second compression algorithm is determined as the target compression algorithm corresponding to the memory page to be compressed.
[0029] Optionally, the determining the target compression algorithm corresponding to the to-be-compressed memory page from the plurality of compression algorithms comprises:
[0030] The target compression algorithm corresponding to the to-be-compressed memory page is determined from the plurality of compression algorithms according to a frequency at which the to-be-compressed memory page is accessed by a corresponding application program;
[0031] And / or, the predicted system load information in a future time period is determined according to historical system load information, so as to determine the target compression algorithm corresponding to the to-be-compressed memory page from the plurality of compression algorithms according to the predicted system load information.
[0032] The second aspect of the present application provides an electronic device, comprising a memory, a processor and a memory;
[0033] The memory comprises a plurality of memory pages;
[0034] The memory is used for storing a computer program;
[0035] The processor is used for executing the computer program to perform:
[0036] The target compression algorithm corresponding to the to-be-compressed memory page is determined from the plurality of compression algorithms;
[0037] The correspondence between the to-be-compressed memory page and the target compression algorithm is recorded in a memory compression module;
[0038] When the memory compression module is in a first mode, the target compression algorithm is obtained according to the correspondence, so as to perform compression processing on the to-be-compressed memory page based on the target compression algorithm. BRIEF DESCRIPTION OF DRAWINGS
[0039] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related art, the drawings needed to be used in the embodiment or related art description will be briefly introduced. Obviously, the drawings in the following description are only embodiments of the present application, and for those skilled in the art, other drawings can be obtained without creative labor based on the provided drawings.
[0040] Figure 1 is a flowchart of a memory compression method provided by the embodiments of the present application;
[0041] Figure 2 is a schematic diagram of a memory compression module provided by the embodiments of the present application;
[0042] Figure 3 is a flowchart of decompression provided by the embodiments of the present application;
[0043] Figure 4is a working flowchart of a memory compression module in a second mode provided by an embodiment of the present application.
[0044] Figure 5 is a flowchart of determining a target compression algorithm provided by an embodiment of the present application.
[0045] Figure 6 is a structural schematic diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0046] The technical solutions in the embodiments of the present application will be clearly and completely described in combination with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by a person of ordinary skill in the art without creative labor fall within the protection scope of the present application.
[0047] The present embodiment provides a memory compression method, please refer to Figure 1 , which can include the following steps.
[0048] S101, determining a target compression algorithm corresponding to a to-be-compressed memory page in a plurality of compression algorithms.
[0049] S102, recording a correspondence between the to-be-compressed memory page and the target compression algorithm in a memory compression module.
[0050] S103, when the memory compression module is in a first mode, obtaining the target compression algorithm according to the correspondence, and performing compression processing on the to-be-compressed memory page based on the target compression algorithm.
[0051] The memory compression method of the present embodiment can be executed by any electronic device.
[0052] The plurality of compression algorithms can include any plurality of memory compression algorithms in memory compression related technologies, without limitation. As some examples, the plurality of compression algorithms of S101 refers to a plurality of memory compression algorithms available to the memory compression module, which can include, but is not limited to, any of Lempel-Ziv-Oberhumer (lzo), lzo-Run-Length Encoding (lzo-rle), high-speed compression algorithm (lz4), LZ4HC, Zstandard (zstd), and Zstd-fast.
[0053] The first mode is one of the working modes of the memory compression module in the embodiment. When in the first mode, the memory compression module can dynamically switch the memory compression algorithm used in the process of compressing a plurality of to-be-processed memory pages according to the recorded correspondence relationship without resetting and re-compressing the already compressed memory pages, so as to respectively compress the to-be-compressed memory pages by using different memory compression algorithms. For example, the memory compression module can remain unchanged in the first mode, continuously compresses to-be-compressed memory pages 1 to 6, and compresses to-be-compressed memory pages 1 and 2 by using the lzo compression algorithm, compresses to-be-compressed memory pages 3 and 4 by using the lz4 compression algorithm, and compresses to-be-compressed memory pages 5 and 6 by using the zstd compression algorithm. When switching from the lzo compression algorithm to the lz4 compression algorithm and when switching from the lz4 compression algorithm to the zstd compression algorithm, the memory compression module does not need to be reset or re-compressed, but continuously works in the first mode.
[0054] Different memory compression algorithms have different performances in different aspects. For example, the lz4 algorithm is faster and can complete compression in a shorter time, and the zstd algorithm has a higher compression rate and the compressed memory page occupies less space.
[0055] The correspondence relationship between the to-be-compressed memory pages and the target compression algorithm can include the identification of the to-be-compressed memory pages and the algorithm identification of the target compression algorithm. For example, the correspondence relationship between the to-be-compressed memory pages and the target compression algorithm can include: page 1~identification 0, page 2~identification 3, where page 1 and page 2 are the identifications of two to-be-compressed memory pages, 0 is the algorithm identification of the lzo algorithm, and 3 is the algorithm identification of the zstd.
[0056] The to-be-compressed memory page is a memory page obtained from the memory of the electronic device and needs to be compressed. Generally, a memory page with a low access frequency or a long-term non-access memory page in the memory can be obtained as a to-be-compressed memory page. The to-be-compressed memory page can be one or more, and the target compression algorithm corresponding to different to-be-compressed memory pages can be the same or different.
[0057] The method of the embodiment can be applied to any memory compression module. The memory compression module is equivalent to a virtual module simulated by software.
[0058] The memory compression module works in the following manner: when the memory compression module is enabled, the electronic device allocates a certain storage space for the memory compression module, which can be denoted as a swap space, and the storage space can generally be a storage space in the memory; when the memory compression module is working, the memory compression module can take out a to-be-compressed memory page from the memory, call a memory compression algorithm to compress the to-be-compressed memory page, obtain a compressed memory page, store the compressed memory page and an index of the compressed memory page in the swap space, and discard the to-be-compressed memory page; when a to-be-compressed memory page needs to be read, the memory compression module finds the corresponding compressed memory page in the swap space according to the index, calls the memory compression algorithm to decompress the compressed memory page, obtains the to-be-compressed memory page, and writes the to-be-compressed memory page back to the memory. The index of the compressed memory page can include the amount of data of the compressed memory page (i.e., how many bytes are included), storage information in the swap space, and a correspondence relationship of the to-be-compressed memory page, and the correspondence relationship of the to-be-compressed memory page can specifically include an identifier of the to-be-compressed memory page, for example, a storage address of the to-be-compressed memory page in the memory.
[0059] For example, the memory compression module can take out a to-be-compressed memory page 1 from the memory, compress the to-be-compressed memory page 1 by using the memory compression algorithm to obtain a compressed memory page 1', discard the to-be-compressed memory page 1, and store the compressed memory page 1' and an index of the compressed memory page 1' in the swap space; when a program needs to read the to-be-compressed memory page 1, the memory compression module decompresses the compressed memory page 1' by using the memory compression algorithm, writes the to-be-compressed memory page 1 obtained by decompression back to the memory, and writes the to-be-compressed memory page 1 back to the memory.
[0060] The method of this embodiment can be applied to any memory compression module, for example, to a zRAM memory compression module.
[0061] The beneficial effects of this embodiment will be described below in combination with the working principle of the memory compression module in the related art.
[0062] In the related art, the memory compression module only records a currently used memory compression algorithm by using a single global variable (for example, comp algorithm) applicable to the entire memory compression module, compresses a memory page according to the algorithm recorded by the variable each time, and decompresses according to the algorithm recorded by the variable each time; if the memory compression algorithm is switched during the working process of the memory compression module, the algorithm used by the original compressed memory page and the algorithm recorded by the variable after the switching will be inconsistent, and an error will occur when the compressed memory page is decompressed according to the algorithm after the switching;
[0063] Based on the above characteristics, in the related art, when the memory compression module switches the used memory compression algorithm, it must decompress the compressed memory pages and then re-compress them according to the switched algorithm, or must reset (equivalent to restart) the memory compression module to clear the compressed memory pages, to ensure that all the compressed memory pages are compressed according to the switched algorithm, thereby avoiding errors during decompression;
[0064] In the method of the embodiment, the memory compression module records the correspondence between each to-be-compressed memory page and the target compression algorithm used thereby, so that each time a memory page is compressed, the corresponding target compression algorithm can be called for compression according to the correspondence, and each time a memory page is decompressed, the corresponding target compression algorithm can be called for decompression according to the correspondence, so that even if the memory compression algorithm is switched while the memory compression module is working, the correct algorithm can be called for decompression based on the above correspondence during decompression, and errors during decompression caused by switching the compression algorithm can be avoided;
[0065] Therefore, by applying the method of the embodiment, the memory compression module can support real-time adjustment of the memory compression algorithm for compressing different memory pages according to actual conditions during working, without the need for resetting and re-compression, so that the efficiency of switching the memory compression algorithm can be improved, and different memory compression algorithms can be flexibly switched to adapt to different resource usage conditions of the electronic device, thereby improving the resource usage rate of the electronic device.
[0066] Referring to Figure 2 , determining the target compression algorithm and recording the correspondence can be performed by a user mode program of the electronic device, and the memory compression module is generally located in the system kernel of the electronic device, so the user mode program cannot directly access the system kernel, and therefore, the way of recording the correspondence between the to-be-compressed memory page and the target compression algorithm in the memory compression module can be:
[0067] According to the device management interface of the system kernel, the algorithm identifier of the target compression algorithm is recorded in the algorithm configuration structure of the memory compression module, to record the correspondence between the to-be-compressed memory page and the target compression algorithm.
[0068] The device management interface is inherent to the system kernel and is used to support the user mode program to access the system kernel. To implement the method of the embodiment, when the memory compression module is started, an algorithm configuration structure can be created in the core module of the memory compression module to record the correspondence, and an interface for accessing the algorithm configuration structure can be created based on the device management interface.
[0069] After obtaining the to-be-compressed memory page, the memory compression module can record the identifier of the to-be-compressed memory page in the algorithm configuration structure, and meanwhile, the user-mode program can configure the algorithm identifier of the target compression algorithm for the identifier of the to-be-compressed memory page that has not been compressed in the algorithm configuration structure after determining the target compression algorithm each time, so that the memory compression module performs compression according to the target compression algorithm.
[0070] The user-mode program can periodically execute steps S101 and S102, and each time step S102 is executed, the user-mode program can call the aforementioned interface to access the algorithm configuration structure, write the aforementioned correspondence in the algorithm configuration structure, and enable the memory compression module to perform compression processing on the to-be-compressed memory page based on the target compression algorithm.
[0071] Taking the operating system of the electronic device as an example, the memory compression module as an example, the device management interface as an example, the interface for accessing the algorithm configuration structure as an example, the kernel module as an example, and the aforementioned algorithm configuration structure as an example, the operating system of the electronic device is a Linux system, the memory compression module is zRAM, the device management interface is a / sysfs node, the interface for accessing the algorithm configuration structure is a cur_comp_idx node based on the / sysfs node, the kernel module is a source code file zram_drv.c of zRAM, and the aforementioned algorithm configuration structure is a zram_meta data structure newly added in zram_drv.c.
[0072] Optionally, the method further comprises:
[0073] storing the compressed memory page obtained by performing compression processing based on the target compression algorithm, and recording the algorithm identifier of the target compression algorithm in an index table for recording indexes of compressed memory pages, so as to call the target compression algorithm according to the algorithm identifier when decompressing the compressed memory page.
[0074] As shown in Figure 2 , the memory compression module can store the compressed memory page in the pre-allocated swap space, and maintain an index table in the swap space, and record the index of the compressed memory page in the index table each time an index of the compressed memory page is obtained.
[0075] In the embodiment, to ensure that the compressed memory page is decompressed according to the correct memory compression algorithm, the algorithm identifier of the target compression algorithm used for compression can be further recorded in the index of the compressed memory page, so that each time a compressed memory page is to be decompressed, the corresponding index can be queried to determine that the compressed memory page is compressed by the target compression algorithm, so that the target compression algorithm is called to correctly decompress the compressed memory page and restore the original to-be-compressed memory page.
[0076] The memory compression module can set a unique algorithm identifier for each available memory compression algorithm in advance, which is not limited in form, for example, it can be a number or other characters. For example, 0 corresponds to lzo, 1 corresponds to lzo-rle, 2 corresponds to lz4, and 3 corresponds to zstd. Thus, the algorithm identifier recorded in the index of the compressed memory page obtained by lzo compression is 0, the algorithm identifier recorded in the index of the compressed memory page obtained by zstd compression is 3, and so on. If the algorithm identifier recorded in the index of a compressed memory page to be decompressed is 3, the memory compression module decompresses the compressed memory page using the target compression algorithm zstd.
[0077] By recording the algorithm identifier of the target compression algorithm in the index table, the original function of the memory compression module for recording the index of the compressed memory page can be reused to record the target compression algorithm corresponding to each compressed memory page, ensuring that the compressed memory page can be correctly decompressed when needed.
[0078] Optionally, the index of the compressed memory page can also not record the algorithm identifier of the target compression algorithm. In this case, the memory compression module can directly query the target compression algorithm used for compression in the previously recorded correspondence between the to-be-compressed memory page and the target compression algorithm, and then decompress according to the target compression algorithm.
[0079] Optionally, in the present embodiment, the memory compression module can also decompress according to the method shown in the following table. Figure 3
[0080] S301, in response to a read request for a to-be-compressed memory page, obtaining a compressed memory page obtained by compressing the to-be-compressed memory page.
[0081] S302, obtaining the target compression algorithm corresponding to the to-be-compressed memory page according to the correspondence, so as to decompress the compressed memory page according to the target compression algorithm to obtain the to-be-compressed memory page.
[0082] The read request for the to-be-compressed memory page can be generated by any program running on the electronic device. The read request can include the identifier of the to-be-compressed memory page, which can be, for example, the storage address of the to-be-compressed memory page.
[0083] After obtaining the read request, the memory compression module queries the correspondence between the to-be-compressed memory page and the target compression algorithm based on the identifier of the to-be-compressed memory page to determine the target compression algorithm used to compress the to-be-compressed memory page. On the other hand, the memory compression module queries the index of the compressed memory page based on the identifier of the to-be-compressed memory page to determine the compressed memory page obtained by compressing the to-be-compressed memory page in the swap space. Then, the memory compression module can decompress the compressed memory page using the target compression algorithm to obtain the original to-be-compressed memory page.
[0084] In combination with the foregoing example, in the compression phase, the memory compression module compresses page 1 using the lzo algorithm based on the correspondence between the to-be-compressed memory page and the target compression algorithm, obtains page 1' (i.e., the compressed page) and stores it in the swap space, and compresses page 2 using the zstd algorithm to obtain page 2' and store it in the swap space.
[0085] When a read request for page 1 is obtained, the correspondence is queried to determine that the target compression algorithm is lzo, the index is queried to obtain page 1', page 1 is obtained by decompressing page 1' using the lzo algorithm, and page 1 is written back to the memory so that the corresponding program can read page 1. Similarly, when a read request for page 2 is obtained, the correspondence and the index are queried to decompress page 2' using the target compression algorithm zstd to obtain page 2.
[0086] Through the foregoing method, the memory compression module can use the pre-recorded correspondence between the to-be-compressed memory page and the target compression algorithm to decompress the compressed memory page according to the corresponding target compression algorithm to accurately restore the to-be-compressed memory page that needs to be read.
[0087] Optionally, the memory compression module can include at least two different working modes, for example, a first mode and a second mode. In the first mode, the memory compression module can support recording the correspondence between the to-be-compressed memory page and the target compression algorithm to support dynamically switching different memory compression algorithms. In the second mode, the memory compression module can stop recording the correspondence between the to-be-compressed memory page and the target compression algorithm. In this case, see Figure 4 , the memory compression module can also switch the memory compression algorithm by the method of re-compression as follows:
[0088] S401, when the memory compression module is in the second mode, compressing the to-be-compressed memory page according to a preset primary compression algorithm to obtain a compressed memory page.
[0089] S402, in response to obtaining a re-compression instruction, decompressing the compressed memory page according to the primary compression algorithm to obtain a compressed memory page.
[0090] S403, compressing the to-be-compressed memory page according to a preset re-compression algorithm to obtain a re-compressed memory page.
[0091] The second mode is another working mode of the memory compression module of the embodiment. When the memory compression module is in the second mode, if the memory compression algorithm used by the memory compression module needs to be changed, the already compressed memory page needs to be re-compressed according to steps S302 and S303, and then the memory compression algorithm used can be switched.
[0092] The compressed memory page is decompressed according to the main compression algorithm to obtain a compressed memory page, and the to-be-compressed memory page is compressed according to a preset re-compression algorithm to obtain a re-compressed memory page. This process is equivalent to a re-compression process performed by the memory compression module in response to a re-compression instruction. The re-compression process can be performed by the re-compression module shown in Figure 2 The specific implementation can refer to related technologies in the field of memory compression, and details are not described herein.
[0093] The main compression algorithm can be recorded in a global variable (for example, comp algorithm) applicable to the entire memory compression module, and the re-compression algorithm can be recorded in another variable (for example, recompress) for recording the re-compression algorithm. The re-compression algorithm can be configured by a user-mode program through a related interface, and the re-compression algorithm can be one of the foregoing multiple compression algorithms, which is different from the main compression algorithm.
[0094] The re-compression algorithm can be configured before the re-compression instruction is obtained, or can be configured after the re-compression instruction is obtained. The re-compression instruction can be an instruction for setting the re-compression algorithm, or can be an instruction for triggering re-compression only, without setting the re-compression algorithm.
[0095] In the second mode, the memory compression module can directly compress all the obtained to-be-compressed memory pages according to the main compression algorithm to obtain compressed memory pages and store the compressed memory pages in the swap space.
[0096] The method for obtaining the re-compression instruction by the memory compression module can refer to related technologies in the field of memory compression, and details are not described herein.
[0097] After the re-compression instruction is obtained, the memory compression module can use the re-compression algorithm to compress the newly obtained to-be-compressed memory pages in the subsequent compression process.
[0098] Therefore, when the re-compression instruction is obtained, if the re-compression process is not performed, there can be a part of compressed memory pages compressed by the main compression algorithm and a part of compressed memory pages compressed by the re-compression algorithm in the swap space. When the compressed memory pages need to be decompressed, the memory compression module cannot confirm whether the compressed memory pages are compressed based on the main compression algorithm or the re-compression algorithm, and thus cannot correctly perform decompression.
[0099] Therefore, when the re-compression instruction is obtained, the memory compression module can perform the foregoing re-compression process to replace the compressed memory pages originally obtained based on the main compression algorithm in the swap space with the re-compressed memory pages obtained based on the re-compression algorithm, so as to ensure that the compressed memory pages stored in the swap space can be correctly decompressed based on the re-compression algorithm.
[0100] In this embodiment, the memory compression module can be switched from the second mode to the first mode based on any of the following modes:
[0101] In the first switching mode, the memory compression module is switched from the second mode to the first mode according to system load information.
[0102] In the second switching mode, the memory compression module is switched from the second mode to the first mode in response to obtaining a switching instruction.
[0103] When working in the first mode, the memory compression module needs to frequently switch between different compression algorithms, which causes a certain load on the central processing unit (CPU). Therefore, the memory compression module can be limited to work in the second mode when the load on the CPU is too high, so as to reduce the load on the CPU, and work in the first mode when the load on the CPU is low.
[0104] Therefore, the first switching mode can be that the memory compression module is switched from the second mode to the first mode when the load on the CPU is determined to be less than a certain threshold according to the system load information. Correspondingly, the memory compression module is switched from the first mode to the second mode when the load on the CPU is determined to be greater than or equal to the threshold according to the system load information.
[0105] In the second switching mode, the electronic device can obtain a switching instruction to switch to the first mode based on the user's operation, or automatically obtain a switching instruction to switch to the first mode based on the running application.
[0106] For example, if the running application belongs to an application that occupies more memory, in order to save memory resources as much as possible, a switching instruction to switch to the first mode can be obtained, so that the memory compression algorithm with high compression rate can be flexibly switched during work. Correspondingly, if the running application belongs to an application that occupies less memory, a switching instruction to switch to the second mode can be obtained.
[0107] Optionally, the memory compression module can be started by default in the first mode or the second mode, and the mode can be switched during running according to the method of the above embodiments.
[0108] Through the method of the above embodiments, the memory compression module can be switched between the first mode and the second mode according to the actual situation, and the use demand in different scenes can be met.
[0109] Optionally, please refer to Figure 5 In the plurality of compression algorithms, the target compression algorithm corresponding to the to-be-compressed memory page can include the following steps.
[0110] S501, obtain system load information according to the running state interface of the system kernel, the system load information including any one or more of processor usage, processor stress information, memory usage and memory stress information.
[0111] S502, determining a target compression algorithm corresponding to the to-be-compressed memory page from a plurality of compression algorithms according to system load information.
[0112] The system load information refers to information capable of representing the usage of various system resources of the electronic device, including but not limited to processor resources, memory resources, and input / output (IO) resources, etc. Since the running of the memory compression module is mainly related to the processor and the memory, the system load information obtained in this embodiment can include information related to the processor resources and information related to the memory resources, for example, including any one or more of the above processor usage, processor pressure information, memory usage, and memory pressure information.
[0113] The above information can be read by a user mode program through a related running state interface. The specific running state interface can refer to related technologies and is not limited. As some examples, in the case where the operating system of the electronic device is a Linux system, the processor usage can be read from the / proc / loadavg interface, the processor pressure information can be read from the / proc / pressure / cpu interface, and the memory pressure information can be read from the / proc / pressure / memory interface. The above pressure information can be, for example, pressure stall information (PSI). The pressure stall information is an information introduced by the kernel of the Linux system for quantifying the pressure of system resources, which can represent the time of “stall” of a task caused by insufficient corresponding system resources. The longer the time, the higher the load of the corresponding system resource.
[0114] In this embodiment, the user mode service can set corresponding application conditions for a plurality of compression algorithms available to the memory compression module, for example, setting a first compression algorithm with a higher compression rate to be applied when the memory is insufficient, and a second compression algorithm with a lower compression rate to be applied when the processor load is too large.
[0115] On this basis, the user mode service can regularly monitor the system load information of the electronic device, and set a corresponding target compression algorithm in the memory compression module according to the condition satisfied by the currently monitored system load information, so that the memory compression module compresses the to-be-compressed memory page that has not been compressed according to the corresponding target compression algorithm.
[0116] In some embodiments, the plurality of compression algorithms includes a first compression algorithm and a second compression algorithm, and the compression rate of the first compression algorithm is less than the compression rate of the second compression algorithm.
[0117] Correspondingly, determining a target compression algorithm corresponding to the to-be-compressed memory page from a plurality of compression algorithms according to system load information includes:
[0118] The system load information represents that the processor is in a high load state, and the first compression algorithm is determined as the target compression algorithm corresponding to the memory page to be compressed.
[0119] The system load information represents that the memory is in a high load state, and the second compression algorithm is determined as the target compression algorithm corresponding to the memory page to be compressed.
[0120] If any one or both of the following conditions are met: the processor usage is greater than or equal to a preset processor usage threshold, and the processor stress information is greater than or equal to a preset processor stress threshold, it can be determined that the processor is in a high load state. If neither of the two conditions is met, it can be determined that the processor is not in a high load state.
[0121] If any one or both of the following conditions are met: the memory usage is greater than or equal to a preset memory usage threshold, and the memory stress information is greater than or equal to a preset memory stress threshold, it can be determined that the memory is in a high load state, that is, it is determined that the current memory resource is insufficient. If neither of the two conditions is met, it can be determined that the memory is not in a high load state.
[0122] The above thresholds can be preset based on actual conditions, or can be dynamically adjusted according to the running condition of the electronic device during the operation of the memory compression module, without limitation.
[0123] Through the method of the above embodiment, the memory compression module can use the first compression algorithm with a low compression rate to compress the memory page to be compressed when the processor resource is insufficient, thereby reducing the load caused by the compressed memory page to the processor and ensuring that the processor can work normally. And when the memory resource is insufficient, the second compression algorithm with a high compression rate is used to compress the memory page to be compressed, thereby releasing more memory resources through memory compression to avoid memory overflow.
[0124] The user mode program can also determine the target compression algorithm corresponding to the memory page to be compressed in the plurality of compression algorithms by any one or more of the following determination methods:
[0125] The first determination method is to determine the target compression algorithm corresponding to the memory page to be compressed in the plurality of compression algorithms according to the frequency of the memory page to be compressed being accessed by the corresponding application program.
[0126] And / or, the second determination method is to determine the predicted system load information in a future time period according to the historical system load information, and to determine the target compression algorithm corresponding to the memory page to be compressed in the plurality of compression algorithms according to the predicted system load information.
[0127] In the determining method one, the user-mode program can determine the process accessing the memory page to be compressed, and then obtain the application program to which the process belongs, so as to determine the application program corresponding to the memory page to be compressed. For example, it is determined that page 1 in the memory page to be compressed corresponds to an audio player application program, and page 2 corresponds to an instant chat application program.
[0128] The user-mode program can also count the frequency of accessing memory of each application program in the past period of time (for example, the last 30 minutes, the last 2 hours, etc.). Then, for the application program with a high frequency of accessing memory, the user-mode program can set the first compression algorithm as the target compression algorithm of the memory page to be compressed corresponding to the application program, so as to avoid frequent compression and decompression and increase the load of the processor, and improve the running efficiency of the corresponding application program. For the application program with a low frequency of accessing memory, the user-mode program can set the second compression algorithm as the target compression algorithm of the memory page to be compressed corresponding to the application program, so as to save the memory space as much as possible.
[0129] As some examples, the application program with a frequency of accessing memory higher than a certain threshold value can be regarded as the application program with a high frequency of accessing memory, and the application program with a frequency of accessing memory lower than the threshold value can be regarded as the application program with a low frequency of accessing memory, or the application programs can be arranged in descending order of the frequency of accessing memory, and the first few application programs can be regarded as the application programs with a high frequency of accessing memory, and the latter application programs can be regarded as the application programs with a low frequency of accessing memory.
[0130] The advantage of the application determining method one is that different target compression algorithms can be configured for different memory pages to be compressed according to the characteristics of different application programs, so that the memory space can be saved sufficiently and the needs of different application programs can be met.
[0131] In the determining method two, the user-mode program can collect historical system load information other than the above-mentioned historical system load information, for example, collect historical system load information in the past few days or weeks, construct a prediction model based on the historical system load information, and then periodically predict the system load in a future period of time by using the prediction model to obtain the above-mentioned prediction system load information, and then set the target compression algorithm according to the condition met by the prediction system load information. For example, if the prediction system load information indicates that the processor is in a high load state in the future period of time, the first compression algorithm with a low compression rate is set as the target compression algorithm, and if the prediction system load information indicates that the memory is in a high load state in the future period of time, the second compression algorithm with a high compression rate is set as the target compression algorithm.
[0132] The future period of time can be a period of time within a certain time length after the current time, for example, the future 5 minutes, the future 30 seconds, etc. starting from the current time.
[0133] The application determines the target compression algorithm based on the system load in the future period of time, so as to prevent the system resource shortage from affecting the program running.
[0134] The electronic device can comprise a memory 601, a processor 602 and a memory 603. Figure 6
[0135] The memory 603 comprises a plurality of memory pages.
[0136] The memory 601 is configured to store a computer program.
[0137] The processor 602 is configured to execute the computer program to perform the following steps.
[0138] The target compression algorithm corresponding to the memory page to be compressed is determined from a plurality of compression algorithms.
[0139] The correspondence between the memory page to be compressed and the target compression algorithm is recorded in the memory compression module.
[0140] When the memory compression module is in the first mode, the target compression algorithm is obtained according to the correspondence, and the memory page to be compressed is compressed based on the target compression algorithm.
[0141] The memory 603 refers to the running memory directly read by the processor 602 during running, and the data stored therein is generally lost when the electronic device is powered off. For example, the memory 603 can be a solid state disk or a mechanical hard disk.
[0142] The working principle of the above electronic device can refer to the related steps in the memory compression method of the foregoing embodiments, and will not be described here.
[0143] It should be noted that each embodiment in the present specification adopts a progressive manner for description, and each embodiment focuses on the difference from other embodiments. The same and similar parts of each embodiment can be referred to each other.
[0144] For the convenience of description, the above system or device is described as various modules or units in terms of functions. Of course, the functions of each unit can be implemented in the same or multiple software and / or hardware in the implementation of the present application.
[0145] Those skilled in the art can clearly understand the application by the description of the above embodiments. The technical solutions of the application can be implemented by means of software and necessary universal hardware platforms. Based on such an understanding, the technical solutions of the application can be embodied in the form of a software product, and the computer software product can be stored in a storage medium, such as a ROM / RAM, a magnetic disk, or an optical disk, and includes a plurality of instructions to cause a computer device (such as a personal computer, a server, or a network device) to execute the methods described in the various embodiments or some parts of the embodiments of the application.
[0146] Finally, it should be noted that the terms such as first, second, third, and fourth, etc. are merely used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the terms "include", "contain" or any other variants thereof are intended to cover non-exclusive inclusion, so that the processes, methods, articles or devices including a series of elements not only include those elements, but also include other elements not explicitly listed or inherent to such processes, methods, articles or devices. Without more limitations, the element defined by the statement "including a" does not exclude the presence of other identical elements in the process, method, article or device including the element.
[0147] The above description is only the preferred embodiments of the application, and it should be pointed out that those skilled in the art can make some improvements and refinements without departing from the principles of the application, and these improvements and refinements should also be regarded as the protection scope of the application.
Claims
1. A memory compression method, comprising: determining a target compression algorithm corresponding to a memory page to be compressed from a plurality of compression algorithms; recording a correspondence between the memory page to be compressed and the target compression algorithm in a memory compression module; when the memory compression module is in a first mode, obtaining the target compression algorithm according to the correspondence, and performing compression processing on the memory page to be compressed based on the target compression algorithm. 2.The method of claim 1, further comprising: when the memory compression module is in a second mode, performing compression processing on the memory page to be compressed according to a preset primary compression algorithm, to obtain a compressed memory page; in response to obtaining a re-compression instruction, decompressing the compressed memory page according to the primary compression algorithm to obtain the memory page to be compressed; performing compression processing on the memory page to be compressed according to a preset re-compression algorithm, to obtain a re-compressed memory page. 3.The method of claim 2, further comprising at least one of: switching the memory compression module from the second mode to the first mode according to system load information; and in response to obtaining a switching instruction, switching the memory compression module from the second mode to the first mode. 4.The method of claim 1, wherein the recording of the correspondence between the memory page to be compressed and the target compression algorithm in the memory compression module comprises: configuring an algorithm identifier of the target compression algorithm for a memory page to be compressed recorded in an algorithm configuration structure of the memory compression module according to a device management interface of a system kernel, to record the correspondence between the memory page to be compressed and the target compression algorithm. 5.The method of claim 1, further comprising: in response to a read request for the memory page to be compressed, obtaining a compressed memory page obtained by performing compression processing on the memory page to be compressed; obtaining the target compression algorithm corresponding to the memory page to be compressed according to the correspondence, to decompress the compressed memory page according to the target compression algorithm, to obtain the memory page to be compressed. 6.The method of claim 1, further comprising: storing the compressed memory page obtained by performing compression processing based on the target compression algorithm, and recording an algorithm identifier of the target compression algorithm in an index table used to record an index of the compressed memory page, to invoke the target compression algorithm according to the algorithm identifier when decompressing the compressed memory page. 7.The method of claim 1, wherein the determining of the target compression algorithm corresponding to the memory page to be compressed from the plurality of compression algorithms comprises: obtaining system load information according to a running state interface of a system kernel, the system load information including any one or more of a processor usage rate, processor stress information, a memory usage rate, and memory stress information; and determining the target compression algorithm corresponding to the memory page to be compressed from the plurality of compression algorithms according to the system load information. 8.The method of claim 7, wherein the plurality of compression algorithms include a first compression algorithm and a second compression algorithm, and a compression rate of the first compression algorithm is less than a compression rate of the second compression algorithm. The step of determining the target compression algorithm corresponding to the memory page to be compressed from multiple compression algorithms based on the system load information includes: The system load information indicates that when the processor is under high load, the first compression algorithm is determined to be the target compression algorithm for the memory page to be compressed. When the system load information indicates that the memory is under high load, the second compression algorithm is determined to be the target compression algorithm for the memory page to be compressed.
9. The method according to claim 1, wherein determining the target compression algorithm corresponding to the memory page to be compressed among multiple compression algorithms includes: Based on the frequency with which the memory page to be compressed is accessed by the corresponding application, the target compression algorithm corresponding to the memory page to be compressed is determined from a variety of compression algorithms; And / or, based on historical system load information, predictive system load information for a future time period is determined, so as to determine the target compression algorithm corresponding to the memory page to be compressed from multiple compression algorithms based on the predicted system load information.
10. An electronic device comprising: Memory, processor, and RAM; The memory includes multiple memory pages; The memory is used to store computer programs; The processor is used to execute the computer program to perform: The target compression algorithm corresponding to the memory page to be compressed is determined from a variety of compression algorithms; The correspondence between the memory pages to be compressed and the target compression algorithm is recorded in the memory compression module; When the memory compression module is in the first mode, the target compression algorithm is obtained according to the correspondence, and the memory page to be compressed is compressed based on the target compression algorithm.