Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
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?

Reply With Quote
  #2  
Old 09-11-2009
Member
 
Join Date: May 2008
Posts: 2,290
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.
Reply With Quote
  #3  
Old 09-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Reply With Quote
  #4  
Old 09-11-2009
Member
 
Join Date: May 2008
Posts: 2,380
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.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 10-11-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How big is an Object in Java? Why Java neglects sizeof?"
Thread Thread Starter Forum Replies Last Post
get classes and object in java preeti makkar Software Development 1 07-05-2012 11:08 AM
Adding an object in java Gunner 1 Software Development 5 27-02-2010 02:20 AM
How to create object in JAVA HP_Crook Software Development 3 09-10-2009 11:52 PM
What is the difference between an object and an instance in java? Lalit5 Software Development 5 02-03-2009 08:00 PM
Object test = new Object() <-- Java, best way in C++ ADISH Software Development 3 25-10-2008 02:32 PM


All times are GMT +5.5. The time now is 01:10 PM.