How can I create a memory leak in Java?

后端 未结 30 1764
没有蜡笔的小新
没有蜡笔的小新 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:46

    The answer depends entirely on what the interviewer thought they were asking.

    Is it possible in practice to make Java leak? Of course it is, and there are plenty of examples in the other answers.

    But there are multiple meta-questions that may have been being asked?

    • Is a theoretically "perfect" Java implementation vulnerable to leaks?
    • Does the candidate understand the difference between theory and reality?
    • Does the candidate understand how garbage collection works?
    • Or how garbage collection is supposed to work in an ideal case?
    • Do they know they can call other languages through native interfaces?
    • Do they know to leak memory in those other languages?
    • Does the candidate even know what memory management is, and what is going on behind the scene in Java?

    I'm reading your meta-question as "What's an answer I could have used in this interview situation". And hence, I'm going to focus on interview skills instead of Java. I believe you're more likely to repeat the situation of not knowing the answer to a question in an interview than you are to be in a place of needing to know how to make Java leak. So, hopefully, this will help.

    One of the most important skills you can develop for interviewing is learning to actively listen to the questions and working with the interviewer to extract their intent. Not only does this let you answer their question the way they want, but also shows that you have some vital communication skills. And when it comes down to a choice between many equally talented developers, I'll hire the one who listens, thinks, and understands before they respond every time.

提交回复
热议问题