Results 1 to 20 of 28
Thread: Garbage Collector
- 05-17-2011, 11:02 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
- 05-18-2011, 03:06 AM #2
- 05-18-2011, 03:56 AM #3
You can get statistics like this using profiling tools - netbeans has a nice suite of them built in! :D
- 05-18-2011, 07:22 AM #4
I'm not sure why you're trying to count objects that got deleted. As far as I know theres no command to check if an object was deleted.
If you set a reference to null, or reference it to something else the abandoned object will get collected by the garbage collector.Last edited by Dark; 05-18-2011 at 09:35 AM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-18-2011, 05:48 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
you are right.i missed one thing.now it works and counts what i want.the code is
Java Code:public class Test { static int count=0; public Test(){ count++; } }
- 05-18-2011, 06:04 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-18-2011, 06:09 PM #7
The roundabout answer is no, as it seems from the general consensus. Every object that is abandoned is collected by the Garbage Collector.
If you need to display it somehow the only real way would be either to display the amount of objects created, or make another counter to go up every time a reference to an object is changed.
IE
Java Code:for (int i = 0; i < 5; i++) { Object ref1 = new Object(); garbageCount++; } if (ref1 != null) garbageCount--; System.out.println("Objects collected by the garbage collector: " + garbageCount);
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-18-2011, 07:01 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
You could try finalize () (**spit**).
However, this will change how garbage collection works...and isn't actually run at the moment the thing has been gced. It's simply run when it gets to its turn in the finalisation queue is reached. And then it becomes eligible for gc.
But if you want something to show that an object has become eligible for gc, then that might do the job. Note, do not try this in real code without knowing what you're doing.
- 05-18-2011, 07:06 PM #9
You may be able to achieve what you want by overriding Object.finalize().
To be completely correct, you should increment and decrement your object count in synchronized static methods. Read the section titled "Publication and data visibility" on this page.Get in the habit of using standard Java naming conventions!
- 05-18-2011, 07:14 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Ha HA!
Finally!
I got in first!
(Tolls does pointless and ever-so-slightly-embarrassing victory dance)
- 05-18-2011, 07:30 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-18-2011, 10:35 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
- 05-19-2011, 09:19 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-19-2011, 09:29 AM #14
Rule #1 of Garbage Collection: forget about it!
You do not need to know about it.
You do not need to worry about it.
You DEFINATELY should not write code to change it in any way.
All that matters is that GC works.
P.S. Why is there no post #13? ;)
- 05-19-2011, 09:49 AM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-19-2011, 10:32 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Ooh.
Has the software changed?
(And I can see a post #13)
- 05-19-2011, 11:10 AM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Junky gave a very good tip: forget about the garbage collector; simply assume an infinite amount of memory. People new to garbage collection want to 'help' it circumvent it with disastrous results. Modern garbage collectors are very well capable of doing their job (almost) invisible, just like repy #13.
kind regards,
Jos ;-)Build a wall around Donald Trump; I'll pay for it.
- 05-19-2011, 11:26 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
No, I mean the forum software.
I never touch gc magic (except via a profiler, or heap analysis).
You can catch nasty things otherwise.
- 05-19-2011, 11:35 AM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Build a wall around Donald Trump; I'll pay for it.
- 05-19-2011, 11:41 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
I only noticed because I couldn't find the buttons in their usual place, and I had to log in.
Then again, I never notice when Mrs Tolls moves the furniture around while I'm away in the week. She only does it to see if I notice, and I never do. Well, until I drop my beer on the floor because the table that used to be there has moved.
Similar Threads
-
about garbage collector
By sudharani in forum New To JavaReplies: 1Last Post: 04-25-2011, 12:25 PM -
Garbage Collector tuning
By javaOrC in forum Advanced JavaReplies: 45Last Post: 03-03-2011, 12:51 AM -
In defense of the Garbage Collector
By Katanagas in forum Advanced JavaReplies: 2Last Post: 10-25-2010, 07:40 PM -
Q about Garbage Collector
By m00nchile in forum New To JavaReplies: 4Last Post: 02-05-2010, 06:57 AM -
Garbage Collector and finalize()
By arefeh in forum New To JavaReplies: 5Last Post: 01-09-2010, 10:04 PM
Bookmarks