-3

How I can create Java Object with fixed size 50 MB?

I need this Object for database testing purposes to measure tablespace name when I insert large object.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

7

I think that the easiest way would be to create an object with an array in it and put 50MB worth of data in the array. If it needs to be exactly 50MB, you'll need to take into account the size of the object's overhead. That would be 12B without compressed pointers and 8B with compressed pointers. The size of your array would depend on your data type. If you use doubles, they will be 64 bits which is 8 bytes. This won't fit evenly with the uncompressed pointers so you may want to just use bytes.

class fiftyMB{
    public byte data[49999988];
}

As mentioned in the comments, I'm assuming 1MB is one million bytes. If you use the traditional value of 1kB = 1024B and 1MB = 1024kB then what you need is:

class fiftyMB{
    public byte data[52428788];
}
Josh A
  • 98
  • 6
  • Depending your [MB definition](https://en.wikipedia.org/wiki/Megabyte#Definitions). – shmosel Jul 01 '16 at 20:21
  • @JoshA we need to define MB :D – Tibrogargan Jul 01 '16 at 20:22
  • I decided to go for the base 10 rather than the base 2 version but yes, the numbers could use some work. I've updated it to reflect both version and to also fix my mistake of putting in doubles instead of bytes. – Josh A Jul 01 '16 at 20:26
  • Where did you get 12B vs 8B? See [here](http://stackoverflow.com/questions/17814293/what-is-the-size-of-reference-variable-in-java/17814352#17814352) for Claiming 8B vs 4B. – jan.supol Jul 01 '16 at 20:29
  • There is the reference plus the length of the array. See [here](http://stackoverflow.com/questions/726404/what-is-the-memory-overhead-of-an-object-in-java) – Josh A Jul 01 '16 at 20:30