Java data type memory allocation

Source: Internet
Author: User

The basic data type is directly allocated in the stack space.

Method form parameters are directly allocated in the stack space. After the method call is completed, the stack space is reclaimed.

To reference the data type, you must use new to create a class variable that allocates an address space in the stack space and objects in the heap space.

Method reference parameters: Allocate an address space in the stack space and point to the object area of the heap space. After the method is called, it is reclaimed from the stack space.

When the local variable is new, space is allocated in the stack space and heap space. When the lifecycle of the local variable ends, the stack space is immediately recycled, and the heap space area is waiting for GC to be recycled.

The literal parameter passed in when the method is called. It is first allocated in the stack space, and then allocated from the stack space after the method is called.

String constants are allocated in the data area.

This is allocated in the heap space.

The array allocates both the array name in the stack space and the actual size of the array in the heap space.

In Java, memory management is divided into Stack-stack, heap-heap, data segment-data segment, and code segment-code segment.

First, identify what is stored in each area.

STACK: It can be translated into a stack area to store some basic data types, command code, constants, and object reference addresses.

The basic data type is the most basic four types and eight types. As mentioned in the previous blog.

Command code is a number of functions and methods.

Constants and object reference addresses can be understood as Defined variables. But only the reference address. The new address is stored in heap, that is, heap memory.

Heap: stores object instances, that is, all new items in the Code are put here.

Data Segment: data segment, which stores static constants and string constants.

Code segment: A code segment that stores the code.

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.