wood burning stoves 2.0*
The moose likes Java in General and the fly likes Garbage Collection in Java (Heap/Stack) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage Collection in Java (Heap/Stack)" Watch "Garbage Collection in Java (Heap/Stack)" New topic
Author

Garbage Collection in Java (Heap/Stack)

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
I am curious to know

1. If the Garbage Collection process frees up memory in Heap only or in stack as well?

2. If it doesn't free up the memory in Stack, then what happens if the stack gets full?

3. And, what is the way to free up the memory in Stack?
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Surely I'm not an expert but I cite
Stack values only exist within the scope of the function they are created in. Once it returns, they are discarded.


1. GC doesn't frees stack.
2. StackOverFlowwException.
3. The stack is empty when the function returns.


Best regards,
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Carles Gasques wrote:Surely I'm not an expert but I cite
Stack values only exist within the scope of the function they are created in. Once it returns, they are discarded.


2. StackOverFlowwException.


Correction - It's StackOverflowError


~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

3. And, what is the way to free up the memory in Stack?


Memory allocation / de-allocation is handled internally by the JVM. You do not have to bother about it unlike in C++ where you explicitly need to dereference pointers to free up memory. The stack simply dissolves once the method completes. Read about the structure of the JVM
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
@Mansukhdeep Thind
Thanks for the correction I have misspelled the word and confused Error with Exception.
My English is deficient and my java knowledge seems to go at the same pace :-)

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Vaibhav G Garg wrote:I am curious to know
1. If the Garbage Collection process frees up memory in Heap only or in stack as well?
2. If it doesn't free up the memory in Stack, then what happens if the stack gets full?
3. And, what is the way to free up the memory in Stack?

Curiosity is fine, so don't let me stop you; but, as Manshukdeep has already pointed out, understanding the mechanics of the garbage collection system (and there are several) isn't likely to make you a better Java programmer - indeed, obsessing about it may actually distract you from that purpose. The garbage collector is there to make sure that, except in rare situations, you don't have to worry about memory.

It's also worth pointing out that the gc does NOT necessarily release memory for general use; it releases it for JVM use. Many people wonder why, when the gc has supposedly released memory, it's not registered on system-based memory managers (such as Windows' Task Manager).
And the reason is: it was never designed to.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection in Java (Heap/Stack)