Several important basic problems in the interview of Java

Source: Internet
Author: User

1.java is there a memory overflow? How to solve?

Memory overflow refers to the application system in the presence of the memory can not be recycled or use too much memory, and ultimately make the program run to use more memory than the virtual function provides the maximum memory. In order to solve the memory overflow problem in Java, we must first understand how Java manages memory. The memory management of Java is the allocation and release of objects. In Java, the allocation of memory is done by the program, and the release of memory is done by the garbage collector (garbage COLLECTION,GC), programmers do not need to call the GC function to free memory, because different JVM implementations may use different algorithms to manage the GC, Some of the memory usage to a certain extent, the GC began to work, there are timed execution, there are some interrupt execution GC. However, the GC can only reclaim space occupied by objects that are useless and are no longer referenced by other objects. The memory garbage collection mechanism of Java is to check the reference chain from the main running object of the program, and when it is traversed, it is found that there are no referenced orphaned objects as garbage collection.

There are a number of reasons for memory overflow, which are common in the following ways:

L The amount of data loaded in memory is too large, such as fetching too much data from the database at once;

The collection class has references to objects that are not emptied after use, making the JVM not recyclable;

An object entity in the code that has a dead loop or a loop that produces too many duplicates;

• Bugs in the use of third-party software;

L Start parameter Memory value setting is too small;

3. Resolution of Memory overflow
Memory overflow Although very tricky, but there are corresponding solutions, can follow from easy to difficult, one-step solution.

The first step is to modify the JVM startup parameters and increase the memory directly. This may seem simple, but it can easily be overlooked. The default memory that the JVM can use for 64m,tomcat is 128MB, which is not enough for a slightly more complex system. In a project, the "OutOfMemory" error is often reported because of the default value used by the startup parameter. Therefore, the-XMS,-XMX parameter must not forget to add.

In the second step, check the error log to see if there are any other exceptions or errors before the "OutOfMemory" error. In a project, the use of two database connection, which is dedicated to send text message database connection using DBCP Connection pool management, the user does not send text messages, intentional database connection user name is wrong, so that the log has many database connection Exception log, after a period of time, there will be "OutOfMemory" Error. After analysis, this is due to the DBCP connection pool bug caused, the database connection is not on, the connection is not released, and eventually make the DBCP "OutOfMemory" error. After modifying the correct database connection parameters, no more memory overflow errors occur.

Viewing logs is important for analyzing memory overflows by carefully reviewing the logs to analyze what has been done before the memory overflow, and can roughly locate the problematic module.

The third step is to arrange for experienced programmers to walk through and analyze the code to find out where a memory overflow might occur. Focus on troubleshooting the following points:

L Check if there is a dead loop or recursive call in the code.

L Check if there is a cycle duplicate to produce the new object entity.

L Check if there is a query in the database query that gets all the data at once. In general, if you take 100,000 records to memory at a time, you can cause a memory overflow. This problem is more covert, before the online, the database less data, not prone to problems, on-line, the database more data, a query may cause memory overflow. Therefore, query the database query as much as possible by paging.

L Check whether the list, map, and other collection objects are not cleared after use. Collection objects such as List, map, and so on will always have a reference to the object so that they cannot be reclaimed by GC.

Fourth step, use the memory viewing tool to dynamically view memory usage. A memory overflow error occurs after a project is launched two days after the system starts. This is typically a slow memory leak in the code, which is not resolved with the three steps above, which requires the use of a memory viewing tool.

--------------------------------------------------------------------------------------------------------------- -----------------

There are a number of reasons, such as:

1. The volume of data is too large; a dead loop; static variables and static methods too many; recursive; Unable to determine whether the object is referenced;

2. The virtual machine does not reclaim memory (memory leaks);

To put it bluntly, a memory overflow occurs when the program runs more memory than the virtual function provides. The problem with memory overflow depends on the size of the business and the system, which may not be common for some systems, but some systems are a common solution.

One is to optimize the program code, if the business is large, complex logic, minimize the global variable reference, let the program use the variable when the release of the reference can let the garbage collector recycle, release resources.
The second is physical resolution, which increases physical memory and then passes:-xms256m-xmx256m-xx:maxnewsize=256m-xx:maxpermsize=256m modification

2. Talk about JVM memory composition
Simple to say heap and Stack in Java

Java has two kinds of memory: one is stack memory, the other is heap memory

1. The basic type variables and the reference variables of the objects defined in the function are allocated in the stack memory of the function;

2. Heap memory is used to hold objects and arrays created by new

When a variable is defined in a function (code block), Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the allocated memory space for that variable, and the memory allocated in the heap is managed by the Java Virtual machine's automatic garbage collector

The advantage of a heap is that it can dynamically allocate memory size, and the lifetime does not have to tell the compiler beforehand because it allocates memory dynamically at run time. The disadvantage is to dynamically allocate memory at run time, the access speed is relatively slow;

The advantage of the stack is that the access speed is faster than the heap, and the disadvantage is that the data size in the stack and the lifetime must be deterministic without flexibility.

The Java heap is divided into three zones: New, old, and Permanent

The GC has two threads:

The newly created object is assigned to the new zone, and when the area is filled, it is moved to the old area by the GC worker thread, and when the old area is filled, all objects in the heap memory that trigger the GC main thread traverse. The size of the old area equals Xmx minus-xmn

Java Stack Storage

Stack adjustment: Parameters have +usedefaultstacksize-xss256k, indicating that each thread can request 256k of stack space

Each thread has his own Stack.

3. What does the "static" keyword mean? Is it possible to overwrite (override) a private or static method in Java?

The "static" keyword indicates that a member variable or a member method can be accessed without an instance variable of the class to which it belongs.

The static method in Java cannot be overridden because method overrides are dynamically bound based on the runtime, while the static method is statically bound at compile time. The static method is not relevant to any instances of the class, so it is conceptually not applicable.

4. Is it possible to access non-static variables in a static environment?

The static variable belongs to the class in Java, and it has the same value in all instances. Static variables are initialized when the class is airborne into Java virtual. If your code tries to access a non-static variable without an instance, the compiler will give an error because the variables have not been created yet and are not associated with any instances.

What is the difference between 5.JDK and JRE?

Java Development Kit,java Development Package JRE---Java Runtime Environment Java Run environment

The Java Runtime Environment (JRE) is the Java virtual machine that will execute the Java program. It also contains the browser plug-in needed to execute the applet. The Java Development Kit (JDK) is a complete Java software development package that includes a JRE, a compiler, and other tools (such as the Javadoc,java debugger) that enable developers to develop, compile, and execute Java applications.

Several important basic problems in the interview of Java

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.