What is Lazy Loading?

2019-01-07 03:02发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

Lazy loading is a concept where we delay the loading of the object unit the point where we need it. Putting in simple words on demand object loading rather than loading the objects unnecessarily. For instance if you have a "Customer" class which has "Orders" object aggregated. So you like to load the customer data but the orders objects you would like to delay until your application needs it.

Below is a youtube video which demonstrates how to use lazy loading , how we can implement lazy loading and advantages and disadvantages of the same.

http://www.youtube.com/watch?v=2SrfdAkwmFo



回答4:

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



回答5:

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.



回答6:

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.



回答7:

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.]



回答8:

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.


回答9:

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



回答10:

An example of Lazy Loading would be a grid or table with lots of data on a webpage to view where the application only loads what the users browser viewpoint size is at that time. When they scroll down to want to view more content or data, more data would be loaded into view at that moment.

This is becoming more of a common visual/interaction design pattern as well via ajax or jQuery.

And as mentioned above, opposite would be Eager Loading where you don't take client into consideration thus potentially having a performance hit.



回答11:

Some of the advantages of lazy loading:

  1. Minimizes start up time of the application.
  2. Application consumes less memory because of on-demand loading.
  3. Unnecessary request to server is avoided.


回答12:

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



回答13:

According to geeksforgeeks, Lazy loading is a software design pattern where the initialization of an object occurs only when it is actually needed and not before to preserve the simplicity of usage and improve performance.

https://www.geeksforgeeks.org/lazy-loading-design-pattern/