Difference between Stack and Heap memory in Java
来源:互联网 发布:windows10下安装mysql 编辑:程序博客网 时间:2024/05/16 11:03
Difference between stack and heap memory is common programming question asked bybeginnerslearningJava or any other programminglanguage. Stack and heap memory are two terms programmers starts hearing once they started programming but without any clear and definiteexplanation. Lack of knowledge onwhat is heap in Java and what is stack memory in Java, results in misconcepts related to stack and heap. To add to this confusion, stack is also a data structure which is used to store elements in LIFO(Last In First out) order and available in Java API as java.util.Stack. In general both stack and heap are part of memory, a program is allocated and used for different purposes.Java program runs inside JVM which is launched as a process by "java" command.Java also uses both stack and heap memory for different needs. In our lastarticle 10 points on Java heap space I have touched base on Java heap space and in this article we will see difference between stack and heap memory in Java.
Difference between Stack vs Heap in Java
Here are few differences between stack and heap memory in Java:
1) Main difference between heap and stack is that stack memory is used to storelocal variables and function call, while heap memory is used to store objects in Java. No matter, where object is created in code e.g. as member variable, local variable or class variable, they are always created inside heap space inJava.
2) Each Thread in Java has there own stack which can be specified using -Xss JVM parameter, similarly you can also specify heap size of Java program using JVM option -Xms and -Xmx where -Xms is starting size of heap and -Xmx is maximum size of java heap. to learn more about JVM options see my post 10 JVM option Java programmer should know.
3) If there is no memory left in stack for storing function call or local variable, JVM willthrow java.lang.StackOverFlowError, while if there is no more heap space for creating object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space. Read more about how to deal with java.lang.OutOfMemoryError in my post2 ways to solve OutOfMemoryError in Java.
4) If you are using Recursion, on which method calls itself, You can quickly fill up stack memory. Another difference between stack and heap is that size of stack memory is lot lesser than size of heap memory in Java.
5) Variables stored in stacks are only visible to the owner Thread, while objects created in heap are visible to all thread. In other words stack memory is kind of private memory of Java Threads, while heap memory is shared among all threads.
That's all on difference between Stack and Heap memory in Java. As I said, It’s important to understand what is heap and what is stack in Java and which kind of variables goes where, how you can run out of stack and heap memory in Java etc. Let us know if you are familiar with any other difference between stack and heap memory in java.
Read more: http://javarevisited.blogspot.com/2013/01/difference-between-stack-and-heap-java.html#ixzz2ki3TdJBo
- Java - Difference between Stack and Heap memory in Java
- Difference between Stack and Heap memory in Java
- Difference between Stack and Heap memory in Java
- Difference between Stack and Heap in Java
- difference between stack and heap
- The difference between stack and heap
- Java Heap Memory vs Stack Memory Difference
- What's the difference between stack and heap?
- What's the difference between Heap and Stack
- What’s the difference between a stack and a heap?
- Java Heap Memory vs Stack Memory Difference(java Heap与Stack的区别)
- Heap and stack in java
- The difference in memory management between LotusScript and Domino Java objects
- Difference between Externalizable and Serializable in Java
- Difference between Vector and ArrayList in java?
- Difference between Comparator and Comparable in Java
- Difference between TreeMap and TreeSet in Java
- Difference between RegularEnumSet and JumboEnumSet in Java
- tomcat无法启动之tj
- build webrtc for android
- 技术人员谈管理之采购管理案例论文
- 非阻塞算法在并发容器中的实现
- Java String Replace Example Tutorial
- Difference between Stack and Heap memory in Java
- How to use ConcurrentHashMap in Java - Example Tutorial and Working
- 易混淆的字母
- nyoj-79-拦截导弹
- 关于日期2013-05-25T19:09:18.769+0800格式化
- 【黑马程序员】 java笔记——抽象类
- RGB图像灰度化
- 【ASP.Net MVC3 】使用Moq让单元测试变得更简单
- [回溯]23 out of 5 UVA 10344