2

Can I increase the stack and the heap in java? I'm using BlueJ.

========

EDIT:

Here is the code:

// ***** Quick-Sort Method *****

public static void quickSort(int[] data, int first, int n)
{
    int p, n1, n2;
    if(n > 1)
    {
        p = partition(data, first, n);
        n1 = p - first;
        n2 = n - n1 - 1;
        quickSort(data, first, n1);
        quickSort(data, p+1, n2);
    }
}

// ***** PRIVATE HELPER FUNCTIONS *****

public static void quickSort(int[] data)
{
    quickSort(data, 0, data.length);
}

private static int partition(int[] A, int first, int n )
{
    int right = first + n - 1;
    int ls = first;
    int pivot = A[first];
    for(int i = first+1; i <= right; i++)
    {
        if(A[i] <= pivot)
        // Move items smaller than pivot only, to location that would be at left of pivot
        {
            ls++;
            swap(A, i, ls);
        }
    }
    swap(A, first, ls);
    return ls;
}

private static void swap(int[] data, int pos1, int pos2)
{
    int temp = data[pos1];
    data[pos1] = data[pos2];
    data[pos2] = temp;
}
Dan J
  • 16,319
  • 7
  • 50
  • 82
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • 8
    are you sure it's not just a bug in your code? – Tony The Lion May 19 '11 at 21:09
  • I don't think so, I looked into google and the same problem heppened many times – Eng.Fouad May 19 '11 at 21:10
  • 3
    @Eng: If your quicksort isn't approximately dividing the size by two on every iteration (which I'll bet it isn't), something's wrong with it. Double-check your algorithm. I'd bet your partition is just splitting the list into 1 less element each time. – user541686 May 19 '11 at 21:11
  • How big is the dataset that you're attempting to quicksort? Is it almost sorted to begin with? Did you write your own quicksort? Increasing the stack size without first determining what's actually going on is a really bad idea. – dlev May 19 '11 at 21:12
  • 2
    You probably have a bug in your alogrithm resulting in an infinte recursion which shows up as a stack overflow error. Paste your code and we'll be able to help you. – Codo May 19 '11 at 21:12
  • @finnw well this is a uni-assignment to measure the execution time of many sorting methods upon large amount of inputs (1000000 int) – Eng.Fouad May 19 '11 at 21:13
  • @dlev: Doesn't matter how big the data set is, the depth needed decreases *exponentially*. A depth of 100 means 2^100 bytes of data for overflow, which I'd bet a million bucks he doesn't have. – user541686 May 19 '11 at 21:13
  • If you are dividing your dataset in two on each recursion, your stack depth should be log2(n) so for a maximum array or list, the largest depth should be 32. – Peter Lawrey May 19 '11 at 21:14
  • @Mehdrad that's true only if he's using a properly written version of quicksort with data that isn't already or almost-already sorted. I suppose I hit those other two points after, so the size question is largely redundant. – dlev May 19 '11 at 21:16

5 Answers5

6

Trying to increase the stack size due to an overflow, would be like buying more rubbish bins, when your bin is full instead of taking it to the dump.

Most probably you go into an endless recursion. Could you post your code?

Hyperboreus
  • 822
  • 5
  • 4
2

You can use the following JVM options:

  • -Xms initial java heap size
  • -Xmx maximum java heap size
  • -Xss Set thread stack size

If you want to set these options by default in BlueJ, you need to do the following:

  • Find bluej.defs file
  • Find bluej.vm.args property (line) within that file
  • Add the option that you want in that line, i.e. bluej.vm.args = -Xmx512m to set the heap size to a maximum of 512 MB.

I hope this helps.

CRM
  • 4,569
  • 4
  • 27
  • 33
1

The stackoverflow error is usually because of a bad recursive call. Are you sure you're not doing anything wrong like specifying proper exit paths (aka terminating conditions ) for your recursion flow?

Kal
  • 24,724
  • 7
  • 65
  • 65
0

to me it looks like it's the partition that's bugged

private static int partition(int[] A, int first, int n )
{
    int right = first + n-1;
    int ls = first;
    int pivot = A[right];//use right most for pivot
    for(int i = first;i<right;i++)
    {
        if(A[i]<pivot){
           swap(A,i,ls);
           ls++;//inc after swap
        }

    }
    swap(A,right,ls);
    return ls;
}

I got this code from wikipedia

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
0

The simplest implementation of Quicksort is vulnerable to O(N) memory use in the worst case. It is possible to modify it to use O(log N) in the worst case, by only recursing into the smaller subarray and by turning the remaining recursion into a while loop:

//the following code probably contains of-by-one errors

quicksort(xs, begin, end):
    while(not empty list){
        mid = partition(xs, begin, end)
        if( mid-begin < end-mid){
            quicksort(xs, begin, mid)
            end = mid
        }else{
            quicksort(xs, mid, end)
            begin = mid
        }
hugomg
  • 68,213
  • 24
  • 160
  • 246