3 ms·
Actually, Java strings work exactly like that. And if you use them correctly, string processing is no problem at all on a modern VM using a generational GC.
by brazzy 14y ago
Actually, Java strings work exactly like that. And if you use them correctly, string processing is no problem at all on a modern VM using a generational GC.
- chubot 14y agoAh OK I didn't know that (not a Java programmer). What happens if you create a huge 10MB string, then a 10 byte substring of it, and then remove all references to the 10MB string. Does that memory ever get reclaimed? I guess the difference in Go is that the string abstraction and the slice abstraction is exposed to the programmer, so it will be apparent if that happens. There is some info here but I was unable to find a good explanation: http://stackoverflow.com/questions/2909848/how-does-java-implement-flyweight-pattern-for-string-under-the-hood http://stackoverflow.com/questions/2909848/how-does-java-imp...
- brazzy 14y agoNope, that's one way to have a memory leak. But you can avoid the problem by using the new String(oldString) constructor - it copies only the part of the underlying char array that the String uses. You're absolutely right that it would be much better if all that were part of the API rather than an implementation detail. The SO question and answers are confusing because they talk about two competely separate issues: the above and the interning mechanism.