Processing of Java heap memory and stack memory

Source: Internet
Author: User

The previous time to learn the binary tree in the processing of the delete operation encountered a headache problem: the deletion of the node when it is clear that the node is still present on the tree, you must also execute Node.father.left = null, you can delete the node nodes, Looking for a reason to find out or because the Java memory management understanding is not deep enough.

The code is as follows:

@Testpublic void Testnode () {node Node1 = new Node ("Node1"); Node Node2 = new Node ("Node2"), Node2.father = Node1;node1.next = Node2;changenode (Node1.next); System.out.println (node1.next.name);} private void Changenode (Node node3) {node3 = null;}

After running the code, it is found that node3 = null has already been set in Changenode () and can still output node1.next.name.

You know why? Understand several concepts first:

1. Stack memory stores variables of the base type and reference variables of the object .
2. Heap memory is used to hold objects and arrays created by new . Each new object creates a fresh storage space in the heap memory to store this instance object.
3, person p = new person ();
The program executes two steps when executing the new command: A: A space in the heap memory to store the new object; b: Add a variable in the stack memory p,p the physical address where the object begins to be stored in the heap memory.
4, p = null;
When you perform this step, the program simply changes the address saved by the P variable in the stack memory , points to null, and does not manipulate the heap memory (the object instance that P points to is emptied for recycling).
5, whether it is a formal parameter or an argument, the execution of xxx = NULL, the operation is to change the address stored in the XXX variable stack to a point to null address. Data in the heap is not manipulated.

Here's a look at the changes in heap memory and stack memory for each step of code:

1. When new object is created, the program first creates a space instantiation object in the heap memory and adds a Node1 variable to the stack memory, which holds the first address of the physical address in the heap memory.

  

2, when the call method passed in the parameters, add a local variable node3 in the stack memory, storage node2 physical first address. That is, the value of the Node2 0x00011 copied into the Node3 stack memory.

Some netizens said this is a reference pass, in fact, the value is passed, but the value of Node2 is the physical address, and then the physical address value to NODE3.

3, when executing node3 = null; , the program changes the 0x00011 in NODE3 to a null-pointing address of 0Xaaaaa. However, the program does not manage the data in the heap memory (data that is not referenced in heap memory is processed by GC at some time).

You can see that performing this step simply empties the physical address in the variable, but does not remove the data from the Node2 in heap memory, which is why the original data still exists after the node has been deleted. You can see that next in Node1 still points to 0x00011.

The program does not change heap memory even if Node2 = null is executed.

The original data still exists!

JVM memory processing is far more complicated than this, if you want to learn more about the JVM memory processing mechanism related data, a search on the internet a lot, no longer repeat. Actually, I don't know either.

  

Processing of Java heap memory and stack memory

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.