Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions Java memory allocation
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


samar
Java memory allocation
1 Answer(s)      3 years and 11 months ago
Posted in : Java Interview Questions


How does Java allocate stack and heap memory?

Advertisement
View Answers

October 28, 2010 at 4:40 PM


Hi,

Here is the answer.

Each time an object is created in Java it goes into the area of memory known as heap. The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked and stack pointer is decremented when a method call is completed.

In a multi-threaded application each thread will have its own stack but will share the same heap. This is why care should be taken in your code to avoid any concurrent access issues in the heap space. The stack is threadsafe (each thread will have its own stack) but the heap is not threadsafe unless guarded with synchronisation through your code.

Thanks.



Related Tutorials/Questions & Answers:
Java memory allocation
Java memory allocation  How does Java allocate stack and heap memory
Java Memory allocation - Java Interview Questions
Java Memory allocation  By default what is the memory allocation size for a single class? How the memory is allocated? Based on what procedure
Advertisements
memory allocation
memory allocation   class Bird { { System.out.print("b1 "); } public Bird() { System.out.print("b2 "); } } class Raptor extends Bird { static... of execution of statement ? memory allocation ?  The code uses static
memory allocation
memory allocation  If we assign memory malloc(5) and access more than the assigned memory then what will happen? 1.segmentation error 2.memory leak 3.program will not compile 4.Your view
java memory allocation for member variables and member functions
java memory allocation for member variables and member functions  ... an object q of test class then when shall the memory be allotted for function fun...() that has three variables a=5,b=6.My questions are 1>When will the memory be allotted
Memory Allocation
Memory Allocation  What Is The Order Of Execution Of Statement 1) Statement Enclosed in {}. 2) Static 3) Public Static Void Main()   When jvm loads a class, static block is executed first, then main method. And at last
Controlling Java Heap Size Memory Allocation
Controlling Java Heap Size Memory Allocation  Hi, Tell me about Controlling Java Heap Size Memory Allocation? I have to run java program from console and allocate 2GB of RAM to Java Heap Size. Thanks   Hi, You can
Java memory leak
Java memory leak  What is memory leak
Creating a memory leak with Java
Creating a memory leak with Java  Creating a memory leak with Java
Java memory optimization
Java memory optimization  When you think about optimization, what..., You can use memory dump and then analyze it to find the process/java code in your program which is using large amount of memory. Ideally java should garbage
Memory - Java Beginners
Memory management in Java  Where are these Heap and Stack? Is there size fixed? what is core area in memory
memory
memory  what is memory
Memory
Memory   what is the different between heap memory and stack memory ? please explain with example
memory
memory   hi i am jane pls explain the difference between heap memory and stack memory
Java Memory Leak - Java Beginners
Java Memory Leak  I am having a memory leak in my java version of the game Snake. I have narrowed the exact step of when the program memory.... The actually object that's eating up my memory is an array of ints which grows
Java Get Memory
Java Get Memory       In this section of Java Example, you will see how to get the memory... can easily learn it. In this Java Get Memory Size example, we have created
Java get Free Memory
Java get Free Memory       In this section, we are going to obtain the free memory. In order to get the memory left of being allocated, the method getruntime() returns
Finding memory consuming process - Java Beginners
Finding memory consuming process  Hi, While debugging or optimizing your java application, how to find the memory consuming processes? Thanks
Java Memory of Apache Tomcat - Java Beginners
Java Memory of Apache Tomcat  Dear All, Please help me to fetch the Java memory occupied by Apache tomcat server using Java Codes... at times. I want to monitor that Java memory Regards, Santhosh
Java memory options from command line
Java memory options from command line  Hi, How to use java memory options from command line? Thanks   Hi, Here is the example code: java -Xms1056m -Xmx1056m MyProgram Thanks
Java Array Initialization
Java Array Initialization       After declaring an array variable, memory is allocated to it. The "new" operator is used for the allocation of  memory
memory management
memory management  memory management in java
Thread Memory Usage in java - Java Beginners
Thread Memory Usage in java  how to get a thread's memory usage? Does it has java api to invoke?  hi friend, import java.io.*; import...://www.roseindia.net/java/thread/thread-creation.shtml   but,if I use
Array in Java
An Array is the static memory allocation that holds a fixed number of values of same type in memory. The size or length of an array is fixed when the array.... Different types of array used in Java are One-dimensional, Two-dimensional and multi
how can clear the browser cache memory using java script
how can clear the browser cache memory using java script  how can clear the browser cache memory using java script
Java Get Memory Usage
Java Get Memory Usage     ... of that unlike c++,  java don't tell that how much memory is occupied... memory from the actual memory. As you know in Java programming memory can
Java read file in memory
Java read file in memory In this section, you will learn how to read a file in memory. Description of code: The class FileInputStream get a channel...++)); } System.out.println(); } } The above code mapped the file in a memory
Java file in memory
Java file in memory This section illustrates you how to map an entire file into memory for reading. Description of code: We used the FileChannel class along with the ByteBuffer class to perform memory-mapped IO for data of type byte
Phantom memory
Phantom memory  What is phantom memory
Phantom memory
Phantom memory  hello,, What is phantom memory
memory management
memory management  Why objects store in heap
Advertisements
 

 

 

DMCA.com