java中堆栈内存

Sometime back I wrote a couple of posts about Java Garbage Collection and Java is Pass by Value. After that I got a lot of emails to explain about Java Heap Space, Java Stack Memory, Memory Allocation in Java and what are the differences between them.

有时我写了几篇有关Java垃圾收集的文章, Java是“按价值传递” 。 之后,我收到了很多电子邮件,以解释Java堆空间Java堆栈内存Java中的内存分配以及它们之间的区别。

You will see a lot of reference to Heap and Stack memory in Java, Java EE books and tutorials but hardly complete explanation of what is heap and stack memory in terms of a program.

您将在Java,Java EE书籍和教程中看到很多有关堆和堆栈内存的参考,但是几乎没有就程序而言完全解释堆和堆栈内存。

Java堆空间 (Java Heap Space)

Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create an object, it’s always created in the Heap space.

Java运行时使用Java堆空间为Objects和JRE类分配内存。 每当我们创建对象时,它总是在堆空间中创建。

Garbage Collection runs on the heap memory to free the memory used by objects that don’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application.

垃圾回收在堆内存上运行以释放没有任何引用的对象使用的内存。 在堆空间中创建的任何对象都具有全局访问权限,并且可以从应用程序的任何位置进行引用。

Java堆栈内存 (Java Stack Memory)

Java Stack memory is used for the execution of a thread. They contain method-specific values that are short-lived and references to other objects in the heap that is getting referred from the method.

Java Stack内存用于执行线程。 它们包含短期的特定于方法的值以及对从该方法引用的堆中其他对象的引用。

Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method.

堆栈存储器始终按LIFO(后进先出)顺序引用。 每当调用方法时,都会在堆栈存储器中创建一个新块,以容纳该方法的本地基本值并引用该方法中的其他对象。

As soon as the method ends, the block becomes unused and becomes available for the next method.
Stack memory size is very less compared to Heap memory.

该方法结束后,该块将变为未使用状态,并且可用于下一个方法。
与堆内存相比,堆栈内存的大小要小得多。

Java程序中的堆和堆栈内存 (Heap and Stack Memory in Java Program)

Let’s understand the Heap and Stack memory usage with a simple program.

让我们通过一个简单的程序来了解堆和堆栈的内存使用情况。

package com.journaldev.test;public class Memory {public static void main(String[] args) { // Line 1int i=1; // Line 2Object obj = new Object(); // Line 3Memory mem = new Memory(); // Line 4mem.foo(obj); // Line 5} // Line 9private void foo(Object param) { // Line 6String str = param.toString();  Line 7System.out.println(str);} // Line 8}

The below image shows the Stack and Heap memory with reference to the above program and how they are being used to store primitive, Objects and reference variables.

下图显示了与上述程序有关的堆栈和堆存储器,以及如何将它们用于存储原始,对象和引用变量。

Let’s go through the steps of the execution of the program.

让我们看一下程序执行的步骤。

  • As soon as we run the program, it loads all the Runtime classes into the Heap space. When the main() method is found at line 1, Java Runtime creates stack memory to be used by main() method thread.一旦运行程序,它将所有运行时类加载到堆空间中。 在第1行找到main()方法后,Java运行时将创建要由main()方法线程使用的堆栈内存。
  • We are creating primitive local variable at line 2, so it’s created and stored in the stack memory of main() method.我们在第2行创建原始局部变量,因此将其创建并存储在main()方法的堆栈存储器中。
  • Since we are creating an Object in the 3rd line, it’s created in heap memory and stack memory contains the reference for it. A similar process occurs when we create Memory object in the 4th line.由于我们在第三行中创建对象,因此它是在堆内存中创建的,而堆栈内存中包含该对象的引用。 当我们在第四行中创建Memory对象时,也会发生类似的过程。
  • Now when we call the foo() method in the 5th line, a block in the top of the stack is created to be used by the foo() method. Since Java is pass-by-value, a new reference to Object is created in the foo() stack block in the 6th line.现在,当我们在第5行调用foo()方法时,将在堆栈顶部创建一个块,以供foo()方法使用。 由于Java是按值传递的,因此在第六行的foo()堆栈块中创建了对Object的新引用。
  • A string is created in the 7th line, it goes in the String Pool in the heap space and a reference is created in the foo() stack space for it.在第7行创建一个字符串,该字符串进入堆空间的“ 字符串池” ,并在foo()堆栈空间中为其创建引用。
  • foo() method is terminated in the 8th line, at this time memory block allocated for foo() in stack becomes free.foo()方法在第8行终止,这时为堆栈中的foo()分配的内存块变为可用。
  • In line 9, main() method terminates and the stack memory created for main() method is destroyed. Also, the program ends at this line, hence Java Runtime frees all the memory and ends the execution of the program.在第9行中,main()方法终止,并且为main()方法创建的堆栈存储器被销毁。 而且,程序在此行结束,因此Java Runtime释放了所有内存并结束了程序的执行。

Java堆空间和堆栈内存之间的区别 (Difference between Java Heap Space and Stack Memory)

Based on the above explanations, we can easily conclude the following differences between Heap and Stack memory.

根据以上解释,我们可以轻松得出Heap和Stack内存之间的以下差异。

  1. Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution.堆内存由应用程序的所有部分使用,而堆栈内存仅由一个执行线程使用。
  2. Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.每当创建对象时,它始终存储在堆空间中,并且堆栈存储器包含对该对象的引用。 堆栈内存仅包含局部基本变量和堆空间中对象的引用变量。
  3. Objects stored in the heap are globally accessible whereas stack memory can’t be accessed by other threads.堆中存储的对象可以全局访问,而其他线程则不能访问堆栈内存。
  4. Memory management in stack is done in LIFO manner whereas it’s more complex in Heap memory because it’s used globally. Heap memory is divided into Young-Generation, Old-Generation etc, more details at Java Garbage Collection.堆栈中的内存管理以LIFO方式完成,而在Heap内存中则更为复杂,因为它在全球范围内使用。 堆内存分为Young-Generation,Old-Generation等,有关更多信息,请参见Java Garbage Collection 。
  5. Stack memory is short-lived whereas heap memory lives from the start till the end of application execution.堆栈内存是短暂的,而堆内存是从应用程序执行的开始一直到结束。
  6. We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory. We can use -Xss to define the stack memory size.我们可以使用-Xms-Xmx JVM选项来定义启动大小和堆内存的最大大小。 我们可以使用-Xss定义堆栈内存大小。
  7. When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error.当堆栈内存已满时,Java运行时将引发java.lang.StackOverFlowError而如果堆内存已满,则将引发java.lang.OutOfMemoryError: Java Heap Space错误。
  8. Stack memory size is very less when compared to Heap memory. Because of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory.与堆内存相比,堆栈内存的大小要小得多。 由于内存分配(LIFO)的简单性,与堆内存相比,堆栈内存非常快。

That’s all for Java Heap Space vs Stack Memory in terms of java application, I hope it will clear your doubts regarding memory allocation when any java program is executed.

就Java应用程序而言,这就是Java堆空间与堆栈内存的全部,我希望它能在执行任何Java程序时消除您对内存分配的怀疑。

References:
https://en.wikipedia.org/wiki/Java_memory_model
https://blogs.oracle.com/jonthecollector/presenting-the-permanent-generation

参考文献:
https://zh.wikipedia.org/wiki/Java_memory_model
https://blogs.oracle.com/jonthecollector/presenting-the-permanent-generation

翻译自: https://www.journaldev.com/4098/java-heap-space-vs-stack-memory

java中堆栈内存

java中堆栈内存_Java堆空间与堆栈– Java中的内存分配相关推荐

  1. java堆内存和堆外内存_Java堆空间,本机堆和内存问题

    java堆内存和堆外内存 最近,我正在和一个朋友讨论为什么Java进程使用的内存比启动Java进程时设置的最大堆多. 代码创建的所有Java对象都是在Java堆空间内创建的,其大小由-Xmx选项定义. ...

  2. JVM学习笔记之-堆,年轻代与老年代,对象分配过程,Minor GC、Major GC、Full GC,堆内存大小与OOM,堆空间分代,内存分配策略,对象分配内存,小结堆空间,逃逸分析,常用调优工具

    堆的核心概述 概述 一个JVM实例只存在一个堆内存,堆也是Java内存管理的核心区域.Java堆区在JVM 启动的时候即被创建,其空间大小也就确定了.是JVM管理的最大一块内存空间. 堆内存的大小是可 ...

  3. 栈空间内存和堆空间内存

    栈空间内存.堆空间内存和方法区内存 ​ 在学习Java的过程中,我们知道,我们编译出的class文件,需要加载到内存中才可以运行.在Java中,有三个很重要的内存空间:栈内存空间.堆内存空间以及方法区 ...

  4. java 堆中的新生代_Java堆内存_Young Gener_Old Generation_新生代和老年代

    使用JDK8 Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象. 在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新 ...

  5. java nio 堆外内存_Java堆外内存之突破JVM枷锁

    对于有Java开发经验的朋友都知道,Java中不需要手动的申请和释放内存,JVM会自动进行垃圾回收:而使用的内存是由JVM控制的. 那么,什么时机会进行垃圾回收,如何避免过度频繁的垃圾回收?如果JVM ...

  6. java堆内存_java堆内存详解

    http://www.importnew.com/14630.html java堆的特点 <深入理解java虚拟机>是什么描述java堆的 Java堆(Java Heap)是java虚拟机 ...

  7. java9 堆外内存_java堆外内存泄漏排查

    当考虑Java中的内存泄漏时,我们通常会考虑Java堆泄漏,即在堆中分配的对象没有被垃圾收集.这是我在处理一台服务器内存泄漏时的想法,但我即将经历的远超出我的想象. 症状:运行Vertx应用程序(没有 ...

  8. java取邮箱前缀_java抓取网页或文件中的邮箱号码

    java抓取网页或文件中的邮箱号码 发布时间:2020-10-18 08:58:32 来源:脚本之家 阅读:69 作者:java大渣渣 本文实例为大家分享了java抓取邮箱号码的具体代码,供大家参考, ...

  9. java opencv 读取视频_java使用OpenCV从视频文件中获取帧

    本文实例为大家分享了java使用OpenCV从视频文件中获取帧的具体代码,供大家参考,具体内容如下 实现功能:使用Java获取mp4.mov.avi等视频文件中的图像帧,每秒获取一帧图像,并保存 环境 ...

最新文章

  1. C++——虚函数(Virtual Member Functions) 【functions语意学】
  2. EXCEL自定义的应用
  3. nginx rewrite 参数和例子
  4. ActiveMQ添加商品接收消息
  5. [terry笔记]Oracle10g/11g安装-redhat5.5
  6. [转载]linux+nginx+python+mysql安装文档
  7. 天邑TY1608卡刷包晶晨S905L3B支持RTL8822CS、MT7668、MT7661
  8. python迅雷下载器_简单的迅雷VIP账号获取器(Python)
  9. 如何向投资人委婉的表达:我们什么都不缺,只缺钱?
  10. 夜晚网速变慢与网站服务器开机数量减少有关,【网络】网速慢的原因与对策
  11. 卡内基梅隆大学计算机科学博士,美国卡内基梅隆大学博士需要几年
  12. 如何在arm-linux下支持2T硬盘
  13. java获取网络时间_java使用ntp同步获取网络时间
  14. 震碎认知!将原理融会贯通到顶点的SpringBoot实战项目,面试涨薪的神器
  15. creator 跳跃弧线_jumpGame
  16. Navigating to current location (/user) is not allowed
  17. 《疯狂的石头》在线观看(网通用户)
  18. 【PTA】PAT (Advanced Level) Practice 1011-1014
  19. 【论文阅读笔记】faster rcnn 代码阅读细节
  20. 使用 Microsoft AI 打造你的首款智能机器人(入门只需要1小时)

热门文章

  1. (转)SSDTShadow Hook的实现,完整代码
  2. MyEclipse7.0及JDK1.6.0的安装及配置过程(修改)
  3. 送6个Gmail邀请!
  4. WPF在一个窗口中实现多个视图
  5. [转载] Python—urllib模块
  6. (数论)51NOD 1136 欧拉函数
  7. C++程序设计方法3:类中的静态成员
  8. 项目需求:基于微信平台的拼团活动系统
  9. 找规律 SGU 107 987654321 problem
  10. 解决vs2005无法连接sql数据库问题