Why should Python use a reference count with obvious defects rather than a mark like JavaScript to clear it?

Source: Internet
Author: User
The reference count has a clear defect in cyclic counting. Why does Python need to use reference counting instead of Mark clearing? The reference count has a clear defect in cyclic counting. Why does Python need to use reference counting instead of Mark clearing? Reply content: the biggest benefit of reference count is timely collection: the moment when the reference count of an object is set to zero is the moment when it becomes garbage and the moment when it is recycled. The disadvantage of tracing GC algorithms such as mark-sweep: After an object becomes spam, it will be retained for a period of time (floating garbage) in the memory until it is cleared by the next GC ).

The GC Design of Python is to use reference count for objects that do not contain references to other objects (such as strings and numeric types), because these objects cannot generate circular references. Mark-sweep is used for objects that may generate circular references, such as List and Map. So I understand that the GC Design of Python integrates the advantages of the two types of GC algorithms to a certain extent, that is, to ensure the integrity of the collection, and strive to ensure the timeliness of collection.

Update

The above description is biased. I will post the supplement of R in the comment:
Reference counts such as List, Map, and Set also work. Mark-sweep is just a backup.

The entire reference counting mechanism is embedded in ceval. c, and all objects will be pushed to it.
In other words, the reference count of a List is not counted by reference, but is released according to the reference counting mechanism if it is reduced to zero. Otherwise, it will be processed when the cycle GC starts.
However, it is still true to improve the timeliness of collection through reference counting. This is a design trade-off. Everyone who uses CPython is happy.
There are many other Python implementations that do not need to reference and count, so you may not be happy to use them (Escape

In addition, the reference count of CPython is backed up by mark-sweep, which is not afraid of circular reference.
The official website explained this option (but didn't actually say anything ...) : Https://docs.python.org/2/faq/design.html#how-does-python-manage-memory
The details of Python memory management depend on the implementation. The standard C implementation of Python uses reference counting to detect your objects, and another mechanic to collect reference cycles, periodically executing a cycle detection algorithm which looks for your cycles and deletes the objects procedure.The gc module provides functions to perform a garbage collection, obtain debugging statistics, and tune the collector's parameters.

In the absence of circularities and tracebacks, Python programs do not need to manage memory explicitly.

Why doesn't Python use a more traditional garbage collection scheme? For one thing, this is not a C standard feature and hence it's not portable. (Yes, we know about the Boehm GC library. it has bits of cipher er code for most common platforms, not for all of them, and although it is mostly transparent, it isn' t completely transparent; patches are required to get Python to work with it .)
...
Traditional GC also becomes a problem when Python is embedded into other applications. while in a standalone Python it's fine to replace the standard malloc () and free () with versions provided by the GC library, an application embedding Python may want to have its own substitute for malloc () and free (), and may not want Python's. right now, Python works with anything that implements malloc () and free () properly.

Then read this article to introduce the newer Python "mark-swep GC" is actually still "generational": http://patshaughnessy.net/2013/10/30/generational-gc-in-python-and-ruby

Then JavaScript does not necessarily use mark-sweep... There is no such rule in terms of language specifications, and mark-sweep is not used in actual implementation. Python still uses reference counting because a long time ago, Python patched the reference counting.

Garbage Collection for Python First, I will give a simple and positive answer to the subject. Why should I use python?

If the subject wants to ask whether it is a better choice to use the "better" mark to clear the question, you can say a little more.

First, this premise is not true. from an academic point of view, you can read the book garbage collection, which uses "no silver bullet" to describe the choice of algorithms, that is, no one is better than anyone else, only who is more suitable for a specific scenario
The biggest benefit of the reference count is the real-time performance. The second is to avoid excessive over-allocation without circular reference, so the memory will not be wasted.
How important is this real-time performance? I guess in some scenarios, it is much more important than most people think. For details, refer to my blog articles, this article is written when talking about the disadvantages of Other gc algorithms that will stop world:
Avalanche effect-xtlisk Column
(The article is a series. If you are interested in language theory, you can talk about it more :)
Furthermore, is loop reference really a huge defect? In fact, this is a constant debate. Some people use statistics to prove that the vast majority of code and objects do not produce circular references. Just use code to circumvent them, however, it is also said that there will be a lot of relationships in a specific scenario (such as the relationship between roles, equipment, and buff in a game). It is generally considered to be a huge defect, but it is not unsolved.

Next, I asked why I checked it out first. The py Standard Edition does mainly use reference counting, but other implementations are not like this. For example, Jython directly depends on the jvm. In addition, the standard version does not ignore the cycle reference defect. The gc module is introduced in 2.5, and the local mark is used for cleaning the cycle reference. Note that the local mark cleaning is not normal mark cleaning, this idea is the same, but there are still some special points. For details, refer to the garbage collection book.

In fact, there is a defect in the use of ref count in py. To a certain extent, it may even result in a mechanism like GIL. Many people complain about GIL, however, it is seldom noticed that Mark clearing is not as popular as the GC selection when Python is just written. Java also comes from reference counting. ObjectiveC still uses reference count.

Most policies always have two sides. For example, you should also look at the advantages of reference counting:
  • If programmers can achieve non-circular reference, the efficiency is still very high and the VM will not be shut down.
  • Again, why is reference count used? It's easy. You can write one as soon as you hear it. Easy to understand. I can understand it in just a few minutes. You can clear the comparison mark
When Python is large enough to start worrying about reference counting like Java, the tragedy will happen. It seems that nothing can be changed. I believe that many people are still working to remove the reference count in Python.

Balabara's nagging
Python memory management compared with other language runtime When you debug a Java program, a jvm gc is triggered when the current state is suspended. Don't you feel stuck? Why does JavaScript need to clear the reference count like Python with a flag with obvious defects? Why does JavaScript need to clear the reference count like Python with a flag with obvious defects?
Mark clearing has a significant defect in poor performance. Why does JavaScript still need to use Mark clearing instead of reference counting? Reference counting facilitates third-party expansion modules to manage the memory of cpython. Third-party expansion modules can easily save cpython pointers without worrying that the memory pointed to by this pointer has been released.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.