Friday 15 February 2013

Optimizing java heap usage by String using StringBuffer , StringBuilder , String.intern() -



Optimizing java heap usage by String using StringBuffer , StringBuilder , String.intern() -

i monitoring performance , cpu of big java application , using visualvm. when @ memory profile see maximum heap (about 50%) beingness used char arrays.

following screenshot of memory profile:

in memory profile @ given time see 9000 char[] objects.

the application accepts big file input. file has 80 lines each line consisting of 15-20 delimited config options. application parses file , stores these lines in arraylist of strings. parses these string individual config options each server.

the application logs each event console.

java implementation of strings uses char[] internally along reference array , 3 integer.

from different posts on net seems stringbuffer , stringbuilder , string.intern() more memory efficient info types.

how compare java.lang.string ? has benchmarked them ? if application uses multithreading (which does)are safe alternative ?

what is have 1 or more string pools. a) not create new strings if have 1 in pool , b) cut down retained memory size, factor of 3-5. can write simple string interner suggest consider how info read in first determine optimal solution. matters can create matters worse if don't have efficient solution.

as ejp points out processing line @ time more efficient, parsing each line read it. i.e. int or double takes far less space same string (unless have high rate of duplication)

here illustration of stringinterner takes stringbuilder avoid creating objects needlessly. first populate recycled stringbuilder text , if string matching text in interner, string returned (or tostring() of stringbuilder is.) benefit create objects (and no more needed) when see new string (or @ to the lowest degree 1 not in array) can 80% 99% nail rate , cut down memory consumption (and garbage) dramatically when loading many strings of data.

public class stringinterner { @notnull private final string[] interner; private final int mask; public stringinterner(int capacity) { int n = nextpower2(capacity, 128); interner = new string[n]; mask = n - 1; } @override @notnull public string intern(@notnull charsequence cs) { long hash = 0; (int = 0; < cs.length(); i++) hash = 57 * hash + cs.charat(i); int h = hash(hash) & mask; string s = interner[h]; if (isequal(s, cs)) homecoming s; string s2 = cs.tostring(); homecoming interner[h] = s2; } static boolean isequal(@nullable charsequence s, @notnull charsequence cs) { if (s == null) homecoming false; if (s.length() != cs.length()) homecoming false; (int = 0; < cs.length(); i++) if (s.charat(i) != cs.charat(i)) homecoming false; homecoming true; } static int nextpower2(int n, int min) { if (n < min) homecoming min; if ((n & (n - 1)) == 0) homecoming n; int = min; while (i < n) { *= 2; if (i <= 0) homecoming 1 << 30; } homecoming i; } static int hash(long n) { n ^= (n >> 43) ^ (n >> 21); n ^= (n >> 15) ^ (n >> 7); homecoming (int) n; } }

this class interesting in not thread safe in tradition sense, work correctly when used concurrently, in fact might work more efficiently when multiple threads have different views of contents of array.

java string optimization heap-memory visualvm

No comments:

Post a Comment