# Can Python use ARC and eliminate GC

**URL:** <https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467>\
**Category:** Python Help\
**Created:** [March 19, 2022, 7:21pm UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467 "2022-03-19T19:21:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljh](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@ljh](https://discuss.python.org/u/ljh)\
**Post date:** [March 19, 2022, 7:21pm UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467/1 "2022-03-19T19:21:51Z")

</div>

Hi forum,

I’m wondering why python uses both reference counting and garbage collection.

Objective-C and Swift use only [Automatic Reference Counting (ARC)](https://clang.llvm.org/docs/AutomaticReferenceCounting.html) to manage memory usage. Can Python use ARC too and eliminate Garbage Collection?

---

<div class="post-metadata">

**Author:** ![tim.one](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tim.one/32/282_2.png) [@tim.one](https://discuss.python.org/u/tim.one)\
**Post date:** [March 19, 2022, 8:24pm UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467/2 "2022-03-19T20:24:12Z")

</div>

> [@ljh](#):
>
> Objective-C and Swift use only [Automatic Reference Counting (ARC)](https://clang.llvm.org/docs/AutomaticReferenceCounting.html) to manage memory usage. Can Python use ARC too and eliminate Garbage Collection?

No. CPython already uses reference counting for the vast bulk of its memory recycling. `gc` is needed in CPython _only_ to reclaim trash in cycles. ARC doesn’t address that at all. Here’s a line from the document you linked to:

> It [ARC] does not provide a cycle collector; users must explicitly manage the lifetime of their objects, breaking cycles manually or with weak or unsafe references.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 20, 2022, 12:15am UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467/3 "2022-03-20T00:15:19Z")

</div>

Why do you want to eliminate Python’s garbage collection?

Technically, reference counting **is** garbage collection, and  
CPython has two garbage collectors: the automatic reference counter, and  
a second gc system for eliminating garbage in cycles.

Other Python implementations use different systems:

- IronPython uses the .Net garbage collector, whatever that is;

- Jython uses the JVM garbage collector, and has no reference counter;

- PyPy allows you to choose different garbage collectors at build-time, when you compile the interpreter.

Older versions of CPython used only reference counting, and we needed to manage cycles manually. I would not want to go back to those Bad Old Days.

---

<div class="post-metadata">

**Author:** ![ljh](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@ljh](https://discuss.python.org/u/ljh)\
**Post date:** [March 20, 2022, 7:05am UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467/4 "2022-03-20T07:05:43Z")

</div>

Thanks Tim,

I think ARC does release memory and weak ref is used to stop cycle reference in Swift. I found that `weakref` mentioned in Python too: `cpython/Doc/tutorial/stdlib2.rst`.

---

<div class="post-metadata">

**Author:** ![ljh](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@ljh](https://discuss.python.org/u/ljh)\
**Post date:** [March 20, 2022, 7:11am UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467/5 "2022-03-20T07:11:37Z")

</div>

> [@steven.daprano](#):
>
> Older versions of CPython used only reference counting

Thanks Steven,

I’m new to Python and did’t know that.

> [@steven.daprano](#):
>
> and we needed to manage cycles manually

Do you mean people used weakref to stop cycle reference in their python script code before?

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 20, 2022, 2:14pm UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467/6 "2022-03-20T14:14:42Z")

</div>

No, this was before weak references existed.

Weak references were added in version 2.1.

[https://docs.python.org/2.7/library/weakref.html](https://docs.python.org/2.7/library/weakref.html)

The cyclic garbage collector was added in Python 2.0:

> **[What's New in Python 2.0](https://web.archive.org/web/20091214142515/http://www.amk.ca/python/2.0)**
>
> What's New in Python 2.0

so anyone who used Python 1.x had to manage cycles manually.

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [March 20, 2022, 8:48pm UTC](https://discuss.python.org/t/can-python-use-arc-and-eliminate-gc/14467/7 "2022-03-20T20:48:36Z")

</div>

For what it’s worth, you can do

```auto
import gc
gc.disable()

```

to disable the garbage collector. Then you only have reference counting. That’s mentioned in the `gc` module docs:

“Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles.”

[https://docs.python.org/3/library/gc.html](https://docs.python.org/3/library/gc.html)

The drawback is that cycles are no longer collected. If you have a cyclic data structure, you need to either use weak references or “de-cyclicize” your structure when you no longer need it. I guess it could also prevent the interpreter from collecting some of its internal structures, which could contain cycles, though I don’t know about that too much.

So, Python without GC is conceptually possible. But it would make Python a vastly different language.
