2

I have a rest service where one can send string field in body like that:

{
    "field1": "b14",
    "fieldWithContent": "there may be something big, it is xml",
    "field3": 0
}

Don't ask me why this fieldWithContent has xml inside :) I must validate whether the fieldWithContent is bigger than 1MB. How could I do that in Java ?

The rest endpoint is exposed through Camel but this has propably some second importance.

michealAtmi
  • 1,012
  • 2
  • 14
  • 36

1 Answers1

0

A char occupies 2 bytes of memory, you can use the following formula:

int size = fieldWithContent.length * 2

And if this is size <= 1024*1024 then your string occupies less than 1MB in memory.

If you want more info about the data and how much they occupy you can check the follow link https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
  • Bear in mind that this technique only approximates the size in Java's memory, which equals the size of the string itself plus overhead. Also, idioms in the program such as copying the string could double the footprint, or more. Whether this is relevant depends on the actual problem you're trying to solve. How was the 1 MB (MiB? The two differ) limit determined? – Lew Bloch Feb 11 '19 at 20:29