Learning Diary (eight) Java garbage collection and finishing

Source: Internet
Author: User

Garbage collection mechanism: When an object is no longer referenced, or when a reference to an object does not exist, we assume that the object is no longer needed and that the memory it consumes is freed.

Garbage collection only happens occasionally during the execution of a program, and Java's different run times produce different garbage collection methods, and for most of the programs we write, we don't need to consider garbage collection issues.

You can invoke the System.GC method to force garbage collection, but this method is not garbage collected as soon as it is called, but with an indeterminate time.

The closing mechanism in Finalization:java is that in Java there are times when an object needs to be undone with some other necessary action, so use this mechanism and use the Finalize () method.

If the Finalize () method is defined, Java calls this method when it recycles an object of that class. In finalize, you want to specify the action to take before recycling. Finalize is called just before garbage collection, and it has the same time uncertainty, so there are other ways to release resources in the program.

In programming, we want to release the useless references as early as possible, usually, when using temporary variables, we have to let the reference variables automatically become null after exiting the active domain, implying that the garbage collector collects them.

In Java, the memory is divided into two kinds, heap memory and stack memory: the stack is stored in the basic type and reference type reference, the data has a specific size and lifetime, access speed, data can be shared, the heap is stored in the object, you can dynamically allocate memory size, you do not have to tell the compiler lifetime, The garbage collector automatically reclaims unused memory because it allocates memory dynamically at run time, so access is slow but more flexible.

In the popular comparison, the heap is used to store objects, and stacks are used to execute programs.

Learning Diary (eight) Java garbage collection and finishing

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.