We help IT Professionals succeed at work.

memory leak: which is better?

royjayd
royjayd asked
on
289 Views
Last Modified: 2012-05-11
hi guys

which is better? case1 or case2. will any of them cause memory leaks?

case1
public class ManagerBusinessService extends BaseBusinessService{//defined globally
//defined globally
private Workflow workflow;
private WorkflowDTO worflowDTO = null;

private WorkflowDao workflowDao;
public void setWorkflowDao(WorkflowDao workflowDao) {
this.workflowDao = workflowDao;
}

public WorkflowDTO fetchDetailsTx(String Id){                   
int workflowId = 0;            
flexworkfloId = getWorkflowNumbers(Id);                  
workflow = workflowDao.loadWorkflowDetailsTx(flexworkfloId);      
worflowDTO = WorkflowDetailAdapter.getInstance().transformDbModelToDTO(workflow);            }
return worflowDTO;
}
...

case2
public class ManagerBusinessService extends BaseBusinessService{
private WorkflowDao workflowDao;
public void setWorkflowDao(WorkflowDao workflowDao) {
this.workflowDao = workflowDao;
      }

      
public WorkflowDTO fetchDetailsTx(String Id){
//defined locally
Workflow workflow;
WorkflowDTO worflowDTO = null;                   
int workflowId = 0;            
flexworkfloId = getWorkflowNumbers(Id);                  
workflow = workflowDao.loadWorkflowDetailsTx(flexworkfloId);      
worflowDTO = WorkflowDetailAdapter.getInstance().transformDbModelToDTO(workflow);      
}
return worflowDTO;
}
...
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
If you need some fields only within one method it is better to keep them local to that method.

For many years doing Java programming never encountered any memory leak
in a pure java program.

So I would not worry about it.
To my reading, memory consumption-wise both are same, in case #1, you
are creating "workflow" instance anyway, which is a local variable. case
#2, you have defined a local variable and created anyway.

Now, case #1, might lead to thread-safety issues as workflow variable
is shared.

On memory leak, there is no *object* leak here.
> On memory leak, there is no *object* leak here.

Sorry, just want to correct myself, the first implementation has object
leak, as ManagerBusinessService is global reference which keeps a
instance of workflow. So, there will be one extra object that will not
be reclaimed by GC
Awarded 2011
Awarded 2011

Commented:
But this is not a memory leak - it will take memory as long as it is needed.
And then memory will be released when it is not needed.
It is not like some C programs which with ceratin bad code or bugs
 may consume more memory with time.
With Java it never happens, so there is no reason to worry about it.

Author

Commented:
>>So, there will be one extra object that will not be reclaimed by GC
are you saying
private Workflow workflow;  --this will not be claimed by GC?
private WorkflowDTO worflowDTO = null;  --this will be claimed by GC?

thx
> With Java it never happens, so there is no reason to worry about it.
If that is the case, there should not be any java memory profiler. Java
doesn't have malloc/de-alloc issues, however references are retained if
GC thinks so.
> private Workflow workflow;  --this will not be claimed by GC?
> private WorkflowDTO worflowDTO = null;  --this will be claimed by GC?
Not true. If you set reference to a value, and forget to set it to null,
it will not reclaimed by GC

Author

Commented:
Ajay- you said
>>ManagerBusinessService is global reference which keeps a instance of workflow. So, there will be one extra object that will not be reclaimed by GC.

can you elaborate little more and tell me which object will NOT be reclaimed by GC?

thx

Author

Commented:
is it the workflow object or the worflowDTO object which will not be collected by GC?
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
actually just reread your code and your loading the workflow the same in both.

In which case (based on what you have posted) the second method is better. There is no need for them to be member vars in that code
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
On the surface neither would create a memory leak but the first has potential to. for example if WorkflowDao kept a reference to things it had loaded
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.