#. There are two types of memory in Java. are called stack (stack) and heap (heap), respectively.
Stack is the memory space of the program, so all the basic types and references to objects are in the stack.
Heap is a Java Virtual machine storage object, it is a huge memory, when you create an object, Java Virtual machine put objects into the heap, the creation of the object of the address into the stack.
Therefore, the base type, the object's reference is stored in the stack, and the object is stored in the heap.
#. The garbage collection mechanism in Java
When you new object, Java allocates the required memory. When you run out of an object, the Java garbage collector reclaims the memory for you.
Garbage collection runs in the background as a thread, looking for objects that have no useful references (reference) and then destroys the object and reclaims the memory after discovering it.
Garbage collection is implemented between Java virtual machines, and they usually have the same steps, and first the garbage collector gets a snapshot of the running thread and all the classes that have been loaded.
The objects involved in all threads are then marked as most recently used, and the left unmarked are discarded when the possible objects are marked.
To help a virtual machine, it is a good idea to proactively remove some objects that are not needed, and you can do this by setting the reference to null.
eg
Text t = new Test ();
T.someaction ();
All done
t = null;