Explanation of each memory region in the JVM, and scope interpretation of variables

Source: Internet
Author: User
Tags variable scope

Reprinted from: http://blog.csdn.net/zz87250976/article/details/42875301

JVM memory is divided into four types:
1, the stack area (stacksegment)-Automatically allocated by the compiler release, stored function parameter value, local variable value, etc., after the execution of the method, the system automatically frees the JVM memory resources;
2. Heap area (heapsegment)-typically released by programmers, storing objects and arrays created by new, the JVM does not periodically view this object, and if no reference points to the object, it is recycled;
3, Static zone (datasegment)-The storage of global variables, static variables and string constants, not released;
4. Code area (codesegment)-a binary code that stores methods in a program, and multiple objects that share a region of code space.


When a variable is defined in a method (code block), Java allocates the JVM memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the JVM memory space allocated for the variable, and the JVM memory allocated in the heap is managed by the Java Virtual machine's automatic garbage collector. The advantage of the heap is that the JVM memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand because it allocates JVM memory dynamically at run time. The disadvantage is that the JVM memory is allocated dynamically at runtime, the access speed is slower, and the advantage of the stack is that the access speed is faster than the heap, and the disadvantage is that the data size in the stack and the lifetime must be deterministic and inflexible.


The line faces the variable scope in Java to explain:
In Java, the scope of a variable is divided into four levels: class-level, object-instance-level, method-level, block-level;
Class-level variables, also known as global-level variables or static variables, need to be decorated with the static keyword, and you can compare learning with static variables in C + +;
Class-level variables exist after the class definition, occupy memory space, can be accessed through the class name, do not need to instantiate;
An object instance-level variable is a member variable, which allocates memory space before it is instantiated for access;
A method-level variable is a variable defined inside a method, which is a local variable.

Explanation of each memory region in the JVM, and scope interpretation of variables

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.