Java 10 minute Knowledge point--reference

Source: Internet
Author: User
Tags java reference

I. Problems caused by amnesia

Busy today in the log to see a outofmemoryerror error, can not help but think of a period of time to see a piece of Java in the question of whether there is a memory leak, this is a long time ago was noted, and generally remember memory overflow and memory leaks are different, as for each of the circumstances, the ... The amount .... Forgot.... Well, memory has always been bad, forget to forget, then summarize it again. Turn over the collection of blog, review the next is to think of ~.~. It seemed like everything was fine, but one of the examples suddenly confused me:

public class Testdemo {    static test[] tests = new TEST[3];    public static void Main (string[] args) {        Test t = new Test ("Test1");        Tests[0] = t;        Set T to NULL, it looks as if we have freed the created object, and when the next GC it will be recycled        t = null;        Then we print the next test[0] See        System.out.println (Tests[0]);}    }

This is an example of a memory leak, which is typical, and almost all examples of memory leaks are similar, as Javaer often take for granted. However, as a learning C + + (bad), and has been cited as the pointer to see the Javaer inevitably feel some doubts:T is a reference to the object, here can be seen as a pointer to the object, then test[0]=t, it should be said that the T-pointer to test[0], As an address pass, and that T points to null, test[0] should also point to null.
It may seem a bit plausible, however, when you understand the Java reference, it is problematic to find that the pointer is equivalent to a reference.

second, what is the reference in the end What exactly is a reference in Java, simply, that a reference is a particular type of data that exists on the stack, which stores the address of the object instance in the heap, with the following characteristics:
    • itself is a data type, stored in the stack area
    • Its value stores the virtual address of the instance object in the heap ( note that it is the virtual address, not the actual memory address, just like the index number in the library, without conversion you do not know the actual location of the book )
    • When an object is created without an assignment (no instance), the reference points to null
    • In Java, parameter passing is only a value passing one, so-called reference passing is the value stored in the reference
From the definition seems to be indistinguishable from the reference to what is different from the pointer, then note the upper red Word, Java in order to mask the direct operation of memory, the actual memory address of the object is wrapped, so that the value in the reference can only be used to find the object, but unable to manipulate memory. This is exactly the same as referring to the most different, C + + pointer is a real memory address, you can use this address to play the memory 18-like pattern. This also shows that it is often wrong to pass a reference as an address (though the actual effect is almost).
Third, the above example what happened OK, look at the top of a lump maybe you can not see what, perhaps itself this piece of a bit around, perhaps I said not clear, then we might as well as direct drawing to explain what happened in the above example (the picture is not necessarily exactly the same as the actual, just to illustrate the problem), perhaps you understand:
Tests because it is a static variable, it is instantiated after the class has been loaded, and it allocates 3 of space in heap memory, but the value is null. After you create T, T points to the objects in the heap memory:

Tests[0]=t, this is where we understand the error, this step test[0] is not pointing to T, but T is assigning the address of the test instance directly to Tests[0], so tests[0] also points to the test instance, which has nothing to do with T.

In fact, we should understand that, after t=null, actually just cut off the T and test instances of the relationship, and did not change the test to tests dependency, so that the GC does not recycle test, which creates a logical memory leak (why logically, Because obviously you let tests still have test, but you think of the release of self-righteous, of course, this sense of disclosure and C + + said memory leaks very different).
Four, only for the end is not abrupt originally wanted to tidy up the memory overflow and memory leak, by the way write down this piece, but found not too well to say clearly, on the single write. Well, that's it, the original JVM learning summary series written half threw that, and now find it is quite interesting, have time to write it.



Java 10 minute Knowledge point--reference

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.