12th Lecture: Flyweight metadata sharing mode

Source: Internet
Author: User

2006.3.28 Li Jianzhong

Object-oriented cost

The object-oriented solution solves the problem of system abstraction well. In most cases, it does not compromise the system performance. However, in some special applications, because the number of objects is too large, the adoption of object-oriented will bring an unbearable memory overhead to the system. Objects such as elements in the application, character objects in the application of word processing, etc.

 

Motivation)

The problem with the use of pure object solutions is that a large number of fine-grained objects will soon flood the system, resulting in a high operating price-mainly the cost of memory demand.

While avoiding a large number of fine-grained objects, how can external customer programs still operate transparently?

 

Intent)

Use the sharing technology to effectively support a large number of fine-grained objects.

-- Design Pattern GoF

 

For example, the Flyweight Application

If the char character adopts the enjoy mode, each character occupies only 2 bytes, And the enjoy mode uses a pointer, at least 4 bytes for each character, which is counterproductive, instead, it consumes more memory resources.

However, the font needs to use the metadata mode, because the font size is much larger than the character size. In our example, there are 12 bytes. If it is a multiplier, the font size will be large, memory consumption.

In fact, for C #, each class also requires a virtual table pointer and space for garbage collection control, which accounts for 8 bytes. Because every class in C # inherits from the object class, each class carries the virtual function and type information. Therefore, the Font class should be a multiplier effect of 20 bytes. In the Charactor class, the char character occupies 2 bytes; the Font type has a pointer, accounting for 4 bytes; the Font has 20 bytes; and the 8 bytes virtual table pointer and garbage collection; because there is a filling effect on 32-bit machines, char characters also need to take up 2 bytes and need to be completed; therefore, the Charactor class occupies 36 bytes. There are 100000 Charactor classes in the Customer Code, which will bring about m of data. 3 M data is acceptable if it is a server program. However, if the number of characters increases, the overhead is self-evident.

Improved Structure:

CFont attribute in Charactor class

Here, when we use Charactor and set its font, if it already exists in its static Hashtable, we direct the pointer to it, do not create a new font object.

Lines 61st and 63 in the Customer Code, f1 and f2 occupy two different memories, but 68 and 73 rows, we actually transparently convert them internally to allow them to use only one f1 memory, so the f2 memory will not be held again.

Note that. when using a string in. NET, it actually uses the enjoy meta mode. For example, we have 20 "Hello World" in the string ", in fact, only one "Hello World" is saved in the system, and others point to it with pointers. The premise is that the cost of the string is higher than that of the pointer.

 

Structure)

This structure can be used as a reference only. It is not an absolutely standardized structure. This is the basic idea, but it is still worth considering whether it is necessary to put it in a static factory. As in the above example, we put it directly in a property.

 

Highlights of Flyweight Mode

Object-oriented solves the abstract problem, but as a program entity running on a machine, we need to consider the cost of the object. The Flyweight design model mainly solves the problem of object-oriented cost, and generally does not touch the abstract problem of object-oriented.

Flyweight uses Object sharing to reduce the number of objects in the system, thus reducing the memory pressure on the system caused by fine-grained objects. In specific implementation, you must pay attention to processing the object status.

The large number of objects leads to an increase in the memory overhead of the object. What quantity is considered as large? This requires us to carefully evaluate the specific application, instead of making decisions out of thin air.

 

Flyweight application in. NET Architecture

. NET has a Code Behind mechanism in C #. It has An aspx file on the surface and a cs file on the back. Its compilation process will actually parse the aspx file into a C # file, then compile it into a dll. In this process, any html code we write in aspx will be converted to literal control. literal control is a common text control, which indicates html markup. When these tags are the same, the Flyweight mode is used to build the control tree.

Its applications are not so ordinary, and we only use them when the efficiency space is really low.

2010.10.6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.