Oop_day03_ memory management, reference type array

Source: Internet
Author: User

Oop_day03_ memory management, reference type array

--20150811


1. memory Management: Distributed and managed by the JVM ------understand the main

1) Heap:

1.1) used to store new objects (including member variables)

1.2) The life cycle of the member variable:

exists when object is created and disappears when object is recycled

1.3) Garbage collector (GC) is used to reclaim objects that do not have any references pointing to

1.4) GC is not periodically recycled, and the process of recycling is transparent

If you want to hurry, call System.GC ()

1.5) Memory leaks: objects that are no longer being used are not recycled in a timely manner

Recommendation: Objects that are no longer used will have their references set to NULL in a timely manner


Memory Graph:


2) Stack:

2.1) for storing all local variables

2.2) The life cycle of local variables:

When the method is called, it disappears after the method has finished executing

2.3) When invoking a method, assign the corresponding stack frame to the method in the stack,

The stack frame contains all the local variables (parameters + variables) in the method,

After the execution of the method, the stack frame is cleared and the local variable disappears.


Memory Graph:


3) method area:

3.1) used to store. Class bytecode files and methods

3.2) method has only one copy, in the method area,

Use this to differentiate between different objects


Memory Graph:



2. Array of reference types

1)

      Cell[] cells = new Cell[4];   Create a cell array object      cells[0] = new cell (n);  Create Cell object      cells[1] = new cell (2,3);      CELLS[2] = new Cell (3,4);      CELLS[3] = new Cell (4,5);

2)
     Cell[] cells = new cell[]{     new cell, new cell (     2,3), new cell     (3,4),     new cell (4,5)      };

3)
      int[][] arr = new int[3][];      Arr[0] = new int[2];      ARR[1] = new Int[3];      ARR[2] = new int[2];      arr[1][0]=100; Assigns the 1th element of the 2nd element in arr to a value of 100

4)
      int[][] arr = new INT[3][4]; ARR contains 3 elements, and each element contains 4 elements for      (int i=0;i<arr.length;i++) {for          (int j=0;j<arr[i].length;j++) {              arr[i ][J] = 100; Assign each element to a value of.          }      }

Memory Graph:


code example:

Package oo.day03;//Reference type array Demo public class Refarraydemo {public static void main (string[] args) {/*//1.cell[] cells = new Cel L[4];cells[0] = new cell, cells[1] = new cell (2,3), cells[2] = new cell (3,4); cells[3] = new cell (4,5); *//*//2.cell[] CE LLS = new Cell[]{new cell ($), new cell (2,3), new cell (3,4), new cell (4,5)};*//*//3.int[][] arr = new Int[3][];arr[0] = new INT[2];ARR[1] = new Int[3];arr[2] = new Int[2];arr[1][0] = 100;*//*//4.int[][] arr = new Int[3][4];for (int i=0;i<arr.le ngth;i++) {for (int j=0;j<arr[i].length;j++) {arr[i][j] = 100;}} */}}



---------------------------------------------------------------------------------------------


Summarize:

Memory map can be helpful to understand, or it is necessary to take a good look at, see the illustrations in the text

An array is a reference type and can have an array of array types (the C language is called a two-dimensional array, and there is no concept of a two-dimensional array in Java)






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Oop_day03_ memory management, reference type array

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.