Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Singleton Lazy vs Eager creation

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading the book Jeanne Boyarsky, Scott Selikoff-OCP_ Oracle Certified Professional Java SE 8 Programmer II Study Guide_ Exam , in which there is a part talking about singleton and  present the eager and lazy creation of a singleton instance, and they said that "

Lazy instantiation reduces memory usage and improves performance

" , which I didn't understance, how would lazy creation reduce memory usage... since we will have the two instances when calling get instance, for example : if Eager and Lazy are two Singleton classes which create an eager/lazy instance of a singleton then when we call :

in this case we have the two instances in memory, so where is the improvment of Lzay ?
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yas sine wrote:. . . . . .

I think that should readThere is no point in creating an instance eagerly and not using it. That will occupy memory for the entire lifetime of your application, both for the Class<Eager> object and the Eager object. Whenever you need an instance, it will be easy enough to create by lazy execution.

Does the book explain why an enum can be better than singletons?

[edit]I think the book means that you don't need the instance of Eager at all. It means you only create the Lazy instance. I think.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thank you for responding, But here is what I understand (or expect ) when talking about lazy/eager and performance :
when using an Eager instance (for example in line 3) it will be their occuping memory even if we will net using it until line 30( for example), even worse, if we are using it in a try/catch bloc then it may never be called, eventhough, it occupied memory, by contrast to lazy creation which create the instance in the specific time when we need it (at line 30).
That's what I get, however when I debug the code I notice that the two instances (eager and lazy) are created and returned (therefore occupied memory).
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, I think you don't need both lines of code. Doesn't the book give you more explanation?
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yas,
The point we are trying to make in the book is that the lazy one doesn't occupy memory until you first use it (if you do at all). In a tiny program, this doesn't help much. But imagine a program that has a bunch of singleton and only uses them under certain circumstances. if those circumstances don't arise, you'd save memory with the lazy approach because it would never get instantiated.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for trying to explain, But I really don't get the point, the point is how could these two declaration :

be different, regarding memory usage and performance ?
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already told you: only use one of those declarations, not both.
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yas sine wrote:Thank you for trying to explain, But I really don't get the point, the point is how could these two declaration :

be different, regarding memory usage and performance ?



I expect that you are asking for a comparison between



and



Am I right?

If so then I'm sorry, I don't have an answer for you. Both of them, as far as I know, return the instance of the singleton class, so after each of them is executed, the instance exists and occupies memory.

I guess there's a possibility that with the Eager version, the instance might have been created at an earlier point in time, perhaps when the Eager class was loaded. That's generally what eager loading is about. But since Eager is said to be a singleton class, it's unlikely that the Eager class would have been loaded earlier for some other reason than creating the singleton instance. However I don't have the book in front of me so maybe there's some more discussion of this issue in the book.
 
Jeanne Boyarsky
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:However I don't have the book in front of me so maybe there's some more discussion of this issue in the book.


No, this is the gist.

The point is that in a large program, you don't necessarily run that line of code right away.
 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:The point is that in a large program, you don't necessarily run that line of code right away.



It's still possible to be confused as to why lazy loading makes any difference. You don't run that line of code right away for an eager-loading version either.
 
Rancher
Posts: 4625
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless there's some other mechanism at work loading classes earlier, or if the class has some other static member that can be accessed (not something I've seen happen) then both the lazy and eager will end up the same.
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:. . . in a large program, you don't necessarily run that line of code right away.

. . . And in a small program, you don't necessarily run that line of code at all.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

yas sine wrote:Thank you for trying to explain, But I really don't get the point, the point is how could these two declaration :

be different, regarding memory usage and performance ?



I expect that you are asking for a comparison between



and



Am I right?

If so then I'm sorry, I don't have an answer for you. Both of them, as far as I know, return the instance of the singleton class, so after each of them is executed, the instance exists and occupies memory.

I guess there's a possibility that with the Eager version, the instance might have been created at an earlier point in time, perhaps when the Eager class was loaded. That's generally what eager loading is about. But since Eager is said to be a singleton class, it's unlikely that the Eager class would have been loaded earlier for some other reason than creating the singleton instance. However I don't have the book in front of me so maybe there's some more discussion of this issue in the book.



Yes you are right this is exactly what I'm asking ( I will not use the two instances at the same time), and according to your response the two instances will be loaded in memory when declared, so what is the improvement that Lazy creation bring, in fact I do not see (apart from code) any difference between lazy and eager creation.
 
Dave Tolls
Rancher
Posts: 4625
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jeanne Boyarsky wrote:. . . in a large program, you don't necessarily run that line of code right away.

. . . And in a small program, you don't necessarily run that line of code at all.



In which case the class won't be loaded at all, so neither the Lazy nor Eager versions will be created.

yas sine wrote:
Yes you are right this is exactly what I'm asking ( I will not use the two instances at the same time), and according to your response the two instances will be loaded in memory when declared, so what is the improvement that Lazy creation bring, in fact I do not see (apart from code) any difference between lazy and eager creation.



Unless there is some other cause for the class to be loaded (eg some static constant that is accessed, or a framework that loads the class) then you are right.
They will both be created when the getInstance is called.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless there is some other cause for the class to be loaded (eg some static constant that is accessed, or a framework that loads the class) then you are right.
They will both be created when the getInstance is called.

Thank you for responding. To conclude this discussion , there is no deference regarding performance and memory usage between eager and lazy creation of a singleton
 
Dave Tolls
Rancher
Posts: 4625
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no.
As I said, if there was some other bit of data being accessed then the Eager would get its instance created, but the Lazy wouldn't.
eg

Then if the code referred to Eager.SOME_STATIC_THING the INSTANCE would be created, whereas in a similar set up for a Lazy singleton it would not.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In first approach eager.getInstance() instance will be created even though application might not be using which leads to memory usage for no use. Instance need to created when application need it which is achieved through Lazy instantiation where the objects get created at runtime. In Lazy Initialization, getInstance method verifies the instance existence instead of creating new one directly. If instance already existing it returns old instance.  
 
yas sine
Ranch Hand
Posts: 38
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Well, no.
As I said, if there was some other bit of data being accessed then the Eager would get its instance created, but the Lazy wouldn't.
eg

Then if the code referred to Eager.SOME_STATIC_THING the INSTANCE would be created, whereas in a similar set up for a Lazy singleton it would not.



That's the response that I'm looking for, I understand now how Lzay creation would optimise memory usage and improve perdormance.
 
Bartender
Posts: 2230
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Then if the code referred to Eager.SOME_STATIC_THING the INSTANCE would be created


Well, no.
A public static final String would be inlined.

This prints:
thing

This prints:
Instance created
thing

This is mandated by JLS:

JLS SE 9 in 13.1 wrote:A reference to a field that is a constant variable (§4.12.4) must be resolved at compile time to the value V denoted by the constant variable's initializer.

If such a field is static, then no reference to the field should be present in the code in a binary file, including the class or interface which declared the field.



But this is only the case with primitives and Strings.
 
Dave Tolls
Rancher
Posts: 4625
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gah!
I knew I should have tested that!


OK...not a String then, but some other Object.

Which makes the Lazy version even less likely to be needed.
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic