473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

object byte size

Hi everyone.
I'm currently implementing a Cache system.
Limiting its capacity with the number of object makes no sense, since
objects can have any size they want.

So I'd like to limit its capacity in term of number of bytes allready
cached. Therefore, I need to know what is the size in byte of any
object in the heap.

I thought about serializing the object to an array of byte, but this
method implies that the user is forced to make its objects serializable,
which is neither practical, nor kind to him or her.
Furthermore, serializing objects just to know their size in byte is
not efficient.

Is there a way to do this in a simple and elegant way ?

Thanks for your answers.

Jérôme.

--
--
He's just a politician trying to save both his faces...
----------------------------
jerome.eteve_at_it-omics.com
Jul 17 '05 #1
2 6849

"Jéjé" <je**********@it-omics.com> wrote in message
news:pa**********************************@it-omics.com...

Hi everyone.
I'm currently implementing a Cache system.
Limiting its capacity with the number of object makes no sense,
since objects can have any size they want.

So I'd like to limit its capacity in term of number of bytes
already cached. Therefore, I need to know what is the size
in byte of any object in the heap.

I thought about serializing the object to an array of byte, but
this method implies that the user is forced to make its objects
serializable, which is neither practical, nor kind to him or her.
Furthermore, serializing objects just to know their size in byte
is not efficient.

Is there a way to do this in a simple and elegant way ?

Thanks for your answers.


Unfortunately Java does not implement the equivalent of the C / C++ 'sizeof'
operator, so it is not possible to actually *know* the size of an object.
AFAIK, all you can do is make estimates of object size [e.g. add up the
sizes of all primitive-type fields (since these values are fixed), and
(maybe ?) assume a size for each reference field].

To 'mark' a class as 'cacheable' you could implement an interface, say:

interface Cacheable
{
int size();
}

which, aside from so identifying a class, also returns an estimate of its
size in bytes. If size estimates meet your requirements this might be
viable. Of course you will encounter the same problem that the classes
implementing the 'Cloneable' interface encounter - no means of ensuring each
and every non-primitive field also implements the interface.

I hope this helps.

Anthony Borla
Jul 17 '05 #2
> Hi everyone.
I'm currently implementing a Cache system.
Limiting its capacity with the number of object makes no sense, since
objects can have any size they want.

So I'd like to limit its capacity in term of number of bytes allready
cached.


OK - I'm not quite sure if this is is something you can use but have a look
at:

public void showMemoryInfo()
{
System.setProperty("java.compiler", "javac");
Runtime runtime = Runtime.getRuntime();
long totalMemory = runtime.totalMemory()/1024;
long freeMemory = runtime.freeMemory()/1024;

System.out.println("Total memory: " + totalMemory + " kilobytes.");
Runtime.getRuntime().gc();
System.out.println("Free memory: " + freeMemory + " kilobytes. Also: ");
System.out.println(Math.round(
(((double)freeMemory)/((double)totalMemory)) * 100.0) + "%)");

}

Regards Jesper
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pierre-Benoit | last post by:
Hi there, I've a strange problem with ado.net and an Access db. I need to create a little C# app that take the content of "ole object" field and then save it into a file. The problem is that...
9
by: pembed2003 | last post by:
Hi all, If I have the following: class Foo{ void something(void){;} }; and then later I say: Foo f;
8
by: ishekara | last post by:
Hi all, An object of a class A which has no member variables and no member functions, still the size of the object is 1 byte, if there is a byte member variable then also the size of the object...
0
by: Asaf | last post by:
Hi, When I am doing a POST to a SSL URL I am getting this error on first attempt "Cannot access a disposed object named "System.Net.TlsStream"." After the first attempt all works fine, here is the...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
8
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
5
by: JoeC | last post by:
I am still working in my maze game and I am making improvments. The main sticking problem that I have is passing my graphic library. This probram uses windows apis but my question has nothing to...
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.