Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default Garbage Collector

    hello guys.i have a question.

    in this code i can count how many object is made:
    Java Code:
    public class Test{
    int count=0;
    public Test(){
    count++;
    }
    }
    now i want to count how many object is deleted by GC.has anybody an idea how to do that?
    thanx

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Quote Originally Posted by giorgi View Post
    in this code i can count how many object is made:
    No, you can't... count will be 1 in every Test object you create with that constructor.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You can get statistics like this using profiling tools - netbeans has a nice suite of them built in! :D

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  5. #5
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by kjkrum View Post
    No, you can't... count will be 1 in every Test object you create with that constructor.
    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++;
    }
    }
    i know that GC deetes an object when the reference on it is lost,but sometimes may GC does not.now my exercise is to count how many objects are deleted by GC.do you understand what i want?it is my school exercise

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by giorgi View Post
    i know that GC deetes an object when the reference on it is lost,but sometimes may GC does not.
    Sometimes it doesn't? Does the garbage collector have a free will or something? eeeeeewww!

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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);
    But then, whenever your program exits, the garbage collector gets rid of all of your objects, references and variables so again the number would be the same as your number of objects made.
    • 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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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!

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Ha HA!
    Finally!
    I got in first!

    (Tolls does pointless and ever-so-slightly-embarrassing victory dance)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    (Tolls does pointless and ever-so-slightly-embarrassing victory dance)
    This looks ever so silly; aren't you ashamed of yourself? And get off that piano.

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    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);
    As i know the GC is free in its work,so we are not sure if it works when the ref1=null.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by giorgi View Post
    As i know the GC is free in its work,so we are not sure if it works when the ref1=null.
    That code doesn't even compile because ref1 is out of scope when it's tested for being null.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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? ;)

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Junky View Post
    P.S. Why is there no post #13? ;)
    I have no idea; maybe the forum owners are superstitious; I bet this reply isn't visible either.
    Garbage collector you said?

    kind regards,

    nobody
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Ooh.
    Has the software changed?
    (And I can see a post #13)

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Ooh.
    Has the software changed?
    (And I can see a post #13)
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    No, I mean the forum software.

    I never touch gc magic (except via a profiler, or heap analysis).
    You can catch nasty things otherwise.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    No, I mean the forum software.
    Ah, yes, I don't know why the look has changed, the old look was fine with me, especially with all those google ads removed lately (now they're back again).

    kind regards,

    Jos

    ps. and there are too many icons around that seem to mean something ...
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. about garbage collector
    By sudharani in forum New To Java
    Replies: 1
    Last Post: 04-25-2011, 12:25 PM
  2. Garbage Collector tuning
    By javaOrC in forum Advanced Java
    Replies: 45
    Last Post: 03-03-2011, 12:51 AM
  3. In defense of the Garbage Collector
    By Katanagas in forum Advanced Java
    Replies: 2
    Last Post: 10-25-2010, 07:40 PM
  4. Q about Garbage Collector
    By m00nchile in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 06:57 AM
  5. Garbage Collector and finalize()
    By arefeh in forum New To Java
    Replies: 5
    Last Post: 01-09-2010, 10:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •