Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

will 'count' variable be garbage collected?

hi guys

need some help, i want to know if 'count '  variable in below code be garbage collected?

My program structure is >>Adapter.java calls TemplateAdapter.java.
count is defined as a global variable in TemplateAdapter.java
here is the code

Adapter.java

public Workflow transformDTOToDbModel(WorkflowDTO workflowdto){
Workflow workflow = new Workflow();      
workflow.setId(workflowdto.getId());      
workflow.setTemplate(TemplateAdapter.getInstance().populateQuestionChoices(workflowdto.getQuestionChoicesDTO()));
//populate the % complete
int questionsanswered = TemplateAdapter.count;            
double percentcompl = questionsanswered / 30.0 * 100;
int  percentageComplete = percentcompl.intvalue
workflow.setPercentComplete(percentageComplete);
return workflow;
}

public class TemplateAdapter implements ITransformer<Template, TemplateDTO> {
public static int count = 0;
private static TemplateAdapter _instance;
private Logger logger = Logger.getLogger(getClass());
      
private TemplateAdapter(){}
public static TemplateAdapter getInstance(){
if(null == _instance)
_instance = new TemplateAdapter();
return _instance;
}
      

public QuestionChoices populateQuestionChoices(QuestionChoicesDTO questionChoicesDTO)
{
QuestionChoices questionChoices = new QuestionChoices();  
List<Answer> answerList = new ArrayList<Answer>();              
for (AnswerDTO answerDTO : questionChoicesDTO.getAnswers())
  {
  answerList.add(populateAnswers(answerDTO));
  }                
  questionChoices.setAnswers(answerList);              
  return questionChoices;
}      
 
public Answer populateAnswers(AnswerDTO answerDTO)
{
  Answer answer = new Answer();  
  answer.setAnswerValue(answerDTO.getDescription());
  if (answer.getAnswer() != null && answer.getAnswer().trim().length() > 0) {
count++;
  }
  return answer;
}            
      
}

Will count take a dump and cause garbage?

thanks      
Avatar of for_yan
for_yan
Flag of United States of America image

http://stackoverflow.com/questions/453023/are-static-fields-open-for-garbage-collection


      

Static variables cannot be elected for garbage collection while the class is loaded. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you use not static but instance variable, then it will be garbge collected when the instance which contains it
itself becomes eligible for garbage collection.
So in case of normal java application you shopuld assume that static variables will llive through the life of your application.
Avatar of Jay Roy

ASKER

Also i just realized i cannot make the above code work unless i make the populateAnswers() method static.
My second question would be how can i access the correct count so that i can
populate the % complete and at the same time i dont want count to cause garbage.


I'm not sure I understand - you can use static variables in the non-static method - it is not a problem
Avatar of Jay Roy

ASKER

>>>you can use static variables in the non-static method - it is not a problem
 i thought static variables can be used only in static methods?
No, it is opposite: in static methods you cannot use instance varaibles
static variables can be used everywhere even in instance of another class, just referencing this class name
And if I were you, I would worry the least about grabge collection especuially of such thing
as one integer variable - that is jvm business and it will do it for you
With static variables you should only worry if this class may be simpulatenousely accessed
by say differernt clients - in this case they may all share the same static variable.
If nothing like that can happen, then it is fine. If diiferent clinets access the same class, then
you should realize what it is that you are counting.
These considerations is good to have in mind and understand; they are more imporatnt than
garbage collection which will happen as it should without us needing to worry about it.
And the rule that
static methods you cannot use instance varaibles
is easy to understand - static method is not specific to any instance -
thisnk of it as just general description of some procedure - it
the result of it should denpend only on the imnput parameterts -
and you call it refreing only to the class in general, not refrring to any instance of this
class, and instance variable is pspecific to particular insatnce - makes no sense to use it
in general for the whole class static method.

And all that does not apply to your case - in the opposite situation there is no
limitation - you can uses staatic variables  in instance methods as much as you want -
you can for instance yuse them to calculate how many times this or that instance method of this
was called overall by all instances exsitinc in your applaications.
OK, this is all probably beyond your question, but it is good to realize all that,
so as you have doubtes i decided to explain.
Avatar of Mick Barry
Its got nothing to do with static. count is a primitive variable.
They have nothing to do with garbage collection which Objects
So no count is never going to get garbage collected
If count is instance variable of the class it will become no longer accessible
as soon as the instance of this class is garbage collected
Avatar of Jay Roy

ASKER

primitive variables dont have to worry about garbage collection? so int/double/ other primitive data types dont have to worry about GC?

In most ordinaly programs you don't have to worry about garbage collection anyway.
That's the advantage of java, that it takes care of garbage collection for you.
This is a good subject for the question in Java classes, and maybe in some
very special cases where memory becomes an issue. In my practice don't recall
having such case in years where I had to think of garbage collection.
> primitive variables dont have to worry about garbage collection? so int/double/ other primitive data types dont have to worry about GC?

no, they aren't stored on the heap so theres no garbage collection to do.
The fact that the variable is static is irrelevant
Avatar of Jay Roy

ASKER

so all the primitives are stored on the stack?
thx.
sorry was a bit unclear with that, they are held on the heap but as a part of the containing class.
It would not however get garbage collected independently like a non-primitive variable would.
Avatar of Jay Roy

ASKER

>>>It would not however get garbage collected independently like a non-primitive variable would.
so you are saying String (non-primitive) would be garbage collected and int would not be garbage collected?
can you give an example to show what you are saying

thx

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial