Heap and Stack Memory in Java मै क्या होता है दोनों मै क्या अंतर है और उदहारण देखेगे
Heap and Stack Memory in java मै heap and stack memory are two distinct areas used for memory management के लिए किया जाता है ....
हीप (Heap)
और स्टैक (Stack) मेमोरी जावा में दो प्रमुख मेमोरी स्थान हैं जिनका उपयोग
वेरिएबल्स और डेटा संग्रहीत करने के लिए किया जाता है।
हीप मेमोरी एक बड़ी और आमतौर पर डायनैमिक आवंटित मेमोरी खंड है। जब आप ऑब्जेक्ट बनाते हैं या 'new' की सहायता से वेरिएबल्स को डायनैमिक रूप से आवंटित करते हैं, तो उन्हें हीप मेमोरी में स्थान प्राप्त करते हैं। हीप मेमोरी में ऑब्जेक्ट्स रखे जाते हैं और वेरिएबल्स उनके रेफरेंस को हीप मेमोरी में संग्रहीत करते हैं। हीप मेमोरी का उपयोग डाटा संग्रह, ऑब्जेक्ट्स, स्ट्रिंग्स और बड़े आकार के वेरिएबल्स को संग्रहीत करने के लिए किया जाता है। हीप मेमोरी गार्बेज कलेक्शन के माध्यम से स्वचालित रूप से मुक्त होती है।
स्टैक मेमोरी छोटी होती है और इसमें मैथड कॉल्स, वेरिएबल्स के स्थानीय इंस्टैंस, पैरामीटर्स और वापसी ठीकरें संग्रहीत की जाती हैं। जब आप एक मैथड को कॉल करते हैं, तो मैथड के लिए एक फ्रेम या स्टैक फ्रेम बनाया जाता है, जिसमें स्थानीय वेरिएबल्स और आपके मैथड के निर्देशांक शामिल होते हैं। मैथड की पूर्णता के बाद, स्टैक फ्रेम स्टैक से हटा दिया जाता है और मेमोरी खाली हो जाती है। स्टैक मेमोरी अपनी प्रक्रिया की नियंत्रण जानकारी रखने के लिए उपयोगी होती है।
ये मेमोरी स्थान हीप और स्टैक के बीच अलग-अलग होते हैं और उनका उपयोग वेरिएबल्स और डेटा को संग्रहीत करने के लिए किया जाता है। हीप मेमोरी डायनैमिक रूप से आवंटित की जाती है जबकि स्टैक मेमोरी श्रमिक रूप से आवंटित की जाती है और उसकी मुक्ति स्वचालित होती है।
Heap and Stack Memory in Java मै differences क्या होता है देखते है ..
a) Memory Allocation:
Stack Memory: Local variables, method parameters, and method
call stack frames are stored in the stack memory. Memory allocation and
deallocation in the stack are managed automatically as methods are called and
returned.
b) Size and Limitations:
Heap Memory: The heap is generally larger in size compared to the stack and is limited by the available system memory. It allows dynamic allocation of memory for objects and data.
Stack Memory: The stack is smaller in size compared to the
heap and has a fixed limit defined by the JVM. It is used for static memory
allocation and follows a Last-In-First-Out (LIFO) structure.
c) Lifetime and Scope:
Heap Memory: Objects in the heap memory exist until they are explicitly deallocated by the garbage collector when they are no longer reachable. They have a longer lifetime and can be accessed from multiple methods or even multiple threads.
Stack Memory: Variables in the stack memory have a shorter
lifetime and are deallocated automatically when they go out of scope or when
the corresponding method returns. They are accessible only within the scope of
the method in which they are defined.
d) Access and Efficiency:
Stack Memory: Accessing variables in the stack is faster as
it follows a simple LIFO structure, allowing efficient memory management for
method calls and local variables.
e) Usage:
Heap Memory: The heap is used for storing objects, arrays, and large data structures. It is suitable for managing complex data that needs to persist beyond the scope of a single method.
Stack Memory: The stack is used for managing method calls, local variables, and small data structures. It is suitable for handling temporary data and maintaining the execution context of method
Heap and Stack Memory in Java मै अब हम example देखते है ....
public class Memory Demo
{
public static void main(String[] args)
{
// Example of objects stored in the heap memory
// Objects are allocated using the 'new' keyword and stored in the heap
Person person1 = new Person("example1");
Person person2 = new Person("example2");
// Example of variables stored in the stack memory
// Variables are allocated in the stack memory within the method's stack frame
int x = 10;
int y = 20;
// Method call
printSum(x, y);
// Example of a local variable within a method
// The 'name' variable is allocated in the stack memory
String name = "example";
// Example of an object reference in the stack memory
// The 'person3' variable is a reference to an object stored in the heap
Person person3 = new Person("example");
}
public static void printSum(int a, int b)
{
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
class Person
{
private String name;
public Person(String name)
{
this.name = name;
}
// Getter and Setter methods
}
Heap and Stack Memory in Java मै आपने देखा Heap and Stack Memory क्या होता है Heap and Stack Memory मै क्या differences क्या होता है Heap and Stack Memory का example देखा .....
और पढने के लिए नीचे Click करे ....
resultset types in javaJava Keyword - Java Keyword क्या होता है
Website vs Webpage vs Web Application

0 टिप्पणियाँ