Developing programs in a graphical operating system to call resources in the system, such as pictures, fonts, colors, and more. Usually these resources are limited, the programmer must be very careful to use these resources: When you use them, please release it as soon as possible, otherwise the operating system will sooner will have the oil, and have to restart, more serious will result in system breakdown.
SWT is developed by Java. The great advantage of Java language itself is JVM's "garbage recycling mechanism", and programmers usually do not have to pay the release of variables, memory recycling. Then, for SWT, is the operation of system resources? The answer is a bad news, a good news.
The bad news is that SWT does not use JVM's garbage collection mechanism to deal with the resource recovery problem of the operating system. A key factor is because JVM's garbage collection mechanism is uncontrollable, that is, the programmer can't know, it is impossible to do it. Let JVM recycle resources at a moment! This is fatal on system resources. Imagine your program hopes to see tens of thousands of pictures in a loop statement, the regular processing method is to release one, then view, then release the image resource, then Turn the next picture in the loop, this is for the operating system, and only the program is only a resource of one image. But if this process is completely handed over to JVM dealing, it may be after the end of the loop statement, the JVM will release the image resource, and the result may be that your program has not yet run, the operating system is over.
But the following good news may make this bad news becomes irrelevant. For SWT, just understand two simple "gold" rules, you can use system resources! The reason is called the gold rule, one is because few, only two, the second is because they are surprisingly simple. The first one is "who occupies, who releases", the second is "the parent member is destroyed, the sub-component is also destroyed". The first principle is a principle without any exception, as long as the program calls the constructor of the system resource class, the program should care about this system resource at a moment. For example, call
Font Font = New Font (Display, "Courier", 10, SWT.NORMAL
Then you should call when you need this Font.
Font.dispose ();
For the second principle, it means that if the program calls the Dispose () method of a component, the sub-component of all this component is also destroyed by the DISPOSE () method. The relationship between the sub-member and the parent member referred to herein is formed when the constructor of the calling member is formed. such as,
Shell shell = new shell ();
Composite Parent = New Composite (shell, swt.null);
Compositive child = new Composite (parent, swt.null);
The parent of which Parent is the shell, and the shell is the main window of the program, so there is no corresponding parent member, and the Parent includes the Child sub-component. If you call the shell.dispose () method, apply the second rule, then the Dispose () method of the PARENT and Child components will be automatically called by the SWT API, and they also destroy.