Learning about memory management

1. C and C++ require manual memory management:

Why c and c++ are memory unsafe:

        There are pointers, there is delete[], and there is no concept of type safety, which is the biggest factor of insecurity; C++ and C do not require type safety, and even the editor does not report an error. For example, A has no inheritance relationship with B, but A* can be forcibly transformed into B*. Pointers are the biggest unsafe factor. The existence of delete[] indicates that there is no good memory management, no garbage collection mechanism, and easy memory leaks .
Mainly wild pointers and memory leaks.

        Misuse of wild pointers. After releasing the memory with free or delete, it is problematic to continue to use this memory. After the memory is released, the pointer must be set to NULL to avoid wild pointers .

        It is possible to forget to release the memory after allocating the memory, resulting in a memory leak . If the memory leak is serious, it will cause insufficient memory or memory exhaustion, so malloc/free, new/delete must be used in pairs.

        Another example is that both functions use an object B. If the first function gives B to delete[], the second function may cause the program to crash because it does not know that the object does not exist. . . It will still be used, delete[] does not exist in a safe language, and the object does not need to be released.

Memory leak: Refers to the situation where the program fails to release memory that is no longer used due to negligence or error. A memory leak does not refer to the physical disappearance of memory, but rather the loss of control over a certain segment of memory due to a design error after the application allocates a certain segment of memory, resulting in a waste of memory. Memory leaks occur in long-running programs, which have a great impact, such as the operating system, background services, etc. Memory leaks will cause slower and slower responses and eventually freeze. (harm)

Dynamic memory management in C language:

malloc, calloc, realloc, and free. The malloc function dynamically allocates memory from

1. Variables:

  • Global variables (external variables): Variables that appear outside the code block {} are global variables.
  • Local variable (automatic variable): In general, the variable defined inside the code block {} is an automatic variable, and auto can also be used to display the definition.
  • Static variable: refers to the variable whose memory location does not change during the execution of the program, and is modified with the keyword static. A static variable inside a code block can only be accessed inside the code block, and a static variable outside the code block can only be accessed by the file in which the variable is defined.

2. Function:

Note: Functions in C language are global by default, and you can use the static keyword to declare a function as a static function (a function that can only be accessed by the file that defines this function).

3. Memory: Constant storage area ( constants are stored and cannot be modified, such as the string "abc"), (global/static) area, heap, stack, free storage area (C++ only) .

Stack area (stack): - automatically allocated and released by the compiler,

Heap area (heap) - generally allocated and released by the programmer, if the programmer does not release it, it may be reclaimed by the OS at the end of the program

Global area (static area) (static)—The storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, - released by the system after the end of the program.

The malloc function dynamically allocates memory from the heap.

 malloc, calloc, realloc, and free functions:

The function of malloc is to open up a memory space of a specified byte size. If the opening is successful, it returns the first address of the space, and if the opening fails, it returns a NULL. When passing parameters, you only need to pass in the number of bytes that need to be developed .

The function of calloc is also to open up a memory space of a specified size. If the opening is successful, it returns the first address of the space, and if the opening fails, it returns a NULL. When the calloc function passes parameters, it needs to pass in the number of elements and the size of each element in the allocated memory for storage . After the calloc function has allocated the memory, each byte in the space content will be initialized to 0.

realloc can adjust the size of the dynamic memory that has been opened. The first parameter is the first address of the dynamic memory that needs to be resized, and the second parameter is the new size of the dynamic memory after adjustment. The realloc function is the same as the above two functions. If the allocation is successful, it will return the first address of the allocated memory, and if the allocation fails, it will return NULL.

The function of free is to release the dynamic memory space applied by the malloc, calloc and realloc functions, and the size of the released space depends on the size of the previously applied memory space.
 

Dynamic memory management in C++:

via the new and delete operators

Compared with c, C++ has one more: free storage area (C++ only)

Free storage area : A term on the C++ level, those memory blocks allocated by new, their release compiler does not care about them, and is controlled by our application program. Generally, a new corresponds to a delete. If the programmer does not release it, the operating system will automatically recycle it after the program ends. The application of new is to call malloc, and the free storage area is similar to the heap, but not equivalent.

The new operator dynamically allocates memory space for objects from the free store , while the malloc function dynamically allocates memory from the heap Any memory application is made through the new operator, and the memory is a free storage area. The heap is a term in the operating system. It is a special memory maintained by the operating system for the dynamic allocation of program memory. C language uses malloc to allocate memory from the heap, and uses free to release the allocated corresponding memory.

So whether the free storage area can be a heap (the question is equivalent to whether new can dynamically allocate memory on the heap), it depends on the implementation details of operator new. The free storage area can be not only a heap, but also a static storage area, depending on where operator new allocates memory for objects.

like:

C++: int* p1 = new int; //apply delete p1; //destroy

C: int* p2 = (int*)malloc(sizeof(int)); //apply free(p2); //destroy


What they have in common:  They all apply for space from the heap and need to be released manually by the user.

2. Automatic memory management of java and C# (the core of management: when an object in the heap is no longer used , the memory can be automatically reclaimed and made available.)        

Students who have experience in C and C++ development must be disgusted with the out-of-bounds memory access or memory leaks encountered at that time. Even with smart pointers and other things in the future, such problems cannot be completely avoided. The automatic memory management mechanism of C# and Java allows programmers to focus on function development without having to manage memory by themselves.

Regarding java's memory unsafety:

Java's memory management is the allocation and release of objects

Allocation: The allocation of memory is done by the program. Programmers use the new keyword to apply for memory space for objects (except for basic data types), and objects are all allocated in the heap (Heap); Release: The release of objects is done by the garbage collection mechanism (Garbage Collection), in order to correctly release objects, GC needs to monitor the running status of each object, including application, reference, referenced, assignment, etc.

Java can handle memory cleanup, memory leaks, and memory optimization in most scenarios. But it's not a panacea either. Java still can't completely solve memory leaks (memory leaks generally mean that some objects in the heap are no longer used, but the garbage collector cannot clear them from memory). When the memory object is obviously no longer needed, it still retains this memory and its access method (reference).

A memory leak is a serious problem because it blocks memory resources and degrades system performance over time. If no effective processing is carried out, the final result will be that the application program will run out of memory resources, fail to serve normally, cause the program to crash, and throw a java.lang.OutOfMemoryError exception.

There are generally two types of objects in heap memory: referenced objects and unreferenced objects. A referenced object is one that still has active references in the application, while an unreferenced object does not have any active references.

The garbage collector reclaims unreferenced objects, but not those that are still referenced. This is also the source of java memory leaks.

Classification scenarios of memory leaks in Java:

1. Unclosed resources: Whenever we create a connection or open a stream, JVM will allocate memory for these resources. For example, database connections, input streams, and session objects. Forgetting to close these resources will block memory and prevent GC from cleaning up. Especially when an exception occurs in the program, there is no resource closing in finally.

2. There is a potential memory leak problem when using the finalize() method. Whenever the finalize() method of a class is overridden, the objects of this class will not be immediately reclaimed by the GC. The GC will put them in a queue for finalization, to be reclaimed at some later point in time. How to avoid this from happening? Always avoid using finalizers.

3String's intern method: If you read a large string object and call its intern method, intern() will put the String in the JVM's memory pool (PermGen), and the JVM's memory pool will not be GC. It will also cause program performance degradation and memory overflow problems.

4. Static attributes lead to memory leaks: One situation that can cause memory leaks is the extensive use of static static variables. In Java, the life cycle of static properties is usually accompanied by the entire life cycle of the application (unless the ClassLoader is eligible for garbage collection).

Memory management through virtual machines:

The so-called memory management must be a matter of runtime, and the reason why C# and Java can achieve automatic management is because they have their own runtime on the real machine binary OS (virtual machine: CLR of C# and JVM of Java ).

                  

1. Memory of java and C#

 The two larger blocks are the heap and the stack

java:

        Heap memory is used to store object instances and arrays created by new. (Important) The Java heap is a memory area shared by all threads. It is created ( when the virtual machine jmv starts . The sole purpose of this memory area is to store object instances. The Java heap is the main area managed by the garbage collector. The heap is the application program in When it is running, it requests the operating system to allocate its own memory. Due to the allocation of memory managed by the operating system, it takes time to allocate and destroy it, so the efficiency of using the heap is very low.

         What is saved in the stack memory is the access address of the heap memory space, or the variables in the stack point to the variables in the heap memory (pointers in Java) (emphasis) .

         The method area is a memory area shared by each thread. It is used to store data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine  (emphasis).

The stack is thread-private, and each method will create a stack frame to store local variables, operand stacks, method exits, and other information when executed. The life cycle of the stack is the same as that of the thread, and the memory stored in the stack will be released automatically after the respective methods are executed.

The heap is shared by all threads. Almost all reference object instances are stored in this area. The CLR or JVM has made great efforts to manage the memory area of ​​the heap . This management mechanism is generally called garbage collection .

2. The garbage collection mechanism is used to manage the memory in the heap. When an object in the heap is no longer used , the memory can be automatically reclaimed and made available.

3. So how do you know that an object is no longer used? This is generally achieved by generating a reachable object graph based on reachability analysis ; reachability analysis starts from some root objects and traverses each The referenced object, when there is no reference path from an object to the root object, it is considered that the object can be recycled .

4. Recycling algorithm As mentioned above, all objects that need to be recycled can be marked through reachability analysis. So how to recycle it? Of course, the easiest way is to clear the memory space of the objects that need to be reclaimed. This method is simple to implement, and because the addresses of surviving objects are not changed, the delay during GC can be greatly reduced. But the problem is also very serious, it will fragment the memory, and finally lead to the fact that although there is still a lot of available memory, there will be no continuous large memory available.

Another method is to compress the memory, which can greatly improve the utilization rate of the memory and bring convenience to the subsequent memory allocation (afterwards, additional allocation can be made from the back of the used space).

For memory compression processing, in fact, it can also be divided into two processing methods, copying and sorting.

The copy algorithm is to copy the surviving objects to another piece of memory. It is not suitable for the situation where there are a large number of surviving objects during garbage collection, because in this case, a large amount of available memory space needs to be prepared for copying. But for the case where there are few surviving objects, this algorithm is very efficient.

Sorting is to move the position of surviving objects after clearing the recyclable objects, so that their memory becomes a continuous block. This algorithm is widely used when there are few recyclable objects.

5.Whether there will be a memory leak in the java program:
  there will be a memory leak. In general, there are two types of memory leaks. One is to delete the memory allocated in the heap when it is not released, and delete all the ways to access this memory; the other is to delete the memory object when it is obviously no longer needed. This memory and its access method (reference) are retained . The first case has been well solved in Java due to the introduction of the garbage collection mechanism. Therefore, memory leaks in Java mainly refer to the second situation.

2. Why can the JVM of java perform memory management? JVM memory management mechanism-Knowledge

1. To do java background development, the inspection of jvm is the most important! The interviewer will definitely ask you about your understanding of jvm, and you have to describe it from the following perspectives:

  1. Division of JVM memory
  2. Garbage collection issues (definition, what to recycle)
  3. GC algorithm

The JVM divides the memory it manages into five different data areas during the Java program.

2. Garbage collection (GC)

The garbage collection mechanism is a thread that comes with jvm, which is used to recycle unreferenced objects.

3. Areas that need garbage collection:

Only the method area and the heap . Because the life cycle of the program counter, JVM stack, and local method stack is synchronized with the thread, the memory occupied by them will be released automatically as the thread is destroyed, so only the method area and the heap need to be garbage collected.

4. Garbage collection algorithm (4 types):

  1. mark-sweep algorithm
  2. copy algorithm
  3. Mark-Collating Algorithm
  4. Generational collection algorithm ( important )

5. How to judge whether the object is garbage

Use two algorithms to judge: (1) program counter (2) reachability analysis (more commonly used than the first one)

3. Rust does not require memory management

Each value in Rust can only be owned by one owner. When this value is assigned to another owner, the original owner can no longer use it. It is this mechanism that guarantees the memory safety of the Rust language, eliminating the need for automatic garbage collection and manual deallocation. Ownership is one of the most important features of Rust

Guess you like

Origin blog.csdn.net/zr_xs/article/details/127759998