What is Lazy Initialization? What is Lazy in this?
We all see words like 'Eager Initialization' & 'Lazy Initialization'.
How many of us think about the definitions given in these names?
Don't you think, we need to think rather blindly following certain terms & spreading them as words of God?
Even I used to think in the same manner about these terms like I read about these in books & over the internet.
Even I have said the same while writing about 'Singleton' design pattern, just have a look on that & come back here.
I agree with definition of 'Eager Initialization' i.e. something is initialized or starts to initialize at the same time you demand for that. So with this we agree that, I need this, then only I will move ahead, no matter how much time it takes to initialize.
So this way we agree that I need that initialzed value for my next immediate task to complete, so we agree to wait for this to initialize.
But think about 'Lazy Initialization'. What does it mean by Lazy here?
The way it is said or explained on various pages on Internet, it doesn't mean 'Lazy', it means 'Not Eager' or 'Not Now' i.e. we agree that I don't need this right now but may be I need it in future or may be don't need that at all. And when I need that I would need to wait to get it initialized. It is not a work of 'Lazy Employee', it is a 'Not Eager Employee', who says that I will not do this work right now, I will do this work only when you come to me & then I will make you wait till I get this job done.
So, I feel whatever the defintion I see about 'Lazy Initialization' around, it is 'Not Eager Initialization' or 'Delayed Initialization' actually.
So now question is, What is 'Lazy Initialization' then?
What benefits we get? What resources we save with such Lazy Initialization?
Are we making our processing more efficient or are we saving time or we are saving the memory?
The kind of LazyInitialization we see for Singletons, it is to save memory i.e. till you need the instance don't create that in memory. So my question here is - Why you are trying to refer the static member of this Singleton if you don't need that yet?
So such LazyInitialization must be marked as DelayedInitialization rather. Else every class we instantiate, we do it lazily.
I don't see any class being loaded in memory till we refer it or use it.
For Lazy Evaluation we have in Stream pipelining, it is again delaying the execution till you need that like don't refer to static Singleton variable till you need that. So I think it is not a work of Lazy but a smart person who is delaying certain task processing till the user needs that & again it is not promising that it will do task faster either.
So in terms of programming, these 'Delayed processing' is just saving the memory till you need it actually.
As per my understanding & perspective I am talking is about object initialization properly or may be time taking initialization of some object property, 'Lazy Initialization' is that it will return the incomplete object to you to move ahead but may keep trying to complete the initialization of the object in background as & when it gets the chance & resources. So being lazy, it doesn't mean that you will always get the initialized value when you need that but chances are quite high that you will get that immediately if sufficient time is given to that lazy initializer.
Example : Suppose you give a task, which takes 2 days, to a 'Not Eager' employee & s/he says 'OK' & you continue with your other tasks. But such employee will not start working on that task till you come to ask for that. It will not be good if you were expecting that task to be done & when you reach to that employee after 2 days, then you come to know that s/he has not yet started work on that task & now s/he starts working on that task & now you have to wait for 2 more days. Such precious time is lost due to this 'Not Eager' Employee.
May be you didn't give any other task to this employee & now your 2 more days are also wasted.
Another Scenario
Suppose you give a task which takes 2 days, to a 'Lazy' employee, then such employee will not start working on that task immediately. But again you already know this as it is tagged 'Lazy'. But this 'Lazy' employee continues working on that task as per own speed. And suppose you reach to this employee after 3 days & surprise, you get your job done.
So this is Lazy Initialization, as you were not expecting this task done in 2 days only, but you were expecting this task to be done if you come after may be 3 or 4 or 5 days. And this employee did that.
And again I will say that both approaches have their own pros & cons but again the user has to think about the approach to take while coding for different scenarios like -
a) Whether we need to initialize right away or can we postpone this.
b) Whether we need to postpone this task or have enough resources
to engange in between for sometime to keep initialization process running in background.
c) What is the probability that you will need the initialized values in future.
d) If you postpone initialization then is it fine for you to wait for the initialization when you need that.
So you will need to understand this before you take any of these paths, but need to be clear with the difference between
'Not Eager Initialization' & 'Lazy Initialization' as explained here & not given on internet generally.
If anyone wants a code sample for this, please let me know & I will provide that.
How many of us think about the definitions given in these names?
Don't you think, we need to think rather blindly following certain terms & spreading them as words of God?
Even I used to think in the same manner about these terms like I read about these in books & over the internet.
Even I have said the same while writing about 'Singleton' design pattern, just have a look on that & come back here.
I agree with definition of 'Eager Initialization' i.e. something is initialized or starts to initialize at the same time you demand for that. So with this we agree that, I need this, then only I will move ahead, no matter how much time it takes to initialize.
So this way we agree that I need that initialzed value for my next immediate task to complete, so we agree to wait for this to initialize.
But think about 'Lazy Initialization'. What does it mean by Lazy here?
The way it is said or explained on various pages on Internet, it doesn't mean 'Lazy', it means 'Not Eager' or 'Not Now' i.e. we agree that I don't need this right now but may be I need it in future or may be don't need that at all. And when I need that I would need to wait to get it initialized. It is not a work of 'Lazy Employee', it is a 'Not Eager Employee', who says that I will not do this work right now, I will do this work only when you come to me & then I will make you wait till I get this job done.
So, I feel whatever the defintion I see about 'Lazy Initialization' around, it is 'Not Eager Initialization' or 'Delayed Initialization' actually.
So now question is, What is 'Lazy Initialization' then?
What benefits we get? What resources we save with such Lazy Initialization?
Are we making our processing more efficient or are we saving time or we are saving the memory?
The kind of LazyInitialization we see for Singletons, it is to save memory i.e. till you need the instance don't create that in memory. So my question here is - Why you are trying to refer the static member of this Singleton if you don't need that yet?
So such LazyInitialization must be marked as DelayedInitialization rather. Else every class we instantiate, we do it lazily.
I don't see any class being loaded in memory till we refer it or use it.
For Lazy Evaluation we have in Stream pipelining, it is again delaying the execution till you need that like don't refer to static Singleton variable till you need that. So I think it is not a work of Lazy but a smart person who is delaying certain task processing till the user needs that & again it is not promising that it will do task faster either.
So in terms of programming, these 'Delayed processing' is just saving the memory till you need it actually.
As per my understanding & perspective I am talking is about object initialization properly or may be time taking initialization of some object property, 'Lazy Initialization' is that it will return the incomplete object to you to move ahead but may keep trying to complete the initialization of the object in background as & when it gets the chance & resources. So being lazy, it doesn't mean that you will always get the initialized value when you need that but chances are quite high that you will get that immediately if sufficient time is given to that lazy initializer.
Example : Suppose you give a task, which takes 2 days, to a 'Not Eager' employee & s/he says 'OK' & you continue with your other tasks. But such employee will not start working on that task till you come to ask for that. It will not be good if you were expecting that task to be done & when you reach to that employee after 2 days, then you come to know that s/he has not yet started work on that task & now s/he starts working on that task & now you have to wait for 2 more days. Such precious time is lost due to this 'Not Eager' Employee.
May be you didn't give any other task to this employee & now your 2 more days are also wasted.
Another Scenario
Suppose you give a task which takes 2 days, to a 'Lazy' employee, then such employee will not start working on that task immediately. But again you already know this as it is tagged 'Lazy'. But this 'Lazy' employee continues working on that task as per own speed. And suppose you reach to this employee after 3 days & surprise, you get your job done.
So this is Lazy Initialization, as you were not expecting this task done in 2 days only, but you were expecting this task to be done if you come after may be 3 or 4 or 5 days. And this employee did that.
And again I will say that both approaches have their own pros & cons but again the user has to think about the approach to take while coding for different scenarios like -
a) Whether we need to initialize right away or can we postpone this.
b) Whether we need to postpone this task or have enough resources
to engange in between for sometime to keep initialization process running in background.
c) What is the probability that you will need the initialized values in future.
d) If you postpone initialization then is it fine for you to wait for the initialization when you need that.
So you will need to understand this before you take any of these paths, but need to be clear with the difference between
'Not Eager Initialization' & 'Lazy Initialization' as explained here & not given on internet generally.
If anyone wants a code sample for this, please let me know & I will provide that.
|
|