Perl's garbage collection (GC) characteristics
The mechanism that automatically releases objects (variable and values) from memory when they are no longer needed is called garbage collection . Garbage means garbage. Collection means to collect.
#
Perl's garbage collection is a reference counting method
Perl's garbage collection is called the reference counting method . Each time a reference to an object is created, it increments the reference count by one. Conversely, it decrements the reference count by one when the object's reference is removed. The object is released when the reference count reaches 0.
Generally speaking, the reference counting method is simple and fast to implement. This is a good performance characteristic of Perl. I've never heard of a garbage collection suffering from performance issues in Perl (so far).
Second, the time when the object is destroyed is when the reference is gone, so the programmer knows exactly when the object is destroyed.
For example, in Java, garbage collection is called generational CG, and because there are objects that will be released later, it is not possible to write post-processing that you want to execute immediately in finalize. In the case of Perl, it is a reference counting method, so if you describe the process in the destructor DESTROY, you can execute the process when there are no more reference.
The second feature is memory saving. Since the processing is performed when the reference disappears, no object remains in the memory. Nowadays, memory can be packed a lot, so this drawback is less noticeable than it used to be, but memory-hungry programs can also cause swap-in and swap-out on small machines. Therefore, it can be said that memory saving is easier to manage.
The third is that it does not stop at the timing of Full GC like the generational GC. In generational GC, there is something called FullGC in which many objects are released at a specific timing. The program will stop during FullGC. But for web apps, you probably don't care too much (probably).
The disadvantage of the reference counting method is that cross - reference cannot be resolved
The obvious drawback of the reference counting method is that it cannot resolve cross-reference. Objects that are referenced to each other are not released from memory. Programmers need to be careful if objects are cross-referenced or circularly referenced.
# Cross-reference my $hash1 = {}; my $hash2 = {}; $hash1->{hash2} = $hash2; $hash2->{hash1} = $hash1;
Simply writing as above will cause a cross-reference and this object will not be released automatically. This can easily happen if the programmer is not careful. So if you're cross-referencing, you'll have to change the reference to either object to a weak reference that doesn't increase the reference count.
use Scalar::Util 'weaken'; # Cross-reference my $hash1 = {}; my $hash2 = {}; $hash1->{hash2} = $hash2; $hash2->{hash1} = $hash1; # Make one a weak reference weaken $hash1->{hash2};
This way, memory will be freed when the reference to $hash1 or $hash2 reaches zero.
Keep in mind that in Perl, garbage collection implementations are reference-counted and rarely suffer from performance issues, but instead you have to be aware of cross-reference yourself. Let's go.