-1

I'm a newbie in memory allocation so I created a code which does not run through big number based tests, because the maximum stack memory is set to 256mb:

java.lang.OutOfMemoryError: Java heap space

at kdiv.main(kdiv.java:35)

My code:

import java.io.*;
import java.util.*;

public class kdiv {

    public static void main(String[] args) {
        
        int sum = 0;
        int minmax = 1;
        Scanner sc = new Scanner(System.in);
        int testcs = 0;
        if (sc.hasNextInt()) {
            testcs = sc.nextInt();
        }

        for (int x = 1; x <= testcs; x++) {
            // testCase();
            // TODO Auto-generated method stub
            int n = 0;
            double k = 0;
//      System.out.print("n: ");
            if (sc.hasNextInt()) {
                n = sc.nextInt();
            }

//      System.out.print("k: ");
            if (sc.hasNextInt()) {
                k = sc.nextInt();
            }

            minmax = 1;
            sum = 0;

            int arr[] = new int[n];

            for (int i = 0; i < arr.length; i++) {
                arr[i] = 1;
                sum += arr[i];
            }

            for (int a = 0; a < k; a++) {
                if (sum % k == 0) {
                    if (a == 0) {
                        System.out.println(minmax);
                    }
                    break;
                }
                for (int b = 0; b < n; b++) {
                    arr[b]++;
                    sum++;
                    minmax = arr[b];
                    // System.out.println(Arrays.toString(arr));
                    if (sum % k == 0) {
                        System.out.println(minmax);
                        break;
                    }
                }
            }
        }
    }
}

Error message picture

It is a trivial issue, I know, but I don't know how to deal with this. Could anyone at least suggest me where do I learn memory allocation so I can understand this thing? 'Cause I'm stuck. Have a nice day.

DevelBase2
  • 139
  • 6

1 Answers1

1

Are you looking for a way to increase the memory allocation of the JVM? If so, that can be found here:

What are the -Xms and -Xmx parameters when starting JVM?