A method for compressing memory occupied by a processor, applied to a scenario in which a first application run by a terminal device is switched from a foreground to a background, wherein the method comprises: determining a virtual memory address range, wherein the virtual memory address range is a range of all or some of virtual memory addresses occupied by a processor of the terminal device when the terminal device runs the first application in the foreground, and the virtual memory address range comprises at least one virtual memory page; determining, in a first page table based on the at least one virtual memory page, each physical memory page corresponding to each virtual memory page, wherein the first page table is a page table of a process of the first application, wherein each physical memory page has a bit mode, where the bit mode is used to indicate a content type of the said physical memory page; and compressing each physical memory page by using a predefined compression algorithm, wherein the compressing each physical memory page by using a predefined compression algorithm comprises: performing the following method for each physical memory page, to compress each physical memory page: using a predefined compression algorithm corresponding to one physical memory page to compress the physical memory page, wherein the using a predefined compression algorithm corresponding to one physical memory page to compress the physical memory page comprises: obtaining a content type of each physical memory page by reading the bit mode of the said physical memory page; determining, based on the content type of the said physical memory page, a predefined compression algorithm corresponding to the content type of the said physical memory page; and compressing the said physical memory page by using the predefined compression algorithm corresponding to the content type of the said physical memory page. determining a virtual memory address range, wherein the virtual memory address range is a range of all or some of virtual memory addresses occupied by a processor of the terminal device when the terminal device runs the first application in the foreground, and the virtual memory address range comprises at least one virtual memory page; determining, in a first page table based on the at least one virtual memory page, each physical memory page corresponding to each virtual memory page, wherein the first page table is a page table of a process of the first application, wherein each physical memory page has a bit mode, where the bit mode is used to indicate a content type of the said physical memory page; and compressing each physical memory page by using a predefined compression algorithm, wherein the compressing each physical memory page by using a predefined compression algorithm comprises: performing the following method for each physical memory page, to compress each physical memory page: using a predefined compression algorithm corresponding to one physical memory page to compress the physical memory page, wherein the using a predefined compression algorithm corresponding to one physical memory page to compress the physical memory page comprises: obtaining a content type of each physical memory page by reading the bit mode of the said physical memory page; determining, based on the content type of the said physical memory page, a predefined compression algorithm corresponding to the content type of the said physical memory page; and compressing the said physical memory page by using the predefined compression algorithm corresponding to the content type of the said physical memory page. performing the following method for each physical memory page, to compress each physical memory page: using a predefined compression algorithm corresponding to one physical memory page to compress the physical memory page, wherein the using a predefined compression algorithm corresponding to one physical memory page to compress the physical memory page comprises: obtaining a content type of each physical memory page by reading the bit mode of the said physical memory page; determining, based on the content type of the said physical memory page, a predefined compression algorithm corresponding to the content type of the said physical memory page; and compressing the said physical memory page by using the predefined compression algorithm corresponding to the content type of the said physical memory page. obtaining a content type of each physical memory page by reading the bit mode of the said physical memory page; determining, based on the content type of the said physical memory page, a predefined compression algorithm corresponding to the content type of the said physical memory page; and compressing the said physical memory page by using the predefined compression algorithm corresponding to the content type of the said physical memory page.