This discussion is archived
6 Replies Latest reply: Dec 17, 2012 5:49 PM by EJP RSS

Garbage Collection

800433 Newbie
Currently Being Moderated
Hi to all
I would like to know How many objects are eligible for garbage collection from the following code.
String s="old string";
          s=s.replace('6', 'r');
          String s1="new string";
          s1=s1.replace('e', '3');
          
          Serializable serializable=(Serializable) "serializable";
          String s2=serializable.toString();
          serializable=null;
          
          String replaceble="replaceble";
          StringBuffer sb=new StringBuffer(replaceble);
          replaceble=null;
          sb=null;
          
          StringBuffer sb1=new StringBuffer("buffered string");
          String s3=sb.toString();

Thanks in Advance.
  • 1. Re: Garbage Collection
    EJP Guru
    Currently Being Moderated
    No free homework answers here. What do you think, and why?
  • 2. Re: Garbage Collection
    800433 Newbie
    Currently Being Moderated
    I wanted to know how object is garbage collected. If this question looks home work for you, practice and tell me. Other wise let others give answers.
  • 3. Re: Garbage Collection
    DrClap Expert
    Currently Being Moderated
    user10137999 wrote:
    I wanted to know how object is garbage collected.
    Then that isn't a good example to work with. You're going to get all mixed up with the string pool and the question of whether string literals are created by the compiler. The question of objects created as part of the internal implementation of the String and StringBuffer arises as well.
  • 4. Re: Garbage Collection
    EJP Guru
    Currently Being Moderated
    If this question looks home work for you, practice and tell me.
    I don't need the practice thanks. It is you who needs the practice, in asking questions that are going to get answers.
    Other wise let others give answers.
    I'm not stopping anyone from giving answers. I couldn't if I wanted to. I'm just telling you what you're going to get on this forum, from long experience. The concensus here has always been that is pointless for anybody to just give you the answer. The point of the homework is the thought process, and it has to be started by you. You have to learn it for yourself. You can't just keep asking on forums for the rest of your working life. I learnt IT many years before there were any forums at all, so it is quite possible.

    I agree with DrClap that this is a particularly poor example for teaching garbage collection. An example not involving Strings would be much more illuminating.

    The other reason for not answering your question directly is that it has already been asked here and elsewhere thousands of times. (For some reason teachers seem to think it a good question. One wonders whether some of them know the correct answer themselves; not that I care because it isn't a particularly useful answer to know.) You don't show any sign of having even looked for a prior answer. In short you have done zero work yourself, and until that changes you are most unlikely to get an answer here.
  • 5. Re: Garbage Collection
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    I learnt IT many years before there were any forums at all, so it is quite possible.
    Ah the good old days where people actually made an effort and were happy to do so. A dying breed.

    I don't understand these legions of people that seemed to have gotten it into their brain that it is actually remotely possible to learn this stuff in forums. Where are these misguided saps coming from? Even in the universe of Dragon Ball Z the dudes have to hit the books to learn stuff, it ain't the television that is teaching this madness.
  • 6. Re: Garbage Collection
    EJP Guru
    Currently Being Moderated
    We used to fight over the manuals as there weren't enough to go round ... Now that there are manuals and specifications and tutorials all over the Web nobody will read them.

    On the other hand I was the only guy in town with a copy of Knuth, nobody else had the wit to buy one, although they could see it on my desk, and they all wondered how I knew all the standard algorithms. I guess that corresponds with the apparent fact that the better the Internet source the less likely it is to be read.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points