Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is lazy initialization and why is it useful?

Tags:

.net

What is lazy initialization of objects? How do you do that and what are the advantages?

like image 863
SNA Avatar asked Jun 11 '09 00:06

SNA


People also ask

Why should I use lazy initialization?

Lazy initialization of an object means that its creation is deferred until it is first used. (For this topic, the terms lazy initialization and lazy instantiation are synonymous.) Lazy initialization is primarily used to improve performance, avoid wasteful computation, and reduce program memory requirements.

Is lazy initialization good practice?

From MSDN: IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the object after creation. You must lock the object before accessing it, unless the type is thread safe.

What is lazy initialization of object in Java?

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources.

What is the use of Lazy?

Use lazy initialization to defer the creation of a large or resource-intensive object, or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program. To prepare for lazy initialization, you create an instance of Lazy<T>.


2 Answers

Lazy Initialization is a performance optimization where you defer (potentially expensive) object creation until just before you actually need it.

One good example is to not create a database connection up front, but only just before you need to get data from the database.

The key reason for doing this is that (often) you can avoid creating the object completely if you never need it.

like image 75
Bevan Avatar answered Oct 13 '22 06:10

Bevan


As others have mentioned, lazy initialization is delaying initialization until a component or object is used. You can view lazy initialization as a runtime application of the YAGNI principle - "You ain't gonna need it"

The advantages from an application perspective of lazy initialization are that users don't have to pay the initialization time for features they will not use. Suppose you were to initialize every component of your application up front. This could create a potentially long start time - users would have to wait dozens of seconds or minutes before your application is ready to use. They're waiting on and paying for initialization of features they may never use or not use right away.

Instead, if you defer initializing those components until use time, your application will start up much quicker. The user will still have to pay the startup cost when using other components, but that cost will be amortized across the run of the program and not condensed into the beginning, and the user may associate the initialization time of these objects with the features they are using.

like image 39
Michael Avatar answered Oct 13 '22 04:10

Michael