Today, to visit the Java Concurrent Programming network, see a Translation Solicitation order, the author of the concurrent programming network from the StackOverflow website selected some classic questions and answers, so decided to translate a few
Solicitation Address: http://ifeve.com/stackoverflow-assembly/
Translation Series articles:
Some design patterns in the 1.Java Core class library
2. The difference between HashMap and Hashtable
3. How to create a memory leak in Java
How to create a memory leak in Java
question: I attended an interview before and was asked how to create a memory leak in Java. Needless to say, I didn't know what to tell you, how to create one, and I don't have a clue now.
Can you give me a demonstration of an example?
Answer:
There is a way to create a pure Java memory leak (objects in the running code are unreachable, but still reside in memory)
1. The app creates a long-running thread (or uses a thread pool, which makes memory leaks faster)
2. Threads load a class from the ClassLoader
3. This class allocates a large block of memory (for example, New byte[1000000]), points it to a static member variable by a strong reference, and then stores its own reference to the threadlocal. Allocating huge memory This step is optional (leaking instances of the class is sufficient, but doing so will make the leak happen faster)
4. Thread clears all references to custom classes or lets the ClassLoader reload
5. Repeat the above steps
Work first, translation to be continued ....
Original: Original address: Http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java
How to create a memory leak in Java