Memory problems in .NET.

I have a C # service that listens for a queue for XML messages, receives them, processes them using XSLT, and writes them to the database. It processes about 60 thousand messages per day about 1 MB each. Downtime memory goes up to 100 MB, which is really good. However, recently I started to process messages with a size of 12 MB. It blows memory and even when it does not work, it has a memory of about 500 MB. Any suggestions why this could be a problem? I don’t think there is a memory leak, because it would pop up after processing so much (60 KB messages 1 MB).

+3
source share
7 answers

It looks great. Why do you think this is a problem?

, , , .

, :

, , - , XSLT. XML. : .

+7

SIR! . . .NET . . , . , ( OOM, ).

+5

? , " ". , .. .

, 500

( , .NET) . , , .

12

XML- (, XmlDocument), ( ). (XDocument XPathDocument - ) , , XML XmlReader , , .

+3

, - , . CLR O/S, . , , , , . ( GC.Collect(2), , , .)

GC, : , .NET .

, - . , , , , (LOH). , , , .

, , - : , LOH.

+2
+1

, , ; profile .

+1

. , , , , GC.
, ( ), IDisposable . , .

- :

public class Publisher
    : IDisposable
{
    private EventHandler _somethingHappened;
    public event EventHandler SomethingHappened
    {
        add { _somethingHappened += value; }
        remove { _somethingHappened -= value; }
    }
    protected void OnSomethingHappened(object sender, EventArgs e)
    {
        if (_somethingHappened != null)
            _somethingHappened(sender, e);
    }

    public void Dispose()
    {
        _somethingHappened = null;
    }
}

( , ), :

public class Subscriber
    : IDisposable
{ 
    private Publisher _publisher;
    public Publisher Publisher
    {
        get { return _publisher; }
        set {
            // Detach from the old reference
            DetachEvents(_publisher);
            _publisher = value;
            // Attach to the new
            AttachEvents(_publisher);
        }
    }

    private void DetachEvents(Publisher publisher)
    {
        if (publisher != null)
        {
            publisher.SomethingHappened -= new EventHandler(publisher_SomethingHappened);
        }
    }
    private void AttachEvents(Publisher publisher)
    {
        if (publisher != null)
        {
            publisher.SomethingHappened += new EventHandler(publisher_SomethingHappened);
        }
    }

    void publisher_SomethingHappened(object sender, EventArgs e)
    {
        // DO STUFF
    }

    public void Dispose()
    {
        // Detach from reference
        DetachEvents(Publisher);
    }
}
0

Source: https://habr.com/ru/post/1527973/


All Articles