This is an automated email from the ASF dual-hosted git repository.

alexpl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 8a6c0d4  IGNITE-11911 Fixed GridToStringBuilder memory overconsumption 
- Fixes #6603.
8a6c0d4 is described below

commit 8a6c0d4a59f60e595801c2d20e764bb620335058
Author: Aleksey Plekhanov <plehanov.a...@gmail.com>
AuthorDate: Thu Jun 13 16:21:05 2019 +0300

    IGNITE-11911 Fixed GridToStringBuilder memory overconsumption - Fixes #6603.
---
 .../internal/util/tostring/SBLimitedLength.java    |  2 +-
 .../util/tostring/GridToStringBuilderSelfTest.java | 33 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/SBLimitedLength.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/SBLimitedLength.java
index 640555a..e18e0ed 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/SBLimitedLength.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/SBLimitedLength.java
@@ -290,7 +290,7 @@ public class SBLimitedLength extends GridStringBuilder {
             return super.toString();
         else {
             int tailLen = tail.length();
-            StringBuilder res = new StringBuilder(impl().capacity() + tailLen 
+ 100);
+            StringBuilder res = new StringBuilder(impl().length() + tailLen + 
100);
 
             res.append(impl());
 
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/util/tostring/GridToStringBuilderSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/util/tostring/GridToStringBuilderSelfTest.java
index 5e220fc..197d346 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/internal/util/tostring/GridToStringBuilderSelfTest.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/util/tostring/GridToStringBuilderSelfTest.java
@@ -17,6 +17,8 @@
 
 package org.apache.ignite.internal.util.tostring;
 
+import com.sun.management.ThreadMXBean;
+import java.lang.management.ManagementFactory;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -554,6 +556,37 @@ public class GridToStringBuilderSelfTest extends 
GridCommonAbstractTest {
     }
 
     /**
+     * Test GridToStringBuilder memory consumption.
+     */
+    @Test
+    public void testMemoryConsumption() {
+        int objCnt = 100;
+
+        ThreadMXBean bean = (ThreadMXBean)ManagementFactory.getThreadMXBean();
+
+        TestClass2 obj = new TestClass2(new String(new char[1_000_000]));
+
+        List<TestClass2> arr = new ArrayList<>(objCnt);
+
+        for (int i = 1; i <= objCnt; i++)
+            arr.add(new TestClass2(new String(new char[i])));
+
+        GridToStringBuilder.toString(TestClass2.class, obj);
+
+        long allocated0 = 
bean.getThreadAllocatedBytes(Thread.currentThread().getId());
+
+        for (TestClass2 item : arr)
+            GridToStringBuilder.toString(TestClass2.class, item);
+
+        long allocated1 = 
bean.getThreadAllocatedBytes(Thread.currentThread().getId());
+
+        log.info("Memory allocated by GridToStringBuilder for " + objCnt + " 
objects: " + (allocated1 - allocated0));
+
+        assertTrue("Too much memory allocated by GridToStringBuilder: " + 
(allocated1 - allocated0),
+            allocated1 - allocated0 < 1_000_000);
+    }
+
+    /**
      * @param exp Expected.
      * @param w Wrapper.
      */

Reply via email to