How is memory allocated for variables in scripting languages?

For example, in javascript

I can say

var x = 5; 

I can do later

 x = 'a'; 

and then

 x = "hello"; 

So how is memory allocated for variables? Be that as it may, all variables are of the common type "var", and the values โ€‹โ€‹of the variables can change at run time, as shown above. Isn't it difficult to allocate and manage memory for these variables? Exactly how is this done?

+6
javascript variables memory-management scripting
source share
3 answers

Python uses a reference counting method, which basically puts a counter in a value. Each time a value reference is created, the counter increments. When a reference to a value is lost (for example, when you assign a new value to "x"), the value decreases. When the counter reaches zero, it means that there is no reference to this value, and it can be freed. This is a simplified explanation, but at least the basics.

+3
source share
+1
source share

Well, these variables are references to immutable lines that are allocated at compile time.

Of course, it depends on the VM, but in general, I think most C scripting languages โ€‹โ€‹allocate a large block of memory, expanding it as necessary and making its own distribution inside it, rarely if ever give O / S. Especially in the lexically limited language, which almost all of them, variables are distributed dynamically inside this block, and not on anything similar to the C stack, and they are freed up either with reference counting or with a garbage collector.

If your scripting language is running on the JVM or .NET or something like this (Parrot?), Creating a variable is just creating something like a Java object. Some time after there are no more references to the object, the garbage collector will return the memory.

0
source share

All Articles