Summary of memory overflow Exception Solutions

Source: Internet
Author: User

These days, I have encountered a Java memory overflow problem. I have been trying to solve this problem. Now I think of three methods: Adjusting virtual machine parameters, releasing useless objects immediately, and using serialization and deserialization technologies.

The following is the specific code implementation:

Package com. Free;

Import java. Io. file;

Import java. Io. fileinputstream;

Import java. Io. filenotfoundexception;

Import java. Io. fileoutputstream;

Import java. Io. ioexception;

Import java. Io. objectinputstream;

Import java. Io. objectoutputstream;

Import java. Math. biginteger;

Import java. util. arraylist;

Import java. util. List;

/**

* @ Author free

* Springcomingagain@gmail.com

*/

Public class outmemorysolution {

/***

* Change JVM arguments

* Adjust VM Parameters

*

*

 

  *

  •  

    * First try

    *

    *-Xloggc: GC. Log

    *-Xms256m

    *-Xmx256m

    *

    * Throw java. Lang. outofmemoryerror: Java heap Space

    *

    *

  •  

    * Second try

    *

    *-Xloggc: GC. Log

    *-Xms512m

    *-Xmx512m

    *

    *

  •  

    */

    Public static void changejvmarguments (){

    Long starttime = system. currenttimemillis ();

    System. Out. println ("START:" + starttime );

    List List = new arraylist (0 );

    For (INT I = 0; I <5000000; I ++ ){

    List. Add (New biginteger ("1000000 "));

    }

    Long endtime = system. currenttimemillis ();

    System. Out. println ("End...:" + endtime + "cost :"

    + (Endtime-starttime ));

    }

    /**

    * Release object immediate

    * Release useless objects immediately

    *

     

      *

  •  

    * Release object

    *

    *

    * // First release object

    * Biginteger temp1 = NULL;

    * For (INT I = 0; I <1200000; I ++ ){

    * Temp1 = array1 [I];

    * Temp1 = NULL;

    * Array1 [I] = NULL;

    *}

    * Array1 = NULL;

    *...

    * // Second release object

    * Temp1 = NULL;

    * For (INT I = 0; I <1200000; I ++ ){

    * Temp1 = array2 [I];

    * Temp1 = NULL;

    * Array2 [I] = NULL;

    *}

    * Array2 = NULL;

    *

    *

  •  

    */

    Public static void releaseobjectimmediate (){

    Long starttime = system. currenttimemillis ();

    System. Out. println ("first start:" + starttime );

    Biginteger [] array1 = new biginteger [1200000];

    For (INT I = 0; I <1200000; I ++ ){

    Array1 [I] = new bigint ("10000000 ");

    }

    // First release object

    Biginteger temp1 = NULL;

    For (INT I = 0; I <1200000; I ++ ){

    Temp1 = array1 [I];

    Temp1 = NULL;

    Array1 [I] = NULL;

    }

    Array1 = NULL;

    Long endtime = system. currenttimemillis ();

    System. Out. println ("first end...:" + endtime + "cost :"

    + (Endtime-starttime ));

    // Do something start

    // Do something end ..

    Starttime = system. currenttimemillis ();

    System. Out. println ("second start:" + starttime );

    Biginteger [] array2 = new biginteger [2, 1200000];

    For (INT I = 0; I <1200000; I ++ ){

    Array2 [I] = new bigint ("10000000 ");

    }

    // Second release object

    Temp1 = NULL;

    For (INT I = 0; I <1200000; I ++ ){

    Temp1 = array2 [I];

    Temp1 = NULL;

    Array2 [I] = NULL;

    }

    Array2 = NULL;

    Endtime = system. currenttimemillis ();

    System. Out. println ("second end...:" + endtime + "cost :"

    + (Endtime-starttime ));

    }

    /**

    * Delaysave

    * Serialization and deserialization Technologies

    *

  • Store object

     

    *

  • Read object

     

    *

  • Do something

     

    */

    @ Suppresswarnings ("unchecked ")

    Public static void delaysave (){

    Final string objectstorefolder = "D: \ OBJ \\";

    System. Out. println ("store object start ..");

    // Store object serialized object

    Objectoutputstream out = NULL;

    Try {

    Out = new objectoutputstream (New fileoutputstream (

    Objectstorefolder + "objectfile. OBJ" + 0 ));

    List List = new arraylist (0 );

    For (INT I = 0; I <5000000; I ++ ){

    If (I + 1) % 100000) = 0 ){

    Out. writeobject (list );

    Out. Close ();

    Out = NULL;

    List = NULL;

    List = new arraylist (0 );

    Out = new objectoutputstream (New fileoutputstream (

    Objectstorefolder + "objectfile. OBJ" + (I % 1000000) + 1 )));

    }

    System. Out. println (I );

    List. Add (New biginteger ("1000000 "));

    }

    } Catch (filenotfoundexception e ){

    System. Err. println (E. getmessage ());

    } Catch (ioexception e ){

    System. Err. println (E. getmessage ());

    }

    Finally {

    If (null! = Out ){

    Try {

    Out. Close ();

    Out = NULL;

    } Catch (ioexception e ){

    // Ignore

    }

    }

    }

    System. Out. println ("store object end ..");

    System. Out. println ("read object start ..");

    // Read object deserialization object

    File [] filelist = new file (objectstorefolder). listfiles ();

    Int filesize = filelist. length;

    Objectinputstream in = NULL;

    Try {

    For (INT I = 0; I <filesize; I ++ ){

    In = new objectinputstream (

    New fileinputstream (filelist [I]);

    List List = (list ) In. readobject ();

    // Do something start

    System. Out. println (list. Size ());

    // Do something end ..

    }

    } Catch (filenotfoundexception e ){

    System. Err. println (E. getmessage ());

    } Catch (ioexception e ){

    System. Err. println (E. getmessage ());

    } Catch (classnotfoundexception e ){

    System. Err. println (E. getmessage ());

    }

    Finally {

    If (null! = In ){

    Try {

    In. Close ();

    In = NULL;

    } Catch (ioexception e ){

    // Ignore

    }

    }

    }

    System. Out. println ("read object end ..");

    }

    /**

    * Test code

    * @ Param ARGs

    */

    Public static void main (string [] ARGs ){

    Changejvmarguments ();

    Releaseobjectimmediate ();

    Delaysave ();

    }

    }

  •  

    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.