Size Of Using Serialization

Hi I would like to implement a size of method in Java. I know it is impossible but I also know that there are turn arounds to the problem using methods such as gc and serialization which give satiffactory results.
In my case acurracy is not of utmost importance but speed is. The sizeOf method will be used in a object caching service and will be called each time an object is put in the cache.
I thought that serialization would be a good option. Do you guys think it is the best one? And if yes how can I go around finding the size of the serializaed stream without writing it to disk.
My main goal would be to turn the following code to write the serialzation to a String instead of the file in order to eliminate disk
IO.
BaseCacheReference B = new BaseCacheReference(new Attributes());
FileOutputStream f_out = new FileOutputStream ("C:\\myobject.data");
ObjectOutputStream obj_out = new ObjectOutputStream (f_out);
obj_out.writeObject (B);
Can anyone help me?
Thanks in Advance
Nick.

Thanks guys and girls,
TO SYLVIA:
I am trying to create a cache engine with different policies such as GC based using soft references
and also my own using LRU, LFU methods. The cache will be used to cache objects that are expensive
to create. Thus it is simple object caching. (theOCS4J specification is similar to what I have in mind)
The cache will be sub-divided into what I called regions of memory
sizes specfied by the user. Each region uses one of the above policies to clear the cache when it reaches its limit.
Now my problem is how can I determine if my region has reached the limit without knowing the size of the objects
being added to the cache.
TO forumNazi:
DO YOU GET THE POINT forumNazi or where you simply just stoned when you read the message? Anyway
could you pls avoid writing useless messages in the near fututure pls since they don'nt get
any of us in a better position.
Back to the problem!! I tried using the ByteArrayOutputStream as DrJohnFoster said
and created the following function:
public int sizeOf(CacheReference CacheObject)
          try
               ByteArrayOutputStream baos = new
ByteArrayOutputStream();
               ObjectOutputStream out = new ObjectOutputStream(baos);
               out.writeObject(CacheObject);
               out.flush();
               // Subtract 4 bytes from the length, because the serialization
               // magic number (2 bytes) and version number (2 bytes) are
               // both written to the stream before the object
               return baos.toByteArray().length - 4;
          catch(IOException e)
               System.out.println(e);
               return 0;
Since you said that serialization includes stuff such as class names, field names then I guess
I could either find an aprroximation to the size of the object by trying to calculate the
amount of mem used in field names, OR I could resort reflection.
I don'nt know if it is possible but could you post me a copy of the code of the sizeOf() using
reflection sylvia.
Thanks to ALL

Similar Messages

  • Video output size to use for a (horizontal) Lower Third.

    Hi. I would like to know what the video output size to use for a (horizontal) Lower Third. I will be very grateful for your help. Thanx in advance. Sam.

    Make it the same horizontal & vertical size as your video.  The upper 2/3 needs to be transparent.

  • I'd like to select print paper size with using AxAcroPDFLib method depend on user

    I'd like to select print paper size with using AxAcroPDFLib method depend on user
    request. Is it possible? in AxAcroPDFLib?
    Is there any method to select print paper size in the C# programming.
    Or if you know another method, please introduce how to use.
    Thank you for your cooperation always.

    I am pretty sure that you can only do this from JavaScript or from C/C++ APIs.

  • I am attempting to print a number of DL size envelopes using Word 2010 and Excel2010 on a HP Officej

    I am attempting to print a number of DL size envelopes using Word 2010 and Excel 2010 on a HP Officejet 6500A Plus printer in Operating system Windows 7. Using the mail merge facility the printer reports a paper mismatch. Word does not appear to have envelope DL size in its list of paper sizes only something called envelope #10. The Print driver software when I run out of stationery asks me to load more stationery and hit the OK button.
    Questions
    1. How do I add envelope DL size to Word 2010
    2. Where is the Officejet 6500A Plus OK button
    3. Has this problem been experienced by others
    Regards

    I am having the same problem does anyone have an answer there are many having the same problem

  • Reducing app size and using memory

    Building with release preference Blank App template will give you a ~200kb .exe, which in running state use ~7mb of memory.
    It has a huge list of external dependencies.
    So, is it possible to reduce app size and using memory?
    Or, how safely unlink from app headers that really didnt used?

    generally speaking  premature optimization is not a good idea.
    Normally those tricks we learned from stone age are picked by tools already.
    (http://stackoverflow.com/questions/6215782/do-unused-functions-get-optimized-out)
    So if you experice performance/optimization issue
    optimization ususally goes with repeated measure -> optimize -> verify
    cycles.

  • Want to learn how to use "serializable"

    Hello, everyone!
    I have never used "serializable" function of Java before. After reading API specification of java.sun.com and some tutorials of java.sun.com dealing with "serializable", I become more puzzled. I just want to know how to use the basic function of "serializable". Can anyone introduce me some basic tutorials or samples?
    Thanks in advance,
    George

    The function of serializable is to be able to serialize an object to a stream. Operations include writing an object to file, or to a network socket connection.
    That object can then be reconstructed from that file (or the reciever on the other end of a network socket can reconstruct the object.)
    Example
    MyClass object = new MyClass() //MyClass implements Serializable
    FileOutputStream fos = new FileOutputStream("C:\test.file"); //create a fileoutputstream to write to a file
    ObjectOutputStream out = new ObjectOutpuStream(fos); //an object outputstream to wrap the fileoutputstream
    out.writeObject(object); //write the object to the fileReading the object is like this
    FileInputStream fis = new FileInputStream("C:\test.file"); // create a fileinputstream to read from a file
    ObjectInputStream in = new ObjectInputStream(fis) // an ObjectinputStream to wrap the FileInputStream
    MyClass object = (MyClass)in.readObject();Exception catch blocks are not included but you do have to catch IOException for reading and writing, plus a few other exceptions for reading. Consult the API for details.

  • Will pages automatically reformat to correct size when using Folio Builder or do I have to make pages iPad format, for example, in InDesign document set up from the start?

    Will pages automatically reformat to correct size when using Folio Builder or do I have to make pages iPad format, for example, in InDesign document set up from the start?

    Moved to DPS forum.
    You need to set up the pages appropriately for the target device. For iPad that's 1024x768.

  • Can I use Serialization if I have to update my class files?

    Hi,
    I'm writing a game right now, and I'm using Serialization to implement saving maps in it. Its saving me a whole bunch of time right now, but I'm worried that if I make even the slightest adjustment to my class files, then old saved maps won't work anywhere.
    Is there anyway to work around this?
    Thank you for any suggestions.
    -Cuppo

    Yes there is; the description of that all can be found in the API docs for the
    Serializable interface; in short: you have to write/read the members of
    your classes yourself by implementing two special methods; that's all.You don't even have to do that. As long as you provide a serialVersionUID
    member and obey the versioning constraints in the Serialization specification
    you don't have to do any extra programing.You're right; your scenario is even simpler; I discovered something funny though:
    my 1.4.2. API docs lack all documentation of the serialVersionUID final member in
    the description of the Serializable interface. It is present in the 1.5. docs, strange ...
    kind regards,
    Jos

  • What is the disadvantages of using Serialization?

    HAi all,
    I want to store objects in a file. I am preferred to use serialization. But I want to know what are the disadvantages of using Serialization over other methods (direct file, Keyed File)?
    Can we store as many records using serialization? Is there any limit? Is there any security related issues?
    Pls help me.
    regards,
    namanc

    I don't think there is disadvantage using serialization compare to direct i/o access to file or any storage, except that you need to design your code such as it is serializable (hence, you may want to use transient keyword, or use externalizable, to be able to store/load the object properly).
    Use serialization if possible, it saves you from inventing new+uncompatible mechanism of doing something in java.

  • Can I resize photos and reduce JPEG file sizes by using Photpshop Elements?

    Can I resize photos and reduce JPEG file sizes by using Photpshop Elements?

    Go to Image>Resize>image size.
    For purpose of web posting, go to File>save for web.

  • Impact of use XMLEncoder to log transactions that use serialization

    Hi,
    i have an application that uses serialization to communicate between client and server, i want to log this transactions in a xml file using XMLEncoder, using the method:
    public static void log(Object transaction) throws Exception {
            XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("log.xml")));
            e.writeObject(transaction);
            e.close();
    }if i put this, how i can check the impact on the system? the XMLEncoder consumes memory to build the XML, then the impact will be only for the garbage collector?
    thanks for any idea.

    A profile would be the best option. NetBeans comes with a free one. There is also OptimizeIt and JProbe. Otherwise you'd have to write a lot of code to get that info.

  • Hello ! how to change document sizes and use our own size (not A3, A2 ...) thank you !

    hello ! how to change document sizes and use our own size (not A3, A2 ...) thank you !

    Experiment. No harm in trying.
    If you are just starting to use Pages you need to make your own assessment given current events whether committing your work to a format and software given this support is a wise move.
    You also need to look at what will produce workable results. Particularly if your large posters are to be commercially printed and contain large bitmap (photographic) images.
    Always work backwards from your output. Ask your printer what they require to do their job.
    Peter

  • Testing Object Equality using Serialization

    Hey everyone! I was wondering if somebody could help me figure out how to compare two objects using serialization.
    I have two objects that I'm trying to compare. Both of these objects extend a common "Model" class that has a method getSerialized() that returns a serialized form of an instance, shown below:
              // Serialize the object to an array
             ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
             ObjectOutputStream oos;
              try {
                   oos = new ObjectOutputStream(baos);
                  oos.writeObject(this);
                  oos.close();
              } catch (IOException e) {
                   e.printStackTrace();
             //Deserialize array into a String array
             return baos.toByteArray();This Model class also has an equals(Model obj) method that allows for the current model object to be compared to a model object that is passed in:
            //Store both models' serialized forms into byte arrays
            byte [] thisClass = this.getSerialized();
            byte [] otherClass = obj.getSerialized();This is where things get a little funny. The byte arrays don't equal - one array is a byte larger than the other. If a byte-by-byte comparison is done, the arrays are equal for the first 15-20% and then not equal for the rest. If I deserialize the byte arrays back into Models and do a toString() on those models, I find that they are equal.
    I have a feeling there's something about the serialization process that I don't fully comprehend. Is there a way to properly implement object comparison using serialization?
    Thanks in advance!

    When you serialize an object, you also serialize the entire tree of references based on that object (except for transient variables). That tree is the complicated business you described there. Serialization stores all the objects in the tree, along with data that explains which objects refer to which other objects. Furthermore if the tree is actually a graph, and there are multiple ways to get to an object, it still only stores each object once. I don't see any reason to believe that all that relationship data would be encoded identically for a pair of trees that you deemed to be equal. And your experiment shows that indeed it isn't.

  • I would like to be able to enter my own page sizes to use in the Book Module, rather than choosing defaults from the drop-down. Is it possible to do this?

    I would like to be able to enter my own page sizes to use in the Book Module (to export to 3rd party printers), rather than choosing defaults from the drop-down. Is it possible to do this?

    No. You are limited to the choices within the Lightroom Book module.

  • Default Heap Size when using jmsc.dll

    We have had some crashes using Weblogic JMS (v 11.x) with one of our C-API apps.
    How can I find out the xmx size being used by Java?

    If you turn on JMSDEBUG (by setting it to 'y') the JMS C API may print out the JVM options. The log messages will go to the files started with "ULOG" in the directory where your app runs from. You can change the location by setting the ULOGPFX variable in your client environment.
    You can override the JVM settings with JMSJVMOPTS variable.
    Hope this helps.

Maybe you are looking for