Java Select Sorting and Garbage Collection Mechanism Details

  • 2021-11-30 00:07:22
  • OfStack

Catalog 1. Garbage collection mechanism 2. Arrays class 3. Selective sorting method 4. Summary

1. Garbage collection mechanism

Creating an object takes up memory. If the program can no longer use an object during execution, the object is memory-consuming junk. As a programmer, you don't have to worry about recycling garbage objects because java Virtual opportunities automatically reclaim the memory space occupied by garbage objects.

When an object becomes garbage, it will be temporarily retained in memory. If the garbage pile is full, Java The virtual machine has a garbage collection mechanism, and the memory space occupied by the collected garbage objects will be released by the garbage collector. However, the program will have a lot of storage space. You can also call the System.gc() Method makes java The virtual machine carries out garbage collection. When an object is released in memory, it can be passed through finalize() Method is automatically called.

Objects have three states in memory:

Reachable state: When an object is created and more than one reference variable points to it, the object is in reachable state. Recoverable state: There are no reference variables pointing to this object. All recoverable state objects finalize () are called to clean up before the virtual machine garbage collects. If the system is calling finalize() Method to re-point a reference variable to the object becomes reachable again; otherwise, the object enters an unreachable state. Unreachable state: When the association of an object with all reference variables is broken, the system has called the finalize() Method still does not make the object reachable, the object permanently loses its reference and eventually becomes unreachable.

java Virtual Machine Garbage Collection Process

For example, the following code:


class Person{ 
// Definition finalize Method is called before garbage collection  
public void finalize(){ 
        System.out.println(" This object will be collected as garbage ..."); 
} 
} 
public class p9 { 
/** 
 * @param args 
 */ 
public static void main(String[] args) { 
// TODO Auto-generated method stub 
        // Create two Person Object of  
Person p1=new Person(); 
Person p2=new Person(); 
// Set the object to null 
p1=null; 
p2=null; 
// Call the garbage collection method  
System.gc(); 
} 


The result of the output is:

This object will be garbage collected...

This object will be garbage collected...

2. Class Arrays

Java provides the Arrays class for easy manipulation of arrays.

Arrays has the following features:

Array assignment: through Arrays.fill() Method is used for array populating; Array sorting: through Arrays.sort() Method sorts all elements of the array in order from small to large; Array comparison: through Arrays.equals() Method to judge whether the values of array elements are equal; Find array elements: Java0 Method to find the specified element in the ordered array by 2-point method, and return the subscript of the element; Array conversion string: Arrays.toString() Method converts the array to a string and outputs it;

Arrays class example

The code looks like this:


public static void main(String[] args) { 
// TODO Auto-generated method stub 
int[] a1 = new int[]{5, 2 , 3, 9}; 
    int[] a2 = new int[]{5, 2 , 3, 9}; 
    //Arrays.equals() Method to determine whether array elements are equal  
    System.out.println("a1 Array and a2 Whether the arrays are equal: " + Arrays.equals(a1 , a2)); 
    int[] b = Arrays.copyOf(a1, 6);  
    System.out.println("a1 Array and b Whether the arrays are equal: " + Arrays.equals(a1 , b)); 
    //toString() Method converts an array to a string  
    System.out.println("b The elements of the array are: " + Arrays.toString(b)); 
    //Array.fill() Method array assignment  
    Arrays.fill(b , 2, 4 , 1); 
    System.out.println("b The elements of the array are: " + Arrays.toString(b)); 
    //Arrsays.sort() Method array sorting  
    Arrays.sort(b); 
    System.out.println("b The elements of the array are: " + Arrays.toString(b)); 
} 


The result of the output is:

a1 Array and a2 Array are equal: true
a1 Array and b Array Equality: false
The elements of the b array are: [5, 2, 3, 9, 0, 0]
The elements of the b array are: [5, 2, 1, 1, 0, 0]
The elements of the b array are: [0, 0, 1, 1, 2, 5]

3. Selective sorting

First, find the subscript (index) where the smallest element is located, and exchange this element with the element at bit 1.

Case of selective ranking method


public static void main(String[] args) { 
// TODO Auto-generated method stub 
int arr[]= {12,31,25,7,38};// Definition 1 Array of numbers  
    for(int i=0;i<arr.length;i++) { 
        int temp=i; 
          // Array from the i The subscript (index) of the location of the smallest element at the beginning is assigned to the temp 
        for(int j=i;j<arr.length;j++) { 
            if(arr[j]<arr[temp]) { 
                temp=j; 
            } 
        } 
        // The above obtains the array from the i Indication of the minimum value of the beginning ( Index )temp The index puts the number of i The element on the bit is exchanged with it  
        int temp1=arr[i]; 
        arr[i]=arr[temp]; 
        arr[temp]=temp1; 
        System.out.println(arr[i]); 
  } 
} 

The output is:

7
12
25
31
38

4. Summary

This paper mainly introduces the java Garbage collection mechanism, Arrays Class, select the sort method. java The garbage collection mechanism of briefly explains that there are three states of objects in memory: 可达状态、可恢复状态、不可达状态 . Pass 1 java The case of virtual machine garbage collection mechanism helps everyone understand this process. Arrays Class is convenient for manipulating arrays, and introduces the Arrays Several functions of. Commonly used selective sorting method, first of all, find the index of the location of the smallest element, and exchange this element with the element in the first place. Through the study of this article, I hope it will be helpful to everyone!


Related articles: