How can I create a memory leak in Java?

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

    I think that a valid example could be using ThreadLocal variables in an environment where threads are pooled.

    For instance, using ThreadLocal variables in Servlets to communicate with other web components, having the threads being created by the container and maintaining the idle ones in a pool. ThreadLocal variables, if not correctly cleaned up, will live there until, possibly, the same web component overwrites their values.

    Of course, once identified, the problem can be solved easily.

提交回复
热议问题