Meteor
Meteor

Reputation: 41

Python memory free

I want to free some memory, for example, I define a variable:

b = 10
id(b)   # it shows 1935260400

Then I changed the value of b:

b = 11
id(b)  # it shows 1935260432

After that, I changed b again:

b = 10
id(b)  # it still shows 1935260400,why is it same with first time?

Here are the questions, b = 10 at first time, then b = 11 at second time,why is the id(b) at third time same with the first time? does the value 10 is still in the memory? How to free the memory that value 10 takes up?

Upvotes: 4

Views: 103

Answers (1)

Oyster773
Oyster773

Reputation: 375

In python documentation of plain integer objects this is explained. Take a look here. The references for values between -5 and 256 remain the same, so when you change a variable - it actually points to that reference.

If you go beyond that range, you can expect different behavior.

Upvotes: 7

Related Questions