Does the memory consumption of a variable increase each time it is used?

Asked

Viewed 1,271 times

6

I have been trying to understand more about the memory consumption of the created variables. And I even did some tests here on my computer with variables! I then created some variables and used it several times. I noticed that the memory consumption - measured in bytes - had no change.

By creating a $variavel the use of memory related to it is accounted for only at the time of creation - considering then that a small amount of memory was allocated for this variable, so the variables identical to it will come from the previously allocated memory- or each time it is allocated a little more memory?

It is worth noting that my doubt here is not the amount of memory that the variable uses, but whether it uses memory each time it is used or whether it uses a little memory only at the time of creation, making the following reuses of the same variable a "reflection" of previously allocated memory.

  • 2

    This is extremely relative to the use of the code, depending on the case, you can create several variables that do several operations, if they do not have a purpose (a writing of any nature) they want to initialized, the copier has several optimization routines, if you want to understand how memory allocation works by variables see how it works in C, it’s quite intuitive.

2 answers

5


It’s not easy to measure

In PHP memory consumption is not so easy to track. It is not simple to monitor consumption. You didn’t report how you did it but probably used some "naive" method that doesn’t consider the whole operation (memory_get_usage() and other functions of the genre that give unreliable information (look what the operating system says is even worse). I could talk more about the tests done if they had been posted.

I don’t know how to give details but most likely PHP already begins allocating certain spaces to use the script. Allocating is different from storing some value. So it may be that "creating" variables at first does not affect at all the memory consumption already allocated initially. Can’t make a direct consumption assessment easily.

Anyway a variable is just a name to access a memory position. The memory consumption of that position is accounted for as long as it has relevant data referenced by that variable. Even if the variable ceases to exist. There are variables that own the content pointed/contained by it, but not always.

Various forms of allocation

As far as I know does not occur with PHP in most situations but it is possible a variable generate several different allocations that remain alive even if there is no more utility of previous different versions.

Some variables said "by value" have a consumption in one place only, others said "by reference" have a location that points to another place where its content really is. And obviously your content can be another note. Take this into account.

You talk about "using" the variables. Depending on what it is, this "using" will make no difference. Taking its value obviously does not affect allocation. Changing its value can affect whether the variable is considered immutable or at least has a new value that occupies more space than previously allocated, but this may not be true and use the same location.

Then trade 1 for 2 in a variable will not affect anything the memory consumption. Already change a string 20 characters per 200 will affect. It seems obvious to me, right?

Already reduce the string 200 to 20 characters can release some memory immediately but it is more likely that this is not done, you may need to use it again soon afterwards.

When you increase the size of string in the example above PHP could make a new allocation for the 180 characters and take advantage of the 20 already allocated. But it is likely that there will be an extra allocation beyond what is necessary to avoid further future relocations. There are several optimizations that can be applied, in most cases this is not important for the programmer. Are you seeing how you are having cases that make it difficult to follow the actual consumption?

The most common is that PHP reuses as much as it gives when a value changes. It is not characteristic of the language to make unnecessary relocations.

But don’t confuse variable with its value. And do not confuse the real value of the variable with the object pointed by the variable in cases where the variable keeps a reference to the object.

Every time you throw a value from one variable to another variable, even if it is by passing parameters you are copying the value from one variable to another. Since you have another variable, of course there is a new allocation. But these variables may be an area called stack, and everything is already allocated.

But note that the more complex types, by reference, this value is only a pointer represented by a memory address. And the real data that matters is in another object pointed by that reference that is copied. There is no copy of the complex object, examples are the array or a string. Since there is a copy of the reference then there can be more than one variable holding references to the same object. Therefore the object cannot be released while there is a reference to it. The fact of releasing a variable does not mean that the object pointed by it will be released.

Object copy occurs in rarer cases.

Completion

I don’t really know what the current implementation of PHP is like (this is a detail that can change for a number of reasons) and I don’t quite remember what it was like in the past when I researched.

There is little information about it and few people are interested. Do you know why? Because in PHP this is not usually relevant. If memory consumption is relevant to your application you are probably using the wrong language. I’m not saying I shouldn’t be curious and research on the subject, only that for normal use this concern is not necessary.

Have you also noticed how difficult it is to accurately assess memory consumption? Nor are there tools that help much since it is not usually relevant to measure this in this language.

Not totally related to PHP because it works a little differently but know the stack and the heap helps understand memory consumption.

3

The answer to your question is nay, if you define a variable it allocates a space in memory, this space will be used until the famous Garbage Collection find it necessary to free up space.

Remembering that you can use unset() to free up space allocated

In php you can point two variables to the same reference in this way

<?php
    $var;
    $a = 2;
    $a =& $var;
?>

Any modification in $a or $b causes mutual modification, see illustration inserir a descrição da imagem aqui

See this article on variables in PHP

I believe your doubt is the result of a confusion between memory X processor

How memory works (basic)

Memory is basically a set of allocated and addressed spaces, any variable is pointed to one of these spaces, but you should take into account that php does not work directly in memory, as well as any program running on any linux/windows/etc operating system, because they implement a virtual memory technique. what can be spent at each access memory would be processing and not memory, to understand this it is necessary to understand how the processor works

How the processor works (basic)

Abstracting to the maximum the processor, we can say that it moves blocks using rules (commands) for this purpose, any read or write operation goes through the processor to be performed, save some data entry operations that are controlled by the controller. summarizing for each access variable the processor takes the address of this variable and plays its contents in some register.

In PHP it is different

We must remember that the responsible for memory management, address allocation or whatever operations with the system are done by the interpreter, so you do not have this control, you do not know how many accesses will be made or the cache engine implemented by it, then it gets pretty real active.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.