StringUtils { public static int estimateMemoryUsage(String s) { long size = s == null ? 0 : 48 + (long)s.length() * 2; if (size > Integer.MAX_VALUE) { LOG.debug("Estimated memory footprint larger than Integer.MAX_VALUE: {}.", size); size = Integer.MAX_VALUE; } return (int) size; } private  StringUtils(); @NotNull static String convertBytesToHex(@NotNull byte[] value); @NotNull static byte[] convertHexToBytes(@NotNull String s); static int estimateMemoryUsage(String s);  }
@Test public void testEstimateMemoryUsage() { final Map<String, Integer> testStrings = Maps.newHashMap(); testStrings.put(null, 0); testStrings.put("", 48); testStrings.put("a", 50); testStrings.put("short string", 72); testStrings.put("a much longer string than the one named 'short string'", 156); for (final Map.Entry<String, Integer> e : testStrings.entrySet()) { assertEquals(e.getValue().intValue(), StringUtils.estimateMemoryUsage(e.getKey())); } }