Garbage collection marking method and device for intelligent JAVA card and storage medium
By dividing the object storage area of the smart JAVA card into multiple areas and saving area information, the problem of slowing performance caused by repeated traversal of the object storage area during garbage collection is solved, and the effect of quickly obtaining object indexes is achieved.
Patent Information
- Application Number
- CN202510035591.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-09
- Publication Date
- 2025-05-09
AI Technical Summary
In the garbage collection process of smart JAVA cards, the existing technology needs to access the object storage area, traverse and count in turn, thereby calculating the object index, resulting in slower garbage collection performance.
The object storage area is divided into multiple regions, the first address and number of objects of each region are saved, and the area where the object is located is traversed during garbage collection, and traversed within the region to realize the conversion of object address to index.
Through this method, traversal of the entire object storage area is avoided, and the object index is quickly obtained, which significantly improves the performance of garbage collection.
Smart Images

Figure CN119960685A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of garbage collection, and in particular to a garbage collection marking method, device and storage medium for a smart JAVA card. Background Art
[0002] JAVA object management is an important part of JAVA-based smart cards. A simple and efficient object storage method is to allocate an independent storage area to store JAVA objects. The offset of the object in the storage area is the object reference. The conversion between the object reference and the object storage address becomes simple and efficient, that is, the object storage address = object reference + storage area base address. JAVA objects are stored in FLASH, and the size of each object may be inconsistent. The index value of the object needs to be calculated by traversing the storage area according to the object size.
[0003] Garbage collection needs to mark all referenced objects and then delete unreferenced objects. A mark is needed to mark whether an object is referenced. Since the object is stored in FLASH, this mark cannot be saved directly to the object, but is usually saved in RAM. Since RAM is usually very small, this mark is usually saved to a buffer accessed in bits and accessed in sequence according to the storage order of JAVA objects. Due to RAM limitations, the mapping between the index of the buffer and the address of the JAVA object cannot be stored in a table and can only be calculated each time it is accessed. For example, to mark an object, it is necessary to calculate the object index based on the object address, which requires accessing the object storage area, variable objects in turn, counting while traversing, and calculating the index. This leads to repeated traversal of the object storage area during garbage collection, which slows down performance. Summary of the invention
[0004] In view of this, the purpose of the present invention is to provide a garbage collection marking method, device and storage medium for a smart JAVA card, so as to solve the problem in the prior art that when obtaining an object index, it is necessary to access the object storage area, variable objects in turn, traverse and count while calculating the index, resulting in repeated traversal of the object storage area during the garbage collection process, resulting in slow performance.
[0005] According to a first aspect of an embodiment of the present invention, a garbage collection marking method for a smart JAVA card is provided, the method comprising:
[0006] Divide the object storage area into multiple areas, divide all objects into the areas in turn, and store the first address of each area and the number of objects in RAM;
[0007] Get all static objects, and determine the area where the static object is located based on the address of each static object and the first address of each area;
[0008] Taking the first address of the area where the static object is located as the traversal start address, traversing the objects in the area until the static object address is found, accumulating the counter during the traversal process, and using the counter when the static object is found as the index of the static object;
[0009] Searching in an object marking table according to the index of the static object, and marking the corresponding static object as referenced in the object marking table;
[0010] Mark all sub-objects in the object tree corresponding to the static object as referenced in the object marking table;
[0011] Traverse each object in the object storage area, obtain the index of each object respectively, query in the object marking table whether the object is marked as referenced according to the index of each object, and recycle all unmarked objects as garbage.
[0012] Preferably,
[0013] The step of dividing all objects into regions in sequence comprises:
[0014] Find the area whose object number is 0. If it exists, divide the object into this area, set the first address of the area to the object address, and set the object number to 1.
[0015] If there is no region with 0 objects, compare the number of objects in the last two regions. If the number of objects in the last region is less than the number of objects in the second-to-last region, divide the objects into the last region and increase the number of objects in the last region by 1.
[0016] If the number of objects in the last area is equal to the number of objects in the second-to-last area, then traverse all areas, obtain the area with the smallest sum of the number of objects in the two adjacent areas, merge the objects in the latter area into the former area, and move the latter blank area to the position of the last area, divide the objects into the last area, set the first address of the last area to the object address, and set the number of objects to 1;
[0017] If there is no region with the smallest sum of the number of objects in two adjacent regions, merge the objects in the second region into the first region, move the second region to the last region, divide the objects into the last region, set the first address of the last region to the object address, and set the number of objects to 1;
[0018] Until all objects are divided.
[0019] Preferably,
[0020] The step of determining the region where the static object is located according to the address of each static object and the first address of each region comprises:
[0021] According to the first address of each area, a region whose first address is larger than the address of the static object is searched. If it exists, the previous region is the region where the static object is located. If it does not exist, the last region is the region where the static object is located.
[0022] Preferably,
[0023] The step of marking all sub-objects in the object tree corresponding to the static object as referenced in the object marking table comprises:
[0024] Obtain a sub-object of a static object, determine the region where the sub-object of the static object is located according to the address of the sub-object of the static object, use the first address of the region where the sub-object of the static object is located as the traversal start address, start traversing objects in the region until the address of the sub-object of the static object is found, accumulate a counter during the traversal process, and use the counter when the sub-object of the static object is found as the index of the sub-object of the static object;
[0025] Searching in an object marking table according to an index of a sub-object of the static object, and marking a corresponding sub-object of the static object as referenced in the object marking table;
[0026] Get the child object of the child object, repeat the above process, and mark the child object of the child object as referenced in the object marking table; until the child object has no child objects, that is, the last layer of child object marking of the object tree corresponding to the static object is completed.
[0027] Preferably,
[0028] The first address of each area indicates the number of objects in all previous areas.
[0029] Preferably, it also includes:
[0030] After obtaining the area where any object is located, start traversing the objects in the area. The counter is initially 0. During the traversal, the counter is accumulated. The counter when any object is found plus the first address of the area is used as the index of the arbitrary object.
[0031] According to a second aspect of an embodiment of the present invention, a garbage collection marking device for a smart JAVA card is provided, the device comprising:
[0032] Region division module: used to divide the object storage area into multiple regions, divide all objects into regions in turn, and store the first address of each region and the number of objects in RAM;
[0033] Region judgment module: used to obtain all static objects and judge the region where the static object is located according to the address of each static object and the first address of each region;
[0034] Index acquisition module: used to use the first address of the area where the static object is located as the traversal start address, start traversing the objects in the area until the static object address is found, accumulate the counter during the traversal process, and use the counter when the static object is found as the index of the static object;
[0035] A marking module: used for searching in an object marking table according to the index of the static object, and marking the corresponding static object as referenced in the object marking table;
[0036] Sub-object marking module: used for marking all sub-objects in the object tree corresponding to the static object as referenced in the object marking table;
[0037] Garbage collection module: used to traverse each object in the object storage area, obtain the index of each object respectively, query whether the object is marked as referenced in the object marking table according to the index of each object, and recycle all unmarked objects as garbage.
[0038] According to a third aspect of an embodiment of the present invention, there is provided a storage medium, wherein the storage medium stores a computer program, and when the computer program is executed by a main controller, each step in the above method is implemented.
[0039] The technical solution provided by the embodiments of the present invention may have the following beneficial effects:
[0040] The present application divides the object storage area into multiple areas, saves the first address of each area and the number of objects in the area, and at the beginning of garbage collection, it is necessary to divide the objects into areas, obtain the area where the object is located by traversing the area information table, and traverse the area to achieve the conversion of the object address to the object index without traversing the entire object storage area, thereby quickly obtaining the object index; this solution solves the problem in the prior art that when obtaining the object index, it is necessary to access each object in the object storage area in turn, counting while traversing, resulting in repeated traversal of the object storage area during garbage collection and slow performance.
[0041] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0043] Figure 1 is a flow chart showing a garbage collection marking method for a smart JAVA card according to an exemplary embodiment;
[0044] Figure 2 is a system schematic diagram of a garbage collection marking device for a smart JAVA card according to another exemplary embodiment;
[0045] In the attached figure: 1-region division module, 2-region judgment module, 3-index acquisition module, 4-marking module, 5-sub-object marking module, 6-garbage collection module. DETAILED DESCRIPTION
[0046] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Instead, they are merely examples of devices and methods consistent with some aspects of the present invention as detailed in the appended claims.
[0047] Embodiment 1
[0048] Figure 1 is a flow chart showing a garbage collection marking method for a smart JAVA card according to an exemplary embodiment. Figure 1 As shown, the method includes:
[0049] S1, divide the object storage area into multiple areas, divide all objects into the areas in turn, and store the first address of each area and the number of objects in RAM;
[0050] S2, obtain all static objects, and determine the area where the static object is located according to the address of each static object and the first address of each area;
[0051] S3, taking the first address of the area where the static object is located as the traversal start address, starting to traverse the objects in the area until the static object address is found, accumulating the counter during the traversal process, and using the counter when the static object is found as the index of the static object;
[0052] S4, searching in an object marking table according to the index of the static object, and marking the corresponding static object as referenced in the object marking table;
[0053] S5, marking all sub-objects in the object tree corresponding to the static object as referenced in the object marking table;
[0054] S6, traversing each object in the object storage area, respectively obtaining the index of each object, querying in the object marking table whether the object is marked as referenced according to the index of each object, and recycling all unmarked objects as garbage;
[0055] It is understandable that this embodiment divides all object storage areas into multiple areas (16 or 32, etc.). The more areas there are, the better the performance of object traversal, but more RAM is required. Each area saves the following information to RAM: the starting address of the area (startAddr), the number of objects in the area (objCount); when garbage collection starts, the area information is initialized first. Initializing the area information means traversing the objects and assigning their address information to a certain area. At the same time, the number of objects in the area is increased by 1. The division requires that all areas are as balanced as possible, and not all objects should be divided into one or a few areas;
[0056] In order to meet the requirement that all regions are as balanced as possible, this embodiment also provides a process for dividing object regions, as shown below:
[0057] Traverse the region information table and search for the region with objCount of 0. If found, set startAddr to the object address and objCount to 1. The object division is completed.
[0058] If there is no region with objCount of 0, compare the objCount of the last two regions. If the last region is smaller than the second-to-last region, add 1 to the objCount of the last region, and the object division is completed.
[0059] If the last region is equal to the second-to-last region, find the two adjacent regions with the smallest sum of objCount in the entire region information and merge them into one, that is, add the objCount of the second region to the objCount of the first region, move the second region to the end, set the startAddr of the last region to the object address, and set objCount to 1, and the object division is completed;
[0060] If the sum of objCount of two adjacent areas is not the smallest, it means that objCount of all areas are equal. Merge the first and second areas, move the second area to the end, set the startAddr of the last area to the object address, set objCount to 1, and the object division is completed.
[0061] It is worth noting that the first address of each area is the number of objects in all previous areas. For example, if there are three areas in total and the number of objects in each area is 5, then the first address of the first area is 0, the first address of the second area is 5, and the first address of the third area is 10.
[0062] After all objects are divided into regions, the addresses of all static objects are obtained, and the region where the static object is located is determined according to the address of the static object, including: traversing the region information table, searching for a region whose startAddr is larger than the address of the static object, if found, the previous region is the region where the static object is stored, if not found, the last region is the region where the object is stored; still taking the above three regions as an example, assuming that the address of any static object is 8, and the first address 10 of the third region is greater than 8, then the region where the static object is located is the previous region of the third region, that is, the second region; after obtaining the region where the static object is located, use the found region startAddr as the traversal start address, that is, the starting number of the counter is 5, and start traversing the objects. Each time an object is traversed, the counter is incremented by 1, and the counter when the object is found is the index of the static object; another method is that the counter starts at 0, and the counter is incremented by 1 each time an object is traversed in the region, and the counter number when the object is found plus the first address of the region is also the index of the static object;
[0063] Mark the corresponding bit according to the index of the static object, that is, query the corresponding static object in the object marking table and mark it as referenced;
[0064] At the same time, each static object is a root object, that is, each static object and its sub-objects form an object tree. Each static object and its sub-objects need to be marked as referenced in the object marking table. After all static objects and their sub-objects are marked;
[0065] Traverse each object in the storage area and obtain its index respectively. The method for obtaining the index of each object is the same as the method for obtaining the index of the static object mentioned above, and this embodiment will not be repeated here. After obtaining the index of each object, query the object marking table according to the index of each object. If the object is not marked as referenced, the object will be recycled as garbage.
[0066] Embodiment 2
[0067] Figure 2 : is a system schematic diagram of a garbage collection marking device for a smart JAVA card according to another exemplary embodiment, the device comprising:
[0068] Region division module 1: used to divide the object storage area into multiple regions, divide all objects into regions in turn, and store the first address and number of objects in each region into RAM;
[0069] Region judgment module 2: used to obtain all static objects and judge the region where the static object is located according to the address of each static object and the first address of each region;
[0070] Index acquisition module 3: used to use the first address of the area where the static object is located as the traversal start address, start traversing the objects in the area until the static object address is found, accumulate the counter during the traversal process, and use the counter when the static object is found as the index of the static object;
[0071] Marking module 4: used for searching in an object marking table according to the index of the static object, and marking the corresponding static object as referenced in the object marking table;
[0072] Sub-object marking module 5: used for marking all sub-objects in the object tree corresponding to the static object as referenced in the object marking table;
[0073] Garbage collection module 6: used to traverse each object in the object storage area, obtain the index of each object respectively, query whether the object is marked as referenced in the object marking table according to the index of each object, and recycle all unmarked objects as garbage.
[0074] Embodiment three:
[0075] This embodiment provides a storage medium, wherein the storage medium stores a computer program, and when the computer program is executed by a main controller, each step in the above method is implemented;
[0076] It is understandable that the storage medium mentioned above can be a read-only memory, a magnetic disk or an optical disk, etc.
[0077] It can be understood that the same or similar parts of the above embodiments can be referenced to each other, and the contents not described in detail in some embodiments can refer to the same or similar contents in other embodiments.
[0078] It should be noted that, in the description of the present invention, the terms "first", "second", etc. are only used for descriptive purposes and cannot be understood as indicating or implying relative importance. In addition, in the description of the present invention, unless otherwise specified, the meaning of "plurality" refers to at least two.
[0079] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, segment or portion of code that includes one or more executable instructions for implementing the steps of a specific logical function or process, and the scope of the preferred embodiments of the present invention includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in the reverse order depending on the functions involved, which should be understood by those skilled in the art to which the embodiments of the present invention belong.
[0080] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, a plurality of steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0081] A person skilled in the art may understand that all or part of the steps in the method for implementing the above-mentioned embodiment may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, which, when executed, includes one or a combination of the steps of the method embodiment.
[0082] In addition, each functional unit in each embodiment of the present invention may be integrated into a processing module, or each unit may exist physically separately, or two or more units may be integrated into one module. The above-mentioned integrated module may be implemented in the form of hardware or in the form of a software functional module. If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it may also be stored in a computer-readable storage medium.
[0083] The storage medium mentioned above can be a read-only memory, a magnetic disk or an optical disk, etc.
[0084] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "examples", "specific examples", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.
[0085] Although the embodiments of the present invention have been shown and described above, it is to be understood that the above embodiments are exemplary and are not to be construed as limitations of the present invention. A person skilled in the art may change, modify, replace and vary the above embodiments within the scope of the present invention.
Claims
1. A garbage collection marking method for a smart JAVA card, characterized in that: The method comprises: Divide the object storage area into multiple areas, divide all objects into the areas in turn, and store the first address of each area and the number of objects in RAM; Get all static objects, and determine the area where the static object is located based on the address of each static object and the first address of each area; Taking the first address of the area where the static object is located as the traversal start address, traversing the objects in the area until the static object address is found, accumulating the counter during the traversal process, and using the counter when the static object is found as the index of the static object; Searching in an object marking table according to the index of the static object, and marking the corresponding static object as referenced in the object marking table; Mark all sub-objects in the object tree corresponding to the static object as referenced in the object marking table; Traverse each object in the object storage area, obtain the index of each object respectively, query in the object marking table whether the object is marked as referenced according to the index of each object, and recycle all unmarked objects as garbage.
2. The method according to claim 1, characterized in that: The step of dividing all objects into regions in sequence comprises: Search for an area with a target number of 0. If it exists, assign the object to the area, set the first address of the area to the target address, and set the target number to 1. If there is no region with 0 objects, compare the number of objects in the last two regions. If the number of objects in the last region is less than the number of objects in the second-to-last region, divide the objects into the last region and increase the number of objects in the last region by 1. If the number of objects in the last area is equal to the number of objects in the second-to-last area, then traverse all areas, obtain the area with the smallest sum of the number of objects in the two adjacent areas, merge the objects in the latter area into the former area, and move the latter blank area to the position of the last area, divide the objects into the last area, set the first address of the last area to the object address, and set the number of objects to 1; If there is no region with the smallest sum of the number of objects in two adjacent regions, merge the objects in the second region into the first region, move the second region to the last region, divide the objects into the last region, set the first address of the last region to the object address, and set the number of objects to 1; Until all objects are divided.
3. The method according to claim 2, characterized in that The step of determining the region where the static object is located according to the address of each static object and the first address of each region comprises: According to the first address of each area, a region whose first address is larger than the address of the static object is searched. If it exists, the previous region is the region where the static object is located. If it does not exist, the last region is the region where the static object is located.
4. The method according to claim 3, characterized in that The step of marking all sub-objects in the object tree corresponding to the static object as referenced in the object marking table comprises: Obtain a sub-object of a static object, determine the region where the sub-object of the static object is located according to the address of the sub-object of the static object, use the first address of the region where the sub-object of the static object is located as the traversal start address, start traversing objects in the region until the address of the sub-object of the static object is found, accumulate a counter during the traversal process, and use the counter when the sub-object of the static object is found as the index of the sub-object of the static object; Searching in an object marking table according to an index of a sub-object of the static object, and marking a corresponding sub-object of the static object as referenced in the object marking table; Get the child object of the child object, repeat the above process, and mark the child object of the child object as referenced in the object marking table; until the child object has no child objects, that is, the last layer of child object marking of the object tree corresponding to the static object is completed.
5. The method according to claim 4, characterized in that The first address of each area indicates the number of objects in all previous areas.
6. The method according to claim 5, characterized in that Also includes: After obtaining the area where any object is located, start traversing the objects in the area. The counter is initially 0. During the traversal, the counter is accumulated. The counter when any object is found plus the first address of the area is used as the index of the arbitrary object.
7. A garbage collection marking device for a smart JAVA card, characterized in that: The device comprises: Region division module: used to divide the object storage area into multiple regions, divide all objects into regions in turn, and store the first address of each region and the number of objects in RAM; Region judgment module: used to obtain all static objects and judge the region where the static object is located according to the address of each static object and the first address of each region; Index acquisition module: used to use the first address of the area where the static object is located as the traversal start address, start traversing the objects in the area until the static object address is found, accumulate the counter during the traversal process, and use the counter when the static object is found as the index of the static object; A marking module: used for searching in an object marking table according to the index of the static object, and marking the corresponding static object as referenced in the object marking table; Sub-object marking module: used for marking all sub-objects in the object tree corresponding to the static object as referenced in the object marking table; Garbage collection module: used to traverse each object in the object storage area, obtain the index of each object respectively, query whether the object is marked as referenced in the object marking table according to the index of each object, and recycle all unmarked objects as garbage.
8. A storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by the main controller, each step of the garbage collection marking method for a smart JAVA card as described in any one of claims 1 to 6 is implemented.