You have to know first.
1. C + + is the programmer manages the memory itself, and the Java memory is automatically recycled by GC.
Although I am not very familiar with C + +, but this should not make common sense mistakes.
2. What is a memory leak?
Memory leaks are memory that cannot be reclaimed in the system, sometimes resulting in out-of-memory or system crashes.
A memory leak is a condition in which memory is not released in C + +.
3. Java exists memory leak
We have to admit this before we can discuss it. While there is a memory leak in Java, there is basically no need to care about it, especially if it's not about the code itself.
Memory leaks in Java are, of course, objects that are useless but that the garbage collector cannot reclaim.
And even if there is a memory leak problem exists, it will not necessarily show.
4. In Java, parameters are passed values.
There is basically no objection to the basic type, but we cannot disagree with the reference type.
Java memory leaks
1, heap memory overflow (Outofmemoryerror:java heap space)
In the JVM specification, memory in the heap is used to generate object instances and arrays.
If subdivided, heap memory can also be divided into young generations and older generations, and the younger generation includes an Eden area and two survivor districts.
When a new object is generated, the memory request process is as follows:
A, the JVM first tries to allocate the memory required for the new object in the Eden area;
b, if the memory size is sufficient, the application is completed, otherwise the next step;
C, the JVM started YOUNGGC, trying to release the inactive objects in the Eden area, after release if Eden Space is still not enough to put new objects, then try to put some of Eden active object into the Survivor area;
D, Survivor area is used as the middle Exchange area of Eden and old, when the old space is sufficient, survivor area objects will be moved to the old area, otherwise will be retained in the Survivor area;
E, when the old space is not enough, the JVM will be in the old area full GC;
F, full GC, if survivor and old areas are still unable to store some of the objects copied from Eden, causing the JVM to not create memory areas for new objects in the Eden area, an "out of memory error" appears:
Outofmemoryerror:java Heap Space
2, Method area memory overflow (outofmemoryerror:permgem space)
In the JVM specification, the method area mainly holds the class information, the constant, the static variable and so on.
So if the program loads too many classes, or uses such dynamic proxies as reflection, Gclib, and so on to generate the class's technology, it could cause a memory overflow in the zone, and the error message for the memory overflow in the area is:
Outofmemoryerror:permgem Space
3, Thread stack Overflow (java. Lang. Stackoverflowerror)
Thread stacks is a unique piece of memory structure, so the thread stack problem must be the error that occurs when a thread runs.
A generic thread stack overflow is caused by too much recursion or too many method call levels.
The error message that occurred with the stack overflow is:
Java. Lang. Stackoverflowerror
Memory leaks in several scenarios:
1, Long life cycle objects hold a reference to a short life cycle object
This is the most common scenario for memory leaks and is a recurring problem in code design.
For example, a local variable is cached in a global static map, and there is no vacuuming operation, and over time the map will grow larger, resulting in memory leaks.
2. Modify the parameter value of the object in the HashSet, and the parameter is the field that computes the hash value
When an object is stored in the HashSet collection, you cannot modify the fields in this object that participate in the computed hash value, otherwise the modified hash value of the object is different from the hash value originally stored in the HashSet collection, in which case Even if the contains method uses the current reference of the object as a parameter to retrieve the object in the HashSet collection, the result of the object cannot be found, which also causes the memory leak to be removed from the HashSet collection by deleting the current object.
3, the Machine connection number and the Shutdown time setting
Opening a very resource-intensive connection over a long period of time can also result in memory leaks.
To see a memory leak example:
public class Stack {
private object[] elements=new object[10];
private int size = 0;
public void push (Object e) {
ensurecapacity ();
elements[size++] = e;
}
Public Object pop () {
if (size = = 0)
throw new Emptystackexception ();
return elements[--size];
}
private void Ensurecapacity () {
if (elements). Length = = size) {
object[] oldelements = elements;
elements = new object[2 * elements. LENGTH+1];
System. Arraycopy (oldelements,0, elements, 0, size);}}
The above principle should be very simple, if the stack adds 10 elements, and then all bounce out, although the stack
is empty, there is nothing we want, but this is an object can not be recycled, this is in line with the memory
Leakage of two conditions: useless, can not be recycled.
But there is no such thing as a result, if the stack is less used,
Also wasted a few k memory, anyway, our memory is on G, where will have any effect, say
This thing will be recycled soon, what's the matter. Here's a look at two examples.
Example 1
public class bad{public
static Stack S=stack ();
static{
S. Push (new Object ());
S Pop (); Here is an object that takes place with memory leaks
s. Push (new Object ()); The objects above can be recycled, which is equivalent to self-healing.
}
Because it's static, it always exits to the program, but we can also see that it has self-healing capabilities,
That is, if your stack has up to 100 objects, then at most 100 objects will not be recycled.
In fact, this should be easy to understand, stack internal hold 100 references, the worst case is that they are
Useless, because once we put new enterprising, the previous reference naturally disappears!
Example 2
public class nottoobad{public
void DoSomething () {
stack s=new stack ();
S Push (new Object ());
Other code
S. Pop ();//This also causes objects to be recyclable and memory leaks.
}//Exit method, S automatically invalid, s can be recycled, stack internal references naturally gone, so
//here can also self-healing, and can be said that this method does not exist memory leakage problem, but one o'clock in the evening
//to the GC only, because it is closed, External is not open, can say above code 99. 9,999%
//situation will not cause any impact, of course, you write such a code will not have any bad effect, but
//absolutely can be said to be garbage code! There's no contradiction, I'll add an empty for loop inside it will not have
//What's the big impact, Would you do that?
}
The above two examples are just a little, but the memory leak in C + + is not bad, but worst.
They can never recycle if there is no recycling, frequent calls to this method memory is not used up!
Because Java also has self-healing functions (my name has not yet patented), so the Java memory leak problem
You can almost ignore it, but don't make a man who knows it.
To avoid memory leaks, you can refer to the following recommendations in the process of writing code:
1, the early release of useless objects of reference;
2, the use of string processing, to avoid using string, should be a large number of StringBuffer, each string object has to occupy a single area of memory;
3, as far as possible with static variables, because the static variables stored in the permanent generation (method area), the permanent generation is basically not involved in garbage collection;
4, avoid creating objects in the loop;
5, open large files or take too much data from the database at one time can easily cause memory overflow, so in these places to calculate the maximum amount of data is how much, and set the minimum and maximum memory space value.