Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 533
Meteor Avatar asked Mar 08 '23 13:03

Meteor


1 Answers

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.

like image 101
Oyster773 Avatar answered Mar 20 '23 06:03

Oyster773