473,796 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6870

"Jéjé" <je**********@i t-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.setPrope rty("java.compi ler", "javac");
Runtime runtime = Runtime.getRunt ime();
long totalMemory = runtime.totalMe mory()/1024;
long freeMemory = runtime.freeMem ory()/1024;

System.out.prin tln("Total memory: " + totalMemory + " kilobytes.");
Runtime.getRunt ime().gc();
System.out.prin tln("Free memory: " + freeMemory + " kilobytes. Also: ");
System.out.prin tln(Math.round(
(((double)freeM emory)/((double)totalM emory)) * 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
2240
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 when I do the following Byte byteBLOBData = new Byte; byteBLOBData = (Byte)(ds.Tables.Rows);
9
7215
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
3092
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 is 1 byte. Could anyone tell me the reason behind this. Thanks ishekara
0
339
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 Stack Trace at System.Net.TlsStream.InnerWrite(Boolean async, Byte buffer, Int32 offset, Int32 size, AsyncCallback asyncCallback, Object asyncState) at System.Net.TlsStream.BeginWrite(Byte buffer, Int32 offset, Int32 size, AsyncCallback...
6
2787
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) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
3
2442
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 object :(((( Anyone pls help me to solve this problem!!! thanks in advance,
8
8934
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 GetChunk and AppendChunk to read and write binary data from the OLE Object field. I am using VBA and DAO for this experiment. The OLE Object field is being used to store Long Binary data.
5
1900
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 do with those. So please dont jump on me for that. I am making this graphics library and my bitmap data is stored in an array. This library works some time and when I have to use the copy constructor it dosn't seem to work. I did my best to...
13
3167
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 stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.