Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Checking memory allocation

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Checking memory allocation

    I need to check that memory was successfully allocated in the following (very, very basic) code. How do I check it? Apparently one option is to use a try....catch block. But what exactly would I be catching?

    And why do I even need to check this?

    public class Square
    {
            // Data members
            final int r = 10;
            final int c = 10;
     
            Cell[][] square;
     
            public Square()
            {
                    square = new Cell[r][c];
             }
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Checking memory allocation

    Quote Originally Posted by ineedahero View Post
    I need to check that memory was successfully allocated in the following (very, very basic) code. How do I check it? Apparently one option is to use a try....catch block. But what exactly would I be catching?
    If there is no more heap space during instantiation of an object, the JVM throws OutOfMemoryError.
    Normally OutOfMemoryError and other Errors should not be caught.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    ineedahero (December 7th, 2013)

  4. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    305
    My Mood
    Bored
    Thanks
    45
    Thanked 85 Times in 71 Posts
    Blog Entries
    4

    Default Re: Checking memory allocation

    Haha, I take it you are a C/C++ coder ? Anyways, don't worry about it, Java takes care of allocation and garbage collection for you. All you need to do is instantiate the object:

    Square mySquare = new Square();

    This object, 'mySquare' has a place in memory and will get cleaned up when it falls out of scope or when the program ends.

    Oh, and try / catch blocks are for exception handling. When something goes wrong at run-time Java throws a stack of exceptions. 'try' attempts to run some code and 'catch' does something when it f's up.

  5. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    ineedahero (December 7th, 2013)

  6. #4
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Checking memory allocation

    Yeah, I am from C++ lol.

    Thanks for the help guys!

Similar Threads

  1. Replies: 0
    Last Post: April 3rd, 2013, 09:17 PM
  2. Properly releasing memory to avoid memory pileup/crash
    By fickletrick in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 22nd, 2012, 10:09 AM
  3. [SOLVED] Memory usage increasing in while loop - is it a memory leak
    By mds1256 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2012, 10:06 AM
  4. Increase Java Memory Allocation
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: July 15th, 2010, 02:34 PM