Design Pattern _ metadata Pattern

Source: Internet
Author: User
Tags map class

Design Pattern _ metadata Pattern
Flyweight Pattern

Use sharing to support large unmbers of fine-grained objects efficiently (using shared objects can effectively support a large number of fine-grained objects)

 

Example

 

"Third Brother, the manufacturer's staff have already determined that the OutOfMemory memory overflows and no memory leakage is found. We are still tracking the situation ...... It's a sudden increase, and it's all about problems in busy periods ......" Memory overflow is too common for Java applications. There are two possibilities. ● Memory leakage: unintentional code defects cause memory leakage. The JVM cannot obtain continuous memory space. ● Too many objects are poorly written in code, too many objects are generated, and the memory is exhausted. The current situation is that there is no memory leakage, there is only one reason-the code is too bad to exhaust the memory.
In the analysis report provided by the system vendor, the report states that the memory suddenly soars from 1.4 MB to GB, and the new object application does not have the memory space, so OutOfMemory appears, the same report also lists the objects in the memory at the time of downtime, where the SignInfo class has 400 MB.
"This is normal, brother three. It is also appropriate to generate so many SignInfo objects with such a large volume of traffic. It does not mean that these objects are being used, it is estimated that a large part has not been recycled. It is not certain when the memory recycler will recycle objects. As you can see, there are more than 200 concurrent jobs. This is the number of concurrent jobs ......"

 

 

public class SignInfo { private String key; private String id; private String subject; private String location; public String getSubject() {  return subject; } public void setSubject(String subject) {  this.subject = subject; } public String getLocation() {  return location; } public void setLocation(String location) {  this.location = location; } public String getKey() {  return key; } public void setKey(String key) {  this.key = key; } public String getId() {  return id; } public void setId(String id) {  this.id = id; }}

 

 

 

Public class SignInfoFactory {private static HashMap
 
  
Pool = new HashMap
  
   
(); Public static SignInfo getSignInfo (String key) {SignInfo result = null; if (! Pool. containsKey (key) {System. out. println (key + ------------- create an object and put it in the pool); result = new SignInfo (); pool. put (key, result);} else {result = pool. get (key); System. out. println (key + --------------- directly from the pool);} return result ;}}
  
 

 

 

Knowledge

 

In the object pool, once an object is generated, there must be a unique and accessible state sign for this object, and the object declaration cycle in the pool is determined by the pool container, it is not determined by the user. Take the String type as the external State to the object pool to retrieve the object, but why not extract the two attributes, such as id and location, into a class, and then use this class as the external state, override the equals and hashcode methods. (If an object is used as the key value of the Map class, make sure that the equals and hashCode methods are overwritten. Otherwise, the search through the key value fails, for example, map. get (object), map. contains (object) and so on will return the result of failure .) The consequence of this is reduced efficiency, because the external State is best marked with the basic java type, such as String and int, which can greatly improve efficiency.

 


public class Client { public static void main(String[] args) {  String state1=sub1shanghai1;  String state2=sub1shanghai2;  String state3=sub1shanghai2;   SignInfoFactory.getSignInfo(state1);  SignInfoFactory.getSignInfo(state2);   long start=System.currentTimeMillis();  for(int i=0;i<100000;i++){   SignInfoFactory.getSignInfo(state3);  }  long end=System.currentTimeMillis();  System.out.println(total time:+(end-start)+ms); }}

 

Note:
Other problems include thread security, performance balance, and so on. We need experience. This is just a direction for development.

I have to say that this is a good thing, but in reality there are many things to consider. This is the best action to solve some connection problems based on the underlying hardware and environment, you still need to check the background and actual problems.

 

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.