Based on the difference between stack and heap in Java, the paper introduces the garbage collection mechanism in Java _java

Source: Internet
Author: User
Tags garbage collection

#. 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;

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.