Results 1 to 4 of 4
  1. #1
    CapsLoc is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Increasing heap/stack size (MacOSX)

    Hello,

    I'm running MacOSX (leopard) and use Terminal to run java programs.
    I was wondering how I should change the heap or stack size. I thought the right way would be:

    java -Xss2048m ProgramName or
    java -Xmx1024k ProgramName


    would do the trick.
    Thing is, I have some code (which I didn't wrote myself) that's generating threads infinetely until my memory is all used (it will then show an out of memory error). It will also show how many threads have been created until memory ran out. If I just type in

    java ProgramName

    it will stop at say 2000 threads. But if I use one of the two commands from earlier (to change heap or stack size), I'd still have the same amount of threads, where it supposedly should be able to generate more threads.

    Is the above way the right way to change the heap or stack size in java for MacOSX (leopard) ?
    or
    Would there be another reason why I can't generate more threads even though I've changed the heap/stack size?

    Here's the code:
    Java Code:
    import java.math.BigInteger;
    
    
    public class MemoryTest extends Thread 
    {
        int number;
        public MemoryTest (int i)
        {
            number = i + 1;
        }
    
        public void run ()
        {
            try
            {
                this.sleep (10);
    
                Thread b = new MemoryTest(number);
                b.start();
                System.out.print(" * " + number);
    
                this.sleep (10);
    
                BigInteger numberPower = (new BigInteger(new Integer(number).toString())).pow(number);
                System.out.println(" (" + number + ")**" + number + " = " + numberPower);
    
                b.join();
    //            b.join(10000L);
            }
            catch (OutOfMemoryError o)
            {
                System.out.println("\n***\n" + o.getMessage() + ": " + 
                                   "\nout of memory while creating thread " + number);
                o.printStackTrace();
                System.exit(1);
            }
            catch (InterruptedException e)
            {
                System.out.println("Thread " + number + " interrupted.");
            }
        }
    
        public static void main (String args[ ]) 
        {
            final int ONE = 1; 
            Thread a = new MemoryTest(ONE);
    
            a.start();
            System.out.print(" * " + ONE);
    
            try
            {
                a.join();
            }
            catch (InterruptedException e)
            {
                System.out.println("Main interrupted whilst joining threads");
            }
        }
    }
    Thanks in advance.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Hey, normally when java runs out of 'Heap space', it simply needs more ram. In OSX as in other platforms, you modify ram usage with:

    java -Xmx1024m -Xms512m yourApp.class

    You need to set the max size first - I have had trouble trying to set the minimum before maximum.

    Also, I have also had issues with needing to modify the MaxPermSize to get large webapp collections to run. For example, here is my startup code for the Jetty app server which hosts several HUGE web apps:

    java -server -XX:MaxPermSize=256m -XX:PermSize=128m -Xmx1024m -jar start.jar

    See if any of this helps!

  3. #3
    CapsLoc is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Hello,
    Thanks for the reply.

    I've tried a few things:

    java MemoryTest
    java -Xmx1024m MemoryTest
    java -Xmx1024m -Xms512m MemoryTest
    java -Xmx1024m -Xms1024m MemoryTest
    java -Xms1024m -Xmx1024m MemoryTest

    but unfortunately all give the same output:
    Java Code:
    unable to create new native thread: 
    out of memory while creating thread 2545
    java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:658)
        at MemoryTest.run(MemoryTest.java:20)
    It will always generate 2545 threads, whereas changing the heap size should allow for more threads to be created.

    Also, running java -Xmx1024m -Xms512m MemoryTest.class doesn't work, I have to do it without the .class at the end.

    The only code I'm compiling is the one I gave above, which doesn't really require any connections to any servers, would it still be usefull to try modifying the MaxPermSize? If so, how should I be doing it? Using
    Java Code:
    java -server -XX:MaxPermSize=256m -XX:PermSize=128m -Xmx1024m -jar start.jar
    gives the error:
    Java Code:
    Unable to access jarfile start.jar
    Thanks again.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Instead of guessing what's going on take a heap dump each time and see.
    Analyse it using something like the MAT for Eclipse.

    Since your code is pretty basic you won't need to up your perm gen, which is the area of memory used for storing classes. Large web apps tend to have lots of classes so often need this to be increased.

Similar Threads

  1. The Stack and the Heap
    By al_Marshy_1981 in forum New To Java
    Replies: 9
    Last Post: 06-25-2010, 09:45 AM
  2. increasing jvm heap space
    By kizyle502 in forum Advanced Java
    Replies: 1
    Last Post: 05-15-2010, 10:51 AM
  3. Changing the stack size
    By saif.hakim in forum New To Java
    Replies: 1
    Last Post: 04-26-2009, 06:58 AM
  4. How to set a stack size. -Xss doesn't work
    By protonus in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 06:59 PM
  5. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 02:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •