Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does assigning a new string value create garbage that needs collecting?

Consider this javascript code:

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

Will the garbage collector (GC) have work to do after this sort of operation?

(I'm wondering whether I should worry about assigning string literals when trying to minimize GC pauses.)

e: I'm slightly amused that, although I stated explicitly in my question that I needed to minimize GC, everyone assumed I'm wrong about that. If one really must know the particular details: I've got a game in javascript -- it runs fine in Chrome, but in Firefox has semi-frequent pauses, that seem to be due to GC. (I've even checked with the MemChaser extension for Firefox, and the pauses coincide exactly with garbage collection.)

like image 426
starwed Avatar asked Nov 29 '12 00:11

starwed


People also ask

Do strings get garbage collected?

In practice, the String objects that correspond to string literals typically do not become candidates for garbage collection. This is because there is an implicit reference to the String object in the code of every method that uses the literal.

Is string constant pool eligible for garbage collection?

Before Java 7, the JVM placed the Java String Pool in the PermGen space, which has a fixed size — it can't be expanded at runtime and is not eligible for garbage collection.

Are Value types garbage collected?

They are not collected because they are the things that are alive that keep almost everything else alive. And obviously they are not deallocated by compacting the GC heap; they're deallocated by popping the stack.

How can you make sure an object is garbage collected?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.


1 Answers

Yes, strings need to be garbage-collected, just like any other type of dynamically allocated object. And yes, this is a valid concern as careless allocation of objects inside busy loops can definitely cause performance issues.

However, string values are immutable (non-changable), and most modern JavaScript implementations use "string interning", that is they store only one instance of each unique string value. This means that if you have something like this...

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

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

A couple of things to keep in mind:

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

  2. Even though both variable point to the same data in memory, there are still two variables to process when the GC cycle runs. Having too many local variables can also hurt you as each of them will need to be processed by the GC, adding overhead.

Some further reading on writing high-performance JavaScript:

  • https://developer.mozilla.org/en-US/docs/JavaScript/Memory_Management
  • https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript
  • http://jonraasch.com/blog/10-javascript-performance-boosting-tips-from-nicholas-zakas
like image 53
alekop Avatar answered Oct 11 '22 02:10

alekop