1

I have java.lang.OutOfMemoryError memory overflow issue. Hello, I have java.lang.OutOfMemoryError memory overflow issue, I am trying to write to write java code which takes a list of number couple and show me the number which couples or doubles here is the input data set line n1: represents the number of times the data set will be entered line n2: indicates the odd number of numbers to enter the third line n3: the odd number list containing a number that does not repeat

exemple:

3 ---->represents the number of times the data set will be entered

3 ----> indicates the odd number of numbers to enter

1 1234567884 1234567884 ----> liste of number

5

4 3 5 4 3

5

1 10 8 10 1

but i execute the code I have an overflow memory error. This is the code:

import java.util.ArrayList;
import java.util.Scanner;

public class myMain {
    /**
     * @function initialize, fonction qui initialise les valeurs d'un tableau
     * @param tab: int, length: int
     * @return void
     * */
    public static void initialize(int[] tab, int length) {
        for(int i = 0; i<length; i++) {
            tab[i] = 0;
        }
    }
    
    public static void main(String[] args) {
        ArrayList<Integer> a_pair_list = new ArrayList<Integer>();
        ArrayList<Integer> a_impair_list = new ArrayList<Integer>();    
        int count[] = new int[Integer.MAX_VALUE]; // 100
        int tab[] = new int[100];
        
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.nextLine());
        
        for(int j = 0; j < N; j++) {
            //on permet a l'utilisateur d'entrer des valeur séparer par des espaces
            String a_num_c = sc.nextLine();
            String[] list = sc.nextLine().split(" ");
            initialize(tab, Integer.parseInt(a_num_c)); 
            //on converti les chaine de nombre en entier
            for(int k = 0; k < list.length; k++) {
                tab[k] = Integer.parseInt(list[k]);
            }       
            
            /* i : compteur, tmp : stock tmporairement la valeur 
            à un certain index du tableau tab[]*/
            int i,tmp = 0;
            initialize(count, tab.length);
            for(i = 0; i < tab.length; i++){
                    tmp = tab[i];
                    count[tmp]++;
            }

            for(i=1; i < count.length; i++){
                if(count[i] > 0 && count[i] == 1){
                    a_impair_list.add(i);
                }else if(count[i] >= 2){
                    a_pair_list.add(i);
                }
             }          
        }
        
       for(int i = 0; i < a_impair_list.size(); i++) {
            System.out.println("Cas n°"+i+": "+a_impair_list.get(i));
        }   
    }
}

3
  • Unrelated: read about java naming conventions. You are getting a lot of things really wrong there. And when writing javadoc: make sure it is meaningful. What you have there is mostly useless noise. Commented Mar 18, 2021 at 15:15
  • In addition to "you want to rewrite this to use normal Java naming conventions", it's also time to start debugging things first: run your code in debug mode in your IDE, and look at what values your variables take on. At what point do they suddenly not match your expectation anymore? Also note that your initialize function is not needed: numerical arrays already initialize all elements to 0. Commented Mar 18, 2021 at 15:16
  • You are probably running out of memory because you are instantiating a massive array with this line int count[] = new int[Integer.MAX_VALUE]; // 100 Commented Mar 18, 2021 at 15:27

2 Answers 2

1

I did run your code in a quick scratch and got the following error:

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at myMain.main(scratch_17.java:19)

Line 19 is int count[] = new int[Integer.MAX_VALUE]; // 100

You should not initialise this array with Integer.MAX_VALUE which is then an array with 2147483647 elements that are initialized with 0

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that if I put a lower value the first case or dataset would throw an error. only the last two datasets will be able to display correctly. what value did you set so that all datasets are executed without returning an error? cordially

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.