Variables Memory Allocation and Interning

In this class, we discuss variables memory allocation and interning in python.

For Complete YouTube Video: Click Here

Variable: is a container for storing data.

Example: x=5

x is a variable holding the data value five.

y=2.8

z=”mahesh”

First, understand how memory is allocated.

The image for memory is provided below.

Variables Memory Allocation and Interning
Memory Visualization

From the image, each block is considered a memory location. and each block is given a address starting from 0 to N.

We are having N memory locations.

Assume x =10, and y =10.

First, we understand how memory is allocated to variables in c language and in java language.

In the c and java language x and y are both having the same value, ie value 10.

x is given a separate memory location let’s say memory location 2. means x is referencing memory location 2.

y is given a separate memory location let’s say memory location 5. means y is referencing memory location 5.

But this is not the case in python. Python uses the concept of interning.

The interning technique is applicable only to few data. ie from -5 to 256 in integers, and simple strings.

Let’s take the above example and understand the concept of interning.

x=10 and y =10

Both having the same value so place value 10 in a memory location and both the variables refer to the same memory location.

Take one more example in strings. x = “hello” and y = “hello”

Here both the values are the same so why use a separate space. save hello in a memory location let’s say in the 10th location and both variables refer to the same location.

As this is our first class we are talking in terms of a memory location. But in reality, both variables refer to the same object.

In this situation, we don’t know what’s object is, so we are talking in terms of memory. we get clarity in our next classes where we discuss class and object.

Let’s understand one more example to have a deeper understanding of the interning concept.

x=10 and y = 10.

Both having the same value, so it is given the same memory. assume it is given location 2.

id(x) and id(y) give us the memory address of variables x and y. both giving the same location.

Let’s take x=x+1. Now the value of x changes to 11.

At this stage both having different values. They are given different locations.

Now take a variable z=10. We already had a value of y=10. so z is assigned to the same memory location y is referring to.

Next, take one more example x= 257 and y= 257. these values are above the limit mentioned above in interning.

The variables x and y are assigned a different memory location. ie only to those values in the above limit interning works.

Take the example in strings also. x = “hello world” and y =” hello world”.

These are not simple strings. so these variables are assigned with a different location.

We can forcefully apply the concept of interning by using the function called an intern.

x=sys.intern(“hello world”)

y=sys.intern(“hello world”)

In the above example, both the variables are assigned the same memory location.

Why they are using the interning concept in the above-mentioned limit. because these values are used frequently. In order to save memory and increase speed.

Example to understand the use of the interning concept.

Take the amazon mobile data set. In this data set, we are having lots of sentences. suppose if someone asks you to identify the number of time the word mobile occur in the entire data set?

What we do. we split the words and find how many times the word mobile occurs in the data set. if we save the word mobile in one location and give reference we are saving so much memory.

This is how interning is helping in improving memory usage and for fast execution.