Does Python VM cache the integer objects which cannot be released automatically?
<p>I have a single script as below:</p>

<pre><code>a = 999999999999999999999999999999
b = 999999999999999999999999999999
print(a is b)
</code></pre>

<p>Output is:</p>

<pre><code>[root@centos7-sim04 python]# python test2.py
True
</code></pre>

<p>On the other hand, the same code with command line:</p>

<pre><code>&gt;&gt;&gt; a = 999999999999999999999999999999
&gt;&gt;&gt; b = 999999999999999999999999999999
&gt;&gt;&gt; print(a is b)
False
</code></pre>

<p>The output is <code>False</code>.</p>

<ul>
<li>What is the difference between the 2 ways?</li>
<li>When python script running, how does Python VM to manage the integer objects?</li>
<li>I see that the number from -5 to 256 is generated by VM automatically when VM started and VM will also assign the empty int blocks(chain struct) to avoid allocating memory frequently for large number storage.</li>
<li>Will these blocks be released automatically by python VM when memory is not enough? For my understanding, Python just keeps these blocks to avoid allocating memory frequently so that them will never be released automatically once allocated?</li>
</ul>

<p>Just test with following code:</p>

<pre><code>for i in range(1, 100000000):
    pass
print("OK")
gc.collect()
time.sleep(20)
print("slept")
for i in range(1, 100000000)
    pass
</code></pre>

<p>The memory is:</p>

<pre><code>PID   USER      PR   NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
17351 root      20   0    3312060 3.039g 2096 S  11.3 82.4   0:03.53 python
</code></pre>

<p>Here is the result of <code>vmstat</code>:</p>

<pre><code>[root@centos7-sim04 ~]# vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
1  0      0 3376524    40 330084    0    0     2     5   25   41  0  0 100 0  0
1  0      0 185644     40 330084    0    0     0     0  714   28 14  3 82  1  0
0  0      0 967420     40 330084    0    0     0     0  292   15  7  0 93  0  0
0  0      0 967296     40 330084    0    0     0     0   20   23  0  0 100 0  0
0  0      0 967296     40 330084    0    0     0     0   15   17  0  0 100 0  0
0  0      0 967312     40 330068    0    0     0     1   27   39  0  0 100 0  0
1  0      0 185288     40 330068    0    0     0     2  701   55 17  0 83  0  0
0  0      0 3375780    40 330068    0    0     0     0  202   75  3  1 96  0  0
</code></pre>

<ul>
<li>It seems that the memory is never released. Is this right?</li>
<li>If I want to release the integer objects when memory is not enough, how can I do?</li>
</ul>

<p>Thanks a lot.</p>

<p>Regarding your first question actually that because of peephole optimizer which simplifies the expressions. Which in this case it means using one integer for all equal ones. It also use this approach for interning the strings.</p>

<p>The reason that you don't see such behavior within the interactive shell is that every command executed separately and gives the corresponding result, whereas in a file or in a function (even in terminal) all the commands interpreted at once. Here is an example:</p>

<pre><code>In [1]: def func():
   ...:     a = 9999999999999
   ...:     b = 9999999999999
   ...:     return a is b
   ...: 

In [2]: func()
Out[2]: True

In [3]: a = 9999999999999

In [4]: b = 9999999999999

In [5]: a is b
Out[5]: False
</code></pre>

<p>And regarding your second question, there are plenty of misunderstanding, first off, the duty of VM in python is executing the machine code corresponding to each bytecode, while managing the integers and actually parsing the code and compiling to the bytecode is the interpreter and compiler's task. And as it mentioned as comment in your question, <code>range()</code> in python 2 returns a list while in python 3 it's a smart object that preserves the start, end and step, and is a generator like object which generated the items on demand.</p>

<p>Also about the functionality of <code>gc.collect</code>, as mentioned in <a href="https://docs.python.org/3/library/gc.html#gc.collect" rel="nofollow">documentation</a> when you don't pass an argument to it, <code>gc.collect</code> run a full collection, and:</p>

<blockquote>
  <p>The free lists maintained for a number of built-in types are cleared
  whenever a full collection or collection of the highest generation (2)
  is run. Not all items in some free lists may be freed due to the
  particular implementation, in particular float</p>
</blockquote>

python integer