The memory management mechanism in a Java program is done through GC , "When an object is created, it is placed in the JVM 's heap memory and will be reclaimed by the JVM in heap memory when the object is never applied." The object being created cannot be regenerated, and there is no way to release it through the program statement "(this is the definition mentioned in the Java GC mechanism, hehe, still vaguely remember) this is the GC 's definition of garbage objects. Personally, that might be a quick explanation. The JVM manages two types of memory in a running environment: heap memory (object instance or variable), stack memory (static or non-static method), and the memory area managed by JVMs is actually heap memory + stack memory ( MS: object instance + instanced variable + static method + non-static method), this object is reclaimed as a garbage object when the JVM cannot reach the object through the root collection in the area of memory it manages.
Here are a few attempts to optimize the code in my Project, memo
1. Cycle optimization
Lack:
List alist=usvr.getuserinfolist ();
for (int i=0;i<alist.size (); i++) {
}
This approach will continue to execute the Alist.size () method, resulting in a performance drain, instead
for (int i=0 p=alist.size (); i<p;i++) {
}
2. Do not create objects within the loop
Ischemia
for (int i=1;i<=domaincount;i++){
.
Auditresult Auditresult = new Auditresult ();
..
}
This practice will save n copies of this object's reference in memory//will waste a lot of memory space, instead
Auditresult Auditresult;
for (int i=1;i<=domaincount;i++){
.
Auditresult=new Auditresult ();
..
}
3. What kind of object can be identified as a non-visual stage? For example, in try{...} catch (Exception) {...} Code, if an obj is declared in the code block of the try, then the entire try{...} catch (Exception) {...} After the code snippet has been executed, this obj actually belongs to the invisible stage.
Therefore, the following methods should be used
Try{
Object Obj=new object ();
}catch (Excepione e){
Obj=null;
}
4. Create objects less with new
When you create an instance of a class with the new keyword, all constructors in the constructor chain are automatically called. But if an object implements the Cloneable interface, we can call its clone () method. The Clone () method does not call any class constructors.
Lack:
When using the design pattern, creating a new object instance using the Clone () method is straightforward if you create the object in Factory mode. For example, here is a typical implementation of the factory pattern:
public static credit Getnewcredit ()
{
return new credit ();
}
Switch
private static Credit Basecredit = new Credits ();
public static credit Getnewcredit (){
return (Credit) Basecredit.clone ();
}
When new creates an object, be careful to avoid using new to initialize an object multiple times. Try to create the object again when you use it.
Lack:
NewObject object = new NewObject ();
int value;
if (i>0)
{
Value =object.getvalue ();
}
Switch
int value;
if (i>0)
{
NewObject object = new NewObject ();
Value =object.getvalue ();
}
5. Multiplication and Division considerations
Lack:
for (val = 0; Val < 100000; Val +=5){
Alterx = val * 8;
Myresult = val * 2;
}
Switch
for (val = 0; Val < 100000; val + = 5){
Alterx = Val << 3;
Myresult = Val << 1;
}
6. Use StringBuffer as much as possible
7 Clear Session
Normally, when a set timeout is reached and some sessions are inactive, the server releases the inactive session. However, in this case, especially when multiple users and visits, the system memory to maintain a number of invalid session.
When the user exits, it should be released manually, recycling resources, implemented as follows:
HttpSession thesession = Request.getsession ();
Get current session
if (thesession! = null){
Thesession.invalidate (); Invalidate the session
}
Note: Heap memory is created when the JVM is started, and heap memory is divided into new objects and old objects. The new object seems to be divided into three regions. The JVM will test the contents of the highest-priority area when the stack is full, and the test content is that those objects are unreachable, and the unreachable objects will be placed in the old object area. At the same time, the JVM copies all objects into two other areas, and then objects that are not referenced for a period of time go into the old object area. For an old object area, it is basically waiting for the object to be reclaimed by GC . (these are the same knowledge of . NET when you are in college.) I think the concept should be the same.
Java code optimization