• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collector  RSS feed

 
peterwil son
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai everyone. I am new to this forum. Can anyone tell me the usage of garbage collectors in java
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you start with any good Java book or this not very famous resource first, so we can talk about more specific things?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We could, and some gladly would, talk for days about the ins and outs of the garbage collector. But the short answer is memory management.

In java don't have to explicitly manage our memory usage in the same way as we do in other languages such as C. The gc's job is to go round and clean up (free memory) from objects that are not being used anymore, specifically objects that are no longer referenced in your code.

There are a number configuration options for the gc to help you optimise its behavior and that's where the discussion can take some time. For the most part you won't need to worry about it at all, just let it do it's thing.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just let it do it's thing

i agree completely. it is very good at doing it's job. btw, calling gc() does not ensure that it will run.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peterwil

Welcome to Java ranch !!

may be link can help you getting started with GC in Java

http://www.artima.com/insidejvm/ed2/gc.html

Happy learning !!

Thanks
Abhay Agarwal


 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:it is very good at doing it's job. btw, calling gc() does not ensure that it will run.

I agree that its doing good job, but I think it would be better to also have retain/release/autorelease mechanism which will allow to count references by yourself and release(deallocate) object right away, because as everybody knows GC cleans up memory with delay.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surlac surlacovich wrote:
Randall Twede wrote:it is very good at doing it's job. btw, calling gc() does not ensure that it will run.

I agree that its doing good job, but I think it would be better to also have retain/release/autorelease mechanism which will allow to count references by yourself and release(deallocate) object right away, because as everybody knows GC cleans up memory with delay.


What make you think that doing it yourself will NOT result in a delay?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surlac surlacovich wrote:I agree that its doing good job, but......

And so it begins. Buckle up children, you're in for a bumpy ride.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surlac surlacovich wrote:
Randall Twede wrote:it is very good at doing it's job. btw, calling gc() does not ensure that it will run.

I agree that its doing good job, but I think it would be better to also have retain/release/autorelease mechanism which will allow to count references by yourself and release(deallocate) object right away, because as everybody knows GC cleans up memory with delay.

The garbage collector in current Oracle JVM versions is much more sophisticated than you think. Garbage collection is a huge topic for which a lot of research has been done. So, it's hard to draw any conclusions about how efficient it is without being an expert. (I'm certainly not an expert with regard to GC).

Since Java 6 update 23, the Oracle JVM also includes escape analysis - a technique that checks if short-lived objects can be allocated on the stack instead of on the heap, which makes cleaning them up very cheap (in fact, it's automatic, when the method returns and the call stack unwinds). That sounds more or less like what you're proposing.

Fortunately escape analysis is also automatic, it would be really cumbersome if we as programmers would have to spend time thinking about low-level optimizations all the time.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!