Q: How can a memory leak be generated in Java?
In A:java, there are many reasons for memory leaks. A typical example is a failure to implement Hascode and
The case where the key class of the Equals method is saved in HashMap. A lot of duplicate objects will be generated at the end. All the memory leaks
Finally, the OutOfMemoryError exception is thrown, and the following is a short pass through an infinite loop to simulate a memory leak
To illustrate the case.
Copy Code code as follows:
Import Java.util.HashMap;
Import Java.util.Map;
public class Memoryleak {
public static void Main (string[] args) {
Map<key, string> map = new Hashmap<key, string> (1000);
int counter = 0;
while (true) {
Creates duplicate objects due to bad Key class
Map.put (New Key ("Dummykey"), "value");
counter++;
if (counter% 1000 = 0) {
SYSTEM.OUT.PRINTLN ("Map size:" + map.size ());
System.out.println ("Free memory after count" + counter
+ "is" + getfreememory () + "MB");
Sleep (1000);
}
}
}
Inner class key without hashcode () or Equals ()-Bad implementation
Static Class Key {
Private String key;
Public Key (String Key) {
This.key = key;
}
}
Delay for a given period in milli seconds
public static void sleep (long sleepfor) {
try {
Thread.Sleep (sleepfor);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
Get available memory in MB
public static long Getfreememory () {
Return Runtime.getruntime (). Freememory ()/(1024 * 1024);
}
}
The results are as follows:
Copy Code code as follows:
Map size:1000
Free memory after Count 1000 is 4MB
Map size:2000
Free memory after count = 4MB
Map size:1396000
Free memory after Count 1396000 is 2MB
Map size:1397000
Free memory after Count 1397000 is 2MB
Map size:1398000
Free memory after Count 1398000 is 2MB
Map size:1399000
Free memory after Count 1399000 is 1MB
Map size:1400000
Free memory after Count 1400000 is 1MB
Map size:1401000
Free memory after Count 1401000 is 1MB
.....
.....
Map size:1452000
Free memory after Count 1452000 is 0MB
Map size:1453000
Free memory after Count 1453000 is 0MB
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap
At Java.util.HashMap.addEntry (hashmap.java:753)
At Java.util.HashMap.put (hashmap.java:385)
At Memoryleak.main (memoryleak.java:10)
Q: How to solve the above memory leak?
A: Implement the Equals and Hascode method of the key class.
Copy Code code as follows:
.....
Static Class Key {
Private String key;
Public Key (String Key) {
This.key = key;
}
@Override
public boolean equals (Object obj) {
if (obj instanceof Key)
Return Key.equals ((key) obj. Key);
Else
return false;
}
@Override
public int hashcode () {
return Key.hashcode ();
}
}
.....
The following results are available for the re-executing program:
Copy Code code as follows:
Map Size:1
Free memory after Count 1000 is 4MB
Map Size:1
Free memory after count = 4MB
Map Size:1
Free memory after Count 3000 is 4MB
Map Size:1
Free memory after Count 4000 is 4MB
...
Free memory after Count 73000 is 4MB
Map Size:1
Free memory after Count 74000 is 4MB
Map Size:1
Free memory after Count 75000 is 4MB
Q: In the actual scene, how do you find the memory leak?
A: Get the thread ID from the following code
Copy Code code as follows:
C:\>jps
5808 Jps
4568 Memoryleak
3860 Main
Open Jconsole from the command line
Copy Code code as follows:
The key classes that implement the Hascode and equals and the diagrams that are not implemented are as follows:
No memory leaks:
Causing the memory leak: