Why a memory leak in a loop and why this happens?

const array = [];
const step = 0.1;
do {
 const item = array[array.length - 1] ? array[array.length - 1] : 0;
 array.push(item + step);
} while (+array[array.length - 1] !== 100);
April 7th 20 at 15:34
2 answers
April 7th 20 at 15:36
Solution
Memory allocated in the operator array.push(item + step); - this is because You wrote it. I don't know how else to explain it. And I don't know why You do it.

You rightly pointed out that the termination condition of the loop while (+array[array.length - 1] !== 100); can not be executed, and in any language on any platform (but it can run). In this case (JS) it is not satisfied (for any instrument, the JS is the standard to work with float numbers).

I suggest to replace the condition "not equals" to "strictly less than" - it seems to me, you get what you need.
Or you can run the for loop counter, calculating in advance the number of repetitions.

And another tip: If the loop behaves incorrectly, then insert inside the debug print modified in this variables - for example, array.length and item
@Loren the Problem was that: "I have the number 0 and an empty array. Fill the array from 0 to 100, where the step should be 0.1". Something like this... actually [0, 0.1, 0.2, 0.3 ...... 100] and when 100 out of the cycle.... And as it turns out [0, 0.1, 0.2, 0.300000543251...] we have infiniti iteration which gradually vyzhiraet memory :). I was just interested in the answer to the question why poluchetsya 0.2 +0.1 = 0.3000004325235.... How to deal with it.. I know the number to a certain floating, but the fact that it fundamentally confuses the concept matimatiki - it scares me. You answered correctly - in other languages it too is because they use all the same "PC" :) which is so arranged :) - eloy_Schulist55 commented on April 7th 20 at 15:39
@Kelton_Ku, Well, for this task, we write:
for (i=0; i<=100/step+1/16; i++) {
array[i]=i*step;
}
Check.

In General, the problem of float numbers is not only on PC but also on mainframes and game consoles. It is fundamental. - mohammad.Price commented on April 7th 20 at 15:42
April 7th 20 at 15:38
There are no memory leaks. It's an endless cycle, which quite naturally falls when vyzhiraet all the memory. Why the cycle of endless Google about the addition of floating point numbers.
It is clear. My question is 0.7 + 0.1 = 0.71, and 0.71 + 0.1 = 0.72 . If I immediately googled, I did not ask the question :) - eloy_Schulist55 commented on April 7th 20 at 15:41
@Kelton_Ku, here in the console, these operations of addition. There exactly as you wrote? I have other numbers to work.

.7 + .1 === 0.7999999999999999
.71 + .1 === 0.8099999999999999
- nikita.Mraz commented on April 7th 20 at 15:44
@Kelton_Ku, you write "it is clear"

But just above you write
the leak is, if it was not Nabi lo would be a heap of memory


Think it completely contradicts the statement "it is clear" - nikita.Mraz commented on April 7th 20 at 15:47
@Paolo97, okay the question was what
0.1 + 0.1 + 0.1 != 0.3
// true

that's why it became a memory leak, because there began to fall 1/9999999999999999 - eloy_Schulist55 commented on April 7th 20 at 15:50
@Kelton_Kuagain — THERE is NO memory leak. Your script went into an infinite loop with infinite memory consumption. Because the physical memory is not infinite, the script was stopped with an error "has run out of available memory". It's not a memory leak. It's an endless cycle. It arose from the classic error of adding the numbers floating point.

Due to the fact that you are using strict comparison with an integer. The value in your cycle will never be equal to a steward, it'll always be either less or more. - nikita.Mraz commented on April 7th 20 at 15:53
What are you stuck? I you one you I more. If your understanding of the endless cycle of not leaking be great., then better to change your work, and better don't answer :))))) what you write, I do not care, I am interested in the result, and the fact that you are trying to prove, I'm writing this post already understood. I don't know how you even explain... @here guys tell the person what I'm asking... he tries to explain to me that there is an infinite loop. Yes, there is a loop! Yay! Salute! Can think why is there a loop? or again let us turn to the discussion of the loop? - eloy_Schulist55 commented on April 7th 20 at 15:56
@Kelton_Ku, read carefully. Why is there a loop?
due to the fact that you are using strict comparison with an integer. The value in your cycle will never be equal to a steward, it'll always be either less or more.


Why?
It arose from the classic error of adding the numbers floating point.


Is there a memory leak? No. Because the memory leak is a particular term for a situation where the garbage collector does not clean up unused sections of memory. In your example there are unused sections of memory? No. - nikita.Mraz commented on April 7th 20 at 15:59
@Kelton_Ku,
If your understanding of the endless cycle of not leaking be great., then better to change your work, and better don't answer :)))))


Hmm... I think I'll listen to the professional advice of a man who does not understand what "memory leak"

:))))))) - nikita.Mraz commented on April 7th 20 at 16:02
@Paolo97, I unobvious, the term "unused sections of memory." Like, fill in the cycle array is not used. - mohammad.Price commented on April 7th 20 at 16:05
@Loren, it is populated in him constantly going on the record, it is used to store values.

Yesterday I not absolutely correctly expressed: a memory leak is a process of uncontrollable reduction of the amount of available memory. Usually in js this occurs when the garbage collector cleans not for the developer. For example, you subscribe to an event and forget to unsubscribe at the right time and it happens many, many times — the number of event handlers grows with time, storage of each processor with its lexical environment need memory, at some point the script will eat up all the memory, but it will not be soon.

In contrast to the situation when the memory Viiratsi for a second or two, when you leak the memory consumption is memory itself grows slowly (or quickly) for hours or days. So you came to work, started you slack, began to enjoy, he eats 200 MB of memory. 8 hours later you leave work, and it takes 7.9 GB of memory. And everything slows down and angry. This is a leak, Yes. - nikita.Mraz commented on April 7th 20 at 16:08

Find more questions by tags JavaScript