|
| ||||||||||
| Tags: java, object, programming, sizeof, sun |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How big is an Object in Java? Why Java neglects sizeof?
|
|
#2
| ||||
| ||||
| 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
| ||||
| ||||
| 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.
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| 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 |
|
#5
| |||
| |||
| 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 |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |