A discussion on the analysis of Java memory

Source: Internet
Author: User

All these days I've been looking for a chance to tidy up the knowledge of Java memory, after all, any knowledge involves this aspect. Java memory analysis is a major focus of Java learning.

Here we go to the formal topic discussion:

We know that Java memory is roughly divided into three blocks:

Let's take a general look at the data storage for each of the Java partitions:

Stack area: Mainly for the method service, there are a lot of methods stack frame, in the method stack frame to open up a local variable space, the basic type of basic data type data size to open up space, and reference type open four bytes size.

Heap Area: The object data that is created is primarily stored.

Method Area: The main storage load class, static variables, static initialization blocks, constants, and program code to run.

Let's analyze a program that will help us learn more about Java memory:

First the JVM enters from the main method to load the Mydog class into the method area and then loads the animal class into the method area and creates space for the static variable MyAnimal in the method area and initializes it by default.

The dog type object is then created in the heap area: 1 first opens up space for the parent class property and initializes it by default, and then calls the parent class construction method to initialize.

2 re-creating new properties for subclasses re-calling sub-class construction method

3 The new methods and subclasses that inherit the parent class of the subclass are further open space. The dog type object is created.

The MyAnimal reference is then directed to the newly created dog type object (also called upward transformation).

The MyAnimal reference then calls the subclass dog to inherit the parent class's Method Setweight ().

Finally call the subclass dog to inherit the parent class's Method Getweight () and print it out.

Ps: It is worth noting that the upward transformation is myanimal=new Dog ();

1. The MyAnimal reference does not access the parent class's private properties (and methods).

2. The MyAnimal reference does not access the subclass new properties (and methods), and you can access the inherited properties and methods.

A discussion on the analysis of Java memory

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.