Mono: Memory leak on all versions mono and .net framework on Windows

Created on 10 Jun 2019  路  1Comment  路  Source: mono/mono

This issue is copy of #14514 which was closed by https://github.com/marek-safar.
So I guess that nobody want to understand problem.

Steps to Reproduce

  1. Create console project 4 framework
  2. Paste my code below
  3. Paste my text file test.log near application.exe (attached at the end)
  4. Run htop on terminal emulator above all windows
  5. Run compiled application
  6. Look at htop - using memory

Code here

class Program
    {
        static TimeSpan _from, _to;
        static bool _isBegin;

        static void Main(string[] args)
        {
            _from = Convert.ToDateTime("2019-05-13 09:00").TimeOfDay;
            _to = Convert.ToDateTime("2019-05-13 11:00").TimeOfDay;
            string text = File.ReadAllText("test.log", Encoding.GetEncoding(1251));
            StringBuilder data = new StringBuilder(100000);
            bool end = false;
            foreach(string line in text.Split('\n'))
            {
                int result = NeedAppendString(line);
                switch (result)
                {
                    case 0:
                        break;
                    case 1:
                        data.Append(line + '\n');
                        break;
                    case 2:
                        end = true;
                        break;
                }
                if (end)
                {
                    break;
                }
            }
            Console.WriteLine(data.ToString());
        }

        static int NeedAppendString(string str)
        {
            try
            {
                bool flag;
                if (str != null && (!str.Contains("[") || !str.Contains(']')) && _isBegin) return 1;
                StringBuilder builder = new StringBuilder();
                char[] array = str.ToCharArray();

                for (byte i = 1; i<array.Length && array[i] != ']'; i++)
                {
                    if (i == array.Length-1)
                    {
                        return 1;
                    }
                    builder.Append(array[i]);
                }
                TimeSpan time;
                string timeS = builder.ToString();
                if (timeS.Length != 8)
                {
                    return _isBegin ? 1 : 0;
                }
                flag = TimeSpan.TryParse(timeS, out time);
                if (flag)
                {
                    if ((time >= _from) && (time <= _to))
                    {
                        _isBegin = true;
                        return 1;
                    }
                    if (_isBegin)
                    {
                        Console.WriteLine("Logs end");
                        return 2;
                    }
                }
                if (_isBegin) return 1;
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return 0;
            }
        }
    }

Current Behavior

Memory Leak due to code

 builder.Append(array[i]);

Expected Behavior

Good work without memory leak

On which platforms did you notice this

[?] macOS
[] Linux
[
] Windows

Version Used:

Mono JIT compiler version 5.20.1.19 (tarball Thu Apr 11 09:02:17 UTC 2019)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: amd64
Disabled: none
Misc: softdebug
Interpreter: yes
LLVM: yes(600)
Suspend: hybrid
GC: sgen (concurrent by default)

But this bug is on all mono version and even microsoft .net framework
test.log

question

Most helpful comment

Your code at some point simply ends up with an endless loop due tobyte type for the i:

            for (byte i = 1; i < array.Length && array[i] != ']'; i++)
            {
                if (i == array.Length)
                {
                    return 1;
                }
                builder.Append(array[i]);
            }

>All comments

Your code at some point simply ends up with an endless loop due tobyte type for the i:

            for (byte i = 1; i < array.Length && array[i] != ']'; i++)
            {
                if (i == array.Length)
                {
                    return 1;
                }
                builder.Append(array[i]);
            }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

smr888 picture smr888  路  38Comments

dbelcher picture dbelcher  路  35Comments

bhaskar-infotmt picture bhaskar-infotmt  路  31Comments

kumpera picture kumpera  路  109Comments

grendello picture grendello  路  33Comments