Common face questions in Java Software Development (i.)

Source: Internet
Author: User
Tags set time

1. How to understand garbage collection?

Garbage collection is a major feature of the Java language, which facilitates programming, at the expense of performance; In the Java Virtual Machine (JVM), there is a garbage collector, which is used to find and reclaim useless objects so that the JVM can use memory more efficiently, and to ensure the efficient use of available memory. With efficient management of available memory, programmers can influence the execution of garbage collection, but they cannot control it.

The runtime of the garbage collector cannot be determined by the JVM, and the runtime is indirectly executed, although it is possible to force garbage collection through System.GC (), but there is no guarantee that the JVM will respond to execution immediately after the command is executed, and the programmer's request will be executed shortly after the command is issued. However, the JVM will perform garbage collection operations when it feels a memory crunch.

Garbage collection too often can cause performance degradation, too sparse can lead to memory shortages, but this problem the JVM will control itself, without the programmer to control, which is a Java language benefits, but sometimes in a short period of time to use a lot of memory, and these objects are not used after a short-term use, It is necessary to release a mandatory recycle command to have more available physical memory.

The JVM determines whether a piece of memory meets the criteria collected by the garbage collector:

1, the object is assigned null value, and has not been used;

2. Assigns a new value to the object, that is, the memory space is redistributed.


2. How to understand thread safety?

Thread safety is in the multi-threaded access, the use of locking mechanism, when a thread access to the class of data, other threads can not access, until the end of the thread access, other threads will continue to read, no data inconsistencies or data pollution.

If the code is in a process where multiple threads are running at the same time, and these threads may access the code at the same time, if the result of each run is the same as a single thread, and the other values are the same as expected, it is thread-safe.

     such as: A ArrayList class, when adding an element, It may take two steps to complete: 1. This element is stored in the location of Items[size]; 2. Increase the value of Size.          in the If you run the case, if Size = 0, after adding an element, this element is at position 0, and size=1;              If it is in multithreading

      thread safety

Bloch presents a classification method that describes the five types of thread security: immutable, thread-safe, conditional thread-safe, thread-compatible, and thread-antagonistic.


3, when a lot of users, how to optimize the server? (servlet+jsp part)

The inactive user will be deactivated for a period of time, and the user object will be serialized to the local disk, thus relieving the memory pressure and activating again when the user is active again!

passivation: is to persist (serialize) objects in session memory (implementing serialization interfaces) to disk;

activation: is to restore the objects on the disk to session memory again .    

(1) passivation and activation of the listener Httpsessionactivationlistener

You can specify the object passivation time through the configuration file---object How long it takes to not be passivated

Under Meta-inf, create a context.xml that reads as follows:

<Context>

<!--How long the objects in the maxidleswap:session are not used, passivated--

<!--directory: The files of the Passivated object are written to the directory of the disk, and the Passivated object file is configured in the work/catalina/localhost/passivation file--

<manager classname= "Org.apache.catalina.session.PersistentManager" maxidleswap= "1" >

<store classname= "Org.apache.catalina.session.FileStore" directory= "Bonnie"/>

</Manager>

</Context>

@Override

public void sessionwillpassivate (Httpsessionevent se) {

System.out.println ("Being passivated ... ");

}

@Override

public void sessiondidactivate (Httpsessionevent se) {

SYSTEM.OUT.PRINTLN ("Being activated ... ");

}

When the specified servlet project is accessed, the "passivation" appears after the set time, and then any file under the project can be activated.

Files that are passivated to work/catalina/localhost/







This article is from the Java Learning Blog, so be sure to keep this source http://12181171.blog.51cto.com/12171171/1958449

Common face questions in Java Software Development (i.)

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.