Virtual machine linear memory expansion method and device, storage medium and electronic equipment

By dynamically expanding the memory linear allocator of Dalvik virtual machine in Android system, the problem of limiting the number of methods in memory management of Dalvik virtual machine is solved, and the stable operation of large-scale applications and the effectiveness of memory management is achieved.

CN119557011AActive Publication Date: 2025-03-04HUNAN HAPPLY SUNSHINE INTERACTIVE ENTERTAINMENT MEDIA CO LTD
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202411629406.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-14
Publication Date
2025-03-04
Estimated Expiration
2044-11-14

AI Technical Summary

Technical Problem

When handling large-scale applications, the memory management of Dalvik virtual machines has a limit on the number of methods, resulting in installation failures and application crashes. The existing MultiDex technology has solved this problem to a certain extent, but there are still problems with file size limitations, increased development costs and runtime loading.

Method used

By initializing the software development toolkit in the target operating system, obtaining the preset memory capacity of the memory linear allocator, reading and parsing the memory map file, determining the memory area range, finding the memory map start address, and performing memory expansion based on this address, and allocating the target buffer using the memory map function to expand the linear memory of the virtual machine.

Benefits of technology

It effectively avoids the problem of limiting the number of virtual machine methods, ensures the effectiveness of memory management in scenarios such as dynamic loading at startup and runtime, so that large-scale applications can run stably, reducing crashes and installation failures.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119557011A_ABST
    Figure CN119557011A_ABST
Patent Text Reader

Abstract

The invention provides a virtual machine linear memory expansion method and device, a storage medium and electronic equipment, and is applied to the technical field of virtual machine memories. When the application program is started, the software development kit is initialized, and the preset capacity of the memory linear distributor of the corresponding system version is obtained. And analyzing the memory mapping file of the SDK, determining a memory area range, and calling a local method to find a memory initial address. And according to the address, expanding a memory linear allocator, and allocating a target buffer area by utilizing a memory mapping function. According to the method, the memory capacity is improved, the problem of limitation of the number of virtual machine methods is effectively solved, stable operation under all scenes including dynamic loading during starting and operation is ensured, and therefore the situations of collapse and installation failures are reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of virtual machine memory technology, and in particular to a virtual machine linear memory expansion method, device, storage medium and electronic device. Background Art

[0002] Prior to Android 5.0, Android used the Dalvik VM to manage application memory and used the LinearAlloc memory allocator to store classes and methods. Each DEX file of Dalvik can contain up to 65535 methods. Exceeding this limit will result in insufficient memory, installation failure, or application crash. This poses a challenge to large-scale application development.

[0003] Although the existing MultiDex technology solves the Dalvik limitation of 65535 methods per DEX file by splitting a single DEX file into multiple smaller DEX files, it still has limitations such as limiting file size, increasing development costs, and runtime loading issues.

[0004] Therefore, how to effectively deal with the LinearAlloc memory limitation in all scenarios has become a technical problem that technical personnel in this field urgently need to solve. Summary of the invention

[0005] In view of the above problems, the present invention provides a method, device, storage medium and electronic device for expanding the linear memory of a virtual machine that overcomes the above problems or at least partially solves the above problems. The technical solution is as follows:

[0006] A method for linear memory expansion of a virtual machine, comprising:

[0007] When an application in the target operating system is started, a software development kit of the application is initialized, and a preset memory capacity of a memory linear allocator corresponding to a system version number of the target operating system is obtained, wherein the memory linear allocator is a module in the virtual machine that manages memory allocation during class loading;

[0008] Read and parse the memory mapping file in the software development kit to determine the memory area range containing the memory linear allocator;

[0009] In the memory area range, calling a local method to find the memory mapping start address of the memory linear allocator;

[0010] Based on the memory mapping start address, performing memory expansion on the memory linear allocator according to the preset memory capacity;

[0011] A memory mapping function is used to allocate a target buffer to the memory linear allocator after memory expansion, so that the virtual machine uses the target buffer to perform a class loading operation.

[0012] Optionally, the reading and parsing of the memory mapping file in the software development kit to determine the memory area range containing the memory linear allocator includes:

[0013] Read and parse the memory mapping file in the software development kit to obtain a parsing result list, wherein the parsing result list includes a plurality of memory mapping information, each of which records a memory mapping start address, a memory mapping end address, a mapping area file name, and readability information;

[0014] The memory area range including the memory linear allocator is searched in the parsing result list.

[0015] Optionally, calling a local method to find a memory mapping start address of the memory linear allocator within the memory area range includes:

[0016] The local method is called using the local interface, and the memory mapping start address of the memory linear allocator is searched in the memory area range according to the offset corresponding to the system version number and the verification logic.

[0017] Optionally, the method further includes:

[0018] When the memory mapping start address of the memory linear allocator cannot be found in the memory area range, the memory mapping start address of the memory linear allocator is scanned out in the heap memory of the virtual machine.

[0019] Optionally, the memory expansion of the memory linear allocator based on the memory mapping start address according to the preset memory capacity includes:

[0020] Determine whether the preset memory capacity is an integer multiple of the page size corresponding to the application; if so, expand the memory size of the memory linear allocator to the preset memory capacity based on the memory mapping start address; if not, adjust the preset memory capacity to a target memory capacity that is an integer multiple of the page size corresponding to the application; based on the memory mapping start address, expand the memory size of the memory linear allocator to the target memory capacity, wherein the target memory capacity is larger than the preset memory capacity.

[0021] Optionally, after allocating the target buffer to the memory linear allocator after the memory expansion using the memory mapping function, the method further includes:

[0022] A write reference count is assigned to each memory page in the target buffer, and the write reference count is initialized to a medium value.

[0023] Optionally, after allocating the target buffer to the memory linear allocator after the memory expansion using the memory mapping function, the method further includes:

[0024] Use a mutex lock function to lock the target buffer;

[0025] When the target buffer is in a locked state, the target buffer is used to replace the original buffer of the memory linear allocator in the virtual machine, and the current allocation offset is maintained;

[0026] After the replacement is completed, the target buffer is unlocked to put the target buffer in an unlocked state.

[0027] A virtual machine linear memory expansion device comprises: an initialization unit, a memory area range determination unit, a memory mapping start address search unit, a memory expansion unit and a buffer allocation unit.

[0028] The initialization unit is used to initialize the software development kit of the application program in the target operating system when the application program in the target operating system is started, and obtain the preset memory capacity of the memory linear allocator corresponding to the system version number of the target operating system, wherein the memory linear allocator is a module in the virtual machine that manages memory allocation when class loading;

[0029] The memory area range determination unit is used to read and parse the memory mapping file in the software development kit to determine the memory area range including the memory linear allocator;

[0030] The memory mapping start address search unit is used to call a local method to search for the memory mapping start address of the memory linear allocator within the memory area range;

[0031] The memory expansion unit is used to perform memory expansion on the memory linear allocator according to the preset memory capacity based on the memory mapping start address;

[0032] The buffer allocation unit is used to use a memory mapping function to allocate a target buffer to the memory linear allocator after memory expansion, so that the virtual machine uses the target buffer to perform a class loading operation.

[0033] A computer-readable storage medium stores a program, wherein when the program is executed by a processor, the method for linear memory expansion of a virtual machine described in any one of the above is implemented.

[0034] An electronic device comprises at least one processor, and at least one memory and a bus connected to the processor; wherein the processor and the memory communicate with each other via the bus; and the processor is used to call program instructions in the memory to execute any one of the above-mentioned methods for linear memory expansion of a virtual machine.

[0035] By means of the above technical scheme, the method, device, storage medium and electronic device for expanding the linear memory of a virtual machine provided by the present invention, when the application in the target operating system is started, initialize the software development kit of the application, obtain the preset memory capacity of the memory linear allocator corresponding to the system version number of the target operating system, wherein the memory linear allocator is a module for managing memory allocation during class loading in the virtual machine; read and parse the memory mapping file in the software development kit, determine the memory area range containing the memory linear allocator; in the memory area range, call the local method to find the memory mapping start address of the memory linear allocator; based on the memory mapping start address, perform memory expansion on the memory linear allocator according to the preset memory capacity; use the memory mapping function to allocate the target buffer to the memory linear allocator after memory expansion, so that the virtual machine uses the target buffer for class loading operations. The present invention avoids the problem of limiting the number of virtual machine methods by increasing the memory capacity of the memory linear allocator, and also ensures that memory can be effectively managed in all scenarios including startup and dynamic loading at runtime, so that large-scale applications can run stably and reduce crashes and installation failures.

[0036] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Moreover, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:

[0038] Figure 1 A schematic diagram showing a flow chart of an implementation of a method for linear memory expansion of a virtual machine provided in an embodiment of the present invention;

[0039] Figure 2 A logic block diagram of a method for linear memory expansion of a virtual machine provided by an embodiment of the present invention is shown;

[0040] Figure 3A schematic diagram showing the structure of a device for expanding a linear memory of a virtual machine provided by an embodiment of the present invention is shown;

[0041] Figure 4 A schematic structural diagram of an electronic device provided by an embodiment of the present invention is shown. DETAILED DESCRIPTION

[0042] The exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present invention and to enable the scope of the present invention to be fully communicated to those skilled in the art.

[0043] In system versions prior to Android 5.0, Android mainly used the Dalvik virtual machine (Dalvik VM) as the runtime environment. Dalvik VM is responsible for managing the memory allocation and execution of applications, but its memory management method has some limitations, especially when dealing with large-scale code. Dalvik VM uses a memory area called LinearAlloc, which is used to store classes, methods, and related metadata.

[0044] When the application is installed, the Dalvik VM needs to decompress and load the code in the APK file and allocate memory space. The code file format in the APK file is DEX (Dalvik Executable), which is designed to optimize memory usage and operating efficiency. Usually, the default size of LinearAlloc is 5MB to 8MB, depending on the Android version.

[0045] However, Dalvik's memory allocation mechanism has an important bottleneck, that is, each DEX file can only contain a maximum of 65535 methods. When the number of methods in the application code exceeds this limit, the Dalvik VM is prone to insufficient memory when loading these codes. Due to the limited size of LinearAlloc, codes with more than 65535 methods will cause the following problems:

[0046] Installation process failed: During the installation process, if the number of methods in the DEX file exceeds the LinearAlloc limit, the system cannot allocate enough memory space for it, resulting in application installation failure.

[0047] Application crash: When the application is running, if the code that exceeds the method limit is triggered to load, the Dalvik VM cannot dynamically allocate more memory for it, which may cause an OutOfMemoryError error, causing the application to crash or force exit.

[0048] The above limitations pose great challenges to modern application development. Especially as the application scale increases and the functions become more complex, developers often encounter the problem of insufficient LinearAlloc memory.

[0049] To solve this problem, existing technical solutions mainly deal with the 65535 method limit when installing APK in Dalvik virtual machine by indirectly using MultiDex library provided by Google. MultiDex technology splits a single DEX file containing a large number of methods into multiple smaller DEX files, and the size and number of methods of each file are within the limit of Dalvik, thus avoiding insufficient memory or crash caused by exceeding the upper limit of methods.

[0050] Although MultiDex solves the problem of the number of methods exceeding 65535 to a certain extent, that is, the problem of installing APK with errors, this technical solution still has the following limitations:

[0051] DEX file size limit: Although applications can be subpackaged, each DEX file is still subject to the Dalvik upper limit of 65535 methods, which requires developers to pay extra attention to code allocation. Usually, developers need to carefully divide the classes and methods in the application and control the size of each DEX file through the configuration file (such as limiting the size of each DEX file to less than 2MB) to avoid exceeding the method limit.

[0052] Increased development costs: Developers must manually adjust the classes and methods that should be included in each DEX file, which complicates the development process. Especially for large projects or frequently updated projects, maintaining the code sub-package structure will bring additional workload.

[0053] Unable to avoid runtime loading problems: MultiDex can dynamically load multiple DEX files when the application starts, but in some scenarios, the method number limit may still be triggered during the application running. For example, in the plug-in solution, the external plug-in DEX file may be loaded at runtime, and the number of methods in the plug-in may exceed 65535, causing Dalvik to be unable to load the plug-in and cause a crash. Similarly, when dynamic hot update technology loads new DEX files at runtime, if the size of the DEX file is not strictly controlled, it may still face the problem of method number limit exceeding.

[0054] In summary, although MultiDex provides a solution to the method limit of the Dalvik virtual machine, it has certain limitations in flexibility, maintainability and performance. Especially in the scenario of dynamically loading DEX files at runtime, it is still impossible to completely circumvent the problem of the 65535 method limit. Therefore, the existing technical solutions cannot effectively deal with the LinearAlloc memory limit in all scenarios. In order to solve this problem, it is necessary to propose a new method that can dynamically expand the size of LinearAlloc in low versions of Android (versions using Dalvik VM), thereby breaking through the memory limit of Dalvik, allowing the code of large-scale applications to be loaded and run smoothly, and reducing the number of application crashes or installation failures.

[0055] Based on this, in an embodiment of the present invention, a method for expanding the linear memory of a virtual machine is provided. First, when an application is started in the target operating system, the software development kit is initialized, and the preset memory capacity of the memory linear allocator corresponding to the system version number is obtained. Secondly, the memory mapping file in the software development kit is read and parsed to determine the memory area range of the memory linear allocator. Then, the local method is called within the range to find the memory mapping start address. Based on the start address, the memory linear allocator is memory expanded according to the preset memory capacity. Finally, a memory mapping function is used to allocate a target buffer for the expanded memory linear allocator so that the virtual machine uses the buffer during class loading operations. It can be seen that the embodiment of the present invention avoids the problem of limiting the number of virtual machine methods by increasing the memory capacity, ensures the effectiveness of memory management in all scenarios such as dynamic loading at startup and runtime, enables large-scale applications to run stably, and reduces crashes and installation failures.

[0056] like Figure 1 As shown, a flow chart of an implementation of a method for linear memory expansion of a virtual machine provided by an embodiment of the present invention is provided, and the method may include:

[0057] S100. When an application in a target operating system is started, a software development kit of the application is initialized to obtain a preset memory capacity of a memory linear allocator corresponding to a system version number of the target operating system, wherein the memory linear allocator is a module in a virtual machine that manages memory allocation during class loading.

[0058] The target operating system may be a mobile phone operating system, that is, basic software for managing mobile phone hardware and software resources, such as the Android operating system.

[0059] Among them, applications are installed on the operating system to provide users with specific functions and services. For example, social media applications, games, office software, etc. Applications rely on the resources and services provided by the operating system to run.

[0060] Among them, the Software Development Kit (SDK) is a set of tools, libraries, sample codes and documents for building software applications. In the embodiment of the present invention, through the SDK interface, developers can easily call the memory expansion function without manually modifying the underlying code of the Android system or virtual machine, thereby reducing the development workload and improving development efficiency.

[0061] The memory linear allocator is a module that uses a linear allocation strategy to manage metadata in the virtual machine, including class definitions, method definitions, field information, etc. These metadata need to be loaded into the memory when the application is running for use by the virtual machine. For example: LinearAlloc manages memory allocation in the Dalvik virtual machine of the Android operating system.

[0062] Specifically, the embodiment of the present invention can call the initialization method provided by the SDK at the application startup entry of the main thread when the application is started. When the SDK is initialized, the corresponding LinearAlloc buffer size is passed in according to different Android versions. For example, for Android 2.3 (Gingerbread) and Android 4.0 (Ice Cream Sandwich), it is recommended to expand the buffer to 16MB; other versions configure different memory sizes according to specific needs.

[0063] S110, reading and parsing the memory mapping file in the software development kit to determine the memory area range including the memory linear allocator.

[0064] The memory mapping file is a virtual file in the Linux system, which displays the memory mapping information of the current process. This file contains the detailed mapping of the process address space, including the start address, end address, permissions, offset, device, node and mapped file name of each memory segment.

[0065] S120. In the memory area range, call a local method to find the memory mapping start address of the memory linear allocator.

[0066] The memory mapping start address of the memory linear allocator refers to the starting position where the memory linear allocator is mapped in the process address space. This address marks the starting point of the memory area managed by the memory linear allocator. It is used to control and manage the memory allocation process to ensure the effectiveness and security of memory use. When using memory mapping, the system will allocate and manage memory according to this start address.

[0067] S130. Based on the memory mapping start address, perform memory expansion on the memory linear allocator according to the preset memory capacity.

[0068] The embodiment of the present invention can allocate a new continuous memory to the memory linear allocator when performing memory expansion, and connect it to the memory area of ​​the current memory linear allocator. The embodiment of the present invention can increase the memory allocation to a larger size by dynamically expanding the LinearAlloc buffer, thereby significantly reducing the risk of application crashes caused by insufficient memory, especially when loading a large number of classes.

[0069] S140: Use a memory mapping function to allocate a target buffer to the memory linear allocator after the memory expansion, so that the virtual machine uses the target buffer to perform a class loading operation.

[0070] Among them, the memory map (MMAP) function is a function used to establish a mapping between a file or device and memory in the process address space. Memory mapping uses anonymous mapping to create a new memory area, manage memory allocation more efficiently, and avoid performance bottlenecks and memory waste in traditional methods.

[0071] The present invention allocates a new buffer for LinearAlloc by calling the mmap function, and creates this area in the memory using an anonymous mapping method. In this way, memory allocation of a custom size can be supported, for example, expansion to 16MB.

[0072] Among them, anonymous mapping is a memory mapping method created by mmap function, and the mapped memory area will not be associated with the actual file system. The mapped area is only used in the memory and will not be exchanged with the disk. The present invention creates a new LinearAlloc buffer through anonymous mapping to ensure that its allocation is independent of the file system, thereby improving the efficiency and flexibility of memory management.

[0073] The present invention provides a method for expanding the linear memory of a virtual machine, the method comprising: when an application in a target operating system is started, initializing a software development kit of the application, obtaining a preset memory capacity of a memory linear allocator corresponding to the system version number of the target operating system, wherein the memory linear allocator is a module for managing memory allocation during class loading in the virtual machine; reading and parsing a memory mapping file in the software development kit, determining a memory area range containing the memory linear allocator; in the memory area range, calling a local method to find a memory mapping start address of the memory linear allocator; based on the memory mapping start address, performing memory expansion on the memory linear allocator according to the preset memory capacity; using a memory mapping function to allocate a target buffer to the memory linear allocator after memory expansion, so that the virtual machine uses the target buffer for class loading operations. The present invention avoids the problem of limiting the number of virtual machine methods by increasing the memory capacity of the memory linear allocator, and also ensures that memory can be effectively managed in all scenarios including startup and dynamic loading at runtime, so that large-scale applications can run stably and reduce crashes and installation failures.

[0074] Optional, in the above Figure 1 On the basis of one or more corresponding embodiments, another optional embodiment provided by the embodiment of the present invention reads and parses the memory mapping file in the software development kit to determine the memory area range containing the memory linear allocator, which may specifically include:

[0075] Read and parse the memory mapping file in the software development kit to obtain a parsing result list, wherein the parsing result list includes multiple memory mapping information, each memory mapping information records a memory mapping start address, a memory mapping end address, a mapping area file name and readability information; search for a memory area range containing a memory linear allocator in the parsing result list.

[0076] Specifically, the embodiment of the present invention can read and parse the memory mapping file " / proc / self / maps" during the SDK initialization process. The file contains all memory mapping information of the current process. After parsing, a parsing result list is generated. Each item in the parsing result list describes a memory mapping information. The memory mapping information records the memory mapping start address, memory mapping end address, mapping area file name and its readability. Then, the embodiment of the present invention searches for all memory areas containing "LinearAlloc" in the parsing result list to obtain the starting address of the LinearAlloc area in the Dalvik virtual machine.

[0077] It is understandable that by parsing the memory mapping file, the approximate range of the memory area of ​​the memory linear allocator can be found. However, the memory area may contain multiple memory blocks with different purposes. With this information alone, the exact starting address of the LinearAlloc area cannot be determined. At the same time, a memory area may contain multiple different memory allocators or other metadata. The address range obtained by parsing the memory mapping file may not be completely accurate, and may contain other unrelated memory areas. In addition, the memory management of the Android system and the Dalvik virtual machine is dynamic, and the size and location of the memory area may change as the application runs. Relying solely on static memory mapping file information cannot cope with such dynamic changes.

[0078] Therefore, in order to accurately find the specific starting address of the LinearAlloc area, the SDK needs to analyze the memory layout more deeply to ensure that the starting address found is indeed the starting address of the LinearAlloc area.

[0079] Optional, in the above Figure 1 On the basis of one or more corresponding embodiments, in another optional embodiment provided by the embodiment of the present invention, in the memory area range, calling a local method to find the memory mapping start address of the memory linear allocator may specifically include:

[0080] Use the local interface to call the local method, and find the memory mapping start address of the memory linear allocator in the memory area range according to the offset and verification logic corresponding to the system version number.

[0081] Specifically, the embodiment of the present invention can use JNI (Java Native Interface) to call the native method nativeFindLinearAllocHeader to directly access the underlying memory, and perform more detailed inspection and analysis to determine the memory mapping start address of LinearAlloc. JNI is an interface between Java and native code (such as C / C++), which is used to call the underlying native method in Android applications. In the present invention, JNI is used to call native methods from Java code to find and replace the LinearAlloc buffer in the Dalvik virtual machine. The native method nativeFindLinearAllocHeader first attempts to locate the memory mapping start address of LinearAlloc in gDvm according to different Android versions using corresponding offsets and verification logic. If it is not found in gDvm, the entire heap memory will be scanned, that is, when the memory mapping start address of the memory linear allocator cannot be found in the memory area range, the memory mapping start address of the memory linear allocator is scanned in the heap memory of the virtual machine.

[0082] Among them, gDvm is the global structure of the Dalvik virtual machine, which saves the core state and runtime information of the virtual machine. In the process of searching for LinearAlloc memory, gDvm is used to quickly locate the entry point of the memory area. In some cases, it is necessary to extract the location of LinearAlloc from gDvm for further memory management operations. If the memory mapping start address of LinearAlloc is successfully found, proceed to the next step. If not found, an exception is thrown and the process is terminated.

[0083] Among them, heap memory is a part of computer memory used for dynamic memory allocation. The allocation and release of heap memory is determined by the operation during program execution and is usually handled by the memory manager of the programming language (such as the garbage collector in Java).

[0084] Among them, the local method can scan the memory area and look for specific tags or structures that can be the characteristics of the LinearAlloc region. In this way, the starting address of the LinearAlloc region can be accurately located. By accurately locating, it is possible to avoid misjudging other memory regions as LinearAlloc regions, thereby improving the reliability of memory expansion.

[0085] For ease of understanding, here is an example: Assume that the memory area obtained by parsing the memory mapping file is from 0x1000 to 0x2000. This range may contain multiple memory blocks for different purposes, such as class definitions, method tables, other metadata, etc. By further calling nativeFindLinearAllocHeader, a specific tag or structure can be scanned in this range, and finally 0x1500 is accurately located as the exact starting address of the LinearAlloc area.

[0086] The embodiment of the present invention obtains an approximate range by parsing a memory mapping file, and then uses JNI to call a native method to perform more precise memory scanning and matching, thereby accurately locating the memory mapping start address of LinearAlloc, thereby improving the accuracy of memory management and operation and avoiding potential misjudgments and memory management problems.

[0087] Optional, in the above Figure 1 On the basis of one or more corresponding embodiments, in another optional embodiment provided by the embodiment of the present invention, based on the memory mapping start address, the memory linear allocator is expanded according to the preset memory capacity, which may specifically include:

[0088] Determine whether the preset memory capacity is an integer multiple of the page size corresponding to the application; if so, expand the memory size of the memory linear allocator to the preset memory capacity based on the memory mapping starting address; if not, adjust the preset memory capacity to a target memory capacity that is an integer multiple of the page size corresponding to the application; based on the memory mapping starting address, expand the memory size of the memory linear allocator to the target memory capacity, wherein the target memory capacity is greater than the preset memory capacity.

[0089] Specifically, the embodiment of the present invention can adjust the memory size of LinearAlloc according to the obtained Android version configuration. For example: in Android 2.3, the default LinearAlloc buffer size is 8MB, which can be expanded to 16MB through the parameters passed in by the SDK. In Android 4.0, the default LinearAlloc buffer size is also 8MB, which can also be expanded to 16MB. The memory size after LinearAlloc expansion will be adjusted according to the page size (systemPageSize) to ensure that the memory size is an integer multiple of the page size.

[0090] It is understandable that once the starting address of LinearAlloc is determined, it usually does not change during program execution. Even if memory is expanded, the new memory block is added to the end of the original memory area instead of changing the starting address. This ensures the consistency of the memory area and helps simplify memory management. When memory is expanded, a new piece of continuous memory is allocated and connected to the current LinearAlloc area. All new memory addresses after memory expansion are still calculated based on the original starting address. For example, if the starting address is 0x1000 and the expanded memory starts at 0x2000, the newly allocated memory address is still calculated and managed relative to 0x1000. When performing memory operations, boundary checks are required (such as preventing out-of-bounds access). These checks are usually performed based on the starting address and the total size of the allocated memory. After the memory is expanded, the total size is updated, but the starting address remains unchanged, which helps keep the logic of boundary checks simple and consistent.

[0091] To make it easier to understand, here is an example: Assume that the starting address of the LinearAlloc area is 0x1000 and the size is 0x1000 (i.e. 4KB). At the beginning, the memory allocation range is 0x1000 to 0x2000. After expansion, assume that the new memory block is from 0x2000 to 0x3000 (another 4KB increase). At this time, the original starting address 0x1000 remains unchanged, but now the total LinearAlloc memory range is 0x1000 to 0x3000.

[0092] It should be noted that since the system manages memory in pages, the allocated memory size must be an integer multiple of the page size, which is also called "page alignment" or "alignment to page boundaries" in this field. For example, if the page size is 4KB, then the allocated memory should be 4KB, 8KB, 12KB, etc. If 6KB needs to be allocated, the system will actually allocate 8KB to ensure that the memory is an integer multiple of the page.

[0093] The embodiment of the present invention adjusts the memory capacity to an integer multiple of the page size, which helps to simplify the memory management process and reduce the complexity of memory management, so that the operating system can more easily perform memory allocation and recovery operations. At the same time, ensuring that the preset memory capacity is aligned with the page size helps to avoid potential memory access errors and abnormal situations, ensure the stability and reliability of the system after memory expansion, and improve the compatibility and adaptability of the program on different platforms after memory expansion.

[0094] Optional, in the above Figure 1 On the basis of one or more corresponding embodiments, in another optional embodiment provided by the embodiment of the present invention, after allocating the target buffer to the memory linear allocator after the memory expansion using the memory mapping function, the method may further include:

[0095] Assign a write reference count to each memory page in the destination buffer and initialize the write reference count to a medium value.

[0096] Specifically, after the SDK uses the memory mapping function to allocate the target buffer to the memory linear allocator after the memory is expanded, it will allocate a write reference count (writeRefCount) to each memory page and initialize it to a medium value (SHRT_MAX / 2) to prevent overflow or underflow during the allocation process.

[0097] The medium value refers to a constant representing the maximum value of a short integer in C / C++, which is usually used to initialize a memory counter. In the present invention, writeRefCount is initialized with half of SHRT_MAX (SHRT_MAX / 2) to prevent overflow or underflow of the memory reference count.

[0098] Among them, the write reference count is a reference count array used to manage LinearAlloc memory page write operations in the Dalvik virtual machine. In the present invention, the reference count array is reallocated for the new LinearAlloc buffer and initialized to half of SHRT_MAX (SHRT_MAX / 2) to cope with the ENFORCE_READ_ONLY mode that may be enabled. ENFORCE_READ_ONLY is a mechanism in the Dalvik virtual machine for forcing some memory areas to be read-only. On some devices, ENFORCE_READ_ONLY may be enabled, affecting the modification of the LinearAlloc buffer. The present invention ensures that LinearAlloc can still be extended when the mechanism is enabled by processing writeRefCount.

[0099] Optional, in the above Figure 1 On the basis of one or more corresponding embodiments, in another optional embodiment provided by the embodiment of the present invention, after allocating the target buffer to the memory linear allocator after the memory expansion using the memory mapping function, the method may further include:

[0100] A mutex lock function is used to lock the target buffer; when the target buffer is in a locked state, the target buffer is used to replace the original buffer of the memory linear allocator in the virtual machine, and the current allocation offset is maintained; after the replacement is completed, the target buffer is unlocked to put the target buffer in an unlocked state.

[0101] The embodiment of the present invention can use the mutex lock functions "pthread_mutex_lock" and "pthread_mutex_unlock" to protect the replacement operation of the LinearAlloc buffer, prevent data inconsistency problems caused by concurrent access of multiple threads, and ensure thread safety. Then, the LinearAlloc buffer in the Dalvik virtual machine is replaced, and the replacement process includes: replacing the old LinearAlloc address in the Dalvik virtual machine with a new mapAddr. Keep the currently allocated memory offset (curOffset) unchanged so that the new buffer continues to use the remaining space of the old buffer, thereby avoiding the impact on the existing memory. Finally, unlock the mutex lock to complete the replacement of the LinearAlloc buffer. After the replacement is completed, the Dalvik virtual machine can use a new, larger LinearAlloc buffer to ensure that the existing allocation is not affected. Through the SDK's extended memory task, a larger memory space is provided for Dalvik's class loading operation. Based on the changes in the memory structure of different Android versions, the embodiment of the present invention adopts flexible offset adjustment and memory scanning technology to ensure that the LinearAlloc buffer can be successfully located and replaced on each version. When replacing the LinearAlloc buffer, the original allocated location and state are maintained to ensure that the allocated memory is not lost or data errors occur, making the expansion process transparent to the application.

[0102] Wherein, the memory offset records the memory location that has been allocated to LinearAlloc, and the new memory allocation will continue from this offset. In the present invention, when replacing the LinearAlloc buffer, curOffset will be retained to continue to use the unused space in the old buffer.

[0103] In order to facilitate understanding of the overall logic of the virtual machine linear memory expansion method provided in the embodiment of the present invention, Figure 2 Let's take an example: Figure 2The figure shows a logic block diagram of a method for expanding the linear memory of a virtual machine provided by an embodiment of the present invention. The method for expanding the linear memory of a virtual machine provided by an embodiment of the present invention can be applied to a software development kit, which aims to dynamically expand the LinearAlloc buffer of the Dalvik virtual machine in the Android system to solve its memory limitation problem. The SDK is called when the access application starts, and searches for the memory mapping information of the LinearAlloc area in the Dalvik virtual machine by parsing the / proc / self / maps file, and obtains its starting address. In order to locate LinearAlloc more accurately, the SDK further processes by calling a local method. The local method first tries to find the starting address of LinearAlloc in the global variable gDvm of Dalvik. If it cannot be found in gDvm, the code will continue to scan the entire heap memory area, adjust the relevant offsets based on different versions of the Android system, and check the starting and ending addresses of the heap memory to ensure accurate positioning of the LinearAlloc area. Once the starting memory address of LinearAlloc is successfully obtained, the SDK will create a new LinearAlloc buffer according to the Android system version. For example, in Android 4.0, the SDK uses anonymous memory mapping (mmap) to create a new 16MB buffer (previously 8MB). Then, the SDK replaces the existing LinearAlloc buffer of the Dalvik virtual machine with the newly allocated buffer, while keeping the current memory allocation unaffected. In order to ensure compatibility and security, during the replacement process, the SDK ensures thread safety through locking operations, and updates and initializes the metadata of LinearAlloc (such as mapAddr and writeRefCount). In this way, the present invention effectively expands the linear memory limit of the original Dalvik virtual machine, provides a larger memory space for Android applications, thereby reducing the risk of crashes due to insufficient memory and improving the stability and performance of the application.

[0104] Although operations are depicted in a particular order, this should not be understood as requiring that the operations be performed in the particular order shown or in a sequential order.Multitasking and parallel processing may be advantageous under certain circumstances.

[0105] It should be understood that the various steps described in the method embodiments of the present invention 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 invention is not limited in this respect.

[0106] Corresponding to the above method embodiment, the embodiment of the present invention also provides a virtual machine linear memory expansion device, whose structure is as follows: Figure 3 As shown, it may include: an initialization unit 10, a memory area range determination unit 20, a memory mapping start address search unit 30, a memory expansion unit 40 and a buffer allocation unit 50.

[0107] The initialization unit 10 is used to initialize the software development kit of the application when the application in the target operating system is started, and obtain the preset memory capacity of the memory linear allocator corresponding to the system version number of the target operating system, wherein the memory linear allocator is a module in the virtual machine that manages memory allocation when class loading.

[0108] The memory area range determination unit 20 is used to read and parse the memory mapping file in the software development kit to determine the memory area range including the memory linear allocator.

[0109] The memory mapping start address search unit 30 is used to call a local method to search for the memory mapping start address of the memory linear allocator within the memory area range.

[0110] The memory expansion unit 40 is used to expand the memory of the memory linear allocator according to the preset memory capacity based on the memory mapping start address.

[0111] The buffer allocation unit 50 is used to allocate a target buffer to the memory linear allocator after the memory expansion using a memory mapping function, so that the virtual machine uses the target buffer to perform a class loading operation.

[0112] Optionally, the memory area range determination unit 20 can be specifically used to read and parse the memory mapping file in the software development kit to obtain a parsing result list, wherein the parsing result list includes multiple memory mapping information, each memory mapping information records the memory mapping start address, memory mapping end address, mapping area file name and readability information; search for the memory area range containing the memory linear allocator in the parsing result list.

[0113] Optionally, the memory mapping start address search unit 30 can be specifically used to call a local method using a local interface, and search for a memory mapping start address of a memory linear allocator in a memory area range according to an offset and verification logic corresponding to a system version number.

[0114] Optionally, the memory mapping start address search unit 30 can also be specifically used to scan the memory mapping start address of the memory linear allocator in the heap memory of the virtual machine when the memory mapping start address of the memory linear allocator cannot be found in the memory area range.

[0115] Optionally, the memory expansion unit 40 can be specifically used to determine whether the preset memory capacity is an integer multiple of the page size corresponding to the application. If so, the memory size of the memory linear allocator is expanded to the preset memory capacity based on the memory mapping starting address. If not, the preset memory capacity is adjusted to a target memory capacity that is an integer multiple of the page size corresponding to the application. Based on the memory mapping starting address, the memory size of the memory linear allocator is expanded to the target memory capacity, wherein the target memory capacity is larger than the preset memory capacity.

[0116] Optionally, the virtual machine linear memory expansion device may further include: a write reference count setting unit.

[0117] The write reference count setting unit is used to allocate a write reference count to each memory page in the target buffer after the buffer allocation unit 50 allocates the target buffer to the memory linear allocator after the memory expansion using the memory mapping function, and initialize the write reference count to a medium value.

[0118] Optionally, the virtual machine linear memory expansion device may further include: a thread protection unit.

[0119] A thread protection unit is used for the buffer allocation unit 50 to use a memory mapping function to allocate a target buffer to the memory linear allocator after the memory expansion, and then use a mutex lock function to lock the target buffer; when the target buffer is in a locked state, use the target buffer to replace the original buffer of the memory linear allocator in the virtual machine and keep the current allocation offset; after the replacement is completed, unlock the target buffer to make the target buffer in an unlocked state.

[0120] The virtual machine linear memory expansion device provided by the present invention, when the application in the target operating system is started, initializes the software development kit of the application, obtains the preset memory capacity of the memory linear allocator corresponding to the system version number of the target operating system, wherein the memory linear allocator is a module in the virtual machine that manages memory allocation during class loading; reads and parses the memory mapping file in the software development kit, determines the memory area range containing the memory linear allocator; in the memory area range, calls the local method to find the memory mapping start address of the memory linear allocator; based on the memory mapping start address, performs memory expansion on the memory linear allocator according to the preset memory capacity; uses the memory mapping function to allocate the target buffer to the memory linear allocator after the memory expansion, so that the virtual machine uses the target buffer to perform class loading operations. The present invention avoids the problem of virtual machine method number limitation by increasing the memory capacity of the memory linear allocator, and also ensures that memory can be effectively managed in all scenarios including startup and dynamic loading at runtime, so that large-scale applications can run stably and reduce crashes and installation failures.

[0121] Regarding the device in the above embodiment, the specific manner in which each unit performs the operation has been described in detail in the embodiment of the method, and will not be elaborated here.

[0122] The virtual machine linear memory expansion device includes a processor and a memory. The above-mentioned initialization unit 10, memory area range determination unit 20, memory mapping start address search unit 30, memory expansion unit 40 and buffer allocation unit 50 are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to realize corresponding functions.

[0123] The processor contains a kernel, which calls the corresponding program unit from the memory. One or more kernels can be set. By adjusting the kernel parameters, the software development kit is initialized and the preset capacity of the memory linear allocator of the corresponding system version is obtained when the application starts. Parse the memory mapping file of the SDK, determine the range of the memory area, and call the local method to find the starting address of the memory. According to the address, the memory linear allocator is expanded, and the target buffer is allocated using the memory mapping function. This method effectively solves the problem of the number of virtual machine methods by increasing the memory capacity, ensuring stable operation in all scenarios including startup and dynamic loading at runtime, thereby reducing crashes and installation failures.

[0124] An embodiment of the present invention provides a computer-readable storage medium on which a program is stored. When the program is executed by a processor, the method for linear memory expansion of a virtual machine is implemented.

[0125] An embodiment of the present invention provides a processor, which is used to run a program, wherein the virtual machine linear memory expansion method is executed when the program is running.

[0126] like Figure 4 As shown, an embodiment of the present invention provides an electronic device 1000, which includes at least one processor 1001, at least one memory 1002 connected to the processor 1001, and a bus 1003; wherein the processor 1001 and the memory 1002 communicate with each other through the bus 1003; the processor 1001 is used to call the program instructions in the memory 1002 to execute the above-mentioned virtual machine linear memory expansion method. The electronic device in this article can be a server, a PC, a PAD, a mobile phone, etc.

[0127] The present invention also provides a computer program product, which, when executed on an electronic device, is suitable for executing a program that initializes the steps of a virtual machine linear memory expansion method.

[0128] The present invention is described with reference to flowcharts and / or block diagrams of methods, apparatuses, electronic devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable device to generate a machine, so that the instructions executed by the processor of the computer or other programmable device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0129] In a typical configuration, an electronic device includes one or more processors (CPU), a memory, and a bus. The electronic device may also include an input / output interface, a network interface, and the like.

[0130] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip. The memory is an example of a computer-readable medium.

[0131] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0132] In the description of the present invention, it should be understood that the terms "up", "down", "front", "back", "left" and "right" etc. indicating directions or positional relationships are based on the directions or positional relationships shown in the accompanying drawings. They are only for the convenience of describing the present invention and simplifying the description. They do not indicate or imply that the positions or elements referred to must have specific directions, be constructed and operate in specific directions. Therefore, they should not be understood as limitations of the present invention.

[0133] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. It should also be noted that the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0134] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.

[0135] The above are only embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent substitution, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of the claims of the present invention.

Claims

1. A method for linear memory expansion of a virtual machine, characterized in that: include: When an application in the target operating system is started, a software development kit of the application is initialized, and a preset memory capacity of a memory linear allocator corresponding to a system version number of the target operating system is obtained, wherein the memory linear allocator is a module in the virtual machine that manages memory allocation during class loading; Read and parse the memory mapping file in the software development kit to determine the memory area range containing the memory linear allocator; In the memory area range, calling a local method to find the memory mapping start address of the memory linear allocator; Based on the memory mapping start address, performing memory expansion on the memory linear allocator according to the preset memory capacity; A memory mapping function is used to allocate a target buffer to the memory linear allocator after memory expansion, so that the virtual machine uses the target buffer to perform a class loading operation.

2. The method according to claim 1, characterized in that The step of reading and parsing the memory mapping file in the software development kit to determine the memory area range containing the memory linear allocator includes: Read and parse the memory mapping file in the software development kit to obtain a parsing result list, wherein the parsing result list includes a plurality of memory mapping information, each of which records a memory mapping start address, a memory mapping end address, a mapping area file name, and readability information; The memory area range including the memory linear allocator is searched in the parsing result list.

3. The method according to claim 1, characterized in that The calling of a local method to find the memory mapping start address of the memory linear allocator within the memory area range includes: The local method is called using the local interface, and the memory mapping start address of the memory linear allocator is searched in the memory area range according to the offset corresponding to the system version number and the verification logic.

4. The method according to claim 3, characterized in that Also includes: When the memory mapping start address of the memory linear allocator cannot be found in the memory area range, the memory mapping start address of the memory linear allocator is scanned out in the heap memory of the virtual machine.

5. The method according to claim 1, characterized in that The memory linear allocator is expanded based on the memory mapping start address according to the preset memory capacity, including: Determine whether the preset memory capacity is an integer multiple of the page size corresponding to the application; if so, expand the memory size of the memory linear allocator to the preset memory capacity based on the memory mapping start address; if not, adjust the preset memory capacity to a target memory capacity that is an integer multiple of the page size corresponding to the application; based on the memory mapping start address, expand the memory size of the memory linear allocator to the target memory capacity, wherein the target memory capacity is larger than the preset memory capacity.

6. The method according to claim 1, characterized in that After allocating the target buffer to the memory linear allocator after the memory expansion using the memory mapping function, the method further comprises: A write reference count is assigned to each memory page in the target buffer, and the write reference count is initialized to a medium value.

7. The method according to claim 1, characterized in that After allocating the target buffer to the memory linear allocator after the memory expansion using the memory mapping function, the method further comprises: Use a mutex lock function to lock the target buffer; When the target buffer is in a locked state, the target buffer is used to replace the original buffer of the memory linear allocator in the virtual machine, and the current allocation offset is maintained; After the replacement is completed, the target buffer is unlocked to put the target buffer in an unlocked state.

8. A virtual machine linear memory expansion device, characterized in that: include: Initialization unit, memory area range determination unit, memory mapping start address lookup unit, memory expansion unit and buffer allocation unit, The initialization unit is used to initialize the software development kit of the application program in the target operating system when the application program in the target operating system is started, and obtain the preset memory capacity of the memory linear allocator corresponding to the system version number of the target operating system, wherein the memory linear allocator is a module in the virtual machine that manages memory allocation when class loading; The memory area range determination unit is used to read and parse the memory mapping file in the software development kit to determine the memory area range including the memory linear allocator; The memory mapping start address search unit is used to call a local method to search for the memory mapping start address of the memory linear allocator within the memory area range; The memory expansion unit is used to perform memory expansion on the memory linear allocator according to the preset memory capacity based on the memory mapping start address; The buffer allocation unit is used to use a memory mapping function to allocate a target buffer to the memory linear allocator after memory expansion, so that the virtual machine uses the target buffer to perform a class loading operation.

9. A computer-readable storage medium having a program stored thereon, characterized in that: When the program is executed by a processor, the method for linear memory expansion of a virtual machine according to any one of claims 1 to 7 is implemented.

10. An electronic device, comprising at least one processor, and at least one memory and a bus connected to the processor; wherein: The processor and the memory communicate with each other via the bus; The processor is used to call the program instructions in the memory to execute the virtual machine linear memory expansion method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Memory mapping method and memory mapping module

    CN103257936A

  • Application program control method and device, terminal and storage medium

    CN112000424A

  • Crash information analysis method and device, electronic equipment and readable storage medium

    CN112905378A

  • Mapping method for process memory and instance processing method based on server-free architecture

    CN113448722A

  • Memory expansion method and related equipment

    CN118838704A