I was asked recently, the following
Given the following interface, write a class that will leak memory every time Processor#doSomething(String) is called and explain why it does so.
public interface Processor {
void doSomething(String msg);
}
I am not sure there is memory leak in Java in the same sense as C/C++. What is the significance of an interface here.
Also, I have never seen a # used to call a method.
Can someone explain this to me? Thanks
1 Answer 1
A simple implementation would have a static list and when doSomething is called, just keep adding the input String to the list. The reason why is because you're storing a strong reference (via the static member) to the strings so the strings cannot be garbage collected.
#is just a notation. You don't see that in actual Java source code. stackoverflow.com/q/11247793/139010#is used in javadocs, not Java source code.