Java Memory leak issues

Source: Internet
Author: User

The garbage collection mechanism in 1:java mainly completes the following two things:
    • Track and monitor each Java object and reclaim the memory that the object occupies when an object is in an unreachable state
    • Cleans up memory allocations, resulting in memory fragmentation during recycling
2: For the garbage collection mechanism of the JVM, the standard of whether to reclaim an object is:

Is there a reference variable that references the object?

As long as there is a reference variable referencing the object, the immediate recycling mechanism does not reclaim it. 3: Basically, the JVM in-memory object reference can be interpreted as a directed graph, the reference variable, the object as the vertex of the directed graph, referring to the relationship as a directed edge of the graph, there is always from the reference side to point to the referenced object.

Because all Java objects are created by a single thread, you can use the thread object as a starting point for the graph. If an object is in an unreachable state in this graph, it is assumed that the object is no longer referenced.

The garbage collection mechanism then goes back to actively reclaim it.

Take the following procedure as an example:

1 classNode2{3Node Next;4String name;5    PublicNode (String name)6{7         This. name = name;8}9}Ten   Public classNodeTest One{ A   Public Static voidMain (string[] args) -{ -Node N1 =NewNode ("first Node"); theNode N2 =NewNode ("A second node"); -Node n3 =NewNode ("A third node"); -N1.next = n2; -N2 =NULL; +N3 = n2; -} +} AAs you can see, there is a path from the main vertex to the first node, so the object is in a reachable state, and the garbage collection mechanism does not recycle it. atThere are two paths from main to the second node, so the object is also in a reachable state, and the garbage collection mechanism does not recycle it. -Starting with the main vertex, no path reaches the "third node", so the object becomes garbage. -  -  -Tips for memory management -  inUse direct volume as much as possible -  toWhen you need to use strings, as well as other instances such as Byte,short,integer,long,float,double,boolean,character wrapper classes, the program should not create objects in new ways, but should use direct amounts to create them +For example: The program requires "Hello"String, the following code should be used: -String str = "Hello"; theThe code above creates a "HelloString, and the JVM's string cache pool also caches this object *But if you use the following code: $String str =NewString ("Hello");Panax NotoginsengAt this point, the program also creates a slow existence of the string in the cache pool of "HelloString, in addition to the bottom of the string object referenced by str, contains a char[] array, which in turn stores characters such as H,e,l,l,o. -  the2: String connection using StringBuffer and StringBuilder +If a program uses multiple string objects to concatenate strings, a large number of temporary string objects are generated at run time, which are stored in memory and cause program performance to degrade A3: Use static variables as little as possible theFor example, the following code: + classperson{ - StaticObject obj =NewObject (); $} $The obj variable is a static variable of the person class, so its life cycle is synchronized with the person, and the class object corresponding to the person class is resident in memory in case the person class is not unloaded until the program finishes running. -As a result, the object objects referenced by obj, once created, will also reside in memory until the end of the run. -4: Release references to useless objects as early as possible the5: Avoid creating Java objects in loops in a frequently called method -6: Cache objects that are used frequentlyWuyiA typical cache is a database connection pool in which a large number of database connections are cached, and each time a program accesses a database, it is good to get a direct connection.







Note: This article is from: cnblogs: Milk, unsweetened

Java Memory leak issues

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.