File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Garbage collector implementation 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 collector implementation" Watch "Garbage collector implementation" New topic
Author

Garbage collector implementation

Nilesh Dhiman
Greenhorn

Joined: Nov 01, 2009
Posts: 5
I know that implementing a garbage collector in a java program is a bad practice. But if we want to implement this logic in our code then how is it possible ?
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

Hi,

I am not entirely sure what you want,


There is a method called :



You can call this method any time to suggest the jvm to perform garbage collection.
Note that the act of garbage collection is a memory intensive operation use it sparingly.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Maybe Nilesh means that calling System.gc(); in a program is usually not a good idea, and he's right. Normally you should just leave garbage collection up to the JVM. Only add calls like System.gc(); if you are sure that you know what you are doing, and if you know that it solves a problem (don't do it just because you have a vague feeling that it's a good idea).

The built-in garbage collector of the JVM is very smart and usually knows better than you yourself when and how to cleanup objects.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nilesh Dhiman
Greenhorn

Joined: Nov 01, 2009
Posts: 5
Yes we can call the System.gc() explicitly in our code. But what if we want to create our own garbage collection function. I mean that how can we collect all those objects in our program which are no longer used or referenced by other variables; and then release the memory occupied by those objects.Here we have to use some kind of destructor logic (which is not avaiable in java) .
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

You can't write your own garbage collector inside your own Java program. Garbage collection is part of the internals of the JVM, and there is not much that you can (or should, normally) do with the garbage collection inside your Java program. Why do you want to interfere with the garbage collection process, how do you think this will make your program run better? Is your program having memory problems?

Why are you calling System.gc() in your own program? Did you add it just because you think it would be a good idea, or did you actually measure the memory usage of your program using a profiler?

If you want to tune garbage collection, search for "java garbage collection tuning".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collector implementation
 
Similar Threads
Once more CLEAN BOWLED in Garbage collection
garbage collection
Terminate garbage collector.
What is Island of Isolation In Grabage Collector
java weakness