Object Memory analysis, I have been learning more vague, today time to tidy up, hope to clarify.
First of all, where does the memory analysis come from, which is more obscure to those of us who are just touching Java, starting with the new keyword.
The new keyword means that creating an object or instantiating an object or applying a memory space requires memory to store the data, so the concept of memory comes out. For example: Define a class person, attribute: char a;int sge; the memory space for creating an object request is the sum of the bytes of all the attributes, which is 6 bytes.
Detail the structure of the object in memory:
Person P=new person (); Person P1=new person ();
Stack memory heap
P |
Name=null Age=0 |
P1 |
|
a=10 (local variable) |
|
No assignment defaults to NULL. The stack memory is stored as a class object or as a local variable (the base data type). Local variables: Acting on a method;
Global variables: Acting on the entire class
If P1=p;p and P1 point to the same heap of memory, the memory that P1 originally points to is not referenced and is garbage collected.
Value is passed in the stack memory, and the value is copied directly.
The reference pass is passed through the address in the heap memory. Like P and P1 addresses, they point to the same piece of memory.
Almost, the memory analysis is summed up here.
Java Object-oriented _ Object memory analysis-value passing and reference passing