Home > database >  Java - Callable Out of memory solution?
Java - Callable Out of memory solution?

Time:11-09

Hi Guys can you help me about the error that I've encountered on my Java program. I do the Callable implementation with a loop, Basically I need to send a request to another webservice and I will be collecting the response ID. Based on my testing, my aim to process it asynchronously is working using the below implementation. One time I try to run again my program then I got this kind of error "Error 500: java.lang.OutOfMemoryError: Failed to create a thread: retVal -1073741830, errno 11". What I did is I just restarted the server then it becomes ok.

So I want to know if there is something wrong with my implementation? like is there any additional line of code that I need to add or remove just to prevent again to experience those kind of error? I only experience it once. Hope you could help me

        //pccsSurvList is a list of details coming from the database.

        ExecutorService executorService = null;
        List<Callable<SyncFlagEntity>> lst = new ArrayList<Callable<SyncFlagEntity>>();
        
        if(pccsSurvList != null && pccsSurvList.size() > 0){
            executorService = Executors.newFixedThreadPool(pccsSurvList.size());
            for(PCCSSurveyInfoEntity user: pccsSurvList){

                NotifyEmailTransactionImpl emailTransact = new NotifyEmailTransactionImpl(user);
                lst.add(emailTransact);
                
            }
        }
        // returns a list of Futures holding their status and results when all complete
        List<Future<SyncFlagEntity>> tasks = new ArrayList<Future<SyncFlagEntity>>();
        tasks = executorService.invokeAll(lst);

        executorService.shutdown();
        

CodePudding user response:

java.lang.OutOfMemoryError: Failed to create a thread

This OutOfMemoryError indicates that Java is out of memory.

Most likely the problem lies on this line ...

executorService = Executors.newFixedThreadPool(pccsSurvList.size());

You are getting a lot of rows and there's no enough RAM for the JVM to create a thread for each one. Try logging the number of rows you get and see what's happening.

CodePudding user response:

I think you are running too many processes at once. You should try to set a limit and use a thread pool.

Maybe you can do something like:

ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(pccsSurvList.size());

Then inside for loop you can do:

executor.submit(() -> emailTransact);

There is more here: https://www.baeldung.com/thread-pool-java-and-guava

Also take a look at reactive programming. Can be of more help in your case: https://www.vogella.com/tutorials/RxJava/article.html

  • Related