1. Object-Oriented disadvantages
Although OOP is a good solution to the problem of system abstraction, and in most cases, it will not lose the performance of the system. However, in some special business, because of the number of objects, the adoption of object-oriented system will bring unbearable memory overhead. The sample code is as follows:
/// <summary> ///font style for word text/// </summary> Public classFont//8+8 (virtual table pointer inheriting object 4 bytes, garbage collection synchronization accounted for 4 bytes) = 16 bytes { PublicFont (stringFontName,intsize) {_fontname=FontName; _size=size; } string_fontname;//4 bytes, but because of the string retention technique, you might actually create a lot of this object and save some space int_size;//4 bytes } /// <summary> ///Word text Object/// </summary> Public classCharactor//2+4 (reference pointer to font type) +16+2 (32-bit operating system memory padded) + 8 bytes (virtual table pointer inheriting object 4 bytes, garbage collection synchronization accounted for 4 bytes) = 32 bytes { PublicCharactor (Charc, font font) {_c=C; _font=font; } Char_c;//is a Unicode character, 16 bits, accounting for 2 bytesFont _font;//16 bytes}
The calling code is as follows:
Public classThirdsystem { Public voidRun () {LongA = GC. Gettotalmemory (true); //The creation of 10 million charactor objects is about to consume 32*10000000/1024/1024=343m intnum =10000000; //Create 10 million Charactor objects varList =NewList<charactor> ();//does not consume memory, and if you use ArrayList and specify the initialization length, memory consumption is generated for(vari =0; i < num; i++) {charactor charactor=NewCharactor ('C',NewFont ("Song Body",6)); List. ADD (charactor); } Longb = GC. Gettotalmemory (true); LongMemoryconsume = B-A; Console.WriteLine (Memoryconsume/1024x768/1024x768);//actual output 369, the actual creation of 10 million charactor objects consumes 369M of memory space } }
It is obviously undesirable to generate 10 million object instances on the client system, resulting in a 369M memory overhead and simply the overhead of an instance of an object.
2. Questions
Using object methods to create a large number of object instances, resulting in high runtime prices-mainly in memory, how can you avoid this overhead by generating a large number of object instances in an object-oriented manner?
On this issue, how the string retention pool is implemented. Provides a good reference. For specific implementation ideas, refer to string retention and string pooling
3. Solutions
4. Key points of using flyweight to enjoy meta-mode
(1), the model does not involve abstract issues, which is not related to abstraction, it is mainly to solve the object-oriented cost problem, in the object-oriented process, the creation of a large number of object instances, the resulting memory consumption.
(2), the mode uses a shared object instance to reduce the number of objects in the system, that is, by holding a reference to the same object through Hashtable to reduce the pressure of the fine-grained object instance to the system
(3), because the use of the same object reference to share the object, so when an object changes, all objects will change, similar to the array, so pay attention to the state of the object processing, can not be blindly modified.
(4), the model is best to calculate the cost of the entire system, in accordance with the actual situation to determine whether to use the enjoy meta-mode.
Flyweight-mode (structured mode)