Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection in Java (Heap/Stack)

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic