Results 1 to 18 of 18

Thread: Java limitaion

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Java limitaion

    The only limitation java have(that I know of) is OutOfMemeoryException heap space.
    What is the max number of characters you can put in a string before running out of heap space?

  2. #2
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Pojahn_M View Post
    The only limitation java have(that I know of) is OutOfMemeoryException heap space.
    What is the max number of characters you can put in a string before running out of heap space?
    I would gather that would depend on how much RAM the computer has.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    but doesnt heap space have a default value for all computers?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I believe you can change the amount of heap space. I'd suggest writing an experiment, get a stringbuilder and continuously concatenating random characters to it.

  5. #5
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    doing it right now(with a string).
    Right now, I am at 17000 characters. I report back when I reach the limit.

  6. #6
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    I canceled the execution after the string was 2 million characters long. It was so slow appending character to such a long string.

  7. #7
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    I wrote this small program to see how many items I could add to an ArrayList to see how big it could get before I exceeded the amount of memory.

    Java Code:
    import java.util.ArrayList;
    
    public class MemoryLimit {
        public static void main( String[] args ) {
            MemoryLimit m = new MemoryLimit();
            m.memoryLimit();
        }
        
        public void memoryLimit() {
            ArrayList<Character> list = new ArrayList<Character>();
            
            int i = 0;
    
            try {
                while( true ) {
                    list.add( 'a' );
                    i++;
                }
            } catch( OutOfMemoryError e ) {
                System.out.printf( "Number of one byte characters added to an ArrayList = %d\n", i );
            }        
        }
    }
    The output is as follows:
    Java Code:
    Number of one byte characters added to an ArrayList = 25764983
    It seems I was able to add nearly 26 million characters to the ArrayList before running out of memory.

    The PC I used was a Core 2 Quad machine running Windows XP with 4GB RAM.

    I was wondering if someone more knowledgeable about computer or OS memory architecture could explain this.

    Thanks.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I can't vouch for the accuracy, but here is a link which may be helpful: [JavaSpecialists 029] - Determining Memory Usage in Java

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The memory tester from that article and the following Object Factory,

    Java Code:
    import java.util.*;
    
    public class ListFactory implements ObjectFactory{
    	public Object makeObject(){
    		ArrayList<Character> charList = new ArrayList<Character>();
    		for(int i = 0; i < 25764982; ++i)
    			charList.add((char)33);
    		return charList;
    	}
    }
    produces this:
    Java Code:
    ListFactory produced java.util.ArrayList which took 103059968 bytes
    103,059,968

    The problem was that a character takes up more than a byte so using the counter directly as you did will cause some problems.

  10. #10
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    I just ran the same program on my Core2Duo laptop running Ubuntu 10.04 64bit with 4GB RAM and I got the following:

    Java Code:
    Number of one byte characters added to an ArrayList = 86956820
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  11. #11
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    I ran the same problem on my six core amd phenom 2 1055t computer with 6 gigs of ram and I got 25764983 as output.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Maximum heap size is (currently) 1/4 of your physical memory or 1Gb, whichever is smaller.
    This can be changed (as said earlier) by using Xmx.

  13. #13
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    I wonder do the makers of the JRE set these heap sizes based on OS? The Linux 64 bit OS has a little over 3X the heap space. I don't know what OS Fabken is running, but I would guess Windows 7 64 bit, but I could be wrong.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Not according to the documentation...of course it all depends if you are both using Oracle JVMs.

    besides, I wouldn't rely on the above code...I'd use the Runtime.maxMemory() method, which will tell you what Xmx is set to, pretty much.

  15. #15
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    I ran the following code on my Windows XP machine:

    Java Code:
    public void memoryUsage() {
        long mem = Runtime.getRuntime().maxMemory();
        System.out.printf( "Number of bytes the JRE will attempt to use = %d bytes.\n", mem );
    }
    I got the following output:

    Java Code:
    Number of bytes the JRE will attempt to use = 259522560 bytes.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Which implies a 1/4 Gb (ish) Xmx by default.
    Which seems a bit odd.
    Does it have dual processors (or dual core) and only about 1Gb of memory?

  17. #17
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    I ran the program on my Core 2 Quad running Ubuntu 10.04 64bit with 6GB RAM

    Java Code:
    import java.util.ArrayList;
    
    public class MemoryLimit {
        public static void main( String[] args ) {
            MemoryLimit m = new MemoryLimit();
            m.memoryLimit();
            m.memoryUsage();
        }
        
        public void memoryLimit() {
            ArrayList<Character> list = new ArrayList<Character>();
            
            int i = 0;
    
            try {
                while( true ) {
                    list.add( 'a' );
                    i++;
                }
            } catch( OutOfMemoryError e ) {
                System.out.printf( "Number of one byte characters added to an ArrayList = %d\n", i );
            }        
        }
        
        public void memoryUsage() {
            long mem = Runtime.getRuntime().maxMemory();
            System.out.printf( "Number of bytes the JRE will attempt to use = %d bytes.\n", mem );
        }
    }
    I got the following output:

    Java Code:
    Number of one byte characters added to an ArrayList = 130435231
    Number of bytes the JRE will attempt to use = 1394409472 bytes.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    The maxmem is about correct.
    Dual core counts as 2 processors for the calculation, so it uses about a quarter.
    Exactly what counts as available memory is probably down to what the OS considers telling the JVM is available.
    In this case about half a gig short of the 6 gig.

Posting Permissions

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