Does the new string value assign the garbage to be collected?

Consider this javascript code:

var s = "Some string"; s = "More string"; 

Will the garbage collector (GC) work after this kind of operation?

(I wonder if you should worry about assigning string literals when trying to minimize GC pauses.)

e: I'm a little surprised that although I explicitly stated in my question that I need to minimize GC, everyone thought that I was wrong about that. If you really need to know the specific details: I have a javascript game - it works fine in Chrome, but there are frequent pauses in Firefox that seem to be related to the GC. (I even checked with the MemChaser extension for Firefox, and the pauses match exactly with garbage collection.)

+8
performance javascript garbage-collection
source share
5 answers

Yes, strings must be garbage collected, like any other type of dynamically allocated object. And yes, this is a fair problem, because the careless distribution of objects within employment cycles can definitely cause performance problems.

However, string values ​​are immutable (unchangeable), and most modern JavaScript implementations use "string interning," meaning they store only one instance of each unique string value. That means if you have something like this ...

  var s1 = "abc", s2 = "abc"; 

... only one instance of "abc" will be highlighted. This applies only to string values, not to String objects.

A few things to keep in mind:

  • Functions like substring , slice , etc. will allocate a new object for each function call (if called with different parameters).

  • Although both variables point to the same data in memory, there are still two variables to process when the GC loop is executed. Too many local variables can also hurt you, as each of them will be handled by the GC, adding overhead.

Some further reading on writing high-performance JavaScript:

+8
source share

Yes, the garbage collector will have a string object containing "Some string" to get rid of it. And, answering your question, this line assignment will do the job for the GC.

Since strings are immutable and used a lot, the JS engine has a pretty efficient way to deal with them. You should not notice any pauses from the garbage collecting several lines. The garbage collector has work to do all the time in the normal course of javascript programming. This is how it should work.

If you observe pauses from the GC, I rather doubt it from a few lines. Most likely, a much bigger problem is occurring. Either you have thousands of objects that need a GC, or a very difficult task for the GC. We could not really reflect on this without studying the general code.

This should not cause concern if you are not making some huge loop and not dealing with tens of thousands of objects. In this case, you can program a little more carefully to minimize the number of intermediate objects that were created. But, if this level of objects is missing, you should first get clear, reliable code, and then optimize performance only when something has shown you that there is a performance problem that needs to be worried about.

0
source share

Yes, but if you do it in a loop millions of times, this is unlikely to be a factor you can worry about.

0
source share

As you already noticed, JavaScript is not JavaScript. It runs on different platforms and therefore will have different performance characteristics. Thus, a definite answer to the question "Will the GC work after this kind of operation?" possible: possible. If the script is as short as you showed it, then the JIT-Compiler may well completely remove the first line. But there is no rule in determining a language that says it should be anyway. Therefore, in the end, it seems like too often in JavaScript: you should try it.

A more interesting question may be: how can you avoid garbage collection. And this is an attempt to minimize the distribution of new objects. Games usually have a fairly constant number of objects, and often there will be no new objects until the old one is used. For strings this can be trickier as they are immutable in JS. So try replacing strings with other (mutable) views where possible.

0
source share

To answer your question, β€œI wonder if you should worry about assigning string literals when trying to minimize GC pauses”: None.

You really do not need to worry about this in relation to garbage collection.

GC is only a problem when creating and destroying a huge number of Javascript objects or a large number of DOM elements.

-2
source share

All Articles