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.
Ajay-Singh
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.
Ajay-Singh
> 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
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.
royjayd
ASKER
>>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
Ajay-Singh
> 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
royjayd
ASKER
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
royjayd
ASKER
is it the workflow object or the worflowDTO object which will not be collected by GC?
For many years doing Java programming never encountered any memory leak
in a pure java program.
So I would not worry about it.