Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating list-like structures in a loop leaks memory indefinitely #3793

Closed
philip-wernersbach opened this issue Jan 29, 2016 · 1 comment
Closed

Comments

@philip-wernersbach
Copy link
Contributor

I have code in an actual program that does the following in a loop:

  1. Receive data via an asynchronous socket
  2. Parse the data
  3. Send the parse results back to the client

(2) creates some linked lists which (3) then converts to the network protocol. After (3), the lists from (2) are not needed, however they are kept in memory for some reason, which means that this program leaks memory indefinitely.

Test Case:

import os
import math
import lists
import strutils

proc mkleak() =
    let numberOfLists = random(10)

    for i in countUp(1, numberOfLists):
        var leakList = initDoublyLinkedList[string]()
        let numberOfLeaks = random(100)

        for j in countUp(1, numberOfLeaks):
            let leakSize = random(4096)

            let leaked = newString(leakSize)
            leakList.append(leaked)

proc mkManyLeaks() =
    while true:
        mkleak()
        echo GC_getStatistics()

        # Force a full collection. This should free all of the
        # lists and bring the memory usage down to a few MB's.
        GC_fullCollect()

        # Surprise! It didn't.
        echo GC_getStatistics()

        sleep(1000)

mkManyLeaks()
@zielmicha
Copy link
Contributor

For anyone coming here depressed that you can no longer write long running programs in Nim - this issue is absent in all GCs except for the default (refc). Just compile your program with one of:

--gc:boehm
--gc:v2
--gc:markAndSweep

(--gc:boehm is probably the best choice)

Araq added a commit that referenced this issue May 16, 2016
@Araq Araq closed this as completed in 7e134b5 May 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants