Java - File size is not incomprehensible


2D-Graphics enthusiast
Edit: Dang, what a typo in the title. It is of course: "File size is incomprehensible".

Hi guys.
I wanna save a class in java using the serialization process.
Now this class has a lot of data.
In fact, inside is a two dimensional array containing "short"'s.
Now this array is huge, it has 2048 * 2048 entries, each of which is a short (16 bit).

But when i saved it i sure didnt expect that file size. The file is 88mb in size on my hard drive.
Thats insane.

So i first checked everything else in the object:
i removed the two dimensional array, just for testing, and saved again. Its 200kb now.
But with the array its 88.something mb.

By my calculation it should be:
((((2048 * 2048) * 2) (since a short is 2 byte) / 1024) / 1024) == 8mb
So how comes its over ten times that amount?
Please, i really need your help here.


Without Intelligence
I could not replicate your problem. Using this code:
produced in a test.dat file of size 8.1 MB:
/home/felix $ javac && java a && du -h test.dat                             
8.1M	test.dat
Then again, I haven't used Java in a while so I might've made some mistakes in there. Anyhow, maybe you could try to provide a minimal working example of the problem?


2D-Graphics enthusiast
Thanks for your reply.
Upon further investigation i think i found the cause.

The array which i was using was not exactly a two dimensional array but a 4 dimensional array with the third and fourth dimension set to a size of 1.
So it was 2048 * 2048 * 1 * 1 as a 4 dimensional array.
As it seems is the overhead produced by this construct quite large.
Changing the array to a two dimensional 2048 * 2048 array resulted in a file size of 8.3mb.
Having 2048 * 2048 * 1 * 1 was 88.6mb though.

I can understand that there is a certain overhead, but i cant explain why it would be 1100%.

I will probably change the array to be two dimensional and emulate the third and fourth dimension by extending either the width or height as required.


Without Intelligence
Ah, that does indeed explain it. Java's serialization stores more information about the objects than just their contents. For example for arrays it saves at least the object's type (1 byte), size (integer, 4 bytes), and class description (handle, integer, 4 bytes). That means the size it takes is
2048*2048*(1+4+4+1+4+4+2)+2048*(1+4+4) bytes = 83 904 512 bytes
so about 83.8 MB. Storing also the type of the value (short) using 1 byte brings it up to about 88 MB. If you want to learn more about the way the data is saved, I'd recommend checking out the serialization protocol (especially the grammar is useful if you understand it).
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    It happens in corporations. They just absorb the companies they buy and it is not about the love of making awesome games it is how much money can we make with the least amount of cost.
  • The Helper The Helper:
    Blizzard is watered down now hopefully they can pull it together
  • The Helper The Helper:
    they got a server engineer job opening :)
  • Ghan Ghan:
    I really do not want to move to California otherwise I might consider it.
  • The Helper The Helper:
    yeah California is not anywhere you really want to live
  • The Helper The Helper:
    That is why I did not take the job Blizzard offered me back in the day, there is no way I could have moved my family there on what they were offering, not even close and that was like 20 years ago
  • The Helper The Helper:
    yeah they wanted me on the tech support team when they did not get me they got one of the next MVPs in Dinobot
  • The Helper The Helper:
    Dinobot was one of the youngest of the MVPs tkron probably could have worked for Blizzard but he had a good job in Chicago doing business programming already
  • The Helper The Helper:
    Dinobot probably still works for Blizzard would love to reconnect with that guy
  • The Helper The Helper:
    I wonder what ever happened to Wargasm?
  • The Helper The Helper:
    This new version of Xenforo really is awesome
  • Ghan Ghan:
    Wargasm is still around. He works for the domain registrar where is kept.

    Members online

    No members online now.


    Hive Workshop