Under what circumstances will Java overflow in memory? __java

Source: Internet
Author: User

Personally feel that, because of the GC, most of the cases will not appear. However, if some resources are not shut down (for example, I/O files and database connections are not closed), it is possible that out of memory occurs.

Above, just personal experience to make the conclusion.

Here's an excerpt from: http://calvin.javaeye.com/blog/91903, a summary of Java memory leaks

Memory leaks

Isn't there a garbage collector in Java? How it still leaks.
Well, this leak is not more than leaking. C + + leakage, is the object is not reachable, and memory is not recycled, the real memory black hole.
The Java leak, for a variety of reasons, objects to the application has been useless, but has been held, can always be reached.
There are several reasons why:

1 is improperly held by a very long lifecycle collection class, claiming to be the first cause of Java memory leaks.
The life cycle of these collection classes is usually extremely long and is an object of a secondary management nature, after a business transaction has run out, if a business object master

This collection will eat more and more memory when the move is removed from it.

2 scope definition is not correct, this is very simple, the method of local variables defined as class variables, class static variables.
3 exception without finally{} to release some resources, the JDBC era is also a common thing.
4 other reasons I do not understand deeply, such as: swing in the listener without explicit remove; internal classes hold implicit references to external objects; Finalizers cause off

The object was not emptied in time.

Detection of memory leaks

There are a lot of tools to help with this, and if you don't have a tool on your hand, you can use the JDK with your own gadget:

Look who's filled up with heap.
1 with JDK6 Jmap can display the types of objects in the running program, the number and size of the
2 First Use JPS to find the process number, and then Jmap-histo PID display or jmap-dump:file=heap_file_name PID export heap File
Why these objects can still be reached.
3 Analyze the Heap file just exported using Jhat (Java Heap analysis Tool).
4 Jhat heap_file_name First, then open browser http://localhost:7000/Browse.

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.