This will be a more evil article. When you want to create a tragedy in someone's life, you may go to Google to search it. In the world of Java, memory overflow is just a bug that you may introduce in this case. Your victims will spend a few days or even a few weeks of sleeping nights in the office.
In this article, I will introduce two types of overflow methods, which are easier to understand and reproduce. And they are all case studies of the source project, but in order to make you clearly grasp, I simplify them.
But rest assured that after we encountered and solved the overflowing bug, similar cases will be more common than you imagine.
Let's first enter the state. When using Hashset/HashMap, the key value used is not or its equals ()/hashcode () method is incorrect, which will cause a notorious error.
Class KeylessEntry {Static Class Key {Integer ID; key ID (Integer ID) {This.id = id;} @Override Public int Hashcode () {Return ID.hashcode (). ;}} Public Static Void Main (String [] ARGS) {Map m = new hashmap (); about (true) for (int i = 0; i <10000; i ++) if (! M.Containskey (i)) m.put (new key (i), "number:" "" + i);}}When you run the code above, you may expect that it will never have a problem to run. After all, the built -in cache scheme will only increase to 10,000 elements, and then it will not increase middle. However, this is not the case. Elements will always grow, because the key category does not implement a suitable Equals () method after hashcode ().
The solution is simple, as long as you add an EQUALS method like the following example. But before finding the problem, you must have spent a lot of valuable brain cells.
@OverridePublic Boolean Equals (Object O) {Boolean Response = False; if (ONSTANCEOF Key) {response = ((key) o) .ids (this.id);} Retu rn response;} The next one you have to remind your friends is to deal with String. Its performance will be weird, especially when the JVM version difference is combined. String's internal working mechanism has been changed in JDK 7U6, so if you find that the product environment is only the difference between a small version number, then you are ready for conditions. Tarze the code similar to the following code, and then ask him why this bug only appears in the product.
Class Stringer {Static Final INT MB = 1024*512; Static String CreatelongString (Int Length) {StringBuilder SB = New StringBuilder (LENGTH); English; I ++) sb.append ('a') ; sb.append (System.nanotime ()); Return sb.tostring ();} Public Static Void Main (String [] ARGS) {List Substrings = New ArrayList (); for (INT I = 0; I <100; I ++) {string longstr = createlongstring (mb); string substr = Longstr.substring (1,10); substrings.add (substr);}}}}What's wrong with the code above? When it runs before the version of the JDK 7U6, the returned string will save a reference to the string of about 1m size. If you are set to -xmx100m when you run, you will get an unexpected OOM mistake. Combined with the differences between platforms and versions in your experimental environment, things that hurt the brain scriptures occur.
Now if you want to cover up your footprints, we can introduce some more advanced concepts. for example
I hope we give you some raw materials that think about and some materials when you want to repair someone. This will bring endless debugging. Unless your friend uses Plumbr to find the overflowing location.