4

I was talking with a friend, comparing languages, and he mentioned that Java's automated memory management is superior to Python's as Java's does compaction, while Python's does not - and hence for long-running servers, Python is a poor choice.

Without getting into which is better or worse, is his claim true - does CPython's garbage collector not compact memory and, thus, long-running Python processes get more and more fragmented over time?

I know that running CPython's garbage collector is optional. Mostly it uses automated reference counting for automated memory management, and as soon as a reference count hits zero, the object is freed - thus the only thing that CPython's garbage collector is needed for, in terms of freeing objects, is to detect cycles which no object in the root set has a reference to. But I don't know the details of whether it does any compaction in addition to that.

If it does not, then how do long-running CPython processes address the memory fragmentation issue?

Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • 1
    You may find the answers here helpful: [Releasing memory in Python](http://stackoverflow.com/q/15455048/4014959). – PM 2Ring Feb 10 '16 at 09:10

1 Answers1

5

I don't know for sure, but CPython uses reference counting and its objects use memory addresses as ids so I would say it does not do compaction... And according to this, "[C]Python does not use memory compaction ... [w]ould Python use memory compaction, implementing C extensions would be much more tedious and error prone and there would be less such extensions - limiting the domain where Python is used."

Claudiu
  • 224,032
  • 165
  • 485
  • 680
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117