Results 1 to 5 of 5

Thread: How big is an Object in Java? Why Java neglects sizeof?

  1. #1
    Join Date
    Sep 2009
    Posts
    125

    How big is an Object in Java? Why Java neglects sizeof?

    I had used C, C++ programming languages in past. I have made several projects in both the prgramming languages. But now I have been given o new project to be made using Java. The problem is that while working on this project I have learnt that Java doesnot has any sizeof() operator as we have in C. Is there any other altenative for this ? Why Java doesnot has a sizeof() operator?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: How big is an Object in Java? Why Java neglects sizeof?

    The other way instead of using the sizeof() operator which Java doesnot support you can do the following:


    Code:
    static Runtime rt = Runtime.getRuntime();
    .
    .
    .
    long begin, finish;
    Object O;
    rt.gc();
    begin = rt.freememory();
    O = new Object();  
    finish =  rt.freememory();
    System.out.println("This had taken " + (begin-finish) + "bytes.");

    This method although has a short coming that a garbage collection could occur in the middle of the code which you are instrumenting. This will throw off the byte count.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How big is an Object in Java? Why Java neglects sizeof?

    Initially when Sun developed Java there was absolutely no way to measure the size of an object. Memory allocation and object construction in Java are both binded together. Thus, making it difficult to get the size of the object. Programmers later on developed an indirect way to get the size of the object. This was by measuring the difference in the memory used by the program before and after creating the object. This difference thus indicated the actual size memory of that object.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How big is an Object in Java? Why Java neglects sizeof?

    A programming language like C has a sizeof operator. This is because the C user has to manage calls to malloc(). And this is since the size of primitive types is not standardized. Whereas in Java the allocation of memeory and construction of object are both binded together This is the reason why Java doesnot needs a sizeof operator.

  5. #5
    Join Date
    Feb 2009
    Posts
    96

    Re: How big is an Object in Java? Why Java neglects sizeof?

    instead of size of() JAVA uses .length() as follows:

    int[] cars = {"ford", "chevy", "honda"};


    for(int j = 0; j <= cars.length; j++)
    System.out.print(cars);


    that will take the array length and use that as the size of

Similar Threads

  1. get classes and object in java
    By preeti makkar in forum Software Development
    Replies: 1
    Last Post: 07-05-2012, 11:08 AM
  2. Adding an object in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 03:20 AM
  3. How to create object in JAVA
    By HP_Crook in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 11:52 PM
  4. Replies: 5
    Last Post: 02-03-2009, 09:00 PM
  5. Object test = new Object() <-- Java, best way in C++
    By ADISH in forum Software Development
    Replies: 3
    Last Post: 25-10-2008, 02:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,416,451.67504 seconds with 17 queries