1

I have a method in a class where I create a string like this:

private void log(HttpServletRequest request, HttpServletResponse response) {
    String result = "Following request " + request.getRequestURI() + " yielded " + response.getStatus();
    log.info(result);
}

My question is, result string is generated millions of times. Does it have impact on heap memory?

My current understanding is that each method has its own heap memory allocation. After method finishes executing, all local variables vanish from memory.

My other understanding is that Strings are stored in global heap memory, and they get cleaned up on next Garbage collection cycle.

Can someone please put some light on this I shall be thankful.

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • Java is free to do whatever it wants with memory, do never rely on it. Java will _eventually_ clean up unused instances, including a local string only known in the method. Note that you pass it to `log` though (but I guess that soon releases the string, after logging it). – Zabuzard Nov 28 '19 at 23:27
  • There isn't really much you can do here anyways (if you want to keep the log, that is). Java already converts the `+` calls to `StringBuilder` usages (or even better since Java 9), so that part is good already. And you can not compute the string at compile time since the URI and response status change each time. The only thing you could optimize is to get rid of the temporary variable `result`. Instead, create the string directly in the `log.info(...)` call. But that is about it. – Zabuzard Nov 28 '19 at 23:30
  • The JIT compiler should already optimise away the local variable for you, so even that is unnecessary. – kaya3 Nov 28 '19 at 23:41

2 Answers2

3

Every time this method executes, it will create a String object in heap memory, and assign a local reference to it on the stack. This local reference will be passed to log.info(), and assuming that method doesn't save the result anywhere, once it returns, that object in the heap will be eligible for garbage collection.

As long as your result doesn't have permanent references to it, it shouldn't have a permanent effect on your programs memory.

jacob13smith
  • 919
  • 5
  • 12
1

Since the string result is only used within the method, and no reference is retained to that string after the method returns, the string is garbage, meaning the memory allocated for it can be freed and subsequently used for something else.

Java is a garbage-collected language, so this happens automatically at runtime without you having to write any code to explicitly free the memory. This could happen at any time; the string object's memory could be freed immediately, or it could be freed once every hundred times the method is called, or it could be freed randomly, or perhaps never until the program ends. All of these behaviours are possible according to the specification.

Note that the string is not guaranteed to be allocated on the heap. Java implementations are permitted to allocate objects on the stack instead, so long as an escape analysis shows that the object will not be referenced once the method returns. This is in fact done in at least some implementations; see this other question and this one for a discussion. If the string is allocated on the stack then it is effectively garbage-collected immediately.

However, whichever of these your compiler or interpreter does, it doesn't make any difference to the behaviour of the program. These optimisations are only done because they don't affect program behaviour.

kaya3
  • 47,440
  • 4
  • 68
  • 97