String/variable definition and subsequent re-initialization

Here is an example from FSBrowser that comes along with the ESP8266 library:

  server.on("/all", HTTP_GET, []() {
    String json = "{";
    json += "\"heap\":" + String(ESP.getFreeHeap());
    json += ", \"analog\":" + String(analogRead(A0));
    json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
    json += "}";
    server.send(200, "text/json", json);
    json = String();
  });

I’m not so interesting in what it is doing (I know that), but more the code structure itself.

First it defines a string String json ="something"; then it does json = String();

Does this have to do with freeing memory or something?

If so, in which cases is it necessary to do this?

It explicitly deallocate the memory by resizing (to smaller) the "json".
In this case, it is not necessary (even harmful) since "json" goes out of scope anyway.

You should do this when "json" is global scope.

arduino_new:
It explicitly deallocate the memory by resizing (to smaller) the "json".
In this case, it is not necessary (even harmful) since "json" goes out of scope anyway.

You should do this when "json" is global scope.

That's pretty much what I thought but wasn't sure if C was different. Thank you.