Results 1 to 12 of 12
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Object creation and memory issues

    Java Code:
    ClassName obj; // line no 1
    obj = new ClassName(); // line no 2
    obj.attributeName = "abc";
    I want to know, when memory for object named obj is reserved? Lets assume that ClassName has 2 attributes but in the example above, we set only one attribute. Does this mean that there is not memory reserved for the second attribute of obj?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    I am not getting you. You mean memory is not reserved at creating time and is set at runtime?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Ok thanks... So even if my class had 10 attributes and an object is using just one, no space will be reserved for the remaining 9.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Okey. I have an object and want to know how much memory it is using in terms of bytes.

    I know it is possible in C/C++. Can we do this in Java?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Great article. So one can use the following to know the memory allocations.

    Java Code:
    Runtime.freeMemory() 
    Runtime.maxMemory()
    Runtime.totalMemory()
    And there is no sizeOf() method in Java.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    yep. There is no sizeOf() like in C++. In Java it is so easy actually, and I don't know how to do it in C++.

  11. #11
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    I like it in C++ :D Its simple there
    In Java, you have to calculate the memory consumption after creating each object while in C++, you just call a method and get the memory consumed.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. removing an object from memory
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-12-2007, 10:23 PM
  2. Java Applet re-creation problem
    By Mikalai.Kardash in forum Java Applets
    Replies: 0
    Last Post: 07-23-2007, 03:09 PM
  3. creation of new function
    By an8086 in forum Advanced Java
    Replies: 1
    Last Post: 07-16-2007, 03:20 PM
  4. creation of new folder in the mail
    By an8086 in forum Advanced Java
    Replies: 1
    Last Post: 07-15-2007, 06:10 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
  •