A detailed description of the memory recycling mechanism in Java

Source: Internet
Author: User

http://blog.csdn.net/mengern/article/details/38150431

Java provides a method for garbage-forced reclamation (SYSTEM.GC), but the system does not guarantee immediate garbage collection, but rather the JVM determines, based on a defined set of garbage collection algorithms, that the algorithm is used to improve the efficiency of garbage collection.

The basis for judging whether a storage unit is garbage is whether the object that corresponds to the storage unit is still being used by the program, that is, whether a reference is pointing to the object. The Java garbage collector automatically scans the object's dynamic memory area, marks the referenced object, and then collects the unreferenced objects as garbage and frees them.

Java does not provide a destructor method, but provides a similar approach: protected void Finalize ();

Program Description:

[Java]View PlainCopy
  1. Class J_book {
  2. private String name;
  3. J_book (String name) {
  4. this.name = name;
  5. }
  6. //Override the default Finalize method
  7. protected Void Finalize () {
  8. System.out.println ("book,\" "+ name + " \ ", is destroyed!");
  9. }
  10. }
  11. Public class J_finalize {
  12. public static void Main (string[] args) {
  13. J_book Book1 = new J_book ("Gone with Wind");
  14. ///Anonymous instance, determined by Java as garbage memory, will be treated as garbage collection
  15. New J_book ("Java How to Program");
  16. New J_book ("Roman Holiday");
  17. Book1 = New J_book ("thingking in Java");
  18. //Forced garbage collection, note that the compilation system does not immediately garbage collection, and its own algorithm to decide when to perform garbage collection
  19. System.GC ();
  20. }
  21. }

Program Run Result:

Book, "Roman Holiday", isdestroyed!

Book, ' Java How Toprogram ', is destroyed!

Book, ' Gonewith Wind ', is destroyed!

Description: The result is eclipse in debug mode!

A detailed description of the memory recycling mechanism in Java

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.