Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

can any one explain me garbage collection with the help of a simple program ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one explain me garbage collection with the help of a simple program ?
 
lowercase baba
Posts: 13063
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no - because you never really see garbage collection (gc). That's the whole point. It happens behind the scenes and you don't have to worry about it. Therefore, there is nothing in a program for you to see.

if you want a better idea of how it works, just search around these forums, and you will find TONS of threads on gc.

 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if you want to know what garbage collection does or how it does it?

Fred is right you can't really demonstrate it because it all happens in the JVM we never see it.

What it does is similar to the Windows Disk Defragmentor. As objects, stack frames and arrays are allocated and deleted the free space in the virtual machine gets fragmented into smaller pieces. Garbage collection is the task that moves the data in memory around to consolidate free space.
 
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although you are correct that garbage collection consolidates free memory, it is more than a defragmenter. It has to identify which object (occupying memory) are no longer in use, and release the memory they are using.
I have found this link, which might be useful (or might be the wrong link ) about garbage collection. Note that garbage collection is a standard technique used in many virtual machines.
 
Joe Areeda
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, of course, Campbell.

It's hard to determine when simplification helps and oversimplification hurts. I assumed (alway a potential problem) from the question that the OP was looking for basic information.

That link you posted seems to discuss the problems with GC in real time applications. It's interesting and informative but may be too narrow.

This one is more basic.

Perhaps we'll elicit some follow up questions.

Joe
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Areeda wrote:You're right, of course, Campbell.

It's hard to determine when simplification helps and oversimplification hurts. I assumed (alway a potential problem) from the question that the OP was looking for basic information.

That link you posted seems to discuss the problems with GC in real time applications. It's interesting and informative but may be too narrow.

This one is more basic.

Perhaps we'll elicit some follow up questions.

Joe


i know what it does but i wanna know , how we gonna know that this particular object is no longer getting referenced
for eg
MyDate date = new MyDate(14 7 2011);
new MyDate(15 7 2011);
now the 1st one (object) is getting referenced by "date" and 2nd object is not getting referenced by any reference variable
is this correct that now gc(garbage collector will de-allocate momory)for the object which is no longer getting referenced ?
 
fred rosenberger
lowercase baba
Posts: 13063
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not an expert on this, but I believe there are multiple ways the gc can be implemented, possibly even within a JVM and which is used determined at runtime.

but basically, the way I understand it is that there is a list of objects that exist. There is a list of references. You could go through the list of references, and mark each object to which it points. When you're done, you clean up any object that has not been marked.

It's a little more complicated, because you could have a references IN objects. If a.ref points to b, and b.ref points to a, but nothing points to either a or b, the objects are unreachable, but still have references to them. The JVM can figure that out and gc them.

You should also be aware that there is no guarantee that gc will run during your programs execution. even calling System.gc() [or whatever the method is] won't FORCE it to run.

You are only guaranteed that gc will run before you get an out-of-memory error.
 
Campbell Ritchie
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Areeda wrote: . . . This one is more basic. . . .

Haven't got the time to read those tutorials, but a more basic one is probably what the poster wants. Thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic