views:

1352

answers:

9

What is Lazy Loading?

[Edit after reading a few answers] Why do people use this term so often?

Say you just use a ASP/ADO recordset and load it with data or ADO.NET Datasource for a gridview.

I guess I should have asked why people use the term Lazy Loading, what "other" types are their?

+3  A: 

wikipedia's Definition Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. ...

http://en.wikipedia.org/wiki/Lazy%20loading

Kevin Goff
+8  A: 

Lazy Loading is a programming practice in which you only load or initialize an object when you first need it. This can potentially give you a big performance boost, especially if you have a lot of components in your application.

As usual, Wikipedia has more details.

Javache
+2  A: 

Here's an example from some actual Python code I wrote:

class Item(Model):
    ...
    @property
    def total(self):
        if not hasattr(self, "_total"):
            self._total = self.quantity \
                  + sum(bi.quantity for bi in self.borroweditem_set.all())
        return self._total

Basically, I have an Item class which represents an item in our inventory. The total number of items we have is the number that we own plus the sum of all of the items that we're borrowing from various sources. These numbers are all stored in our database, and it would be pointless to calculate this until the total is actually requested (since often Items will be used without the total being requested).

So the total property checks whether the _total field exists. If it doesn't, then the property code queries the database and computes it, then stores the value in the _total field so that it need not be recomputed the next time it's requested.

Eli Courtwright
+4  A: 

It's called lazy loading because, like a lazy person, you are putting off doing something you don't want to. The opposite is Eager Loading, where you load something right away, long before you need it.

If you are curious why people might use lazy loading, consider an application that takes a LOOOOONG time to start. This application is probably doing a lot of eager loading... loading things from disk, and doing calculations and whatnot long before it is ever needed.

Compare this to lazy loading, the application would start much faster, but then the first time you need to do something that requires some long running load, there may be a slight pause while it is loaded for the first time. Thus, with lazy loading, you are amortizing the load time throughout the course of running your application... and you may actually save from loading things that the user may never intend to use.

Mike Stone
+2  A: 

The term lazy loading is usually used when talking about object relational mappers. If you use ADO.NET directly you always get eager loading (ie it always loads just what you specify).

OR-mappers like nHibernate support returning proxy objects that get "filled in" with the right data only when you access the data. That way you only load the data that you really use. This is a usefull feature when you specify a lot of relations between objects that can get loaded from the database, you don't want the OR-mapper to load all the related objects and the objects related to the related objects and so on. That can result in your whole database getting loaded.

This problem can be prevented by carefull design of your object model too. (using aggregates and only loading aggregate roots like in domain driven design is a way to get around this without using lazy loading).

Lazy loading can result in the or mapper doing lots of small database accesses instead of retrieving all the data you need once. This can result in performance problems too.

Mendelt
+1  A: 

is a Design pattern.

Lazy loading: Untill your code require some operation done by a particular object, object is not initilaized, and once it's initialized it doesn't re-initialize the object but uses the previously initialized object.

This makes your code much more efficient and helps managing memory usage.

Example Applications of Lazy loading:

Ghost Lazy initialization Value holder

Pasan Indeewara
+1  A: 

Lazy loading: you don't waste your time (nor your memory) with stuff you might not need. Then when you need it, it takes longer, but that's fine.

Example from life: instead of actually learning that French phrasebook, you learn the phrases one at a time, as they're needed. When does this make sense? If you're only going to be in France for a short time (i.e., you won't need a lot of the phrases) or if you need to leave very soon. If you're there for two years and/or you have a long time to study, then it might be much more efficient to just learn the whole phrasebook up front (eager loading).

[Inspired by the Atom as taught in gang terms by Venus on WKRP.]

Yar
A: 

Lazy loading is a term frequently used in databases to refer to the concept of loading parts of the required info only when it's needed.

I.e. suppose you needed to have a record which has a join of several tables. If you fetched it all at once it would take longer than if you would fetch say only the main table. Using lazy-loading the rest of the information will be fetched only if it is needed. So it is actually 'efficient-loading' in certain scenarios.

The other types of 'loading' is:

  1. Eager Loading - Loading all the connected tables at once.
Cyril Gupta
A: 

Lazy<T> is now part of c# 4.0 - there is a nice page on MSDN which explains the concept.

Philip Wallace