Everything is a object in Python.
Python is a dynamically typed language. So there is no need to specify the type.
- heap
- the objects and instance variables are created on heap
- when an onject in heap is not reference anymore,
it is removed immediately.
The algorithm used for
Garbage Collection
is calledReference Counting
.
- stack
- the methods and variables are created on stack.
- store reference to object on heap
- a new stack frame is created on invocation of a funcition/method.
- stack
- method invocations
- local variables: variables declared inside a method
- primitive variable: integers, decimals or characters
- reference to non-primitive variable
- heap (garbagr-collectable heap)
- all objects live here.
- garbage collector
- generations:
- young generation
- minor garbage collection: young generations, 'stop the world event' - all thread stop and wait
- old generation
- major garbage collection: 'Stop the World' event
- permanent generation
- young generation
- generations:
Variables stored in stacks are only visible to the owner Thread, while objects created in heap are visible to all thread.
Operation on stack is one CPU instruction, which is easy and efficient.
Operation on heap is a bunch of things.