Algorithm (4th Edition) -1.4.9 memory

Source: Internet
Author: User

Summary: This section describes the memory allocation mechanism of Java and the amount of memory used by various data structures.

Focus:

1. A large part of the circuit in the computer is to help the program save some values and remove them later.

2. The use of memory in Java on the computer has been carefully designed (the amount of memory required for each run of the program is the same), but there are many devices that implement java, and memory usage is related to Implementation.

3. Common memory for raw data types, requirements See algorithm (4th Edition) -1.1.2 raw data types and Expressions.

4.

· Cost of the object Itself: 16 bytes

· Object reference (memory address): 8 bytes

· General memory usage is populated with multiples of 8 bytes (machine words on 64-bit computers)

An integer object: 24 bytes (16 bytes of Object overhead, 4 bytes to hold its int value, 4 padding bytes)

A date object: 32 bytes (16 bytes of Object overhead, 3 int instance variables 4 bytes each, and 4 padding bytes)

A counter object: 32 bytes (16 bytes of Object overhead, 8 bytes for its string instance variable (one reference), 4 bytes for the int instance variable, and 4 padding bytes) // Why not add the memory occupied by the object that the reference to the string instance variable refers to?

5.

· Nested non-static (internal) classes, such as our node class, also require an additional 8 bytes (for A reference to an external class)

A node object: 40 bytes (16 bytes of Object overhead, 8 bytes for each reference to item and node object, Plus 8 extra bytes of overhead)

A linked-list-based stack of n integers: 64N bytes (16 bytes overhead for a stack object, a reference type instance variable of 8 bytes, an int instance variable of 4 bytes, 4 padding bytes, 64 bytes per element, 40 bytes of a node object and 24 bytes of an integer Object)

6.

· Array requires 4 bytes to save length

· An array typically requires 24 bytes of header information (16 bytes of Object overhead, 4 bytes for saving length and 4 padding bytes)

An array of n int values: 4N bytes (filled in multiples of 8)

An array containing n double values: 8N bytes

An array containing n date objects: 24 bytes (array cost) plus 8N bytes (all references) Plus 32 bytes per object, total (40N) bytes.

A two-dimensional array of double types of M * N: 24 bytes (the cost of an array of arrays , no errors ) plus a 8M byte (a reference to all element Arrays) plus a 24M byte (the cost of all element Arrays) plus a 8MN byte (an array of double of type m with length n), Total (8MN + 32M +) ~ 8MN bytes.

7. The string represented by the object consists of characters from value[offset] to Value[offset + count-1].

8. A String object of length n: 40 bytes (string Object itself) plus (2N) bytes (character array), A total of (+ 2N) Bytes.

9. When you call the substring () method, a new string Object (40 bytes) is created, but it still reuses the same value[] array, so the substring of the string uses only 40 bytes of memory.

An alias for an array of characters containing the original string exists in the substring, and the offset and length fields of the substring object are marked with the position of the Substring.

In other words, the extra memory required for a substring is a constant, and the time required to construct a substring is also constant, even if the length of the string and substring is great.

10. Creating arrays or other large objects in a recursive program can be dangerous because it means that every recursive call uses a lot of memory.

Algorithm (4th Edition) -1.4.9 memory

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.