How to create a memory leak in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.