我是靠谱客的博主 明亮导师,最近开发中收集的这篇文章主要介绍Java Memory Usage,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

eg:

1.5.4
The correct answer is: 944
public class MysteryBox {                     //   16 (object overhead)
    private long x0;                          //    8 (1 long)
    private double y0, y1;                    //   16 (2 double)
    private boolean z0, z1;                   //    2 (2 boolean)
    private int[] a = new int[216];           //    8 (reference to array)
                                              //  888 (int array of size 216)
    ...                                             6 (padding to round up to a multiple of 8)
}                                                ----
                                                  944

(The explanation below is copied from http://algs4.cs.princeton.edu/11model/)

Memory usage.

 To estimate how much memory our program uses, we can count up the number of variables and weight them by the number of bytes according to their type. For a  typical  64-bit machine,
  • Primitive types. the following table gives the memory requirements for primitive types.

    memory requirements for primitive types
  • Objects. To determine the memory usage of an object, we add the amount of memory used by each instance variable to the overhead associated with each object, typically 16 bytes. Moreover, the memory usage is typically padded to be a multiple of 8 bytes (on a 64-bit machine).

    memory requirement of Integer  memory requirement of Date
  • References. A reference to an object typically is a memory address and thus uses 8 bytes of memory (on a 64-bit machine).
  • Linked lists. A nested non-static (inner) class such as our Node class requires an extra 8 bytes of overhead (for a reference to the enclosing instance).

    memory requirement of Node
  • Arrays. Arrays in Java are implemented as objects, typically with extra overhead for the length. An array of primitive-type values typically requires 24 bytes of header information (16 bytes of object overhead, 4 bytes for the length, and 4 bytes of padding) plus the memory needed to store the values.

    memory requirement of arrays
  • Strings. A String of length N typically uses 40 bytes (for the String object) plus 24 + 2N bytes (for the array that contains the characters) for a total of 64 + 2N bytes.

    memory requirement of String
    Beginning with Oracle and OpenJDK Java 7, Update 6, the representation of the string data type has changed. See this article for more details.


最后

以上就是明亮导师为你收集整理的Java Memory Usage的全部内容,希望文章能够帮你解决Java Memory Usage所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部