Windows Mobile Development (IV)-Closed-doors and practices, windows closed-doors

Source: Internet
Author: User
Tags website server website performance

Windows Mobile Development (IV)-Closed-doors and practices, windows closed-doors
I haven't written a blog for a long time, not because I don't want to write it. I just recently had a lot of tasks in the company and finally had time to bubble up.Today, we will continue to introduce memory management, the top priority in mobile development. C # code is a hosting code. C # programmers seldom release program resources as C/CPP programmers do. a c/CPP master must be a master of memory management, although C # programs do not require memory resource management like C/CPP programmers, they still need to have a deep understanding of the memory mechanism. These code resources are managed resources and handed over to GC for processing, those resources need to be manually released by programmers. Of course, you are more concerned about unmanaged resources, because managed resources are automatically cleared by GC.
Generally, you can check whether a class is a managed resource. In VS, F12 is converted to the definition to check whether the class or parent class implements the IDisposable interface, all classes that implement the IDisposable interface need to manually release resources. GC will not clean up these resources. Common classes that implement the IDisposable interface in C # include: database Connection parent class DbConnection and various database Adapter, Command and other classes, various file Stream parent class Stream and other classes.
We have two policies for these classes. Policy 1: Use the try... catch... finally statement to release resources in finally when using these classes. The procedure is as follows:

Public class SqlHelper {// obtain the database connection string in the configuration file private static readonly string ConnStr = ConfigurationManager. connectionStrings ["connStr"]. connectionString; /// <summary> /// query method for the database parameters of the DataTable that is returned for general purposes in the Stored Procedure /// </summary> /// <param name = "sqlstr"> Query SQL string </param> /// <param name = "param type"> command type </param> /// <param name = "paras"> parameter </param>/ // <returns> DataTable result set </returns> public static DataTable ExecuteDat ATable (string sqlstr, CommandType parameter type, params SqlParameter [] paras) {// create an interface class object that implements IDisposable: SqlDataAdapter adapter = null; try {adapter = new SqlDataAdapter (sqlstr, ConnStr ); dataTable dt = new DataTable (); adapter. selectCommand. commandType = partition type; if (paras! = Null & paras. length> 0) {adapter. selectCommand. parameters. addRange (paras);} adapter. fill (dt); // The adapter has run out of return dt;} catch (Exception e) {// return null when recording error logs ;} finally {// release the unmanaged resource adapter. dispose ();}}

In this way, when we call the ExecuteDataTable method of SqlHelper, we do not have to worry about the release of the Adapter object resource. The finally statement will automatically release the resource when we use the Adapter, although this satisfies our requirements, it seems a little troublesome and good. Policy 2: Use the C # using statement as follows:
Public static DataTable ExecuteDataTable (string sqlstr, CommandType parameter type, params SqlParameter [] paras) {// Class Object using (SqlDataAdapter adapter = new SqlDataAdapter (sqlstr, ConnStr) that implements the IDisposable Interface )) {DataTable dt = new DataTable (); adapter. selectCommand. commandType = partition type; if (paras! = Null & paras. Length> 0) {adapter. SelectCommand. Parameters. AddRange (paras);} adapter. Fill (dt); return dt ;}}


In this way, try .. finally is much more convenient. As long as the using implements the IDisposable interface object, the using statement block will be automatically released after the IDisposable object is completed. Note that classes that do not implement the IDispoable interface cannot be using, in fact, using is also a try... catch... the finally implementation is only encapsulated by Microsoft, which is also the proprietary syntactic sugar of C # programmers.
Of course, if you want to implement the IDisposable interface and be able to be using, you can refer to the example suggested by Microsoft MSDN. I will not have any ink here.
When we talk about unmanaged objects, we should say that the managed objects are completely managed by the clr gc, so when Will GC reclaim the managed resources?
The so-called managed resources do not implement the general class of the IDisposable interface, and there are no file and database operations, such as int, string, List, etc. When we are using these objects, we do not need to care about their release. They will be processed by GC in a unified manner. Generally, if the system memory is sufficient for GC, these managed resources will not be recycled, when the memory is insufficient, GC will recycle objects that do not have any reference points. Of course, C # also provides programmers with a method to manually call GC collection, GC. collect () method, but even if the programmer calls this method manually, GC does not necessarily recycle managed resources at that time. In addition, Collect () it is not recommended that programmers manually call the method. If the method is called frequently, the performance of the program will be severely degraded. Because the memory is frequently recycled, C # has three levels of garbage collection. If you want to know more, you can Baidu.
During website development, users are often willing to sacrifice appropriate memory to improve website performance, because the memory access speed far exceeds the disk speed, which will relieve the pressure of high concurrency of websites, therefore, a batch of memory management software such as Memcached and Redis are generated, and even MySql Databases can store data in the memory, of course, when the server restarts the memory, the data cannot be recovered (Redis can be recovered). Therefore, it is critical to put the data in the memory.
As a mobile developer, memory management is even more important. Because we don't have as much memory as our website server. At present, I heard that the biggest memory is Microsoft's Surface Pro3. This is the configuration of the tablet, but what if it is a mobile phone? The maximum memory should be 3G memory of Xiaomi's 4th-generation mobile phone, and the current memory of mainstream mobile phones should be around GB. Apple's mobile phone memory should be smaller. In the face of such a small memory, programmers must grasp the memory usage during development.
Since the cell phone memory is so small, Should I release all the resources immediately after they are used? Which saves the most resources? Actually not. This depends on the specific requirements. In some cases, we can temporarily put some common resources in the memory, when it is used again, calling from the memory can greatly increase the speed of Program Calling. here we can use the Framework thread pool principle. If there is a thread object in the thread pool, it uses the objects in the thread pool and does not start a new thread.
An important knowledge point that must be mentioned here, even if it is weak reference, the WeakReference class principle is to point the object with WeakReference, then, set the object to null (which can be recycled in a timely manner by GC). We can directly use the object pointed to by the WeakReference object, and WeakReference will automatically manage the object, when there is a required object in the memory, you can directly use it. If not, you can create one in the memory. The usage is as follows:
Object obj = new object (); // The object is directed by a weak reference to WeakReference wref = new WeakReference (obj); // sets the object to null obj = null; // use the object currObj = wref pointed to by the weak reference. target; // use currObj to complete the business

Here is a brief introduction to weak references. Of course, the use of weak references is more than just that. You can search for them by yourself, in the subsequent Windows Store application development, I will give a detailed example of the specific application scenarios with weak references. If you want to have a better understanding of memory usage, you are advised to go to the Internet to view the metadata sharing mode in the design mode video of Lu Jianzhong. The metadata sharing mode is about Memory sharing. In fact, many things can be shared during development, it is important to thoroughly understand the concept of a pool.
Now, we will be here today. We will see you later.

Why did ancient people have to retreat for a long time?

[Closed cultivation] is a very rigorous practice, most of which are: Stop the language, break the outer margin (do not go out) dedicated to practice a method, the group generally has 10 days, 20 days, and 30 days. A lot of personal retreat is not a stop! The meaning of the Retreat:
The Buddhist and Taoist practitioners must go through the closed-off phase. Simply put, the practitioners close themselves in the closed room, And do not contact others or foreign affairs except the maintainer, it is called a closed lock to sit and settle for a long time. However, this is only a superficial secular view of retreat. In fact, the interpretation of the Buddhist retreat is also profound. The Buddhist family believes that a closed person is an activity that has been infected by pollution in more than three circles. The behavior of a closed person refers to the behavior that has nothing to do with the entire legal community and its certification system, that is, the evidence must be based on a certain degree of sub-division.
The closed customs conditions for explicit religion, Zen Buddhism, and BCC are different. The most important thing to do is to have the "four concepts" as the ideological basis. The thought points are the view of the body, the view of the suffering, the impermanence of the mind, and the view of the law without me. Because the view is not clean, the life of impermanence, refinement, reduce pollution, to prevent the impermanence and death; view is bitter and the suffering of all the knowledge is bitter, to feel empty, in order to avoid troubles and perseverance, the mind is impermanence and will not go along, so as to reduce the scattered and faint heart; the view of law is to prove emptiness. Only when these conditions are met can we stick to them only when we leave the customs closed. Zen Buddhism is required to visit its feet before its enlightenment. After the Enlightenment, the house will be closed, which is called the first pass; when the first pass is broken, the House will live in the mountains, but it can freely communicate in the mountains. When the second stop is broken, the city will be closed, in the voice field, it is called prison, and it is regarded as a Buddha after the prison is broken. The important condition for the retreat of BCC is the four fundamentals, that is, the above division, the current statue, the empty mother, the protection of the four fundamental, the following details. All in all, whether the retreat is successful or not is the most important thing to do is whether the big Bodhi heart of all sentient beings has been published and practiced.
Types of closures:
Closing is divided into formal closing and convenient closing. A formal close is a real close, or in a broad sense. It is convenient for a non-real close, or a narrow sense.
From the perspective of closing time, the official closing time is designed for family members. It not only prevents family members from being infected by life and death, but also ensures that the verification system is completed according to the second practice. That is to say, we should officially close the door to solve the problem of family members achieving the ultimate achievement, solving the major issues of life and death, and breaking through the issue of life and death. Therefore, the shutdown time must be long-lasting, from years to decades or even years. However, it is easy to close the customs. It is set for residents or beginners at home, or people busy with work and daily work. The time for closing the customs can be as short as several months or days, for example, hot and cold holidays or other holidays can be closed.
From the point of view of the closed location, the location specified for the formal closed location is very strict. Milazaba's ancestral Division requires that only the dead torlin (three places can be closed, namely the dead cemetery, the grave, the rock cave and the mountain mountains. In the secret of the corpse, you can apply for the body and prove the emptiness, and make sure that you can cooperate with the emptiness. In the rock hole closed, select the hole to the south, this close door can be closed, the rock hole should be in the inaccessible place. When the highest snow-capped mountains are closed, there is no cloud or clear sky throughout the year. Because the mountains are high in the cloud, there are strong winds or falls in the clear snow, which makes it easy to repair big prints and complete the big. You don't have to pay attention to the convenience of closing the door. You can close the door by setting up a close room in the home, government agencies, factories, and other places .)
From the perspective of the purpose of closing the customs, the purpose of closing the customs is to solve the major issues of life and death, to achieve the goal, to close it to death or achievement, and not to reach the goal. The convenience of closing the door is just a short-term move away from pollution. It is a kind of exercise and solves some problems related to the practice.
As for the living conditions in the closed customs, according to the traditional regulations, the meals in the formal closed customs are vegetarian and subject to the prescribed laws. You cannot read General books, you can only read the scriptures related to the Goals. You cannot communicate with the outside world or communicate with others. There are no waiters in Guanzhong. It is much more free to close the door. You can also read books, have attendants, and consult a physician. However, at least ban, ban, and reform schedules must be implemented. For modern people who do not leave their homes, it is appropriate to implement a convenient retreat, but when they begin to close their doors, they still need to check whether their motives are pure and pure, and do not contain private desires. They must aim to save lives, in this way, the magic obstacle can be avoided.
 
Synonyms for practice

Yan Guangyang

Related Article

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.