String constant memory leak?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

String constant memory leak?

kkingdon
The suggested method for implementing string constants is to call String.new(length_of_string) and to call String.appendChar(c) for each character in the string. However, no mention is made of calling String.dispose() for the constant when it goes out of scope. If the string constant is not disposed, won't the VM eventually run out of heap space if the method containing the constant is called repeatedly? Any suggestions on how to track the constant until it goes out of scope? How should the scope be defined for this purpose? For example, if the constant is assigned to a String variable, the 'dispose responsibility' is transferred to that variable. One could imagine requiring an explicit call to 'variable.dispose()' in this case. On the other hand, if the string is used as a parameter, e.g., to Output.printString(), perhaps the string should automatically be disposed after the call to Output.printString() returns.
Reply | Threaded
Open this post in threaded view
|

Re: String constant memory leak?

Noam
You are right.  The simple handling of strings does leak memory. Handling this properly does require some effort though.  The easiest fix I can think of is moving all String allocations to an "initialization block of the class.