Memory Management System in Java
Memory managment is most importent thing in java .Because when we write programe, we usually focus on making the code work. We create…
Memory Management System in Java
introduction
Memory managment is most importent thing in java .Because when we write programe, we usually focus on making the code work. We create variables, call methods, and make objects without thinking much about where everything is stored. But behind the scenes, memory plays a very important things in the java. If we not manage memory handled properly, programs can become slow or even crash.That is why understanding stack memory, heap memory, and garbage collection is useful, even if we are beginners.From that we can manage memory properly and write succsesful code.
Stack memory
So, let’s we talk about stack memory. Stack memory is like a neat pile of plates. You put a plate on top each other, and when you need one, you can take the top plate first. In the Java, the stack works in the same way. It follows something called “last in, first out.” When a method is called, Java puts its details on the stack. This includes local variables, method parameters, and return information. When the method finishes, everything related to that method is removed from the stack automatically. Because of this memory managment sysytem have simple behavior, stack memory is very fast and easy to manage. Also, each thread in Java has its own stack, so one thread cannot directly mess with another thread’s in stack memory.
Heap memory
And another memory is heap memory. Heap memory is used to store objects. Whenever we use the new keyword or characters in Java, that object is created in the heap. This is not stack memory, this heap is shared by all threads. This makes it more flexible, but also it is be more complex. Objects in the heap do not disappear when a method ends. They stay there as long as they are needed. For the example, if one method creates an object and another method need to uses it, that object must public to in memory. Because of this, memory managing heap memory is manually would be very difficult and risky.
Grabage Collection
So , what is grabage collection ?, Garbage collection is Java’s way of cleaning up unused objects automatically. If an object in the heap memory and it is no longer being used by the written program, it will becomes garbage. The garbage collector looks for such objects and frees up their memory.so ,then developers do not need to delete objects manually like in some other programming languages. This can reduces mistakes such as memory leaks or accessing invalid memory.
Garbage collection begins with important points known as roots, such as static data and active variables. It then identify which objects can still be reached.The written program variables that are unable to access an object are detect useless and then it is will eliminated. In the background, this grabge collection process runs automatically without manualy. we usualy not notice it, it help and contributes to the program’s overall health.
So that is basic system is memory managemnt in java.if we summerize this systems, The stack memory is fastest and temporary managment system ,and heap memory is flexible and long lasting memory management system.So grabge collection is keep the memory and written code clean,it is very importance things for devolpers.From that we can understand about how memory mangement in java and how we use it properly.
메타데이터
- post_id
- 2cecbf1ea197
- slug
- memory-management-system-in-java-2cecbf1ea197
- url
- https://medium.com/@shamithasrimal1/memory-management-system-in-java-2cecbf1ea197
- canonical_url
- https://medium.com/@shamithasrimal1/memory-management-system-in-java-2cecbf1ea197
- author_url
- https://medium.com/@shamithasrimal1
- status
- ok
- fetched_at
- 2026-08-10 15:03:03