Virtual machine memory application method, device, storage medium and electronic device
By intercepting OOM exceptions and executing memory statistics optimization strategies, the application crash problem caused by insufficient virtual machine heap memory is solved, and higher system stability and user experience are achieved.
Patent Information
- Application Number
- CN202110719745.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-06-28
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2041-06-28
AI Technical Summary
In Internet applications, especially when images or animation resources are used in large quantities, the virtual machine heap memory space is insufficient, resulting in Java OOM exceptions, which in turn causes application crashes and affects user experience.
By obtaining memory allocation requests, intercepting the OOM exception thrown when memory allocation fails, and executing a preset memory statistics optimization strategy, reducing the memory usage in the heap memory statistics module of the virtual machine, thereby re-allocating memory for the target application object.
It effectively reduces the number of thrown Java OOM exceptions, avoids the target application crash caused by OOM exceptions, and improves the stability and user experience of the system.
Smart Images

Figure CN113407309B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of virtual machines, and in particular to a method, device, storage medium and electronic device for applying for memory of a virtual machine. Background Art
[0002] At present, Internet applications still need to be compatible with lower versions of electronic devices. As the version iterations of various applications and product operation needs become more and more frequent, various pictures or animation resources will be used in large quantities online during major events or festivals. Since there are many objects that need to be allocated for pictures, etc., the virtual machine heap memory may be insufficient. In this case, the relevant technology directly calls the Java OOM (Out Of Memory) exception interface, throws a Java OOM exception, and then causes the application to crash, affecting users' use of electronic devices. Summary of the invention
[0003] This summary is provided to introduce concepts in a brief form that will be described in detail in the detailed description below. This summary is not intended to identify key features or essential features of the claimed technical solution, nor is it intended to limit the scope of the claimed technical solution.
[0004] In a first aspect, the present disclosure provides a method for applying for memory of a virtual machine, comprising:
[0005] Obtaining a memory allocation request, where the memory allocation request is used to request allocation of memory for an object of a target application;
[0006] Intercept the OOM exception thrown when memory allocation fails;
[0007] Executing a preset memory statistics optimization strategy, wherein the memory statistics optimization strategy is used to reduce the memory usage in the heap memory counted by the memory statistics module of the virtual machine;
[0008] Reallocate memory for the object of the target application according to the memory allocation request.
[0009] In a second aspect, the present disclosure provides a virtual machine memory application device, comprising:
[0010] A first acquisition module, used for acquiring a memory allocation request, wherein the memory allocation request is used for requesting to allocate memory for an object of a target application;
[0011] The interception module is used to intercept the OOM exception thrown when memory allocation fails;
[0012] An execution module, used for executing a preset memory statistics optimization strategy, wherein the memory statistics optimization strategy is used for reducing the memory usage in the heap memory counted by the memory statistics module of the virtual machine;
[0013] The allocation module is used to reallocate memory to the object of the target application according to the memory allocation request.
[0014] In a third aspect, the present disclosure provides a computer-readable medium having a computer program stored thereon, which, when executed by a processing device, implements the steps of the virtual machine memory application method described in the first aspect.
[0015] In a fourth aspect, the present disclosure provides an electronic device, including:
[0016] a storage device having a computer program stored thereon;
[0017] A processing device is used to execute the computer program in the storage device to implement the steps of the virtual machine memory application method in the first aspect above.
[0018] Through the above technical solution, a memory allocation request is obtained, and the memory allocation request is used to request memory allocation for the target application object. The OOM exception thrown when the memory allocation fails is intercepted, and the memory usage in the heap memory counted by the virtual machine's memory statistics module is reduced through a preset memory statistics optimization strategy. Since the memory not counted by the memory statistics module can be used for allocation, based on this, memory can be reallocated to the target application object. In this way, by intercepting the thrown OOM exception and executing the memory statistics optimization strategy to reduce the number of times the OOM exception is actually thrown, the target application is further prevented from crashing due to the OOM exception.
[0019] Other features and advantages of the present disclosure will be described in detail in the following detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale. In the drawings:
[0021] Figure 1 The present invention is a flowchart of a method for applying for memory of a virtual machine according to an exemplary embodiment of the present invention.
[0022] Figure 2 The figure is a comparative schematic diagram of virtual machine memory management partition according to an exemplary embodiment of the present disclosure.
[0023] Figure 3This is another flow chart of a method for applying for virtual machine memory according to an exemplary embodiment of the present disclosure.
[0024] Figure 4 A block diagram of a virtual machine memory application device according to an exemplary embodiment of the present invention is shown
[0025] Figure 5 It is a schematic diagram of the structure of an electronic device according to an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION
[0026] Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are only for exemplary purposes and are not intended to limit the scope of protection of the present disclosure.
[0027] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.
[0028] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.
[0029] It should be noted that the concepts such as "first" and "second" mentioned in the present disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0030] It should be noted that the modifications of "one" and "plurality" mentioned in the present disclosure are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0031] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.
[0032] As the background technology says, electronic devices are constantly enriching various functions and improving performance, and mobile Internet is also developing rapidly. The various Internet applications on the market have long become comprehensive flagship applications. The application installation package is large, and after a period of use, it occupies up to 1G of disk memory. Although increasingly complex applications have a higher visual experience, they also have higher requirements for the system. As the background technology says, Internet applications still need to be compatible with low-version electronic devices, and when pictures or animation resources are used in large quantities online, due to the large number of objects that need to be allocated, it may cause insufficient space for the use of virtual machine heap memory. In this case, the relevant technology directly calls the Java OOM (Out Of Memory, memory overflow) exception interface, throws a Java OOM exception, and the OOM exception will return a NULL object, which will cause the application to flash back. Therefore, how to reduce the number of times the Java OOM exception is thrown is crucial.
[0033] In view of this, an embodiment of the present disclosure provides a virtual machine memory application method to intercept Java OOM exceptions, reduce the number of OOM exceptions thrown, and avoid the target application from crashing due to OOM exceptions.
[0034] Figure 1 is a flowchart of a virtual machine memory application method according to an exemplary embodiment of the present invention. The virtual machine memory application method is applied to an electronic device, referring to Figure 1 , the virtual machine memory application method includes:
[0035] Step 101: Obtain a memory allocation request, where the memory allocation request is used to request memory allocation for an object of a target application.
[0036] It should be understood that the target application is an application on an electronic device.
[0037] Specifically, during the operation of the target application, the target application will execute a business execution message, and the virtual machine will allocate memory space for the business to be executed corresponding to the business execution message, so a memory allocation request will be generated, generally writing the data used by the business to be executed into the memory in the form of an object.
[0038] For example, the object of the target application may be an object corresponding to data such as a picture, a video, or music.
[0039] Step 102, intercepting the OOM exception thrown when memory allocation fails.
[0040] It should be noted that when there is no allocable memory for a memory allocation request, the electronic device will return a NULL object (i.e., an empty object), and the target application may crash when receiving the returned NULL object. Therefore, intercepting the OOM exception thrown when memory allocation fails can avoid returning a NULL object for the memory allocation request.
[0041] Step 103: execute a preset memory statistics optimization strategy, where the memory statistics optimization strategy is used to reduce the memory usage in the heap memory counted by the memory statistics module of the virtual machine.
[0042] It should be noted that the memory statistics optimization strategy is used to reduce the memory usage in the heap memory counted by the memory statistics module of the virtual machine. Specifically, the heap memory counted by the memory statistics module does not include the occupied memory in the preset module, and the memory not counted by the memory statistics module can be used for allocation, so as to release and increase the available memory in the current heap area.
[0043] For example, the memory usage of the preset module in the virtual machine may be removed from the memory statistics module. The following takes removing the memory usage of the preset module from the memory statistics module as an example to further explain this embodiment.
[0044] Figure 2 is a comparative schematic diagram of a virtual machine memory management partition according to an exemplary embodiment of the present disclosure, referring to Figure 2 , Figure 2 A1 represents an example diagram of virtual machine memory management partition in the related prior art, and A2 represents an example diagram of virtual machine memory management partition in the present disclosure. First of all, it should be noted that there are differences in memory management between the objects of the LargeObjectSpace module in the related prior art and in the present disclosure. Specifically, in the memory management partition of A1, the total memory usage (i.e., heap memory) counted by the memory statistics module includes two modules, the other space module and the LargeObjectSpace module (i.e., the preset module in the present disclosure), and does not include Figure 2 The 0~2G part is the Native Merm module in the virtual machine (here is the memory expansion part). In the memory management division of A2, the total memory usage counted by the memory statistics module only includes one module, namely the other space module, and does not include the memory usage corresponding to the Other Object object stored in LargeObjectSpace (i.e. the preset module in this disclosure), which is Figure 2 It can be seen that under the memory structure of A1, the memory usage of the memory statistics module of the virtual machine needs to be limited to a certain memory (for example Figure 2In A2's memory management division, the virtual machine's memory management module will not count the memory usage of the LargeObjectSpace module, which indirectly increases the memory usage range of the LargeObjectSpace module and other space modules, and optimizes the memory usage range by optimizing the memory management structure.
[0045] For example, the virtual machine is an Android virtual machine, and the preset module is a module for storing JAVA large objects.
[0046] Step 104: reallocate memory for the target application object according to the memory allocation request.
[0047] by Figure 3 As an example, the implementation process of the present disclosure is further explained. Figure 3 First, execute the memory application for the memory allocation request, monitor the OOM exception during the memory application process, and when the OOM exception is detected, it indicates that the memory application has failed. The OOM exception is intercepted and the memory statistics optimization strategy is executed. The memory application for the memory allocation request is executed again to ensure that the memory application is successful. When the OOM exception is not detected, directly determine that the memory application is successful.
[0048] In the above manner, a memory allocation request is obtained, and the memory allocation request is used to request memory allocation for the target application's object. The OOM exception thrown when the memory allocation fails is intercepted. Since the memory that is not counted by the memory statistics module can be used for allocation, the memory usage in the heap memory counted by the memory statistics module of the virtual machine is reduced through the preset memory statistics optimization strategy. In this way, the memory that can be used for allocation by the memory statistics module can be increased, and memory can be reallocated to the target application's object, reducing the number of times the OOM exception is actually thrown, and avoiding the target application from crashing due to the OOM exception. In addition, when the OOM exception is not intercepted, it means that there is enough memory space for allocation, and there is no need to execute the preset memory statistics optimization strategy, so as to minimize the intrusion into the system and maximize the memory protection effect.
[0049] In possible ways, Figure 1 The steps shown for intercepting the OOM exception thrown when memory allocation fails may include: determining the type of the identifier of the preset interface, the preset interface is an interface used to throw the OOM exception when called, and the identifier type includes an allowed interception type and a prohibited interception type; when the identifier type is an allowed interception type, when the preset interface is monitored to be called, the OOM exception thrown by the preset interface is intercepted.
[0050] For example, the preset interface may be a ThrowOutOfMemoryError interface, and when the ThrowOutOfMemoryError interface is called, an OOM exception is thrown.
[0051] It should be noted that when the type of the preset interface identifier is the type that allows interception, it means that interception can be performed when the preset interface is called; when the type of the preset interface identifier is the type that prohibits interception, it means that interception is prohibited when the preset interface is called, and an OOM exception is thrown normally.
[0052] For example, the type of the identifier of the preset interface may be a symbolic representation such as a number or a character, which is not limited in this embodiment.
[0053] Through the above method, considering that OOM exceptions cannot be intercepted in all cases, the type of the identifier of the preset interface is determined to determine whether it can be intercepted when the preset interface is monitored to be called, so that the accuracy of the interception can be guaranteed.
[0054] In a possible manner, the method further includes: obtaining the actual memory usage that has been removed from the heap memory; and setting the type of the identifier of the preset interface according to the relationship between the preset threshold and the actual memory usage.
[0055] The preset threshold is the maximum value of the memory usage that is allowed to be reduced from the heap memory counted by the memory statistics module of the virtual machine. The preset threshold can be set according to actual conditions and is not limited in this embodiment.
[0056] The actual memory usage represents the actual amount of memory reduced from the memory statistics module of the virtual machine so far, that is, the actual amount of memory that is not counted in the memory statistics module and has been used.
[0057] Specifically, when the preset threshold is greater than the actual memory usage, the type of the preset interface identifier is set to the interception-allowed type; when the preset threshold is less than or equal to the actual memory usage, the type of the preset interface identifier is set to the interception-prohibited type.
[0058] Through the above method, a preset threshold is set, and the released space of the virtual machine memory statistics module is dynamically controlled through the preset threshold, so as to ensure that the memory usage of the virtual machine Native Mem module side is maintained within a reasonable range and avoid the preset module from occupying the memory of the Native Mem module without limit.
[0059] In a possible manner, the step of obtaining the actual memory usage removed from the heap memory may include: obtaining the actual memory usage removed from the heap memory when allocating memory for an object of the target application according to a memory allocation request for the first time. In this way, when allocating memory for an object of the target application according to a memory allocation request for the first time, the type of the identifier of the preset interface is determined based on the relationship between the actual memory usage and the preset threshold, thereby maximally avoiding unlimited occupation of the memory of the Native Mem module.
[0060] In a possible manner, the method further includes: when memory is reallocated to the object of the target application according to the memory allocation request, the type of the identifier of the preset interface is set to a prohibited interception type. It is understandable that after an OOM exception is intercepted once, memory will be allocated to the object of the target application according to the memory allocation request again. If an OOM exception still occurs during this memory application process, it means that the expanded memory is insufficient to meet the current memory application. If it is intercepted again, it will cause memory confusion and affect the normal operation of subsequent services. Therefore, in this case, it is still necessary to ensure that the OOM exception is thrown normally, and then when memory is reallocated to the object of the target application according to the memory allocation request, the type of the identifier of the preset interface needs to be set to a prohibited interception type.
[0061] For example, the amount of memory to be removed from the amount of memory counted by the memory statistics module can be determined based on the current memory usage of the preset module. Specifically, removing the memory usage of the preset module in the virtual machine from the heap memory can be achieved in the following ways: the current memory usage of the preset module in the virtual machine can be obtained, wherein the preset module is a module for storing JAVA large objects; the amount of memory to be hidden is determined based on the current memory usage of the preset module; based on the amount of memory to be hidden, the current total amount of memory usage counted by the memory statistics module of the virtual machine is updated, wherein the current total amount of memory usage counted by the memory statistics module does not include the amount of memory to be hidden, and the memory not counted by the memory statistics module can be used for allocation.
[0062] Through the above method, the memory statistics module is used to count the memory used in the heap area of the virtual machine. Since the memory statistics module counts the memory that is not counted and can be used for allocation, the memory usage in the preset module is separated from the statistics of the memory statistics module of the virtual machine, so that the memory statistics module of the virtual machine will not count the memory usage in the preset module, which indirectly increases the memory available for allocation in the heap area of the virtual machine, avoids the Java OOM exception caused by the inability to allocate memory to the object due to insufficient space for allocable memory, and thus improves the reliability of the device. In possible methods, the current memory usage of the preset module can be obtained in the following ways: obtain the current memory usage object of the preset module; decompile the current memory usage object to obtain the relative memory address offset of the current memory usage object; obtain the current memory usage of the preset module according to the relative memory address offset of the current memory usage object.
[0063] It should be noted that the current memory usage object can be represented by "num_bytes_allocated_".
[0064] For example, the current memory usage object of the preset module can be obtained through the proxy interface.
[0065] For example, a high-level language source program is compiled into an executable file, and decompilation is the reverse process, that is, the process of converting machine code (assembly language) into a high-level programming language. The implementation method of decompilation can refer to related technologies, and this embodiment will not be repeated here.
[0066] It should be noted that the relative memory address offset represents the physical address offset of the current memory usage object relative to the starting physical address of the disk where the current memory usage object is located. Based on the relative memory address offset, the position of the current memory usage object can be directly located, and then the data of the current memory usage object can be obtained. It can be understood that the data includes the current memory usage of the preset module.
[0067] Since the virtual machine does not directly expose an interface for obtaining the memory usage of the preset module, the relative memory address offset of the current memory usage object can be determined through the above method, and then the current memory usage of the preset module can be read according to the relative memory address offset, thereby solving the problem that the memory usage of the preset module cannot be directly obtained in the related technology.
[0068] In a possible manner, the method also includes: obtaining an object quantity object of a preset module before and after allocating memory for the object for the first time; before obtaining the current memory occupancy of the preset module in the virtual machine, determining that the difference between the two obtained object quantity objects is a preset difference, and the preset difference is used to verify whether the relative memory address offset of the current memory occupancy object is accurate.
[0069] It should be noted that the object number object can be represented by "num_objects_allocated_".
[0070] The acquisition of the object quantity object can refer to the acquisition method of the current memory usage object. Specifically, the object quantity object is decompiled to obtain the relative memory address offset of the object quantity object, and then the data corresponding to the object quantity object is obtained according to the relative memory address offset. It can be understood that the data includes the number of objects.
[0071] For example, the preset difference is 1.
[0072] Specifically, it is necessary to obtain the object quantity object before and after the first memory allocation for the object, and determine whether to execute the step of obtaining the current memory usage of the preset module in the virtual machine based on whether the difference between the two object quantity objects obtained is the preset difference. If it is determined that the difference between the two instance quantity objects obtained is the preset difference, then execute the step of obtaining the current memory usage of the preset module in the virtual machine.
[0073] Through the above method, the object quantity object is checked for difference. If the difference is equal to 1, it means that the relative memory address offset calculation is normal, otherwise the relative memory address offset calculation is wrong, so as to ensure the accuracy and reliability of obtaining the memory occupancy of the preset module.
[0074] In a possible way, you can use InlineHook (monitor any function) to proxy the interface associated with the relevant steps, and change the relevant parameters during the execution process to be compatible with different Android system versions.
[0075] In this embodiment, the implementation process of InlineHook may refer to related technologies, and this embodiment is not limited here.
[0076] Based on the same inventive concept, the embodiment of the present disclosure provides a virtual machine memory application device. Figure 4 is a block diagram of a virtual machine memory application device according to an exemplary embodiment of the present invention, referring to Figure 4 ,include:
[0077] A first acquisition module 401 is used to acquire a memory allocation request, where the memory allocation request is used to request to allocate memory for an object of a target application;
[0078] The interception module 402 is used to intercept the OOM exception thrown when the memory allocation fails;
[0079] An execution module 403 is used to execute a preset memory statistics optimization strategy, wherein the memory statistics optimization strategy is used to reduce the memory usage in the heap memory counted by the memory statistics module of the virtual machine;
[0080] The allocation module 404 is used to reallocate memory to the object of the target application according to the memory allocation request.
[0081] Optionally, the interception module 402 includes:
[0082] An identification type determination submodule, used to determine the type of the identification of a preset interface, where the preset interface is an interface used to throw the OOM exception when called, and the type of the identification includes an allowed interception type and a prohibited interception type;
[0083] The interception submodule is used to intercept the OOM exception thrown by the preset interface when it is monitored that the preset interface is called when the type of the identifier is a type that allows interception.
[0084] Optionally, the device 400 further includes:
[0085] A second acquisition module is used to obtain the actual memory usage that has been removed from the heap memory;
[0086] The first setting module is used to set the type of the identifier of the preset interface according to the relationship between the preset threshold and the actual memory usage.
[0087] Optionally, the first setting module includes:
[0088] A first setting submodule, configured to set the type of the identifier of the preset interface to an allowed interception type when the preset threshold is greater than the actual memory usage;
[0089] The second setting submodule is used to set the type of the identifier of the preset interface to a prohibited interception type when the preset threshold is less than or equal to the actual memory usage.
[0090] Optionally, the second acquisition module is specifically configured to acquire the actual memory occupancy that has been removed from the heap memory when allocating memory for the object of the target application for the first time according to the memory allocation request.
[0091] Optionally, the device 400 further includes:
[0092] The second setting module is used to set the type of the identifier of the preset interface to a prohibited interception type when reallocating memory for the object of the target application according to the memory allocation request.
[0093] Optionally, the execution module 403 is specifically used to remove the memory usage of a preset module in the virtual machine from the heap memory, wherein the virtual machine is an Android virtual machine, and the preset module is a module for storing large objects in JAVA.
[0094] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0095] Based on the same inventive concept, an embodiment of the present disclosure further provides a computer-readable medium on which a computer program is stored. When the program is executed by a processing device, the steps of the virtual machine memory application method described in the method embodiment are implemented.
[0096] Based on the same inventive concept, the present disclosure also provides an electronic device, including:
[0097] a storage device having a computer program stored thereon;
[0098] A processing device is used to execute the computer program in the storage device to implement the steps of the virtual machine memory application method in the method embodiment.
[0099] Reference below Figure 5 , which shows a schematic diagram of the structure of an electronic device 500 suitable for implementing the embodiment of the present disclosure. The terminal device in the embodiment of the present disclosure may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0100] like Figure 5As shown, the electronic device 500 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage device 508 into a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the electronic device 500 are also stored. The processing device 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0101] Typically, the following devices may be connected to the I / O interface 505: an input device 506 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 507 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 508 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 509. The communication device 509 may allow the electronic device 500 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 5 The electronic device 500 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0102] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 509, or installed from a storage device 508, or installed from a ROM 502. When the computer program is executed by the processing device 501, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.
[0103] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.
[0104] In some embodiments, the electronic device can communicate using any currently known or future developed network protocol such as HTTP (HyperText Transfer Protocol), and can be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.
[0105] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0106] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: obtains a memory allocation request, the memory allocation request is used to request memory allocation for an object of a target application; intercepts an OOM exception thrown when memory allocation fails; executes a preset memory statistics optimization strategy, the memory statistics optimization strategy is used to reduce the memory occupancy in the heap memory counted by the memory statistics module of the virtual machine; and re-allocates memory for the object of the target application according to the memory allocation request. .
[0107] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0108] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0109] The modules involved in the embodiments described in the present disclosure may be implemented by software or hardware. The name of a module does not limit the module itself in some cases. For example, the first acquisition module may also be described as a "module for acquiring a memory allocation request".
[0110] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0111] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0112] According to one or more embodiments of the present disclosure, Example 1 provides a method for applying for memory of a virtual machine, including:
[0113] Obtaining a memory allocation request, where the memory allocation request is used to request allocation of memory for an object of a target application;
[0114] Intercept the OOM exception thrown when memory allocation fails;
[0115] Executing a preset memory statistics optimization strategy, wherein the memory statistics optimization strategy is used to reduce the memory usage in the heap memory counted by the memory statistics module of the virtual machine;
[0116] Reallocate memory for the object of the target application according to the memory allocation request.
[0117] According to one or more embodiments of the present disclosure, Example 2 provides the method of Example 1, wherein intercepting the OOM exception thrown when memory allocation fails includes:
[0118] Determine the type of the identifier of the preset interface, where the preset interface is an interface used to throw the OOM exception when called, and the type of the identifier includes an allowed interception type and a prohibited interception type;
[0119] In the case where the type of the identifier is a type that allows interception, when it is monitored that the preset interface is called, the OOM exception thrown by the preset interface is intercepted.
[0120] According to one or more embodiments of the present disclosure, Example 3 provides the method of Example 2, wherein the method further includes:
[0121] Obtain the actual memory usage removed from the heap memory;
[0122] The type of the identifier of the preset interface is set according to the relationship between the preset threshold and the actual memory usage.
[0123] According to one or more embodiments of the present disclosure, Example 4 provides the method of Example 3, wherein the type of the identifier of the preset interface is set according to the relationship between the preset threshold and the actual memory usage, including:
[0124] When the preset threshold is greater than the actual memory usage, setting the type of the identifier of the preset interface to an interception-allowed type;
[0125] When the preset threshold is less than or equal to the actual memory usage, the type of the identifier of the preset interface is set to a prohibited interception type.
[0126] According to one or more embodiments of the present disclosure, Example 5 provides the method of Example 3, wherein obtaining the actual memory usage that has been removed from the heap memory includes:
[0127] When allocating memory for the object of the target application for the first time according to the memory allocation request, the actual memory occupancy removed from the heap memory is obtained.
[0128] According to one or more embodiments of the present disclosure, Example 6 provides the method of Example 5, wherein the method further includes:
[0129] When memory is reallocated to the object of the target application according to the memory allocation request, the type of the identifier of the preset interface is set to a prohibited interception type.
[0130] According to one or more embodiments of the present disclosure, Example 7 provides the method of any one of Examples 1-6, wherein the execution of the memory statistics optimization strategy includes:
[0131] The memory usage of a preset module in the virtual machine is removed from the heap memory, wherein the virtual machine is an Android virtual machine and the preset module is a module for storing JAVA large objects.
[0132] According to one or more embodiments of the present disclosure, Example 8 provides a virtual machine memory application device, including:
[0133] A first acquisition module, used for acquiring a memory allocation request, wherein the memory allocation request is used for requesting to allocate memory for an object of a target application;
[0134] The interception module is used to intercept the OOM exception thrown when memory allocation fails;
[0135] An execution module, used for executing a preset memory statistics optimization strategy, wherein the memory statistics optimization strategy is used for reducing the memory usage in the heap memory counted by the memory statistics module of the virtual machine;
[0136] The allocation module is used to reallocate memory to the object of the target application according to the memory allocation request.
[0137] According to one or more embodiments of the present disclosure, Example 9 provides a computer-readable medium having a computer program stored thereon, and when the program is executed by a processing device, the steps of the method described in any one of Examples 1-7 are performed.
[0138] According to one or more embodiments of the present disclosure, Example 10 provides an electronic device, including:
[0139] a storage device having a computer program stored thereon;
[0140] A processing device is used to execute the computer program in the storage device to implement the steps of any one of the methods described in Examples 1-7.
[0141] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.
[0142] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0143] Although the subject matter has been described in language specific to structural features and / or method logic actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims. Regarding the device in the above embodiment, the specific manner in which each module performs the operation has been described in detail in the embodiment related to the method, and will not be elaborated here.
Claims
1. A method for applying for virtual machine memory, characterized in that: include: Obtaining a memory allocation request, where the memory allocation request is used to request allocation of memory for an object of a target application; Intercept the OOM exception thrown when memory allocation fails; Execute a preset memory statistics optimization strategy, the memory statistics optimization strategy is used to reduce the memory usage in the heap memory counted by the memory statistics module of the virtual machine, the heap memory counted by the memory statistics module does not include the occupied memory in the preset module, and the memory not counted by the memory statistics module can be used for allocation, so as to release and increase the available memory in the current heap area; Re-allocate memory for the object of the target application according to the memory allocation request and the memory not counted by the memory statistics module.
2. The method according to claim 1, characterized in that The interception of the OOM exception thrown when the memory allocation fails includes: Determine the type of the identifier of the preset interface, where the preset interface is an interface for throwing the OOM exception when called, and the type of the identifier includes an allowed interception type and a prohibited interception type; In the case where the type of the identifier is a type that allows interception, when it is monitored that the preset interface is called, the OOM exception thrown by the preset interface is intercepted.
3. The method according to claim 2, characterized in that The method further comprises: Obtain the actual memory usage removed from the heap memory; The type of the identifier of the preset interface is set according to the relationship between the preset threshold and the actual memory usage.
4. The method according to claim 3, characterized in that: The setting of the type of the identifier of the preset interface according to the relationship between the preset threshold and the actual memory usage includes: When the preset threshold is greater than the actual memory usage, setting the type of the identifier of the preset interface to an interception-allowed type; When the preset threshold is less than or equal to the actual memory usage, the type of the identifier of the preset interface is set to a prohibited interception type.
5. The method according to claim 3, characterized in that: The obtaining of the actual memory usage of the heap memory that has been removed includes: When allocating memory for the object of the target application for the first time according to the memory allocation request, the actual memory occupancy that has been removed from the heap memory is obtained.
6. The method according to claim 5, characterized in that The method further comprises: When memory is reallocated to the object of the target application according to the memory allocation request, the type of the identifier of the preset interface is set to a prohibited interception type.
7. The method according to any one of claims 1 to 6, characterized in that: The execution of the preset memory statistics optimization strategy includes: The memory usage of a preset module in the virtual machine is removed from the heap memory, wherein the virtual machine is an Android virtual machine and the preset module is a module for storing JAVA large objects.
8. A virtual machine memory application device, characterized in that: include: A first acquisition module, used for acquiring a memory allocation request, wherein the memory allocation request is used for requesting to allocate memory for an object of a target application; The interception module is used to intercept the OOM exception thrown when memory allocation fails; An execution module is used to execute a preset memory statistics optimization strategy, wherein the memory statistics optimization strategy is used to reduce the memory usage in the heap memory counted by the memory statistics module of the virtual machine, wherein the heap memory counted by the memory statistics module does not include the occupied memory in the preset module, and the memory not counted by the memory statistics module can be used for allocation, thereby releasing and increasing the available memory in the current heap area; The allocation module is used to reallocate memory to the object of the target application according to the memory allocation request and the memory not counted by the memory statistics module.
9. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processing device, the steps of the method described in any one of claims 1 to 7 are implemented.
10. An electronic device, characterized in that: include: a storage device having a computer program stored thereon; A processing device, configured to execute the computer program in the storage device to implement the steps of the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Memory allocation method, mobile terminal and computer-readable storage medium
CN107357656A
Memory management method and system
CN108062247A