Recover from java.lang.OutOfMemoryError

So today, while studying Java, I finally came across this particular error. This error seems to be fairly common, and trying to recover it caused mixed reactions. Some people think that it is useful in certain scenarios , so its good to know, some used it in their projects , while others vehemently oppose the idea of catching this error, and then many others are just as confused as I am .

Edit: Oh by the way, this is the error I encountered. I want to catch these errors and reduce the incrementor value by 1/10 the next time until another error is found
(and so on ... until I find the upper bound) This is the error I encountered

Since I am taking my children’s steps in Java and cannot find anything special about this topic, I would like to ask your help in this specific example:

public class SpeedDemoClass {
        static int iterations=0;
        static int incrementor=10000000;
    public static void main(String[] args) {
        while(incrementor>1){
            try{
              iterations=iterations+incrementor;
              iterator(iterations);
            }
            catch(Exception e){
                System.out.println("So this is the limiting error: "+e);
                iterations=iterations-incrementor;
                incrementor=incrementor/10;
                iterations=iterations+incrementor;
            }
        }
        System.out.println("The upper limit of iterations is: "+iterations);
    }

    public static void iterator(int a){
            long start_time= System.currentTimeMillis();
            StringBuilder sbuild= new StringBuilder("JAVA");
            for(int i=0;i<a;i++){
                sbuild.append("JAVA");
            }
            System.out.println("Performing "+a+" append operations;"
                    +"process completed in :"
                    +(System.currentTimeMillis()-start_time)+"ms");
    }
}

Did you try to compile it?

Of course it doesn't work

Here is a brief description of what I'm trying to do!

I am trying to do the following:

  • Initialize incrementor = 10000000 and iterations = 0 and pass the result incrementor = incrementor + iterations iterator () .
  • , , * ** incrementor, incrementor 10, li >
  • 1 2, .
  • , , ,
    (.. 0)

, 92274686. . , , , . , - , .

+4
5

bro, outofmemoryerror, Exception catch OutOfMemoryError

catch (OutOfMemoryError e)

+1

Exception, OutOfMemoryError - Error. , Error Exception - Java! Throwable.

catch Throwable. Error Exception, OutOfMemoryError.

+2

" " , , , .. . - !

, Java, . ensureCapacityInternal.

- , .

, :

StringBuilder sbuild= new StringBuilder("JAVA");

StringBuilder sbuild= new StringBuilder(500000000);

, OOM. . ; , Java, .

, 8 ? java -Xmx7500m. 3 . , Java 25% , . , Java , , .

+2

, , VM . , - , , , Java . , , , . , , , .

, , VM, . , , , . , , .

0

, - . , , , , .

, , " ", , , . *. , , . , java.lang.OutOfMemoryError, , .

* , :)

, , (, ..)

, , :

public class SpeedDemoClass {
    static int iterations=0;
    static int incrementor=10000000;
    public static void main(String[] args) {
        while(incrementor>0){
            try{
                iterations=iterations+incrementor;
                int a = iterations;
                long start_time= System.currentTimeMillis();
                StringBuilder sbuild= new StringBuilder("JAVA");
                for(int i=0;i<a;i++){
                    sbuild.append("JAVA");
                }
                System.out.println("Performing "+a+" append operations;"
                    +"process completed in :"
                    +(System.currentTimeMillis()-start_time)+"ms");
            }
            catch(OutOfMemoryError e){
                System.out.println("OutOfMemory bound reached beyond this point with error: "+e
                +"\nReverting back, and Changing the value of incrementor by 1/10th...");
                iterations=iterations-incrementor;
                incrementor=incrementor/10;
                iterations=iterations+incrementor;
            }
        }
        System.out.println("The upper limit of iterations is: "+iterations);
    }
}

Changelog:
, .
(1) iterator(), , .
(2) catch(Exception e) catch(OutOfMemoryError e). , , , .

:

Exit-Part-1 Exit-Part-2

, , . , , ?:)

0

Source: https://habr.com/ru/post/1652870/


All Articles