09 November, 2014

How much memory is allocated to the String object in Java

Size in bytes
String() object
Object header 16
Hash field 4
Reference to char array 4
Char[] array
Object header 16
Array length 4
Array size N*2

String allocates in memory at least (24+20+N*2) bytes, where N - number of characters in the string. If N=10 then string size = 64 bytes.

One million of these strings = 64 000 000 bytes. But also we need array or collection to store these strings.
This requires additional memory.
ArrayList<String>(1 000 000) ~= 4 000 000 bytes (4 bytes per reference to string object).

So, total size will be 68 000 000 bytes.

Gist code: Output:
3,355,792 Initial
1,217,624 After GC
218,921,768 Created list of strings
76,489,936 After GC
68,000,000 Expected size in memory
But if you have many similar strings, then using of String.intern() greatly reduces amount of allocated memory. Output:
3,355,792 Initial
1,217,792 After GC
106,750,232 Created list of strings
13,127,480 After GC
68,000,000 Expected size in memory
10x less memory! 6.8 megabytes instead of 68

03 November, 2014

The best resolutions

List of multiple resolutions in the most popular proportions:

Base width 1/2 2/3 3/4 4/5 5/6 9/16 10/16
240 120 160 180 192 200 135 150
480 240 320 360 384 400 270 300
720 360 480 540 576 600 405 450
960 480 640 720 768 800 540 600
1200 600 800 900 960 1000 675 750
1440 720 960 1080 1152 1200 810 900
1680 840 1120 1260 1344 1400 945 1050
1920 960 1280 1440 1536 1600 1080 1200
2160 1080 1440 1620 1728 1800 1215 1350
2400 1200 1600 1800 1920 2000 1350 1500
2640 1320 1760 1980 2112 2200 1485 1650
2880 1440 1920 2160 2304 2400 1620 1800
3120 1560 2080 2340 2496 2600 1755 1950
3360 1680 2240 2520 2688 2800 1890 2100
3600 1800 2400 2700 2880 3000 2025 2250
3840 1920 2560 2880 3072 3200 2160 2400

01 November, 2014

Read SAS file in Java

Some companies (banks, medical and pharmaceutical companies) store their data in the SAS (.sas7bdat) format. This is old, legacy, but still actual format for tabular data. And this format is proprietary and fiddly.

But what if you have to work with the data from the sas-file? For me, solution is pretty simple - convert the data from the sas-file to some convenient and popular format. For example, to the MongoDB. And then access this data via nice query-like API.

In my new mini-project sas2mongo I show how to convert data from the sas-file into the MongoDB in Java.

26 October, 2014

Sas2Mongo

I've just released my new micro-project: sas2mongo

This is a command line tool for copying data from sas-files to mongo-db.

12 October, 2014

Me on GitHub

I'm moving my projects to GitHub.

Most of them are open and free for all.

Me on GitHub
My GitHub pages

Already moved projects:
  • MusicBox (sources and demo) - digital musicbox
  • Harmonica (sources and demo) - set of musical instruments
  • Xameleon (demo) - webcam application
  • XenoHarmonica (demo) - MIDI bayan (russian accordion) emulator
  • Pkmx (sources and demo) - JavaScript library for optimal blocks layout
  • WebTools (sources and demo) - collection of online web tools