How can I create a memory leak in Java?

后端 未结 30 1765
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 22:26

I just had an interview, and I was asked to create a memory leak with Java.

Needless to say, I felt pretty dumb having no clue on how to eve

30条回答
  •  醉酒成梦
    2020-11-21 22:47

    The interviewer might have be looking for a circular reference solution:

        public static void main(String[] args) {
            while (true) {
                Element first = new Element();
                first.next = new Element();
                first.next.next = first;
            }
        }
    

    This is a classic problem with reference counting garbage collectors. You would then politely explain that JVMs use a much more sophisticated algorithm that doesn't have this limitation.

    -Wes Tarle

提交回复
热议问题