Java memory analysis, java memory

Source: Internet
Author: User

Java memory analysis, java memory
Preface

Looking at the code, we may only know the sequence of program execution. Through memory analysis, we can understand the memory allocation during program execution. The former is analyzed in the time dimension, and the latter is analyzed in the spatial dimension.PurposeIt is to map the specific code with the memory allocation during execution, so that everyone can have a clear understanding of this content.

Overview

According to, we will first introduce the entire program execution process. There are three main steps:

 

Supplement:
The program file is stored on a hard disk. The extension name can be .exe or. class.
Memory Management is the main problem described in this article. During memory management, the memory is divided into four areas: Heap, Stack, Data Segment, and Code Segment ).
  In general, remember the following four points:

  1.Place new items in the heap area;
  2.Place local variables in the stack area;
  3.Put static variables and string constants in the Data zone;
  4.Put the code in the code area.

The above four points are the fundamental principles, and the following are the details to be emphasized.

Case classes and objects

During program execution, the code of the defined class is loaded into the code area in the memory;
Objects are generated during the instantiation process (new.

As shown in, the code of class C is loaded into the code area. When the main method is executed, c1 and c2 are local variables and two spaces are allocated in the stack area. new C () during the process, space will be allocated in the heap area. This space belongs to an object and includes the space of member variables in the control space of this object.

Note: The array also belongs to the reference type, so its memory allocation status is the same as that of the object.

Method

When the method is executed, space is allocated for the method parameters. The parameters are equivalent to local variables, so space is allocated in the stack memory. Then, using the value transfer method, the real parameter copies the value to the form parameter. When the method has a return value, the returned value also has a block of temporary memory in the stack, with no name.
After the method is executed, the local variables allocated for the method execution and the temporary space for the returned values disappear.

Inheritance

As shown in, if "Class Student extends Person" is used, the space allocated to this object in the heap will also have the space of the parent object. That is, subclass objects are larger than parent objects, and subclass objects have parent objects. It has both this and super references, pointing to the entire object and its parent object respectively.

Static variables and string constants

The following code contains static member variables and string constants:

Public class Cat {private static int sid = 0; // static member variable private String name; // member variable int id; Cat (String name) {this. name = name; id = sid ++;} public void info () {System. out. println ("My name is" + name + "No. "+ id);} public static void main (String arg []) {Cat mimi = new Cat (" mimi "); // "mimi" is a String constant Cat pipi = new Cat ("pipi"); mimi.info (); pipi.info ();}}

As shown in, only one static variable exists in the Data zone, and the String constant also exists in the Data zone.

Polymorphism

Polymorphism has three conditions: inheritance, rewriting, and parent class reference pointing to a subclass object. The memory allocation is the same as the memory allocation during inheritance. Only when performing the upward transformation, you can only access the member variables of the parent class. When executing the rewrite method, the method of the subclass object is executed based on the actually bound subclass object.
For example, Animal a = new Dog (); this object can only access the name attribute, but cannot access the furColor attribute of the subclass object.

  

Dynamically decide which method code to execute. Both Animal and Cat have the enjoy method. When Animal a = new Cat (); a. enjoy (); During execution, the code of the enjoy method in Cat is determined.

Summary

To understand the memory allocation process is to understand the story behind the code. The above are some basic content. If you want to associate it with the content you have learned before, I think this knowledge may be helpful for understanding pointers, constructors, value passing calls, and address passing calls in C ++. Looking forward to the potential value of these basic knowledge may help me optimize my code and reduce memory consumption in the future!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.