A memory expansion method and related device

By creating two zygote virtual machines in electronic devices, one with larger Java memory and the other with default Java memory, and selecting the appropriate virtual machine for copying based on application needs, the system lag problem caused by application memory constraints was solved, achieving stable expansion of Java memory and performance improvement.

CN119883581BActive Publication Date: 2025-12-12HONOR DEVICE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311354061.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-10-18
Publication Date
2025-12-12
Estimated Expiration
2043-10-18

AI Technical Summary

Technical Problem

In existing technologies, limited memory usage in applications leads to memory leaks and delayed object release, causing system lag and performance issues, which are particularly severe in multi-threaded asynchronous frameworks. How to expand Java memory while ensuring system stability has become an urgent problem to be solved.

Method used

Electronic devices create two zygote virtual machines: one with a larger Java memory space and the other with a default Java memory space. The appropriate virtual machine is selected and copied according to the application's needs to achieve memory expansion and avoid global expansion for all applications.

Benefits of technology

While ensuring system stability, Java memory expansion is implemented for specific applications to avoid stuttering and performance issues caused by excessive memory usage, thereby improving system performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119883581B_ABST
    Figure CN119883581B_ABST
Patent Text Reader

Abstract

The application discloses a memory expansion method and related equipment. According to the method, an electronic device can create a first zygote virtual machine and a second zygote virtual machine. The memory size of the Java memory space of the first zygote virtual machine is greater than the default memory size, and the memory size of the Java memory space of the second zygote virtual machine is the default memory size. If a specific application cold start is triggered, the electronic device can copy the virtual machine corresponding to the application based on the first zygote virtual machine. The memory of the Java memory space in the virtual machine corresponding to the application is the same as the memory of the Java memory space in the first zygote virtual machine, and is greater than the default memory size. Through the method, the electronic device can expand the memory of the Java memory space while ensuring the stability of the system, thereby avoiding problems such as lag caused by excessive memory occupation.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of terminals, and in particular to a memory expansion method and related equipment. BACKGROUND

[0002] Since an application program using too much memory can cause the system to crash or become unstable, a certain application program occupying a large amount of memory can cause other application programs to be unable to normally run, and the hardware resources of an electronic device are relatively limited, in order to ensure system stability, fair allocation of resources, and be limited by device hardware resources, the operating system limits the memory usage of the application program. For example, in the Android operating system, the maximum Java memory available to the application program is 512 megabytes (MB). In the case where the memory of the above-mentioned application program is limited, if there are use specification problems in the process of the application program calling interfaces or controls, the application program business is complex and the development cycle is tight, which can cause memory leaks, and if the application program uses a multi-thread asynchronous framework, it can cause some objects to be delayed in release. Memory leaks and object delays in release can both cause the application program to occupy a high amount of memory and insufficient available memory. Once the Java memory available to the application program is insufficient, the application program triggers garbage collection (GC) for Block type. In this case, the application program will pause other tasks, causing serious lag and causing serious performance problems.

[0003] Therefore, how to expand the Java memory available to the application program while ensuring system stability is a problem to be solved at present. SUMMARY

[0004] The present application provides a memory expansion method and related equipment. According to the memory expansion method, the electronic device can create 2 zygote processes and corresponding 2 zygote virtual machines. The size of the memory (i.e. Java memory) of the Java memory space in one of the zygote virtual machines is greater than a second value, and the size of the memory of the Java memory space in the other zygote virtual machine is the second value. For a specific application cold start scenario, the electronic device can copy the virtual machine corresponding to the specific application based on the zygote virtual machine with larger Java memory, so that the Java memory available to the specific application is expanded to be greater than the second value.

[0005] In a first aspect, the present application provides a memory expansion method. The memory expansion method can be applied to an electronic device. The electronic device creates a first zygote virtual machine and a second zygote virtual machine. A memory size of a Java memory space in the first zygote virtual machine is a first value, and a memory size of a Java memory space in the second zygote virtual machine is a second value. The first value is greater than the second value. According to the memory expansion method, the electronic device can receive a first instruction for cold starting a first application, and in response to the first instruction, in a case where the first application meets a preset condition, the electronic device can copy the first zygote virtual machine to obtain a first virtual machine. The first virtual machine is a virtual machine instance corresponding to a first application process. The first application process is a process corresponding to the first application. A memory size of a Java memory space in the first virtual machine is the first value.

[0006] In the scheme provided in the present application, the electronic device can create two zygote processes, i.e., a first zygote process and a second zygote process. The virtual machine instances corresponding to the first zygote process and the second zygote process are a first zygote virtual machine and a second zygote virtual machine, respectively. The electronic device can apply a larger virtual address than a default memory size in the process of creating a Java memory space in the first zygote virtual machine to perform memory expansion, and apply a virtual address corresponding to the default memory size in the process of creating a Java memory space in the second zygote virtual machine without performing memory expansion. In this way, for a part of applications that have an expansion memory demand, the electronic device can copy the virtual machine corresponding to the application according to the first zygote virtual machine, so that the application can use a Java memory larger than the default memory size. For other applications that do not have an expansion memory demand, the electronic device can copy the virtual machine corresponding to the application according to the second zygote virtual machine, so that the application can use a Java memory of the default memory size. Through this method, the electronic device can realize memory expansion of the Java memory space for specific applications while ensuring system stability, thereby avoiding problems such as lag caused by GC when the memory occupancy is too large.

[0007] It can be understood that the zygote virtual machine is a virtual machine instance corresponding to the zygote process. The first virtual machine is a virtual machine instance corresponding to the process corresponding to the first application. It can be understood that the memory size of the Java memory space is the Java memory size.

[0008] In some embodiments of the present application, the first zygote process can be zygote process_1, and the second zygote process can be zygote process_2. The first zygote virtual machine can be zygote virtual machine_1, and the second zygote virtual machine can be zygote virtual machine_2.

[0009] In some embodiments of the present application, the first application can be application_1. The first virtual machine can be application_1 virtual machine.

[0010] In some embodiments of the present application, the first instruction can be the instruction in step S103 (as shown in Figure 8 ), can be the instruction in step S204 (as shown in Figure 9 ), and can also be the instruction in S701 (as shown in Figure 16 ).

[0011] It can be understood that the units of the first value and the second value are the units of the memory of the Java memory space. It can be understood that the unit of the memory of the Java memory space (i.e., the Java memory) can be MB. Of course, the unit of the Java memory can also be other. In the case of a change in the unit of the Java memory, the first value and the second value will also change. For example, in the case of the unit of the Java memory being MB, the first value can be 768 and the second value can be 512, since 768MB = 0.75GB and 512MB = 0.5GB, then in the case of the unit of the Java memory being GB, the first value is converted to 0.75 and the second value is converted to 0.5 accordingly. It should be noted that the unit of the Java memory corresponding to the first value and the second value can be set according to actual needs. For example, the unit of the Java memory corresponding to the first value and the second value is uniformly set to MB.

[0012] The default memory size involved in the present application can be the default memory size of the Java memory space. The default memory size of the Java memory space refers to the maximum available Java memory originally adopted by the Java memory space. The default memory size carries a unit. For example, the default memory size can be 512MB. It can be understood that the difference between the default memory size and the second value is that the default memory size carries a unit, while the second value does not carry a unit. For example, the second value can be 512, which indicates that the default memory size is 512MB.

[0013] In some embodiments of the present application, the virtual address corresponding to the Java memory space can be understood as a virtual address segment corresponding to the Java memory space, and the memory size corresponding to the virtual address segment is related to the memory size of the Java memory space. In the present application, the virtual address segment corresponding to the Java memory space can be understood as a virtual address segment that can be used by the Java memory space. In the case where the virtual address segment corresponding to the Java memory space is the virtual address currently used by the Java memory space, the memory size corresponding to the virtual address segment is the current memory size of the Java memory space. In the case where the virtual address segment corresponding to the Java memory space is not all the virtual address currently used by the Java memory space, the memory size corresponding to the virtual address segment currently used by the Java memory space is the current memory size of the Java memory space.

[0014] With reference to the first aspect, in a possible implementation, after the electronic device receives the first instruction for cold starting the first application by the user, the method can further include: in response to the first instruction, in the case where the first application does not meet the preset condition, the electronic device copies the second zygote virtual machine to obtain a second virtual machine. The second virtual machine is a virtual machine instance corresponding to a first application process. The memory size of the Java memory space in the second virtual machine is a second numerical value.

[0015] In the scheme provided in the present application, the electronic device does not have to perform memory expansion on each cold-starting application, but only performs memory expansion on the applications that have memory expansion needs (for example, the applications that meet the preset condition), and does not perform memory expansion on other applications. This way can avoid the problem of high memory occupation of the operating system caused by performing memory expansion on each cold-starting application, thereby ensuring system stability.

[0016] With reference to the first aspect, in a possible implementation, the first zygote virtual machine is a virtual machine instance corresponding to a first zygote process, and the second zygote virtual machine is a virtual machine instance corresponding to a second zygote process. Before the electronic device receives the first instruction for cold starting the first application by the user, the method can further include: the electronic device receives a second instruction for starting the electronic device by the user; in response to the second instruction, the electronic device can create the first zygote process and the first zygote virtual machine, and create the second zygote process and the second zygote virtual machine; and the electronic device can hatch a system service SystemServer process based on the second zygote process.

[0017] In the scheme provided in the application, after the electronic device creates the first zygote process and the second zygote process, the SystemServer process can be hatched based on the second zygote process. The SystemServer process is a system process, and the electronic device can not have to extend the memory for it.

[0018] In combination with the first aspect, in a possible implementation, the electronic device receives a first instruction of a user to cold start a first application, which can specifically include that the electronic device receives the first instruction through an initiating process. After the electronic device receives the first instruction of the user to cold start the first application, the method can further include that, in response to the first instruction, the electronic device can notify a SystemServer process in the electronic device to start the first application through the initiating process; and the electronic device can determine whether the first application meets a preset condition through the SystemServer process.

[0019] It can be understood that the SystemServer process determines whether the first application meets the preset condition. The SystemServer process is responsible for starting and managing the entire application framework layer, and the SystemServer process is an entrance for the operating system to start a service. In the scheme provided in the application, the electronic device can determine whether the first application meets the preset condition through the SystemServer process, so as to subsequently directly notify the corresponding zygote process to fork the application process through the SystemServer process, without having to instruct the SystemServer process to notify the corresponding zygote process to fork the application process after the above determination through other modules, thereby simplifying the process.

[0020] In combination with the first aspect, in a possible implementation, in the case where the first application meets the preset condition, the electronic device copies the first zygote virtual machine to obtain a first virtual machine, which can specifically include that, in the case where the electronic device determines through the SystemServer process that the first application meets the preset condition, the electronic device can notify a first zygote process in the electronic device to hatch a first application process through the SystemServer process; and the electronic device copies the first virtual machine from the first zygote virtual machine in the process of hatching the first application process through the first zygote process.

[0021] In the scheme provided in the application, in the case where the first application meets the preset condition, the electronic device can directly notify the first zygote process in the electronic device to hatch the first application process through the SystemServer process. The SystemServer process can directly notify the first zygote process to hatch the first application process through the established Socket channel, without being transferred through other modules, thereby simplifying the process.

[0022] In combination with the first aspect, in a possible implementation, in the case where the first application does not meet the preset condition, the electronic device copies the second zygote virtual machine to obtain the second virtual machine, which can specifically include: in the case where the electronic device determines that the first application does not meet the preset condition through the SystemServer process, the electronic device can notify the second zygote process in the electronic device to hatch the first application process through the SystemServer process; and the electronic device copies the second zygote virtual machine to obtain the second virtual machine in the process of hatching the first application process through the second zygote process.

[0023] In the scheme provided in the application, in the case where the first application does not meet the preset condition, the electronic device can directly notify the second zygote process in the electronic device to hatch the first application process through the SystemServer process. The SystemServer process can directly notify the second zygote process to hatch the first application process through the established Socket channel, without being transferred through other modules, thereby simplifying the process.

[0024] In combination with the first aspect, in a possible implementation, the virtual address corresponding to the Java memory space in the first zygote virtual machine is a first virtual address segment. The memory size corresponding to the first virtual address segment is a first value. The virtual address corresponding to the Java memory space in the second zygote virtual machine is a second virtual address segment. The memory size corresponding to the second virtual address segment is a second value. The method can further include: in the process of creating the first zygote virtual machine, the electronic device can determine that the memory size of the Java memory space in the first zygote virtual machine is the first value, and for the Java memory space in the first zygote virtual machine, the electronic device can apply the first virtual address segment and map the first virtual address segment to a physical address; and in the process of creating the second zygote virtual machine, the electronic device can determine that the memory size of the Java memory space in the second zygote virtual machine is the second value, and for the Java memory space in the second zygote virtual machine, the electronic device can apply the second virtual address segment and map the second virtual address segment to a physical address.

[0025] In the scheme provided in the present application, the electronic device can apply for a virtual address corresponding to a larger memory than the default memory size in the process of creating the Java memory space in the first zygote virtual machine, and the other virtual machines obtained by subsequently copying the first zygote virtual machine can use the applied virtual address corresponding to the larger memory, that is, the Java memory that the other virtual machines can use is larger than the default memory size. However, the electronic device can still apply for a virtual address corresponding to the default memory size in the process of creating the Java memory space in the second zygote virtual machine, and the other virtual machines obtained by subsequently copying the second zygote virtual machine can use the virtual address corresponding to the default memory size. In this way, the electronic device can provide two available maximum Java memories, that is, the Java memory with a size of the default memory size and the Java memory larger than the default memory size. The electronic device can select the available maximum Java memory corresponding to different applications according to the preset condition, so as to realize memory expansion for specific applications (for example, applications that meet the preset condition), that is, the Java memory that the other virtual machines can use is larger than the default memory size, which can avoid the problem of lag caused by GC when the memory occupation is too large.

[0026] In combination with the first aspect, in a possible implementation manner, the electronic device can apply for the first virtual address segment and map the first virtual address segment to the physical address, specifically, the electronic device can apply for an anonymous page corresponding to the first value. The anonymous page corresponding to the first value can include the first virtual address segment. The anonymous page can be used to represent a segment of virtual addresses in the process virtual address space which has a mapping relationship with the physical address. The electronic device can apply for the second virtual address segment and map the second virtual address segment to the physical address, specifically, the electronic device can apply for an anonymous page corresponding to the second value. The anonymous page corresponding to the second value can include the second virtual address segment.

[0027] In the scheme provided in the present application, for the electronic device with an Android system as the operating system, the electronic device can create a zygote process by applying for an anonymous page to create a Java memory space. Specifically, when the electronic device creates a first zygote virtual machine, the electronic device can apply for an anonymous page corresponding to the first value, so as to create a Java memory space with a memory size of the first value. Similarly, when the electronic device creates a second zygote virtual machine, the electronic device can apply for an anonymous page corresponding to the second value, so as to create a Java memory space with a memory size of the second value.

[0028] In some embodiments of the present application, the anonymous page corresponding to the first value can refer to the anonymous page corresponding to the Java memory space when the memory size of the Java memory space is the first value. For example, the anonymous page corresponding to 768 MB. The anonymous page corresponding to the second value can refer to the anonymous page corresponding to the Java memory space when the memory size of the Java memory space is the second value, i.e., the anonymous page corresponding to the default memory size of the Java memory space. For example, the anonymous page corresponding to 512 MB.

[0029] With reference to the first aspect, in a possible implementation, the first application satisfies the preset condition, and specifically includes: in the historical records of memory occupation after cold starting the first application for a plurality of times, the number of first type historical records is greater than a first historical record number; the first type historical record can include: a historical record in which the occupied memory of the Java memory space in the first application virtual machine is greater than a first threshold.

[0030] In the scheme provided in the present application, the electronic device can determine the application with high load according to the historical record of the Java memory occupation of the application, and perform memory expansion on the application with high load.

[0031] In some embodiments of the present application, the historical record of memory occupation after cold starting the first application for a plurality of times can include: a historical record of Java memory occupation of the first application after cold starting the first application for S times.

[0032] With reference to the first aspect, in a possible implementation, the first application satisfies the preset condition, and specifically includes: the first application is an application in a preset list.

[0033] In the scheme provided in the present application, the electronic device can perform memory expansion on the application in the preset list. The specific setting manner of the preset list can be referred to below, and will not be described here.

[0034] In a second aspect, the present application provides a memory expansion method. The memory expansion method can be applied to an electronic device. The electronic device creates a first zygote virtual machine and a second zygote virtual machine. A memory size of a Java memory space in the first zygote virtual machine is a first value, and a memory size of a Java memory space in the second zygote virtual machine is a second value. The first value is greater than the second value. The memory expansion method can include: the electronic device receiving a first instruction of a user to cold start a first application, and in response to the first instruction, the electronic device copying the first zygote virtual machine to obtain a first virtual machine; and the electronic device receiving a second instruction of a user to cold start a second application process, and in response to the second instruction, the electronic device copying the second zygote virtual machine to obtain a third virtual machine. The first virtual machine is a virtual machine instance corresponding to the first application process. The first application process is a process corresponding to the first application. The memory size of the Java memory space in the first virtual machine is the first value. The third virtual machine is a virtual machine instance corresponding to the second application process. The second application process is a process corresponding to the second application. The memory size of the Java memory space in the third virtual machine is the second value.

[0035] In the scheme provided in the present application, the electronic device can create two zygote processes and corresponding virtual machine instances, i.e., a first zygote process and a second zygote process, and a first zygote virtual machine and a second zygote virtual machine. The electronic device can apply a larger virtual address than the default memory size in the process of creating the Java memory space in the first zygote virtual machine to perform memory expansion, and apply a virtual address corresponding to the default memory size in the process of creating the Java memory space in the second zygote virtual machine without performing memory expansion. In this way, for some applications, the electronic device can copy the corresponding virtual machine of the application according to the first zygote virtual machine, so that the application can use a larger memory than the default memory size. For other applications, the electronic device can copy the corresponding virtual machine of the application according to the second zygote virtual machine, so that the application can use the Java memory of the default memory size. Through this method, the electronic device can implement memory expansion of the Java memory space for a part of the applications while ensuring system stability, thereby avoiding problems such as lag caused by GC when the memory occupancy is too large.

[0036] In some embodiments of the present application, the second application can be a system application. The second application process can be a system process. For example, the second application process can be a SystemServer process.

[0037] In combination with the second aspect, in a possible implementation manner, the first application satisfies a preset condition, and the second application does not satisfy the preset condition.

[0038] With reference to the second aspect, in a possible implementation manner, after the electronic device receives the first instruction of the user for cold starting the first application, the method further includes: the electronic device, in response to the first instruction, notifying a SystemServer process in the electronic device to start the first application, and determining, by the SystemServer process, whether the first application meets the preset condition. After the electronic device receives the second instruction of the user for cold starting the second application, the method further includes: the electronic device, in response to the second instruction, notifying the SystemServer process in the electronic device to start the second application, and determining, by the SystemServer process, whether the second application meets the preset condition.

[0039] In a third aspect, the present application provides an electronic device, including: one or more processors, and one or more memories; the one or more processors are coupled with the one or more memories, and the one or more memories are configured to store computer program codes, the computer program codes including computer instructions, and the one or more processors invoke the computer instructions to enable the electronic device to perform the method described in the first aspect or any implementation manner of the first aspect.

[0040] In a fourth aspect, the present application provides an electronic device, including: one or more processors, and one or more memories; the one or more processors are coupled with the one or more memories, and the one or more memories are configured to store computer program codes, the computer program codes including computer instructions, and the one or more processors invoke the computer instructions to enable the electronic device to perform the method described in the second aspect or any implementation manner of the second aspect.

[0041] In a fifth aspect, the present application provides a computer storage medium, including computer instructions, when the computer instructions are run on an electronic device, enabling the electronic device to perform the method described in the first aspect or any implementation manner of the first aspect.

[0042] In a sixth aspect, the present application provides a computer storage medium, including computer instructions, when the computer instructions are run on an electronic device, enabling the electronic device to perform the method described in the second aspect or any implementation manner of the second aspect.

[0043] In a seventh aspect, the present application provides a chip, which can be applied to an electronic device, and the chip includes one or more processors, and the processor is configured to invoke computer instructions to enable the electronic device to perform the method described in the first aspect or any implementation manner of the first aspect.

[0044] In an eighth aspect, an embodiment of the present application provides a chip, which can be applied to an electronic device, and the chip comprises one or more processors configured to invoke computer instructions to cause the electronic device to perform the method described in the second aspect or any possible implementation manner of the second aspect.

[0045] In a ninth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on an electronic device, cause the electronic device to perform the method described in the first aspect or any possible implementation manner of the first aspect.

[0046] In a tenth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on an electronic device, cause the electronic device to perform the method described in the second aspect or any possible implementation manner of the second aspect.

[0047] It can be understood that the electronic device provided in the third aspect, the computer storage medium provided in the fifth aspect, the chip provided in the seventh aspect, and the computer program product provided in the ninth aspect are all used to perform the method described in the first aspect or any possible implementation manner of the first aspect. Therefore, the beneficial effects that can be achieved thereby can refer to the beneficial effects of any possible implementation manner of the first aspect, which will not be described here again. The electronic device provided in the fourth aspect, the computer storage medium provided in the sixth aspect, the chip provided in the eighth aspect, and the computer program product provided in the tenth aspect are all used to perform the method described in the second aspect or any possible implementation manner of the second aspect. Therefore, the beneficial effects that can be achieved thereby can refer to the beneficial effects of any possible implementation manner of the second aspect, which will not be described here again. BRIEF DESCRIPTION OF DRAWINGS

[0048] Figure 1 A zygote process hatching principle schematic diagram provided by an embodiment of the present application;

[0049] Figure 2 A software structure schematic diagram of an electronic device provided by an embodiment of the present application;

[0050] Figure 3 Another zygote process hatching principle schematic diagram provided by an embodiment of the present application;

[0051] Figure 4A And Figure 4B A virtual machine schematic diagram provided by an embodiment of the present application;

[0052] Figure 5 A runtime data area schematic diagram provided by an embodiment of the present application;

[0053] Figure 6A AndFigure 6B A schematic diagram of different zygote virtual machines provided by an embodiment of the present application;

[0054] Figure 7 A schematic diagram of two hatching modes provided by an embodiment of the present application;

[0055] Figure 8 A flowchart of a memory extension method provided by an embodiment of the present application;

[0056] Figure 9 A flowchart of another memory extension method provided by an embodiment of the present application;

[0057] Figure 10 A flowchart of creating a zygote process and a SystemServer process provided by an embodiment of the present application;

[0058] Figure 11 A schematic diagram of a specific implementation of creating a zygote process provided by an embodiment of the present application;

[0059] Figure 12 A flowchart of creating a zygote process and its corresponding virtual machine provided by an embodiment of the present application;

[0060] Figure 13 A flowchart of another creating a zygote process and its corresponding virtual machine provided by an embodiment of the present application;

[0061] Figure 14A A flowchart of creating a zygote virtual machine_1 provided by an embodiment of the present application;

[0062] Figure 14B A flowchart of creating a zygote virtual machine_2 provided by an embodiment of the present application;

[0063] Figure 15 A schematic diagram of a newly created process provided by an embodiment of the present application;

[0064] Figure 16 A flowchart of starting an application program and creating a corresponding virtual machine provided by an embodiment of the present application;

[0065] Figure 17 A schematic diagram of a hardware structure of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION

[0066] The technical solutions in the embodiments of the present application will be described clearly and completely in the following with reference to the drawings in the embodiments of the present application. In the description of the embodiments of the present application, unless otherwise specified, " / " represents the meaning of or, for example, A / B can represent A or B; the "and / or" in the text is only to describe the association relationship of the associated objects, which means that there can be three relationships, for example, A and / or B, which can represent the three cases of A alone, A and B together, and B alone. In addition, in the description of the embodiments of the present application, "multiple" means two or more than two.

[0067] It should be understood that the terms "first", "second" and the like in the description of the present application and claims and drawings are used to distinguish different objects, and are not used to describe a specific order. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but can optionally include steps or units not listed, or can optionally include other steps or units inherent to the process, method, product or device.

[0068] In the present application, referring to "embodiments" means that the specific features, structures or characteristics described in conjunction with the embodiments can be included in at least one embodiment of the present application. The appearance of this phrase at various places in the specification does not necessarily mean the same embodiment, nor is it an independent or alternative embodiment to other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described in the present application can be combined with other embodiments.

[0069] In the use process of electronic devices such as mobile phones, computers, etc., as shown in Figure 1 When the application program in the electronic device is cold started, the zygote process in the electronic device can hatch (fork) the processes corresponding to the application program (i.e., the application program processes shown in Figure 1 At the same time, the virtual machine instances corresponding to the zygote process (i.e., the zygote virtual machines shown in Figure 1 ) are copied to obtain the virtual machine instances corresponding to the application program (i.e., the application program virtual machines shown in Figure 1The application virtual machine inherits the spatial layout of the zygote virtual machine. As a result, the available Java memory for an application in the electronic device is limited after the application is cold started. For example, for an electronic device using an Android operating system (which can be referred to as Android system for short), the maximum available Java memory for an application in the electronic device is limited (for example, the default value of the maximum Java memory can be 512 MB). It can be understood that the Java memory refers to the memory provided for Java objects. The reason why the available Java memory for an application is limited is that the use of too much memory by an application can cause the system to crash or become unstable, and the system needs to limit the memory usage of the application to ensure the stability of the system, and the system can limit the memory available for each application to avoid the situation that some applications occupy a large amount of memory and cause other applications to be unable to normally run, and the hardware resources of the electronic device are relatively limited, and the system needs to limit the memory of the application to ensure that all applications can normally run on the device.

[0070] However, in the case where the memory of the application is limited, the application can frequently appear to be insufficient in available memory. For example, there are use specification problems in the process of the modules of the application calling interfaces or controls, and the application business is complex and the development cycle is tight, which can cause memory leaks. Memory leaks refer to the heap memory that has been dynamically allocated in the program and is not released or cannot be released due to some reason. In the case where the memory is limited, memory leaks can further cause the memory occupancy of the application to be high and the available memory to be insufficient. For another example, some applications use a multi-thread asynchronous framework, which can cause some objects to be delayed in release. In the case where the memory is limited, the delayed release of some objects can cause the memory occupancy of the application to be high and the available memory to be insufficient. Once the available memory of the application is insufficient, the application triggers Block type GC, which causes serious lag and serious performance problems.

[0071] It can be understood that the Block type GC refers to: when the memory of the application is insufficient and needs to be applied, the thread that needs to apply memory in the application is paused and waits for the GC to end before the thread that needs to apply memory can continue to run.

[0072] Embodiments of the present application provide a memory expansion method and related equipment. According to the memory expansion method, the electronic device can create two zygote processes, namely zygote process_1 and zygote process_2. The virtual machine instances corresponding to the zygote process_1 and the zygote process_2 are zygote virtual machine_1 and zygote virtual machine_2 respectively. The memory size of the Java memory space of the zygote virtual machine_1 is greater than the default memory size, and the memory size of the Java memory space of the zygote virtual machine_2 is the default memory size. When creating the zygote virtual machine_1, the electronic device can apply for a virtual address corresponding to a larger memory for the Java memory space, and when creating the zygote virtual machine_2, the electronic device can apply for a virtual address corresponding to the default memory size for the Java memory space. In the case of triggering cold start of a specific application program, the electronic device can hatch the process of the specific application program through the zygote process_1, and copy the virtual machine corresponding to the specific application program based on the zygote virtual machine_1. The memory size of the Java memory space in the virtual machine corresponding to the specific application program is the same as the memory size of the Java memory space in the zygote virtual machine_1, and both are the expanded memory size, that is, both are greater than the default memory size. It can be understood that the Java memory space refers to the memory space provided for Java objects. Through this method, the electronic device can realize memory expansion of the Java memory space while ensuring system stability, thereby avoiding problems such as freezing caused by GC when the memory occupancy is too large.

[0073] First, the software structure of the electronic device related to the present application will be introduced.

[0074] Please refer to Figure 2 , Figure 2 The software structure of the electronic device provided by the embodiments of the present application is shown in the figure.

[0075] As Figure 2 indicated, the software framework of the electronic device related to the present application can include an application layer, an application framework layer (FWK), a system library, a runtime, a hardware abstraction layer (HAL) and a kernel layer.

[0076] Among them, the application layer can include a series of application packages. For example, application_1, calendar, gallery, call, Bluetooth, video, music, short message and WLAN and the like application (which can also be referred to as application). Among them, application_1 can be a system application, or a third-party application, which is not limited by the present application. For example, application_1 can be a short video application, a live application, or a news application.

[0077] The application framework layer provides an application programming interface (API) and programming framework for applications of the application layer. The application framework layer includes some pre-defined functions. The application framework layer can include a series of system services. A system service is a modular component that focuses on a specific function. The functions provided by the application framework API can communicate with the system services to access the underlying hardware.

[0078] The application framework layer can include a zygote process. The zygote process is a very important daemon service in the Android system. Other processes can be forked out through the zygote process.

[0079] In this application, the electronic device can create two zygote processes. The two zygote processes can fork other processes. For ease of description, the two zygote processes created by the electronic device are denoted as zygote process_1 and zygote process_2. Among them, the memory size of the memory space corresponding to zygote process_1 is greater than the default memory size, and the memory size of the memory space corresponding to zygote process_2 is the default memory size. It can be understood that the memory space mentioned here can include Java memory space. In this case, the memory size of the memory space refers to the size of the Java memory, and the default memory size refers to the default memory size corresponding to the Java memory space, that is, the default Java memory.

[0080] For ease of description, the memory size of the Java memory space corresponding to zygote process_1 is denoted as the first value, and the memory size of the Java memory space corresponding to zygote process_2 is denoted as the second value. The first value and the second value correspond to the Java memory unit in the following description.

[0081] It can be understood that the memory space corresponding to the zygote process can refer to the memory space of the virtual machine instance corresponding to the zygote process (hereinafter can be referred to as zygote virtual machine). The related description of the virtual machine instances corresponding to zygote process_1 and zygote process_2 can be referred to in the following, and will not be expanded here.

[0082] The application framework layer can also include a System Server process. The System Server process is the first process forked from the zygote process_2. The virtual machine instance corresponding to the System Server process is copied from the virtual machine instance corresponding to the zygote process_2, and thus the virtual machine instance corresponding to the System Server process inherits the memory space layout of the virtual machine instance corresponding to the zygote process_2.

[0083] The System Server process is responsible for starting and managing the entire application framework layer. After the System Server process is created, it can mainly handle the following: (1) initialize some system settings, virtual machine configurations, etc.; (2) start a Binder thread pool, so that Binder cross-process communication with other processes can be performed; (3) create a System Service Manager, which is used to create, start, and manage the life cycle of system services; (4) create a main thread Looper and enter a loop to wait for messages; (5) start various system services, such as an Activity Manager Service (AMS), a Power Manager Service, a Package Manager Service (PMS), a Window Manager Service (WMS), and an Input Manager Service (IMS), etc.

[0084] The application framework layer can further include a killing module and a task stack recovery module. The killing module can be used to release the occupied memory in the application. In some embodiments of the present application, the killing module can register an application background event. In this way, the killing module can monitor whether the application is backgrounded. Once the killing module monitors that the application is backgrounded, the killing module can release the occupied memory in the application. It can be understood that the killing module can also monitor other unperceived scenarios in addition to the backgrounding of the application, which are not limited in the present application. For example, the other unperceived scenarios can also include that the electronic device does not perceive user operations acting on the application within a period of time (for example, 5 minutes). The task stack recovery module can be used to restore the interface displayed by the application before being killed. It can be understood that the task stack recovery module can restore the interface displayed by the application before being killed to the background. That is, the process of the application restoring the corresponding interface can not be perceived by the user. In some embodiments of the present application, the task stack recovery module can determine whether to re-launch and restore the corresponding interface of the killed application according to the priority decision. It can be understood that the priority refers to the priority of the killed application. In some embodiments of the present application, in the case that the priority of the killed application is high, the task stack recovery module can re-launch and restore the corresponding interface of the application. It can be understood that the related setting rules of the priority can refer to the related technical documents, which are not expanded here.

[0085] The application framework layer can further include a window manager, a content provider, a view system, a phone manager, a resource manager, and a notification manager, etc. The specific meanings thereof can refer to the related technical documents, which are not expanded here.

[0086] The runtime (Runtime) is responsible for the scheduling and management of the system. The runtime includes a core library and a virtual machine. The core library includes two parts: one part is the function function required to be called by the programming language (for example, java language), and the other part is the core library of the system. The application layer and the application framework layer run in the virtual machine. The virtual machine executes the programming files (for example, java files) of the application layer and the application framework layer into binary files. The virtual machine is used to perform the management of object life cycle, stack management, thread management, security and exception management, and garbage collection, etc. It can be understood that the virtual machine can include a plurality of process corresponding virtual machine instances. For example, a virtual machine instance corresponding to zygote process_1 (denoted as zygote virtual machine_1 in the present application), a virtual machine instance corresponding to zygote process_2 (denoted as zygote virtual machine_2 in the present application), and a virtual machine instance corresponding to application_1 process (denoted as application_1 virtual machine in the present application), etc.

[0087] The application programs in the Android system are written in Java language and run in independent virtual machines. If each application program needs to run and initialize a virtual machine separately when starting, it will greatly reduce the system performance. Therefore, the Android system first creates a zygote virtual machine, and then hatches other virtual machine processes through it to share the virtual machine memory and framework layer resources, thereby greatly improving the starting and running speed of the application program. Understandably, the zygote process creates a corresponding virtual machine instance, that is, the zygote virtual machine when starting, so that the application program process and the SystemServer process created by the fork form can obtain a virtual machine instance copy inside. This means that the initial space layout of the virtual machine corresponding to the other processes obtained by the zygote process fork inherits the space layout of the zygote virtual machine. In short, it can also be understood that the electronic device copies the zygote virtual machine to obtain the virtual machine corresponding to other processes.

[0088] Specifically, please refer to Figure 3 , the zygote process_1 corresponds to the zygote virtual machine_1, and the zygote process_2 corresponds to the zygote virtual machine_2. Understandably, the zygote virtual machine_1 is the virtual machine instance corresponding to the zygote process_1, and the zygote virtual machine_2 is the virtual machine instance corresponding to the zygote process_2. The zygote process_1 can fork the application program process. It is worth noting that the application program process created by the zygote process_1 through fork can obtain a virtual machine instance copied from the zygote virtual machine_1. Similarly, the zygote process_2 can also fork the application program process. It is worth noting that the application program process created by the zygote process_1 through fork can obtain a virtual machine instance copied from the zygote virtual machine_1. It should also be noted that the zygote process_2 can also fork the SystemServer process.

[0089] Understandably, each application program runs in its corresponding process (that is, the application program process). Each application program process corresponds to its own virtual machine instance. For ease of description, the application program process corresponding virtual machine instance is recorded as the application program virtual machine in this application. In short, it can be understood that the application program runs in the virtual machine (for example, Dalvik / ART virtual machine). For example, application_1 runs in application_1 process, and the virtual machine instance corresponding to the application_1 process is application_1 virtual machine. It can also be simply understood that the virtual machine instance corresponding to the application_1 is the application_1 virtual machine.

[0090] It can be understood that if the application process is forked by the zygote process_1, the corresponding application virtual machine inherits the space layout of the zygote virtual machine_1. This means that the memory size of the memory space in the corresponding application virtual machine is the same as the memory size of the memory space in the zygote virtual machine_1, and is greater than the default memory size. Similarly, if the application process is forked by the zygote process_2, the corresponding application virtual machine inherits the space layout of the zygote virtual machine_2. This means that the memory size of the memory space in the corresponding application virtual machine is the same as the memory size of the memory space in the zygote virtual machine_2, and is the default memory size.

[0091] It should be noted that the electronic device can determine whether the application process is forked by the zygote process_1 or the zygote process_2 by judging whether the application satisfies the preset condition. It can be understood that the related description of the preset condition can be referred to below, and will not be expanded here.

[0092] It can be understood that the specific description of the application_1 virtual machine, the zygote virtual machine_1 and the zygote virtual machine_2 can be referred to below, and will not be expanded here.

[0093] It can be understood that the Dalvik virtual machine is an Android virtual machine used before the Android 5.0 version, and the ART virtual machine is an Android virtual machine fully used in the Android 5.0 version.

[0094] The runtime can also include a process management module. The process management module can be used to manage the processes. For example, creating a virtual machine instance corresponding to the zygote process_1 (i.e., zygote virtual machine_1), a virtual machine instance corresponding to the zygote process_2 (i.e., zygote virtual machine_2) and a virtual machine instance corresponding to the application (e.g., application_1 virtual machine).

[0095] The system library can include a plurality of functional modules. For example, a surface manager (Surface Manager), media libraries, a three-dimensional graphics processing library (e.g., OpenGL ES) and a two-dimensional graphics engine (e.g., SGL) and the like. The specific meaning and role of these functional modules can be referred to the related technical documents, and will not be expanded here.

[0096] A hardware abstraction layer (HAL) is an interface layer between the operating system kernel and the upper layer software, and its purpose is to abstract hardware. The hardware abstraction layer is an abstract interface of the device kernel driver, which is used to implement an application programming interface for providing access to the underlying device to a higher level Java API framework. The HAL can provide a standard interface to show the device hardware functions to the higher level Java API framework. The HAL includes a plurality of library modules, such as a camera HAL, an audio HAL, and the like. Each of the library modules implements an interface for a specific type of hardware component. When the system framework layer API requires access to the hardware of the portable device, the operating system will load the library module for the hardware component.

[0097] The kernel layer is the foundation of the Android system. The kernel layer is responsible for the functions of hardware drivers, network, power, system security, and memory management. The kernel layer is an intermediate layer between hardware and software, and its role is to pass the requests of the application program to the hardware. The kernel layer can include a camera driver, a display driver, a camera driver, an audio driver, and a sensor driver.

[0098] It should be noted that the memory expansion method provided in the present application Figure 2 The software structure diagram of the electronic device shown is only an example, and does not limit the specific module division in different layers of the Android system. For specific details, reference can be made to the introduction of the software structure of the Android system in the conventional technology. In addition, the memory expansion method provided in the present application can also be implemented based on other operating systems, which will not be illustrated one by one in the present application.

[0099] The virtual machine and its corresponding memory space will be described in detail below.

[0100] Please refer to Figure 4A , Figure 4A A schematic diagram of a virtual machine provided in an embodiment of the present application.

[0101] As Figure 4A shown, the virtual machine can include a Runtime (runtime) module and a runtime data area. The Runtime module can be used to control the memory space (for example, the runtime data area) in the virtual machine. The runtime data area can be used to store class Class file metadata, objects and arrays, and method parameter local variables. The runtime data area can include a plurality of spaces (Spaces), and the functions of the plurality of Spaces can be different. For example, as Figure 4A shown, the runtime data area can include a Java memory space. The Java memory space refers to the memory space provided for the use of Java objects. The memory corresponding to the Java memory space is the Java memory. It can be understood that, as Figure 4BAs shown, for the Android system, the Java memory space can be RegionSpace. Of course, for other operating systems, the Java memory space can also be other modules, which are not limited in the present application. It can be understood that a plurality of 256 kilobyte (KB) regions (regions can also be understood as memory blocks) constitute the RegionSpace.

[0102] In some embodiments of the present application, the Runtime module can control the Space in the runtime data area to apply for a virtual address and establish a mapping relationship between the virtual address and the physical address. Specifically, in a possible implementation, the Space in the runtime data area can call the address management module to apply for a virtual address. It can be understood that the address management module can be used to implement the mapping relationship between the virtual address and the physical address corresponding to the memory of the Java memory space (for example, RegionSpace). It should be noted that the virtual address involved in the present application can be understood as a virtual memory address, and the physical address can be understood as a physical memory address (i.e. RAM memory address). Virtual memory refers to a piece of logical memory virtually created by using disk space. Physical memory refers to the address space range that can be expressed by the real physical memory (i.e. RAM memory).

[0103] It can be understood that the full name of RAM is Random Access Memory, that is, random access register. RAM is also called main memory, which is an internal memory directly exchanging data with the Central Processing Unit (CPU). It can be read and written at any time (except when refreshing), and the speed is very fast, and is usually used as a temporary data storage medium for operating systems or other programs running. RAM can write (store) or read (take out) information from any specified address at any time when working.

[0104] It should be noted that the runtime data area can not only include the Java memory space (for example, RegionSpace), but also include other Spaces. For example, Figure 5 As shown, the runtime data area can include RegionSpace, LargeObjectSpace (LargeObjectSpace), ZygoteSpace (ZygoteSpace), ImageSpace (ImageSpace) and jit code cache (jit code cache) and the like. The specific meanings and functions of these memory spaces can be referred to related technical documents, which are not expanded here.

[0105] It can be understood that the runtime data area can also control other modules such as the stack and the method area. The stack is a memory instruction area used to store basic data types, instruction codes and constants. The method area is similar to the runtime data area and is a memory area shared by various threads, which can be used to store type information, constants, static variables, code cache after just-in-time compiler compilation and other data loaded by the virtual machine.

[0106] It can be understood that the memory allocated in the runtime data area is managed by the GC mechanism of the virtual machine. It can be understood that the related description of GC can refer to the above and related technical documents, and will not be repeated here.

[0107] It can be understood that the Runtime module can also include an execution engine, a native library interface, a native method library and a Java thread, and the specific structure of the Runtime module is not limited in the present application.

[0108] It can be understood that the virtual machine mentioned in the present application can be a Java Virtual Machine (JVM).

[0109] The zygote virtual machine_1, zygote virtual machine_2 and its hatching process are introduced below.

[0110] It can be understood that, as shown in Figure 6A , the present application records the Runtime module in the zygote virtual machine_1 as Runtime module_1, records the runtime data area in the zygote virtual machine_1 as runtime data area_1, and records the Java memory space in the zygote virtual machine_1 as RegionSpace_1. Similarly, as shown in Figure 6B , the present application records the Runtime module in the zygote virtual machine_2 as Runtime module_2, records the runtime data area in the zygote virtual machine_2 as runtime data area_2, and records the Java memory space in the zygote virtual machine_2 as RegionSpace_2. According to the above, the memory size of RegionSpace_1 is a first value, the memory size of RegionSpace_2 is a second value, and the memory size of RegionSpace_1 is larger than that of RegionSpace_2. The memory size of RegionSpace_2 is the default memory size corresponding to RegionSpace.

[0111] As described above, the zygote process can create other processes using fork. The electronic device may include zygote process_1 and zygote process_2, and their respective virtual machine instances, namely zygote virtual machine_1 and zygote virtual machine_2. For ease of description, this application refers to zygote process_1 creating other processes using fork as the first fork method, and zygote process_2 creating other processes using fork as the second fork method.

[0112] Understandably, during the fork process, other processes (e.g., application processes) can obtain a copy of a virtual machine instance from the zygote process. Taking an application process as an example, if the electronic device obtains the application process using the first fork method, the application process can obtain a virtual machine instance copied from the virtual machine instance corresponding to zygote process_1 (i.e., zygote virtual machine_1). In other words, the electronic device can obtain the application virtual machine by copying zygote virtual machine_1. Similarly, if the electronic device obtains the application process using the second fork method, the application process can obtain a virtual machine instance copied from the virtual machine instance corresponding to zygote process_2 (i.e., zygote virtual machine_2). In other words, the electronic device can obtain the application virtual machine by copying zygote virtual machine_2.

[0113] For example, such as Figure 7 As shown, the electronic device can obtain the process corresponding to application_1 (i.e., application_1 process) using either the first or second fork method. In this way, the virtual machine instance corresponding to application_1 process (i.e., application_1 virtual machine) can be copied (or replicated) from the virtual machine instance corresponding to zygote process_1 (i.e., zygote virtual machine_1) or the virtual machine instance corresponding to zygote process_2 (i.e., zygote virtual machine_2). For ease of description, this application designates the Runtime module in application_1 virtual machine as Runtime module_3, the runtime data area in application_1 virtual machine as runtime data area_3, and the Java memory space in application_1 virtual machine as RegionSpace_3.

[0114] Specifically, if the electronic device adopts the first fork mode to obtain the application_1 process, the application_1 virtual machine can be copied from the zygote virtual machine_1. In this case, the application_1 virtual machine is the same as the zygote virtual machine_1. That is, the Runtime module_3 and the Runtime module_1 are the same, the runtime data area_3 and the runtime data area_1 are the same, and the RegionSpace_3 and the RegionSpace_1 are the same. Notably, the RegionSpace_3 and the RegionSpace_1 are the same, which specifically can include that the memory size of the RegionSpace_3 is the same as the memory size of the RegionSpace_1. That is, the memory size of the RegionSpace_3 is the first numerical value, which is larger than the default memory size. For example, the memory size of the RegionSpace_3 can be 768 MB. Similarly, if the electronic device adopts the second fork mode to obtain the application_1 process, the application_1 virtual machine can be copied from the zygote virtual machine_2. In this case, the application_1 virtual machine is the same as the zygote virtual machine_2. That is, the Runtime module_3 and the Runtime module_2 are the same, the runtime data area_3 and the runtime data area_2 are the same, and the RegionSpace_3 and the RegionSpace_2 are the same. Notably, the RegionSpace_3 and the RegionSpace_2 are the same, which specifically can include that the memory size of the RegionSpace_3 is the same as the memory size of the RegionSpace_2. That is, the memory size of the RegionSpace_3 is the second numerical value, which is the default memory size. For example, the memory size of the RegionSpace_3 can be 512 MB.

[0115] According to the foregoing, in some embodiments of the present application, if the application_1 satisfies the preset condition, the electronic device can adopt the first fork mode to obtain the application_1 process, and if the application_1 does not satisfy the preset condition, the electronic device can adopt the second fork mode to obtain the application_1 process. It can be understood that the related description of the preset condition can be referred to below, which is not described here.

[0116] In some embodiments of the present application, after the application_1 virtual machine is created, the application_1 can set the application_1 virtual machine according to actual needs (for example, change the memory space layout in the application_1 virtual machine), which is not limited in the present application.

[0117] It can be understood that the zygote process_1 and the zygote process_2 can also fork other processes (for example, processes corresponding to other application programs such as short messages, music, video, etc.), and the present application will not be illustrated one by one.

[0118] It can be understood that in the Android system, the Java memory space in the zygote virtual machine can be RegionSpace, however, in other operating systems, the Java memory space in the zygote virtual machine can not be RegionSpace, but other modules, which are not limited by the present application.

[0119] The following describes a memory expansion method provided by an embodiment of the present application.

[0120] Please refer to Figure 8 , Figure 8 A flowchart of a memory expansion method provided by an embodiment of the present application. The method can include but not limited to the following steps:

[0121] S101: The electronic device receives an instruction to start the electronic device.

[0122] The user can trigger the start of the electronic device by long-pressing the power key and the like. Correspondingly, the electronic device can receive the instruction to start the electronic device.

[0123] S102: In response to the instruction to start the electronic device received by the electronic device, the electronic device starts to create a zygote virtual machine_1 and a zygote virtual machine_2. In the process of creating the zygote virtual machine_1, the electronic device creates a Java memory space based on a first value. In the process of creating the zygote virtual machine_2, the electronic device creates a Java memory space based on a second value. Wherein the first value is greater than the second value.

[0124] In response to the instruction to start the electronic device received by the electronic device, the electronic device can create a zygote process_1 and a zygote process_2. It can be understood that the electronic device can create a virtual machine instance corresponding to the zygote process after creating the zygote process. That is, the electronic device can start to create a zygote virtual machine_1 after creating a zygote process_1, and similarly, the electronic device can start to create a zygote virtual machine_2 after creating a zygote process_2. It can be understood that the present application does not limit the order of creating the zygote virtual machine_1 and the zygote virtual machine_2 by the electronic device.

[0125] It can be understood that the memory size of the Java memory space in the completed zygote virtual machine_1 is the first value, and the memory size of the Java memory space in the completed zygote virtual machine_2 is the second value. The first value is greater than the second value.

[0126] It can be understood that the first value can be set according to actual needs, and the specific value is not limited in the present application. For example, the first value can be 768. The unit of the Java memory corresponding to the first value can be MB, and in this case, the memory of the Java memory space created based on the first value is 768MB. In some embodiments of the present application, the first value is not greater than a preset memory threshold. It can be understood that the preset memory threshold can be set according to actual needs, and the present application does not limit this. For example, the preset memory threshold can be 768. For another example, the preset memory threshold can be 750.

[0127] According to the foregoing, the second value is the default memory size of the Java memory space. In some embodiments of the present application, the second value is 512. The unit of the Java memory corresponding to the second value can be MB, and in this case, the memory size of the Java memory space created based on the second value is 512MB.

[0128] S103: The electronic device receives an instruction to cold start the application_1.

[0129] The user can cold start the application_1 through touch (for example, click, slide, etc.), sound, etc. For example, the user can click the application icon (hereinafter referred to as the application_1 icon) corresponding to the application_1, and accordingly, the electronic device can receive an instruction to cold start the application_1.

[0130] It can be understood that when the application program is started, the electronic device does not have the process of the application program in the background, at this time, the system will create a new process and assign it to the application, and this starting method is called cold start (the background does not exist The process of the application program).

[0131] S104: In response to the instruction to cold start the application_1 received by the electronic device, in the case that the application_1 meets the preset condition, the electronic device copies the zygote virtual machine_1 to obtain the application_1 virtual machine. The memory size of the Java memory space in the application_1 virtual machine copied from the zygote virtual machine_1 is the first value.

[0132] In response to the instruction of the cold start application_1 received by the electronic device, the electronic device can determine whether the application_1 meets the preset condition. If the application_1 meets the preset condition, the electronic device can obtain the application_1 process based on the zygote process_1 fork, and correspondingly, obtain the application_1 virtual machine based on the zygote virtual machine_1 copy. In this case, the initial memory space layout of the application_1 virtual machine is consistent with the memory space layout in the created zygote virtual machine_1, that is, the initial memory size of the Java memory space in the application_1 virtual machine is the first value.

[0133] In some embodiments of the present application, the application_1 meets the preset condition, which can specifically include that the application_1 is an application in the preset list.

[0134] In some embodiments of the present application, the application_1 meets the preset condition, which can specifically include that the category of the application_1 is a preset category. For example, the preset category application can include any one or more of the following: social application, game application, video application, news application. It can be understood that the preset category application can also include other applications, which are not limited in the present application.

[0135] It can be understood that the preset list / pre-set category can be set according to actual needs, which is not limited in the present application.

[0136] In some embodiments of the present application, the preset list can be set based on the historical memory occupation of each application. For example, the preset list can include: for the memory occupation after a cold start a times, the number of times of memory occupation rate exceeding b% reaches c%*a applications. It can be understood that the memory occupation rate can be the percentage of occupied Java memory and maximum available Java memory. It can be understood that a, b and c can be set according to actual needs, which is not limited in the present application. For example, a can be 5, b can be 90, and c can be 80. Similarly, the preset category can be set based on the historical memory occupation of each category of application, which can be specifically referred to the above method of setting the preset list based on the historical memory occupation of each application.

[0137] In some embodiments of the present application, the preset list can be set based on the historical memory leakage of each application. For example, the preset list can include: for the memory leakage after a cold start a times, the number of times of memory leakage reaches c%*a applications. For another example, any one application in the preset list meets the following condition: during the running process after X cold starts, the number of times of memory leakage reaches Y. It can be understood that X and Y are positive integers, and Y is not greater than X. Similarly, the preset category can be set based on the historical memory leakage of each category of application, which can be specifically referred to the above method of setting the preset list based on the historical memory leakage of each application.

[0138] In some embodiments of the present application, the application_1 meets the preset condition, which can specifically include that, in the historical records of the Java memory occupation of the application_1 after S cold starts of the application_1, the number of the first type of historical records is greater than the first historical record times. In a possible implementation manner, the first type of historical records can include historical records in which the occupied Java memory space in the application_1 virtual machine is greater than a threshold_1. In another possible implementation manner, the first type of historical records can include historical records in which the ratio of the occupied Java memory space in the application_1 virtual machine to the maximum available Java memory (for example, the default memory size of the Java memory space) is greater than a ratio_1.

[0139] It can be understood that S is a positive integer. S can be set according to actual needs, and the present application does not limit this. For example, S can be 10.

[0140] It can be understood that the threshold_1 and the ratio_1 can be set according to actual needs, and the present application does not limit this. In some embodiments of the present application, the threshold_1 can be b% of the second value. For example, the threshold_1 can be 400. In some embodiments of the present application, the ratio_1 can be 0.8.

[0141] It can be understood that the first historical record times can be set according to actual needs, and the present application does not limit this. For example, the first historical record times can be 5 times. In some embodiments of the present application, the ratio of the first historical record times to S can be greater than a ratio_2. It can be understood that the ratio_2 can be set according to actual needs, and the present application does not limit this. For example, the ratio_2 can be 0.8. For another example, the ratio_2 can be 0.9.

[0142] In some embodiments of the present application, the application_1 meets the preset condition, which can specifically include that, in the historical records of the Java memory occupation of the application_1 after S cold starts of the application_1, the ratio of the number of the first type of historical records to S is greater than the ratio_2.

[0143] In some embodiments of the present application, the application_1 meets the preset condition, which can specifically include that, for the Java memory occupation after a cold start of the application_1 a times, the number of times that the Java memory occupation rate of the application_1 exceeds b% is greater than c%*a.

[0144] In some embodiments of the present application, the application_1 meets the preset condition, which can specifically include that, for the memory leakage after a cold start of the application_1 a times, the number of times of memory leakage of the application_1 reaches c%*a.

[0145] In some embodiments of the present application, the electronic device determines whether the application_1 meets the preset condition, which can specifically include that the unoccupied memory of the operating system of the electronic device is greater than a threshold value_2. It can be understood that the threshold value_2 can be set according to actual needs, and the present application does not make any limitation thereon. For example, the threshold value_2 can be 6 gigabytes (GB).

[0146] In some embodiments of the present application, the electronic device determines whether the application_1 meets the preset condition, which can specifically include that the ratio of the unoccupied memory of the operating system of the electronic device to the total memory of the operating system is greater than a threshold value_3. It can be understood that the total memory of the operating system is not greater than the RAM memory of the electronic device. It can be understood that the threshold value_3 can be set according to actual needs, and the present application does not make any limitation thereon. For example, the threshold value_3 can be 0.2.

[0147] Of course, the electronic device can also determine whether the application_1 meets the preset condition in other manners, and the present application does not make any limitation thereon.

[0148] It can be understood that in the case where the application_1 meets the preset condition, the application_1 will not actively apply for the virtual address segment applied for by the electronic device when creating the zygote virtual machine_1, so as to avoid the conflict caused by the partial or complete overlap of the virtual address segment actively applied for by the application_1 and the virtual address segment applied for when creating the zygote virtual machine_1, and to affect the system stability.

[0149] S105: In response to the instruction of cold starting the application_1 received by the electronic device, in the case where the application_1 does not meet the preset condition, the electronic device copies the zygote virtual machine_2 to obtain the application_2 virtual machine. The memory size of the Java memory space in the application_1 virtual machine copied from the zygote virtual machine_2 is a second value.

[0150] In response to the instruction of cold starting the application_1 received by the electronic device, the electronic device can determine whether the application_1 meets the preset condition. If the application_1 does not meet the preset condition, the electronic device can fork the zygote process_2 to obtain the application_1 process, and correspondingly, copy the zygote virtual machine_2 to obtain the application_1 virtual machine. In this case, the initial memory space layout of the application_1 virtual machine is consistent with the memory space layout in the completed zygote virtual machine_2, that is, the initial memory size of the Java memory space in the application_1 virtual machine is a second value.

[0151] Next, taking the Java memory space as an example, the memory expansion method provided by the present application (as shown in FIG. 6) will be further introduced in combination with the software structure of the electronic device shown in FIG. 5. Figure 2 Figure 9 ​​

[0152] Please refer to Figure 9 , Figure 9 A flowchart of another memory expansion method provided by an embodiment of the present application. The method can include, but is not limited to, the following steps:

[0153] S201: The electronic device receives an instruction to start the electronic device.

[0154] S202: In response to the instruction to start the electronic device received by the electronic device, the electronic device creates zygote process_1 and zygote process_2, and starts to create zygote virtual machine_1 and zygote virtual machine_2. The memory size of RegionSpace_1 in the created zygote virtual machine_1 is a first value. The memory size of RegionSpace_2 in the created zygote virtual machine_2 is a second value.

[0155] In some embodiments of the present application, the Java memory space can be RegionSpace. In this case, the second value refers to the default memory size of RegionSpace.

[0156] It can be understood that the specific implementation of steps S201-S202 can refer to steps S101-S102, which will not be described here in detail.

[0157] S203: The electronic device hatches SystemServer process based on zygote process_2.

[0158] After the electronic device creates zygote process_2, SystemServer process can be forked from zygote process_2. The specific implementation can refer to the following, which will not be described here.

[0159] S204: The electronic device receives an instruction to cold start application_1 through a launch process.

[0160] The user can cold start application_1 through touch (for example, click, slide, etc.), sound, etc. Accordingly, the electronic device can receive an instruction to cold start application_1 through a launch process. The launch process refers to a process that triggers zygote process to create a new process.

[0161] In some embodiments of the present application, the initiating process can be a desktop process (i.e., Launcher process). The Launcher requests the information of the applications already installed in the system during the startup process, and encapsulates the information into a shortcut icon list displayed on the system screen, so that the user can start the corresponding application by clicking the shortcut icon. In a popular way, the Launcher can be understood as the desktop of the Android system.

[0162] Of course, the initiating process can also be other processes, which are not specifically limited in the present application. For example, the initiating process can also be the process corresponding to other applications, system user interface (systemUI) process, negative screen process, etc.

[0163] S205: The electronic device notifies the SystemServer process of cold starting the application_1 through the initiating process.

[0164] After the electronic device receives the instruction of cold starting the application_1 through the initiating process, the electronic device can also notify the SystemServer process of cold starting the application_1 through the initiating process. It can be understood that the electronic device can send a corresponding message to the SystemServer process through the initiating process to notify the SystemServer process of cold starting the application_1.

[0165] S206: The electronic device determines whether the application_1 meets the preset condition through the SystemServer process.

[0166] After the electronic device notifies the SystemServer process of cold starting the application_1 through the initiating process, the electronic device can determine whether the application_1 meets the preset condition through the SystemServer process. If the application_1 meets the preset condition, the electronic device can execute steps S207-S208, and if the application_1 does not meet the preset condition, the electronic device can execute steps S209-S210.

[0167] It can be understood that the related description of the preset condition can be referred to the above, which will not be repeated here.

[0168] S207: If the application_1 meets the preset condition, the electronic device instructs the zygote process_1 to hatch the application_1 process through the SystemServer process.

[0169] If the electronic device determines that the application_1 meets the preset condition through the SystemServer process, the electronic device can instruct the zygote process_1 to fork the application_1 process through the SystemServer process. The specific implementation manner can be referred to the following, which will not be expanded here.

[0170] S208: In the process of hatching the application_1 process by the zygote process_1, the electronic device copies the zygote virtual machine_1 to obtain the application_1 virtual machine.

[0171] In the process of forking the application_1 process by the zygote process_1, the electronic device can also create a virtual machine instance corresponding to the application_1 process, that is, copy the zygote virtual machine_1 to obtain the application_1 virtual machine. In this case, the memory size of RegionSpace (that is, RegionSpace_3) in the application_1 virtual machine is the first value.

[0172] S209: If the application_1 does not meet the preset condition, the electronic device instructs the zygote process_2 to hatch the application_1 process through the SystemServer process.

[0173] If the electronic device determines through the SystemServer process that the application_1 does not meet the preset condition, the electronic device can instruct the zygote process_2 to fork the application_1 process through the SystemServer process. For specific implementation, reference can be made to the following, and no further description is made here.

[0174] S210: In the process of hatching the application_1 process by the zygote process_2, the electronic device copies the zygote virtual machine_2 to obtain the application_1 virtual machine.

[0175] In the process of forking the application_1 process by the zygote process_2, the electronic device can also create a virtual machine instance corresponding to the application_1 process, that is, copy the zygote virtual machine_2 to obtain the application_1 virtual machine. In this case, the memory size of RegionSpace (that is, RegionSpace_3) in the application_1 virtual machine is the second value.

[0176] The following introduces a specific implementation of the foregoing embodiment from two aspects.

[0177] The first aspect: the electronic device creates the zygote process_1 and the zygote process_2, and the corresponding zygote virtual machine_1 and the zygote virtual machine_2.

[0178] 1. The electronic device creates the zygote process_1 and the zygote process_2

[0179] Please refer to Figure 10 , Figure 10 A flowchart for creating a zygote process and a SystemServer process provided by an embodiment of the present application. Figure 10The illustrated content can be understood as a specific implementation of the above steps S201-S203. It can be understood that the process of creating the zygote process and the SystemServer process can include but is not limited to the following steps:

[0180] S301: The electronic device detects a user operation on the power key, and in response to the user operation, the electronic device starts the bootloader.

[0181] When the electronic device is in a shutdown state, the user can press the power key of the electronic device to turn it on. At this time, the electronic device will first run the bootloader solidified in the read-only memory (ROM).

[0182] It can be understood that the bootloader is a boot program before starting the Android system, mainly checking the random access memory (RAM), initializing parameters, etc. The electronic device can initialize the hardware (such as memory, central processor, etc.) in the electronic device through the bootloader and load it into the RAM, thereby establishing a memory space mapping and preparing for loading the Linux kernel.

[0183] S302: The electronic device loads and starts the Linux kernel.

[0184] After the electronic device starts the bootloader and prepares for loading the Linux kernel, the electronic device can load and start the Linux kernel, that is, the Linux kernel in the electronic device will perform initialization of the entire system, and after completion, load the root file system.

[0185] S303: The electronic device starts the init process and parses the init.rc file.

[0186] After the Linux kernel in the electronic device is loaded, it will be called from the kernel kernel layer to the user space layer, and first the init process will be started. The init process is the first process of the system. In the startup process of the init process, the electronic device will parse the main init.rc file. According to the description of the init.rc file, the system will create files and directories and assign permissions, initialize attributes, and start important daemon processes of the Android system.

[0187] S304: The electronic device creates two zygote processes, and creates a SystemServer process. Among them, the electronic device creates one zygote process corresponding to a first value of Java memory size, and another zygote process corresponding to a second value of Java memory size.

[0188] In the process of parsing the init.rc file, the electronic device creates two zygote processes with different Java memories, namely zygote process_1 and zygote process_2. Among them, the size of the Java memory corresponding to zygote process_1 is a first value, and the size of the Java memory corresponding to zygote process_2 is a second value. After the electronic device creates zygote process_2, it can create a SystemServer process based on zygote process_2, that is, obtain the SystemServer process by forking zygote process_2. After zygote process_1 and zygote process_2 are started, they also become daemon processes, responsible for responding to requests to start an Android application package (APK).

[0189] The following will be described in conjunction with Figure 11 The process of creating zygote process_1 and zygote process_2 by the electronic device will be described in detail from the start of the init process of the electronic device.

[0190] As shown in Figure 11 When the electronic device starts the init process in the user space, it can execute the main function, that is, the main() function, by parsing the main function source file (main.cpp), so as to execute the responsibilities of the init process. It can be understood that the file with the extension.cpp is a C++ source code file, for example, main.cpp is a file that implements the main() function of the C++ program. The main() function is the entry of the application. Its main functions are to create an application, create a window, display a window, run an application, start a message loop and event processing of the application. That is, the main() function that can be implemented by main.cpp is the entry of the init process.

[0191] The process that the electronic device parses main.cpp and executes the main() function can include a first stage (FirstStageMain stage), a related setting of a security enhanced system (Security Enhanced Linux, SELinux, hereinafter uniformly written as selinux) (SetupSelinux stage), and a second stage (SecondStageMain stage). The main task of the FirstStageMain stage includes creating a file system directory and mounting a related file system, and shielding standard input / output / initializing a kernel log system. The FirstStageMain stage is mainly implemented by parsing a first stage main function source file (first_stage_main.cpp). The main task of the SetupSelinux stage is to complete a related setting of the selinux. The SetupSelinux stage is mainly implemented by parsing a security enhanced system source file (selinux.cpp). The main task of the SecondStageMain stage includes creating a process session key and initializing an attribute system, performing a second stage of the selinux and restoring some file security contexts, newly creating an extensible input / output (IO) event processing mechanism (epoll) and initializing a sub-process termination signal processing function, starting a service server matching an attribute, parsing an init.rc file, establishing an action sentence (action) of an rc file, a service sentence (service), etc., and starting other processes (for example, zygote process_1 and zygote process_2). The SecondStageMain stage is mainly implemented by parsing an initial source file (init.cpp), an activity management source file (action_manager.cpp), and an application main function source file (app_main.cpp).

[0192] In detail, as Figure 11As shown, after the electronic device parses main.cpp and starts to execute the main() function, the electronic device can enter the FirstStageMain stage. In the FirstStageMain stage, the electronic device can execute the first stage main function, i.e., the FirstStageMain() function, by parsing first_stage_main.cpp. After the electronic device executes the FirstStageMain() function and completes the task of the FirstStageMain stage, the main() function can be called again to enter the SetupSelinux stage. In the SetupSelinux stage, the electronic device can execute the setup selinux function, i.e., the SetupSelinux() function (also referred to as the SetupSelinux method), by parsing selinux.cpp. Specifically, the selinux rules can be loaded, the selinux log can be set, and the selinux related work can be completed. After the electronic device executes the SetupSelinux function and completes the task of the SetupSelinux stage, the main() function can be called again to enter the SecondStageMain stage. In the SecondStageMain stage, the electronic device can execute the second stage main function, i.e., the SecondStageMain() function, by parsing source files such as init.cpp, action_manager.cpp, and app_main.cpp. After starting to execute the SecondStageMain() function, the electronic device can load and parse the init.rc file through init.cpp, obtain an ActionManager instance through action_manager.cpp, add actions and triggers to the corresponding queues of the ActionManager, loop to read and execute the commands in the init.rc, until the commands in the queue are executed, and create zygote process_1 and zygote process_2 through app_main.cpp.

[0193] It can be understood that the FirstStageMain() function is mainly used for initializing the hardware and drivers of the bootloader, which specifically includes the following three aspects: (1) loading the first-stage boot image (e.g., boot.img) of the Android system into the memory; (2) setting the initial memory layout and startup parameters; and (3) jumping to the second-stage main function SecondStageMain() function to continue the second stage. The SecondStageMain() function is mainly used for initializing the global environment and system properties, which specifically includes the following four aspects: (1) loading and starting the zygote process_1 and zygote process_2 as the hatcher of all application programs in the Android system; (2) starting the SystemServer process responsible for managing system services; (3) starting and loading application programs and system components; and (4) finally entering the main event loop of the system to wait for user interaction and handle system events.

[0194] It can be understood that the electronic device can execute the loading script LoadBootScripts() function by parsing init.cpp to load and parse the init.rc file (i.e., the script function shown in Figure 11 It can be understood that the init.rc file contains the binary executable file corresponding to the zygote process, as well as the process name, priority, address, and other related configuration parameters. The related configuration parameters include the related configuration parameters corresponding to the zygote process_1 and the zygote process_2. In this way, the electronic device can create the zygote process_1 based on the binary executable file corresponding to the zygote process and the related configuration parameters corresponding to the zygote process_1, and create the zygote process_2 based on the binary executable file corresponding to the zygote process and the related configuration parameters corresponding to the zygote process_2. The electronic device can also execute the trigger queue event function QueueEventTrigger() function by parsing action_manager.cpp to add Trigger to the corresponding queue of ActionManager. The electronic device can also execute the ExcuteOneCmmand() function, i.e., the execute one command function, by parsing action_manager.cpp to loop through the commands and execute the commands in init.rc until the execution of the commands in the queue is completed. The electronic device can also execute the main() function implemented by app_main.cpp by parsing app_main.cpp to create the zygote process_1 and the zygote process_2.

[0195] 2. The electronic device creates a zygote virtual machine_1 and a zygote virtual machine_2

[0196] The following takes any one of the zygote process_1 and the zygote process_2 as an example to briefly introduce the process of creating the zygote process and the corresponding virtual machine thereof.

[0197] Referring to Figure 12 , Figure 12 A flowchart of creating a zygote process and the corresponding virtual machine thereof is provided for the embodiments of the present application. The flowchart of creating the zygote process and the corresponding virtual machine thereof can include but is not limited to the following steps:

[0198] S401: The electronic device creates a zygote process.

[0199] The electronic device can create the zygote process according to the relevant configuration parameters obtained by parsing the init.rc file. It can be understood that the zygote process created by the electronic device mentioned in step S401 can be the zygote process_1 or the zygote process_2.

[0200] S402: The electronic device determines whether the currently created zygote process is a zygote process that needs to expand memory.

[0201] After the electronic device creates a zygote process, it can determine whether the zygote process is a zygote process that needs to expand memory. If the zygote process currently created by the electronic device is a zygote process that needs to expand memory, that is, the zygote process currently created is the zygote process_1. In this case, the electronic device can continue to perform step S403. However, if the zygote process currently created by the electronic device is not a zygote process that needs to expand memory, that is, the zygote process currently created is the zygote process_2. In this case, the electronic device can continue to perform step S404 and step S405.

[0202] In some embodiments of the present application, the electronic device determines whether the currently created zygote process is a zygote process requiring memory expansion, which can specifically include that the electronic device can determine whether the name of the currently created zygote process is the same as a preset name (i.e., the name of the zygote process requiring memory expansion). If the name of the currently created zygote process is the same as the name of the zygote process requiring memory expansion, the electronic device can determine that the currently created zygote process is the zygote process requiring memory expansion. However, if the name of the currently created zygote process is not the same as the name of the zygote process requiring memory expansion, the electronic device can determine that the currently created zygote process is not the zygote process requiring memory expansion.

[0203] For example, the name of the zygote process not requiring memory expansion can be zygote, and the name of the zygote process requiring memory expansion can be zygote_heap_expansion. If the name of the currently created zygote process recorded in the init.rc file is zygote, the electronic device can determine that the currently created zygote process is not the zygote process requiring memory expansion. However, if the name of the currently created zygote process recorded in the init.rc file is zygote_heap_expansion, the electronic device can determine that the currently created zygote process is the zygote process requiring memory expansion.

[0204] In some embodiments of the present application, the electronic device can also determine whether the currently created zygote process is a zygote process requiring memory expansion based on the identifier of the currently created zygote process and the identifier of the zygote process requiring memory expansion. Specifically, the electronic device can determine whether the identifier of the currently created zygote process is the same as a preset identifier (i.e., the identifier of the zygote process requiring memory expansion). If the identifier of the currently created zygote process is the same as the identifier of the zygote process requiring memory expansion, the electronic device can determine that the currently created zygote process is the zygote process requiring memory expansion. However, if the identifier of the currently created zygote process is not the same as the identifier of the zygote process requiring memory expansion, the electronic device can determine that the currently created zygote process is not the zygote process requiring memory expansion.

[0205] It can be understood that the identifier of the zygote process can be represented in the form of a word, a number, a character, etc., and the present application does not make a specific limitation thereon. For example, the identifier of the zygote process that needs to expand memory can be zygote_1, and the identifier of the zygote process that does not need to expand memory can be zygote_2. For another example, the identifier of the zygote process that needs to expand memory can be 1, and the identifier of the zygote process that does not need to expand memory can be 2.

[0206] Of course, the electronic device can also determine whether the currently created zygote process is a zygote process that needs to expand memory in other manners, and the present application does not make a limitation thereon.

[0207] S403: The electronic device creates a virtual machine corresponding to the currently created zygote process based on the first numerical value.

[0208] If the electronic device determines that the currently created zygote process is a zygote process that needs to expand memory, i.e., the currently created zygote process is zygote process_1, the electronic device can create a virtual machine corresponding to the currently created zygote process, i.e., zygote virtual machine_1, based on the first numerical value. It can be understood that the specific process of creating zygote virtual machine_1 by the electronic device based on the first numerical value can be referred to below, and will not be expanded here.

[0209] S404: The electronic device creates a virtual machine corresponding to the currently created zygote process based on the second numerical value.

[0210] If the electronic device determines that the currently created zygote process is not a zygote process that needs to expand memory, i.e., the currently created zygote process is zygote process_2, the electronic device can create a virtual machine corresponding to the currently created zygote process, i.e., zygote virtual machine_2, based on the second numerical value. It can be understood that the specific process of creating zygote virtual machine_2 by the electronic device based on the second numerical value can be referred to below, and will not be expanded here.

[0211] S405: The electronic device creates a SystemServer process by the fork manner based on the currently created zygote process.

[0212] After the electronic device determines that the currently created zygote process is not a zygote process that needs to expand memory, i.e., the currently created zygote process is zygote process_2, and creates zygote virtual machine_2 based on the second numerical value, the electronic device can obtain the SystemServer process by zygote process_2 fork.

[0213] It is worth noting that the electronic device can create the process (i.e., zygote process_1) that needs to expand memory and the zygote process (i.e., zygote process_2) that does not need to expand memory according to the different configuration parameters parsed, which means that the electronic device can perform the steps shown in FIG. 2 twice in the present application. Figure 12 It is understood that the present application does not specifically limit the order of the electronic device creating zygote process_1 and zygote process_2.

[0214] The following will take the zygote process_2 created first as an example to specifically introduce the process of the electronic device creating zygote process_1, zygote process_2 and the corresponding virtual machine.

[0215] Please refer to Figure 13 , Figure 13 A flowchart of creating two zygote processes and their corresponding virtual machines provided by an embodiment of the present application. The flowchart of creating two zygote processes and their corresponding virtual machines can include but not limited to the following steps:

[0216] S501: The electronic device creates zygote process_2.

[0217] It is understood that the init.rc file can first record the command of creating zygote process_2 and the corresponding related setting parameters of zygote process_2, so that the electronic device can first execute the command of creating zygote process_2 when parsing the init.rc file, that is, create zygote process_2 based on the corresponding related setting parameters of zygote process_2.

[0218] Specifically, in the process of parsing the init.rc file, the electronic device can parse the rc file about zygote, that is, the init.zygote.rc file, then the electronic device can create zygote process_2 first according to the parameters in init.zygote.rc, and create an Android runtime environment object (i.e., AppRuntime), and call its start method, that is, AppRuntime.start() method. It is understood that AppRuntime is a class defined in app_process.cpp, which inherits from the system's AndroidRuntime, and is mainly used to create and initialize the virtual machine.

[0219] S502: The electronic device creates zygote virtual machine_2. In the process of creating zygote virtual machine_2, the electronic device determines that the name of zygote process_2 is not the preset name, then the electronic device creates the Java memory space of zygote virtual machine_2 based on the second value.

[0220] After the electronic device creates the zygote process_2, the electronic device can create the corresponding virtual machine of the zygote process_2, i.e., the zygote virtual machine_2. In the process of creating the zygote virtual machine_2, the electronic device can first determine that the name of the zygote process_2 is not the preset name (e.g., zygote_heap_expansion), and then the electronic device can determine that the zygote process_2 does not need to expand the memory, and can create the Java memory space of the zygote virtual machine_2 based on the second value.

[0221] It can be understood that the electronic device can include two sets of zygote virtual machine startup parameters, and the two sets of zygote virtual machine startup parameters correspond to the zygote virtual machine_1 and the zygote virtual machine_2 respectively.

[0222] Specifically, after the electronic device creates the zygote process_2 and calls the AppRuntime.start() method, the electronic device can execute the startVM() method. When the electronic device executes the startVM() method, the electronic device can parse the startup parameters of the zygote virtual machine_2, and call the JNI_CreateJavaVM to create the zygote virtual machine_2. The startup parameters of the zygote virtual machine_2 can include the memory setting parameter of the RegionSpace (i.e., RegionSpace_2) in the zygote virtual machine_2. It can be understood that the memory size represented by the memory setting parameter is the second value. In this way, the memory size of the RegionSpace_2 in the zygote virtual machine_2 created by the electronic device calling the JNI_CreateJavaVM is the second value.

[0223] It can be understood that after the electronic device invokes the JNI_CreateJavaVM, the electronic device can create a Runtime object (i.e., a Runtime module) through a Runtime::create method. The Runtime object is responsible for providing a runtime environment of the virtual machine. Then, the electronic device can initialize the virtual machine through a Runtime::init method, i.e., create the virtual machine based on the virtual machine start parameters. In the process of initializing the virtual machine, the electronic device can create a heap object through a new gc::heap() method, i.e., create a runtime data area. In some embodiments of the present application, after the electronic device creates the heap object through the new gc::heap() method, the electronic device can determine whether the name of the zygote process is the preset name. The electronic device can set the memory size of the Java memory space (e.g., RegionSpace) in the virtual machine based on the determination result. According to step S502, after the electronic device creates the heap object through the new gc::heap() method, the electronic device can determine that the name of the zygote process_2 is not the preset name. The electronic device can set the memory size of the RegionSpace_2 in the zygote virtual machine_2 by using a default memory size (e.g., 512 MB).

[0224] It can be understood that after the electronic device creates the zygote process_2 and invokes the AppRuntime.start() method, the electronic device can further execute a startReg() method to register a JNI method, and call a ZygoteInit.main() method through the JNI method. After the electronic device invokes the ZygoteInit.main() method, the electronic device can further execute a new ZygoteServer to create a socket channel of the zygote process_2. The zygote process_2 as a server of communication can be used to respond to a client request. In addition, after the electronic device invokes the ZygoteInit.main() method, the electronic device can further execute a preload() method to preload general classes, color resources (drawable and color resources), graphics processing libraries (openGL), shared libraries, and web views (WebView), so as to improve the application program start efficiency.

[0225] S503: The electronic device determines that the name of the zygote process_2 is not the preset name, and then the electronic device hatches the zygote process_2 to obtain a SystemServer process.

[0226] After the electronic device creates the zygote process_2 and the zygote virtual machine_1, it can be determined that the name of the zygote process_2 is not the preset name, in which case the electronic device can call the startSystemServer() method to create the SystemServer process by means of forking, that is, based on the zygote process_2 to fork the SystemServer process.

[0227] S504: The electronic device puts the zygote process_2 into a loop to wait for a request to create a new process.

[0228] After the electronic device creates the SystemServer process, it can execute the runSelectLoop() method to make the zygote process_2 enter a loop, and then the zygote process_2 can wait for a request to create a new process. When the zygote process_2 receives a request to create a new process, the zygote process_2 can immediately wake up and perform the corresponding work.

[0229] S505: The electronic device creates the zygote process_1.

[0230] It can be understood that after the command for creating the zygote process_2 recorded in the init.rc file, a command for creating the zygote process_1 and the corresponding related setting parameters of the zygote process_1 can also be recorded. In this way, when the electronic device parses the init.rc file, it can first execute the command for creating the zygote process_2 (as shown in step S501), and then execute the command for creating the zygote process_1, that is, create the zygote process_1 based on the corresponding related setting parameters of the zygote process_1.

[0231] Specifically, in the process of parsing the init.rc file, the electronic device can parse the init.zygote.rc file, and then the electronic device can first create the zygote process_2 according to the parameters in the init.zygote.rc, execute the steps shown in steps S501-S504, and then create the zygote process_1 according to the parameters in the init.zygote.rc. Similar to step S501, after the electronic device creates the zygote process_1, it can create AppRuntime and call its start method, that is, the AppRuntime.start() method.

[0232] S506: The electronic device creates the zygote virtual machine_1. In the process of creating the zygote virtual machine_1, the electronic device determines that the name of the zygote process_1 is the preset name, and then the electronic device creates the Java memory space of the zygote virtual machine_1 based on the first numerical value.

[0233] After the electronic device creates the zygote process_1, the electronic device can create the corresponding virtual machine, i.e., the zygote virtual machine_1. In the process of creating the zygote virtual machine_1, the electronic device can first determine that the name of the zygote process_1 is the preset name (e.g., zygote_heap_expansion), and then the electronic device can determine that the zygote process_1 needs to expand the memory, and create the Java memory space of the zygote virtual machine_1 based on the first numerical value. It can be understood that, according to the above, the first numerical value is greater than the second numerical value.

[0234] Specifically, after the electronic device creates the zygote process_1 and calls the AppRuntime.start() method, the electronic device can execute the startVM() method. When the electronic device executes the startVM() method, the electronic device can parse the start parameters of the zygote virtual machine_1, and call the JNI_CreateJavaVM to create the zygote virtual machine_1. The start parameters of the zygote virtual machine_1 can include the memory setting parameters of the RegionSpace (i.e., RegionSpace_1) in the zygote virtual machine_1. It can be understood that the memory size represented by the memory setting parameters can be greater than the default memory size. In this way, the memory size of the RegionSpace_1 in the zygote virtual machine_1 created by the electronic device calling the JNI_CreateJavaVM will be greater than the default memory size.

[0235] Similar to step S502, after the electronic device calls the JNI_CreateJavaVM, the electronic device can create a Runtime object (i.e., a Runtime module) through the Runtime::create method. The Runtime object is responsible for providing the runtime environment of the virtual machine. Then, the electronic device can initialize the virtual machine through the Runtime::init method, i.e., create the virtual machine based on the virtual machine start parameters. In the process of initializing the virtual machine, the electronic device can create a heap object through the new gc::heap() method, i.e., create the runtime data area. After the electronic device creates the heap object through the new gc::heap() method, the electronic device can determine that the name of the zygote process_1 is the preset name. The electronic device can set the memory size of the RegionSpace_1 in the zygote virtual machine_1 by using the first numerical value (e.g., 768).

[0236] Similar to step S502, after the electronic device creates the zygote process_1 and invokes the AppRuntime.start() method, the electronic device can further execute the startReg() method to register the JNI method, and invoke the ZygoteInit.main() method through the JNI method. After the electronic device invokes the ZygoteInit.main() method, the electronic device can further execute the new ZygoteServer to create the socket channel of the zygote process_1. The zygote process_1 as the server of communication can be used to respond to the request of the client. It can be understood that the related operation of the electronic device after invoking the ZygoteInit.main() method can refer to step S502, and the present application will not be described here.

[0237] S507: The electronic device determines that the name of the zygote process_1 is the preset name, and the electronic device does not need to hatch the SystemServer process based on the currently created zygote process, but puts the zygote process_1 into a loop to wait for receiving the request of creating a new process.

[0238] It can be understood that the electronic device can determine that the name of the zygote process_1 is the preset name, in which case the electronic device does not need to invoke the startSystemServer() method to obtain the SystemServer process based on the zygote process_1, but executes the runSelectLoop() method to make the zygote process_1 enter a loop, and then the zygote process_1 can wait for receiving the request of creating a new process. When the zygote process_1 receives the request of creating a new process, the zygote process_1 can immediately wake up and perform the corresponding work.

[0239] It should be noted that in some embodiments of the present application, as shown in steps S506 and S507, in the process of creating the virtual machine corresponding to the zygote process, the electronic device needs to judge whether the name of the zygote process is the preset name, and after completing the creation of the virtual machine corresponding to the zygote process, the electronic device needs to judge again whether the name of the zygote process is the preset name. If the name of the zygote process is the preset name, the electronic device does not need to invoke the startSystemServer() method, and if the name of the zygote process is not the preset name, the electronic device needs to invoke the startSystemServer() method. In some other embodiments of the present application, as shown in steps S506 and S507, in the process of creating the virtual machine corresponding to the zygote process, the electronic device needs to judge whether the name of the zygote process is the preset name, and after completing the creation of the virtual machine corresponding to the zygote process, the electronic device does not need to judge again whether the name of the zygote process is the preset name. If the name of the zygote process is the preset name, the electronic device does not need to invoke the startSystemServer() method, and if the name of the zygote process is not the preset name, the electronic device needs to invoke the startSystemServer() method. Figure 12As shown, the electronic device only needs to perform the above step of judging whether the name of the zygote process is the preset name once, and the electronic device can create a virtual machine based on the corresponding virtual machine startup parameter after obtaining the judgment result, and judge whether the SystemServer process needs to be forked. That is, in the case where the electronic device determines that the name of the zygote process is the preset name, the electronic device can create the zygote virtual machine_1 based on the virtual machine startup parameter corresponding to the zygote process_1, and in the case where the electronic device determines that the name of the zygote process is not the preset name, the electronic device can create the zygote virtual machine_2 based on the virtual machine startup parameter corresponding to the zygote process_2, and call the startSystemServer() method to fork the SystemServer process based on the zygote process_2.

[0240] The process of creating the corresponding virtual machine after the electronic device creates the zygote process_1 and the zygote process_2 will be described in detail below.

[0241] 1, the electronic device starts, creates and starts the zygote virtual machine_1 (such as Figure 14A As shown)

[0242] Please refer to Figure 14A , Figure 14A A flowchart for creating a zygote virtual machine_1 provided by an embodiment of the application. The flowchart for creating a zygote virtual machine_1 can include but is not limited to the following steps:

[0243] S601: The process control module receives the instruction to start the electronic device.

[0244] It can be understood that the electronic device can detect the user operation of starting the electronic device (for example, the user operation of acting on the power key of the electronic device as shown in step S301). Specifically, the user can press the power key on the electronic device, and the process control module in the electronic device can receive the instruction to start the electronic device. It can be understood that before the process control module receives the instruction to start the electronic device, the electronic device can perform steps S301-S303 and create the zygote process_1 (as shown in steps S401 and S505). During the process of the electronic device performing steps S403 and S506, the process control module in the electronic device can receive the instruction to start the electronic device.

[0245] In some embodiments of the application, the process control module can create an Android runtime environment object for the electronic device.

[0246] S602: The process management module creates a zygote virtual machine_1. The zygote virtual machine_1 includes a Runtime module_1, an address management module_1, and a runtime data area_1. The runtime data area_1 includes a RegionSpace_1.

[0247] After the electronic device receives the instruction to start the electronic device, in response to the instruction to start the electronic device, the electronic device can create a zygote process_1 and a zygote virtual machine_1. Specifically, the process management module in the electronic device can create a zygote virtual machine_1 after receiving the instruction to start the electronic device. It can be understood that the zygote virtual machine_1 is a virtual machine instance corresponding to the zygote process_1. The zygote virtual machine_1 includes a Runtime module_1, an address management module_1, and a runtime data area_1. The runtime data area_1 includes a RegionSpace_1. The related description of the zygote virtual machine_1 can be referred to in the foregoing, and will not be expanded here.

[0248] S603: The process management module starts the zygote virtual machine_1.

[0249] After the electronic device creates the zygote virtual machine_1, the electronic device can start the zygote virtual machine_1. Specifically, after the zygote virtual machine_1 is created, the process management module can start the zygote virtual machine_1.

[0250] When the electronic device performs step S602, the electronic device can specifically perform the following steps:

[0251] S6021: The process management module creates a Runtime module_1.

[0252] After the process management module receives the instruction to start the electronic device, in response to the instruction to start the electronic device, the process management module can create a zygote process_1 and start creating a zygote virtual machine_1. Specifically, the process management module can first create a Runtime module_1. The related description of the Runtime module_1 can be referred to in the foregoing, and will not be expanded here.

[0253] In some embodiments of the present application, the Runtime module_1 can be a Runtime object created by the Runtime::create method after the electronic device creates the zygote process_1. For details, refer to step S506, which will not be repeated here.

[0254] S6022: The Runtime module_1 creates a runtime data area_1.

[0255] After the process management module creates the Runtime module_1, the Runtime module_1 can further create a runtime data area_1. The description of the runtime data area_1 can be referred to the above, and will not be described here.

[0256] In some embodiments of the present application, the runtime data area_1 can be a heap object created by the electronic device through the new gc::heap() method.

[0257] It can be understood that after the Runtime module_1 creates the runtime data area_1, the runtime data area_1 can perform the layout of the memory space. That is, the electronic device can perform the layout of the runtime data area_1. The specific implementation of the layout of the runtime data area_1 can include steps S6023-S6028.

[0258] In some embodiments of the present application, the electronic device performs the layout of the runtime data area_1, which can specifically include that the electronic device can specifically set the memory size of each space (for example, RegionSpace_1) in the heap object in the zygote virtual machine_1 based on the virtual machine startup parameter corresponding to the zygote virtual machine_1.

[0259] S6023: The runtime data area_1 determines that the memory size corresponding to the RegionSpace_1 in the runtime data area_1 is a first value.

[0260] After the Runtime module_1 creates the runtime data area_1, the runtime data area_1 can determine the memory size corresponding to each Space in the runtime data area_1. Specifically, the runtime data area_1 can determine that the memory size corresponding to the RegionSpace_1 is a first value. It can also be understood that the runtime data area_1 can determine that the initial memory size corresponding to the RegionSpace_1 is the first value. The first value is greater than a second value. In this case, the second value refers to the second value corresponding to the RegionSpace. It can be understood that the RegionSpace_1 is a block of memory space in the runtime data area_1, and its detailed description can be referred to the above.

[0261] Of course, the runtime data area_1 can also determine the memory size corresponding to other Spaces (for example, ImageSpace, etc.) in the runtime data area_1, and its specific implementation can be referred to the related technical documents, which will not be described here.

[0262] In some embodiments of the present application, the runtime data area_1 can determine the memory size corresponding to each Space in the runtime data area_1 by reading a related configuration file (for example, a configuration file corresponding to the virtual machine startup parameters of the zygote virtual machine_1).

[0263] It can be understood that the configuration file records the initial memory size corresponding to the zygote process (for example, the zygote process with the same name as the preset name) that needs to expand memory as the first value, that is, the initial memory size corresponding to RegionSpace_1 as the first value, and records the initial memory size corresponding to the zygote process (for example, the zygote process with a name different from the preset name) that does not need to expand memory as the second value, that is, the default memory size corresponding to RegionSpace.

[0264] For example, the first value can be 768, and the second value can be 512.

[0265] Of course, the first value can also be set to other values greater than the second value, which is not limited in the present application.

[0266] S6024: The runtime data area_1 creates RegionSpace_1 based on the first value.

[0267] After the runtime data area_1 determines that the memory size corresponding to RegionSpace_1 is the first value, it can divide a block of memory space in the runtime data area_1 as RegionSpace_1. In this case, RegionSpace_1 has not been created. In the subsequent process, the runtime data area_1 can control RegionSpace_1 to call the address management module_1 to apply for the virtual address corresponding to the first value, and establish the mapping between the virtual address and the physical address, and divide the region, thereby realizing the creation of RegionSpace_1.

[0268] S6025: RegionSpace_1 calls the address management module_1 to apply for the anonymous page corresponding to the first value. The anonymous page is used to represent a segment of virtual address in the process virtual address space which has a mapping relationship with the physical address.

[0269] After RegionSpace_1 creates the RegionSpace_1, RegionSpace_1 can call the address management module_1 to apply for the anonymous page corresponding to the first value. That is, RegionSpace_1 can call the relevant interface and / or the relevant function of the address management module_1 to apply for the virtual address corresponding to the first value (which can also be referred to as the virtual address segment corresponding to the first value), and establish the mapping relationship between the virtual address corresponding to the first value and the physical address.

[0270] It can be understood that the heap, the stack and the data segment in the process virtual address space are generally referred to as anonymous pages. The anonymous page refers to a page without a file background, which does not exist in the form of a file, and thus cannot be exchanged with a disk file.

[0271] In some embodiments of the present application, RegionSpace_1 calls the address management module_1 to apply for the anonymous page corresponding to the first value, which can specifically include that RegionSpace_1 calls the mmap interface (or mmap function) in the address management module_1 to apply for the anonymous page corresponding to the first value. That is, RegionSpace_1 can call mmap through the address management module_1 to apply for the virtual address corresponding to the first value, and establish the mapping relationship between the virtual address corresponding to the first value and the physical address. It can be understood that mmap refers to Memory Mapped, that is, memory mapping. Mmap can be used to map a file or other object into memory. That is, mmap can specifically implement the mapping of the virtual address to the physical address (for example, the mapping between the virtual address segment corresponding to the first value and the real physical address).

[0272] For example, the first value can be 768. The virtual address segment corresponding to the first value (for example, the address segment of the anonymous page corresponding to the first value) is 75a00000-d5a00000. RegionSpace_1 can call the address management module_1 to map 75a00000-d5a00000 to the physical address. After the mapping is completed, the memory size of RegionSpace_1 is the memory corresponding to 75a00000-d5a00000, that is, 768MB (the Java memory size corresponding to the first value).

[0273] S6026: The address management module_1 returns the relevant information of the anonymous page corresponding to the first value to RegionSpace_1. The relevant information of the anonymous page corresponding to the first value is the relevant information of the virtual address corresponding to the first value.

[0274] RegionSpace_1 can return the information of the anonymous page corresponding to the first value to RegionSpace_1 after successfully applying for the anonymous page corresponding to the first value by RegionSpace_1. That is, after RegionSpace_1 applies for the virtual address corresponding to the first value and successfully establishes the mapping relationship between the virtual address corresponding to the first value and the physical address, the address management module_1 can return the information of the virtual address corresponding to the first value to RegionSpace_1. It can be understood that the virtual address corresponding to the first value can be understood as the virtual address corresponding to RegionSpace_1.

[0275] In some embodiments of the present application, the information of the anonymous page corresponding to the first value can include the start address of the anonymous page corresponding to the first value (i.e. the start address of the virtual address segment corresponding to the first value).

[0276] In some embodiments of the present application, the information of the anonymous page corresponding to the first value can include the end address of the anonymous page corresponding to the first value (i.e. the end address of the virtual address segment corresponding to the first value).

[0277] In some embodiments of the present application, the information of the anonymous page corresponding to the first value can include the start address of the anonymous page corresponding to the first value, and the end address of the anonymous page corresponding to the first value.

[0278] For example, the start address of the anonymous page (i.e. the start address of the virtual address segment corresponding to the first value) can be 75d80000, and the end address of the anonymous page (i.e. the end address of the virtual address segment corresponding to the first value) can be d5d80000. In this case, the virtual address segment corresponding to the first value is 75d80000-d5d80000.

[0279] S6027: RegionSpace_1 divides the region in RegionSpace_1 based on the information of the anonymous page corresponding to the first value.

[0280] RegionSpace_1 receives the information of the anonymous page corresponding to the first value returned by the address management module_1, and can divide the regions in RegionSpace_1 based on the information of the anonymous page corresponding to the first value, and determine the region information. In the case that the information of the anonymous page corresponding to the first value includes the start address of the anonymous page corresponding to the first value, RegionSpace_1 can determine the virtual address segment corresponding to the first value based on the start address of the anonymous page and the size of the first value, and divide the virtual address segment corresponding to the first value into address segments with a size of 256 KB. It can be understood that each address segment with a size of 256 KB is a region. Similarly, in the case that the information of the anonymous page corresponding to the first value includes the end address of the anonymous page corresponding to the first value, RegionSpace_1 can determine the virtual address segment corresponding to the first value based on the end address of the anonymous page and the first value, and divide the virtual address segment corresponding to the first value into address segments with a size of 256 KB. In the case that the information of the anonymous page corresponding to the first value includes both the start address of the anonymous page corresponding to the first value and the end address of the anonymous page corresponding to the first value, RegionSpace_1 does not need to determine the virtual address segment corresponding to the first value based on the first value, but can obtain the virtual address segment corresponding to the first value based on the start address and the end address of the anonymous page corresponding to the first value, and then divide the address segment of the anonymous page into address segments with a size of 256 KB.

[0281] S6028: The runtime data area_1 obtains and records the information of the regions in RegionSpace_1 from RegionSpace_1.

[0282] After RegionSpace_1 divides the regions, the runtime data area_1 can obtain and record the information of the regions divided by RegionSpace_1 from RegionSpace_1. It can be understood that the information of the region can include the address segment corresponding to the region, and can also include the state and type of the region. Of course, the information of the region can also include other contents (for example, the type of the storage object in the region), which is not limited in the present application.

[0283] It can be understood that the runtime data area_1 can also determine the memory corresponding to other Spaces, and create the corresponding Spaces based on the memory. The application does not limit the order of creating each Space by the runtime data area_1. It can be understood that after the Spaces in the runtime data area_1 are created, the layout of the runtime data area_1 is completed. In some embodiments of the application, after the runtime data area_1 obtains and records the related information of the region in the RegionSpace_1 from the RegionSpace_1, the Runtime module_1 can determine that the layout of the runtime data area_1 is completed.

[0284] 2, the electronic device is started, and a zygote virtual machine_2 is created and started (as shown in Figure 14B

[0285] Please refer to Figure 14B , Figure 14B a flowchart for creating a zygote virtual machine_2 provided by an embodiment of the application. The flowchart for creating a zygote virtual machine_2 can include but is not limited to the following steps:

[0286] S601: The process control module receives an instruction to start the electronic device.

[0287] It can be understood that the specific implementation of step S601 can refer to the above, and the application will not be repeated here.

[0288] S604: The process control module creates a zygote virtual machine_2. The zygote virtual machine_2 includes a Runtime module_2, an address management module_2 and a runtime data area_2. The runtime data area_2 includes a RegionSpace_2.

[0289] Similar to step S604, after the electronic device receives an instruction to start the electronic device, the electronic device can create a zygote process_2 and a zygote virtual machine_2 in response to the instruction to start the electronic device. Specifically, after the process control module in the electronic device receives an instruction to start the electronic device, the zygote virtual machine_2 can be created. It can be understood that the zygote virtual machine_2 is a virtual machine instance corresponding to the zygote process_2. The zygote virtual machine_2 includes a Runtime module_2, an address management module_2 and a runtime data area_2. The runtime data area_2 includes a RegionSpace_2. The related description of the zygote virtual machine_2 can refer to the above, and will not be expanded here.

[0290] S605: The process control module starts the zygote virtual machine_2.

[0291] ​After the electronic device creates the zygote virtual machine_2, the zygote virtual machine_2 can be started. Specifically, after the zygote virtual machine_1 is created, the process management module can start the zygote virtual machine_2.

[0292] It should be noted that the electronic device can first create the zygote process_1 and the zygote virtual machine_1, and then create the zygote process_2 and the zygote virtual machine_2, or the electronic device can first create the zygote process_2 and the zygote virtual machine_2, and then create the zygote process_1 and the zygote virtual machine_1. That is, the order in which the electronic device executes step S603 and step S604 is not limited in the present application.

[0293] When the electronic device executes step S604, the following steps can be specifically executed:

[0294] S6041: The process management module creates a Runtime module_2.

[0295] After the process management module receives the instruction to start the electronic device, in response to the instruction to start the electronic device, the zygote process_2 can be created, and the creation of the zygote virtual machine_2 can be started. Specifically, the process management module can first create the Runtime module_2. The related description of the Runtime module_2 can be referred to the above, which will not be described here.

[0296] In some embodiments of the present application, the Runtime module_2 can be a Runtime object created by the Runtime::create method after the zygote process_2 is created for the electronic device, which can be specifically referred to step S502, and will not be described here.

[0297] S6042: The Runtime module_2 creates a runtime data area_2.

[0298] After the process management module creates the Runtime module_2, the Runtime module_2 can create the runtime data area_2. The related description of the runtime data area_2 can be referred to the above, which will not be expanded here.

[0299] In some embodiments of the present application, the runtime data area_2 can be a heap object created by the new gc::heap() method for the electronic device.

[0300] It can be understood that after the Runtime module_2 creates the runtime data area_2, the runtime data area_2 can be laid out in the memory space. That is, the electronic device can lay out the runtime data area_2. The specific implementation of the layout of the runtime data area_2 can include steps S6043-S6048.

[0301] In some embodiments of the present application, the electronic device lays out the runtime data area_2, which can specifically include that the electronic device can specifically set the memory size of each space (for example, RegionSpace_2) in the heap object in the zygote virtual machine_2 based on the virtual machine startup parameter corresponding to the zygote virtual machine_2.

[0302] S6043: The runtime data area_2 determines that the memory size corresponding to the RegionSpace_2 in the runtime data area_2 is a second value.

[0303] After the Runtime module_1 creates the runtime data area_2, the runtime data area_2 can determine the memory size corresponding to each Space in the runtime data area_2. Specifically, the runtime data area_2 can determine that the memory size corresponding to the RegionSpace_2 is a second value. It can also be understood that the runtime data area_2 can determine that the initial memory size corresponding to the RegionSpace_2 is a second value, that is, the second value corresponding to the RegionSpace. It can be understood that the RegionSpace_2 is a block of memory space in the runtime data area_2, and its detailed description can be referred to in the above.

[0304] Of course, the runtime data area_2 can also determine the memory size corresponding to other Spaces (for example, ImageSpace) in the runtime data area_2, and its specific implementation can be referred to in the related technical documents, which will not be expanded here.

[0305] In some embodiments of the present application, the runtime data area_2 can determine the memory size corresponding to each Space in the runtime data area_2 by reading the related configuration file (for example, the configuration file corresponding to the virtual machine startup parameter corresponding to the zygote virtual machine_2). It can be understood that the configuration file records the memory corresponding to each Space in the runtime data area_2. In this way, the runtime data area_2 can be laid out according to the memory corresponding to each Space recorded in the configuration file.

[0306] It can be understood that the configuration file can record that the initial memory size corresponding to the zygote process (for example, the zygote process with a name different from the preset name) that does not need to expand the memory is a second value, that is, the initial memory size corresponding to the RegionSpace_1 is a second value.

[0307] S6044: Runtime Area_2 creates RegionSpace_2 based on the second value.

[0308] After determining the memory size corresponding to RegionSpace_2 as the second value corresponding to RegionSpace, Runtime Area_2 can divide a memory space in Runtime Area_2 as RegionSpace_2. In this case, RegionSpace_2 has not been completed. In the subsequent process, Runtime Area_2 can control RegionSpace_2 to call Address Management Module_2 to apply for the virtual address corresponding to the second value, establish the mapping between the virtual address and the physical address, and divide the region, so as to realize the creation of RegionSpace_2.

[0309] S6045: RegionSpace_2 calls Address Management Module_2 to apply for the anonymous page corresponding to the second value.

[0310] After Runtime Area_2 creates RegionSpace_2, RegionSpace_2 can call Address Management Module_2 to apply for the anonymous page corresponding to the second value, that is, the anonymous page corresponding to the second value corresponding to RegionSpace. That is, RegionSpace_2 can call the related interface and / or the related function of Address Management Module_2 to apply for the virtual address corresponding to the second value (also referred to as the virtual address segment corresponding to the second value), and establish the mapping relationship between the virtual address corresponding to the second value and the physical address.

[0311] Similar to step S6025, RegionSpace_2 can call the mmap function in Address Management Module_2 to apply for the anonymous page corresponding to the second value. That is, RegionSpace_2 can call mmap in Address Management Module_2 to apply for the virtual address corresponding to the second value, and establish the mapping relationship between the virtual address corresponding to the second value and the physical address.

[0312] For example, the second value can be 512. The virtual address segment corresponding to the second value (for example, the address segment of the anonymous page corresponding to the second value) is 75d80000-b5d80000. RegionSpace_2 can call Address Management Module_2 to map 75d80000-b5d80000 to the physical address. After the mapping is completed, the memory size of RegionSpace_2 is the memory corresponding to 75d80000-b5d80000, that is, 512MB (the Java memory size corresponding to the second value).

[0313] S6046: The address management module_2 returns the information about the anonymous page corresponding to the second value to the RegionSpace_2. The information about the anonymous page corresponding to the second value is the information about the virtual address corresponding to the second value.

[0314] After the RegionSpace_2 successfully applies for the anonymous page corresponding to the second value, the address management module_2 can return the information about the anonymous page corresponding to the second value to the RegionSpace_2. The specific implementation manner can be referred to step S6026, which will not be described here again.

[0315] S6047: The RegionSpace_2 divides the region in the RegionSpace_2 based on the information about the anonymous page corresponding to the second value.

[0316] S6048: The runtime data area_2 obtains and records the information about the region in the RegionSpace_2.

[0317] It can be understood that the specific implementation manners of steps S6047-S6048 can be referred to steps S6027-S6028, which will not be described here again.

[0318] It can be understood that the runtime data area_2 can also determine the memory corresponding to other Spaces and create the corresponding Spaces based on the memory. The order of creating the Spaces by the runtime data area_2 is not limited in the present application. It can be understood that after the Spaces in the runtime data area_2 are created, the layout of the runtime data area_2 is completed. In some embodiments of the present application, after the runtime data area_2 obtains and records the information about the region in the RegionSpace_2, the Runtime module_2 can determine that the layout of the runtime data area_2 is completed.

[0319] Second aspect: After triggering a new process, the electronic device selects one of the zygote process_1 and the zygote process_2 to fork to obtain the process corresponding to the new process and the corresponding virtual machine.

[0320] Please refer to Figure 15 , Figure 15 A new process provided by an embodiment of the present application.

[0321] As Figure 15As shown, the user can trigger a new process, and accordingly, the electronic device can detect the user operation of triggering the new process. In response to the user operation, the initiating process in the electronic device can notify the SystemServer process in the electronic device of the new process by using the Binder communication mode. After receiving the notification of the initiating process, the SystemServer process can determine whether to select the first fork mode to create the new process or select the second fork mode to create the new process. If the SystemServer process selects the first fork mode to create the new process, the SystemServer process can notify the zygote process_1 to fork the new process by using the Socket communication mode, and copy the zygote virtual machine_1, so as to obtain the virtual machine corresponding to the new process. In this case, the Socket communication mode is used between the new process and the zygote process_1. If the SystemServer process selects the second fork mode to create the new process, the SystemServer process can notify the zygote process_2 to fork the new process by using the Socket communication mode, and copy the zygote virtual machine_2, so as to obtain the virtual machine corresponding to the new process. In this case, the Socket communication mode is used between the new process and the zygote process_2.

[0322] It can be understood that the Binder communication is an Interprocess Communication (IPC) mechanism. The Socket communication is also a kind of interprocess communication. Two programs on a network exchange data through a bidirectional communication connection, one end of the connection is called a socket. The specific meanings and effects of Binder communication and Socket communication can be referred to related technical documents, which will not be described herein.

[0323] The following describes a specific implementation of the SystemServer process determining the fork mode.

[0324] In some embodiments of the present application, the SystemServer process can include a fork mode selection module, a zygote process_1 parameter preparation module, and a zygote process_2 parameter preparation module. The fork mode selection module is configured to determine whether the electronic device adopts the first fork mode to create a new process or adopts the second fork mode to create a new process. The zygote process_1 parameter preparation module is configured to prepare parameters required when the zygote process_1 forks a new process. The zygote process_2 parameter preparation module is configured to prepare parameters required when the zygote process_2 forks a new process.

[0325] In some embodiments of the present application, the fork mode selection module can be a ProcessList class (ProcessList.java), the zygote process_1 parameter preparation module can be a ZygoteProcess class (ZygoteProcess.java), and the zygote process_2 parameter preparation module can be a HeapExpansionZygoteProcess class (HeapExpansionZygoteProcess.java).

[0326] Referring to Figure 16 , Figure 16 A flowchart of starting an application and creating a corresponding virtual machine is provided for embodiments of the present application. The flowchart of starting an application and creating a corresponding virtual machine can include, but is not limited to, the following steps:

[0327] S701: The initiating process receives an instruction to cold start an application 1.

[0328] A user can trigger the electronic device to cold start the application 1. Correspondingly, an initiating process in the electronic device can receive an instruction to cold start the application 1. In some embodiments of the present application, the user can trigger the electronic device to cold start the application 1 by clicking the application 1 icon. In this case, the initiating process is a Launcher process. The Launcher process can receive an instruction to cold start the application 1. It can be understood that the related description of the initiating process can be referred to the above, which will not be repeated here.

[0329] S702: The initiating process notifies a fork mode selection module in the SystemServer process to cold start the application 1.

[0330] After the initiating process in the electronic device receives an instruction to cold start the application 1, the initiating process can notify the SystemServer process in the electronic device to cold start the application 1. In some embodiments of the present application, the initiating process can specifically notify the fork mode selection module in the SystemServer process to cold start the application 1.

[0331] S703: The fork mode selection module in the SystemServer process determines whether the application 1 meets a preset condition.

[0332] After the initiating process notifies the SystemServer process to cold start the application 1, the fork mode selection module in the SystemServer process can determine whether the application 1 meets a preset condition. It can be understood that the related description of the preset condition can be referred to the above, which will not be repeated here.

[0333] S704: In the case that the application_1 meets the preset condition, the fork mode selection module in the SystemServer process notifies the zygote process_1 parameter preparation module in the zygote process_1 in the SystemServer process to obtain the related parameters required for the zygote process_1 to perform the fork.

[0334] In some embodiments of the present application, in the case that the application_1 meets the preset condition, the fork mode selection module in the SystemServer process can call the Process.java by calling the hostingRecord.usesHeapExpansionZygote() method, and then call the Process.HeapExpansionStart method of the Process.java to notify the zygote process_1 parameter preparation module to obtain the related parameters required for the zygote process_1 to perform the fork.

[0335] S705: The zygote process_1 parameter preparation module in the SystemServer process obtains the related parameters required for the zygote process_1 to perform the fork.

[0336] After the fork mode selection module notifies the zygote process_1 parameter preparation module to obtain the related parameters required for the zygote process_1 to perform the fork, the zygote process_1 parameter preparation module can prepare the related parameters required for the zygote process_1 to perform the fork and the necessary conditions. In some embodiments of the present application, the related parameters required for the zygote process_1 to perform the fork and the necessary conditions can include any one or more of the following: startup commands and parameters, environment variables (for example, CLASSPATH), file descriptors, security contexts, Binder handles, and other system attributes and settings. Among them, the startup commands and parameters can include various command line parameters required to start the SystemServer process. The CLASSPATH is used to specify the class path of the system service. The security context can include a user identifier (User Identifier, UID) and a group identifier (Group Identifier, GID).

[0337] S706: The zygote process_1 parameter preparation module in the SystemServer process sends the related parameters required for the zygote process_1 to perform the fork to the zygote process_1.

[0338] It can be understood that after the zygote process_1 parameter preparation module obtains the relevant parameters required for the zygote process_1 process to fork, the zygote process_1 parameter preparation module can send the relevant parameters required for the zygote process_1 to fork to the zygote process_1 through the Socket channel between the SystemServer process and the zygote process_1.

[0339] In some embodiments of the present application, the zygote process_1 parameter preparation module can send the relevant parameters required for the zygote process_1 to fork to the zygote process_1 by calling the openZygoteSocketIfNeeded method.

[0340] S707: The zygote process_1 forks the application_1 process based on the relevant parameters required for the zygote process_1 to fork, and the application_1 virtual machine.

[0341] The zygote process_1 can fork the application_1 process based on the relevant parameters required for the zygote process_1 to fork, and copy the application_1 virtual machine by the zygote virtual machine_1. It can be understood that the copying process can refer to Figure 7 and the related description above, which will not be repeated here.

[0342] S708: In the case that the application_1 does not meet the preset condition, the fork mode selection module in the SystemServer process notifies the zygote process_2 parameter preparation module in the SystemServer process to obtain the relevant parameters required for the zygote process_2 to fork.

[0343] In some embodiments of the present application, in the case that the application_1 does not meet the preset condition, the fork mode selection module in the SystemServer process can call the Process.java by calling the hostingRecord.usesZygote() method, and then call the Process.Start method of the Process.java to notify the zygote process_2 parameter preparation module to obtain the relevant parameters required for the zygote process_2 to fork.

[0344] S709: The zygote process_2 parameter preparation module in the SystemServer process obtains the relevant parameters required for the zygote process_2 to fork.

[0345] It can be understood that the specific implementation of step S709 can refer to step S705, which will not be repeated here.

[0346] S710: The zygote process 2 parameter preparation module in the SystemServer process sends the zygote process 2 the relevant parameters required for the zygote process 2 to fork.

[0347] Similar to step S706, after obtaining the relevant parameters required for the zygote process 2 to fork, the zygote process 2 parameter preparation module can send the zygote process 2 the relevant parameters required for the zygote process 2 to fork through the Socket channel between the SystemServer process and the zygote process 2. In some embodiments of the present application, the zygote process 2 parameter preparation module can send the zygote process 2 the relevant parameters required for the zygote process 2 to fork by calling the openZygoteSocketIfNeeded method.

[0348] S711: The zygote process 2 forks the application 1 process based on the relevant parameters required for the zygote process 2 to fork, and the application 1 virtual machine.

[0349] The zygote process 2 can fork the application 1 process based on the relevant parameters required for the zygote process 2 to fork, and copy the application 1 virtual machine by the zygote virtual machine 2. It can be understood that the copying process can refer to Figure 7 and the relevant description above, which will not be repeated here.

[0350] The hardware structure of the electronic device related to the embodiments of the present application is introduced as follows.

[0351] Please refer to Figure 17 , Figure 17 The hardware structure of the electronic device provided by the embodiments of the present application is shown in the following figure.

[0352] As Figure 17As shown, the electronic device can include a processor, an external memory interface, an internal memory, a Universal Serial Bus (USB) interface, a charging management module, a power management module, a battery, an antenna 1, an antenna 2, a mobile communication module, a wireless communication module, a sensor module, a key, a motor, an indicator, a camera, a display screen, and a Subscriber Identity Module (SIM) card slot, etc. Among them, the audio module can include a speaker, a receiver, a microphone, an earphone interface, etc., the sensor module can include a pressure sensor, a gyroscope sensor, a barometric pressure sensor, a magnetic sensor, an acceleration sensor, a distance sensor, a proximity light sensor, a fingerprint sensor, a temperature sensor, a touch sensor, an ambient light sensor, a bone conduction sensor, etc.

[0353] It can be understood that the structure shown in the embodiments of the present application does not constitute a specific limitation on the electronic device. It can be understood that the components shown can be implemented in hardware, software or a combination of software and hardware. In some embodiments of the present application, the electronic device can include more components than shown. For example, the electronic device can include other types of sensors. In yet other embodiments of the present application, the electronic device can include fewer components than shown, or combine certain components, or split certain components, or different arrangement of components. The interface connection relationship between the modules shown in the embodiments of the present application is only illustrative and does not constitute a structural limitation on the electronic device.

[0354] The processor can include one or more processing units, for example: the processor can include an Application Processor (AP), a modem processor, a Graphics Processing Unit (GPU), an Image Signal Processor (ISP), a controller, a video codec, a Digital Signal Processor (DSP), a baseband processor, and / or a Neural-network Processing Unit (NPU), etc. The processor can also be provided with a memory for storing instructions and data.

[0355] The electronic device implements display functions through the GPU, the display screen, and the application processor, etc.

[0356] A GPU is a microprocessor for image processing, connecting a display screen and an application processor. A GPU is used to perform mathematical and geometric calculations for graphics rendering. A processor can include one or more GPUs that execute program instructions to generate or change display information. A display screen is used to display images, videos, and the like. In some embodiments, an electronic device can include one or more display screens.

[0357] A camera is used to capture still images or videos. An ISP is used to process data fed back by the camera. Light is transmitted onto a camera photosensitive element through a lens, and the light signal is converted into an electrical signal, which is transmitted by the camera photosensitive element to the ISP for processing and converted into an image visible to the naked eye. An electronic device can include one or more cameras.

[0358] Internal memory can include one or more RAMs and one or more non-volatile memories (NVMs). A random access memory can be directly read and written by a processor, and can be used to store executable programs (e.g., machine instructions) of an operating system or other programs that are running, and can also be used to store data of users and application programs, and the like. A non-volatile memory can also store executable programs and store data of users and application programs, and can be loaded into a random access memory in advance for direct reading and writing by a processor.

[0359] In embodiments of the present application, the code implementing the memory expansion method described in the embodiments of the present application can be stored on a non-volatile memory. When running a camera application, the electronic device can load the executable code stored in the non-volatile memory into the random access memory.

[0360] An external memory interface can be used to connect an external non-volatile memory to achieve the expansion of the storage capacity of the electronic device.

[0361] An electronic device can implement audio functions through an audio module, a speaker, a receiver, a microphone, a headphone interface, and an application processor, and the like.

[0362] An audio module is used to convert digital audio information into an analog audio signal output, and is also used to convert an analog audio input into a digital audio signal. A speaker, also known as a "loudspeaker", is used to convert an audio electrical signal into a sound signal. A receiver, also known as a "earpiece", is used to convert an audio electrical signal into a sound signal. A microphone, also known as a "microphone", "sound pickup", is used to convert a sound signal into an electrical signal. A headphone interface is used to connect a wired earphone.

[0363] In embodiments of the present application, the electronic device can enable the microphone to collect sound signals and convert the sound signals into corresponding text when the voice-to-text function is enabled.

[0364] Touch sensor, also called "touch device". The touch sensor can be disposed on the display screen, and the touch sensor and the display screen form a touch screen, also called "touch screen". The touch sensor is used to detect the touch operation acting on or near the touch sensor. The touch sensor can transmit the detected touch operation to the application processor to determine the touch event type. The visual output related to the touch operation can be provided through the display screen. In other embodiments, the touch sensor can also be disposed on the surface of the electronic device, which is different from the position of the display screen.

[0365] The above embodiments are only used to illustrate the technical solutions of the present application, but not limit the same; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that the technical solutions recorded in the foregoing embodiments can be modified, or some technical features can be replaced by equivalent replacements; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A memory extension method, characterized by, The application is applied to an electronic device; the electronic device creates a first zygote virtual machine and a second zygote virtual machine; a memory size of a Java memory space in the first zygote virtual machine is a first value; a memory size of a Java memory space in the second zygote virtual machine is a second value; The first value is greater than the second value, and the second value is a default memory size; the method comprises: The electronic device receives a first instruction of a user for cold starting a first application; In response to the first instruction, in a case where the first application meets a preset condition, the electronic device copies the first zygote virtual machine to obtain a first virtual machine; the first virtual machine is a virtual machine instance corresponding to a first application process; a memory size of a Java memory space in the first virtual machine is the first value.

2. The method of claim 1, wherein, After the electronic device receives the first instruction of the user for cold starting the first application, the method further comprises: In response to the first instruction, in a case where the first application does not meet the preset condition, the electronic device copies the second zygote virtual machine to obtain a second virtual machine; the second virtual machine is a virtual machine instance corresponding to the first application process; a memory size of a Java memory space in the second virtual machine is the second value.

3. The method of claim 2, wherein, The first zygote virtual machine is a virtual machine instance corresponding to a first zygote process, and the second zygote virtual machine is a virtual machine instance corresponding to a second zygote process; Before the electronic device receives the first instruction of the user for cold starting the first application, the method further comprises: The electronic device receives a second instruction of a user for starting the electronic device; In response to the second instruction, the electronic device creates the first zygote process and the first zygote virtual machine, and creates the second zygote process and the second zygote virtual machine; The electronic device hatches a system service SystemServer process based on the second zygote process.

4. The method of claim 3, wherein, The electronic device receives the first instruction of the user for cold starting the first application, specifically comprising: the electronic device receives the first instruction through an initiating process; After the electronic device receives the first instruction of the user for cold starting the first application, the method further comprises: in response to the first instruction, the electronic device notifies the SystemServer process in the electronic device to start the first application through the initiating process; and the electronic device determines whether the first application meets the preset condition through the SystemServer process.

5. The method of claim 4, wherein, In the case where the first application meets the preset condition, the electronic device copies the first zygote virtual machine to obtain a first virtual machine, specifically comprising: In a case where the electronic device determines, through the SystemServer process, that the first application meets the preset condition, the electronic device notifies, through the SystemServer process, the first zygote process in the electronic device to hatch the first application process; In a process of hatching the first application process through the first zygote process, the electronic device copies the first zygote virtual machine to obtain the first virtual machine.

6. The method of claim 4 or 5, wherein, In a case where the first application does not meet the preset condition, the electronic device copies the second zygote virtual machine to obtain a second virtual machine, and the copying specifically includes: In a case where the electronic device determines, through the SystemServer process, that the first application does not meet the preset condition, the electronic device notifies, through the SystemServer process, the second zygote process in the electronic device to hatch the first application process; In a process of hatching the first application process through the second zygote process, the electronic device copies the second zygote virtual machine to obtain the second virtual machine.

7. The method of any one of claims 1-5, wherein, The first zygote virtual machine includes a Java memory space, and a virtual address corresponding to the Java memory space is a first virtual address segment; a memory size corresponding to the first virtual address segment is the first value; the second zygote virtual machine includes a Java memory space, and a virtual address corresponding to the Java memory space is a second virtual address segment; a memory size corresponding to the second virtual address segment is the second value; and the method further includes: In a process of creating the first zygote virtual machine, the electronic device determines that a memory size of the Java memory space in the first zygote virtual machine is the first value, and, for the Java memory space in the first zygote virtual machine, the electronic device applies the first virtual address segment and maps the first virtual address segment to a physical address; In a process of creating the second zygote virtual machine, the electronic device determines that a memory size of the Java memory space in the second zygote virtual machine is the second value, and, for the Java memory space in the second zygote virtual machine, the electronic device applies the second virtual address segment and maps the second virtual address segment to a physical address. The electronic device applies the first virtual address segment and maps the first virtual address segment to a physical address, and the applying and mapping specifically include: the electronic device applies an anonymous page corresponding to the first value; the anonymous page corresponding to the first value includes the first virtual address segment; and the anonymous page is used to represent a segment of virtual addresses in a process virtual address space that has a mapping relationship with a physical address.

8. The method of claim 7, wherein, ​ The electronic device applies for the second virtual address segment, and maps the second virtual address segment to a physical address, specifically including: the electronic device applies for an anonymous page corresponding to the second value; the anonymous page corresponding to the second value includes the second virtual address segment.

9. The method of any one of claims 1-5, 8, wherein, The first application meets a preset condition, specifically including: in a history record of memory occupation situations after multiple cold starts of the first application, a number of first type history records is greater than a first history record number; the first type history record includes: a history record in which an occupied memory of a Java memory space in the first application virtual machine is greater than a first threshold.

10. The method of any one of claims 1-5, 8, wherein, The first application meets a preset condition, specifically including: the first application is an application in a preset list.

11. The method of any one of claims 1-5, 8, wherein, The default memory size is 512 MB.

12. An electronic device, comprising one or more memories, and one or more processors, wherein the one or more processors are configured to perform the method of any of claims 1-11. The memory is configured to store a computer program; and the processor is configured to invoke the computer program, so that the electronic device executes the method in any one of claims 1-11.

13. A computer storage medium, comprising, The computer program product comprises: computer instructions; when the computer instructions run on an electronic device, the electronic device executes the method in any one of claims 1-11.

Citation Information

Patent Citations

  • Memory management method and device

    CN105512050A

  • Method and device for monitoring and managing external memory of jvm heap and electronic equipment

    CN115048212A