aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes program to show Objects available for GC(garbage collection). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "program to show Objects available for GC(garbage collection)." Watch "program to show Objects available for GC(garbage collection)." New topic
Author

program to show Objects available for GC(garbage collection).

Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
As we dont know that when will GC takes place but we do know that automatic garbage collector calls the finalize()method in an object that is eligible for garbage collection before it actually destroys the object,so we can use this utility of finalize() method to show the info. that actually how many objects are eligible for GC.
I have written a program (with help of eg no.9.2 from book by Khalid A Mughal) to demonstrate this fact :
Now ,at first we have created a class a with two static fields idCount , population which will keep track of current no. of objects.
Then we will take two command line parameters as input ,1st will be no. of objects to be created and 2d will be size of each object.
Since we don't know that GC will ever take place so we have taken size of the object which will force JVM to call objects for termination.The first output shows that all the 5 objects were created and were not garbage collected thus they are eligible for GC (printed at line no.37),now after increasing the size and no of objects to 15 we can see that in output 2 that objects 0 and 1 were called for garbage collection to freeup some space in heap.







No Dream Too Big
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
Output1

c:\>java b 5 5000000
0HELLO
1HELLO
2HELLO
3HELLO
4HELLO
5 : Objects alive i.e available for GC

Output2
c:\>java b 10 50000000
0HELLO
1HELLO
2 : Objects alive i.e available for GC
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
1bye
0bye
Here we can see that 2 objects that were eligible for GC have been terminated or garbage collected
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Bravo for the link to this thread, RE: Software to display objects eligible for GC.

I compiled and ran your code, and it displays error:

0 : Objects alive i.e available for GC
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at b.main(b.java:29)



Of course, I will trace the code to figure out why this error is happening. Since I am unfamiliar with this concept (i.e. I'm still on chapters 1 and 5 of Sierra/Bates), kindly provide some guidance to understanding this concept.

Thanks!


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Actually, I am unable to grasp your code.

Are we supposed to pass arguments to the execution of the program, i.e.

java b arg1 arg2 arg3

How is this program supposed to work. I am perplexed
Samrat Debroy
Greenhorn

Joined: Oct 23, 2009
Posts: 13
How is this program supposed to work. I am perplexed


Sandra, I presume you are using Eclipse to run this and somehow the arguments are not getting passed properly.
You may set the arguments in the Run Configuration in Eclipse.
But for SCJP, it is advised not to use any advanced IDE like Eclipse.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@ Samrat: I'm using text pad and compiling and running using terminal on my Mac.
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
Sandra Bachan wrote:Actually, I am unable to grasp your code.

Are we supposed to pass arguments to the execution of the program, i.e.

java b arg1 arg2 arg3

How is this program supposed to work. I am perplexed

actually no!!
run it in your terminal like this
c:\>java b (no of objects to be created i.e here 5) (size of each object)
here i am providing screen shots of outputs



[Thumbnail for Screenshot-abhinav@abhinav-laptop_ ~-1.png]


[Thumbnail for Screenshot-abhinav@abhinav-laptop_ ~.png]

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

As an exercise - 'What flaw does this program have ? '

Hint: What happens when one object is created and a parallel thread tries to GC some other object at the same time.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Thanks!

Now that I got it to work, I'll try it out
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: program to show Objects available for GC(garbage collection).