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

std::vector<boost::dynamic_bitset<>>unable to free memory #51

Closed
robot-kimbo opened this issue Nov 20, 2019 · 5 comments
Closed

std::vector<boost::dynamic_bitset<>>unable to free memory #51

robot-kimbo opened this issue Nov 20, 2019 · 5 comments

Comments

@robot-kimbo
Copy link

Hi,
I am using boost 1.66.0 version, gcc/g++ -7, and I found when using std::vector<boost::dynamic_bitset<>>, it is unable to free memory when destruction, even using function swap(), resize(), reset(). it doesn't work.

The test program is show below
int main()
{
cout << "[ memory start] " << getMemoryUsage();
{
std::vector<boost::dynamic_bitset<>> m_nodes;
m_nodes.resize(1000000 + 1);
uint64_t descriptor[4] = {1, 2, 3, 4};
for(size_t i = 0; i < m_nodes.size(); i++)
{
m_nodes[i] = boost::dynamic_bitset<>(descriptor, descriptor + 4);
}
}
cout << "[ memory end] " << getMemoryUsage() << endl;
}

result is:
[ memory start ] 13
[ memory end ] 59

As the result show:
The memory of boost::dynamic_bitset can not release, so it is the problem.
Could you please confirm it, and tell me how to solve it?

@mclow mclow transferred this issue from boostorg/boost Nov 21, 2019
@mclow
Copy link
Contributor

mclow commented Nov 28, 2019

I'm not seeing a memory leak here. What is getMemoryUsage, and what system are you running this on?
My test program (attached) reports that every allocation is paired with a deallocation. (Boost trunk, on Mac OS 10.14)

#include <vector>
#include <iostream>
#include <cstdio>

#include <boost/dynamic_bitset.hpp>

void* operator new (std::size_t count)
{
    void *ptr = malloc(count);
    ::printf ("Allocating %ld bytes at %p\n", count, ptr);
    
    return ptr;
}

void* operator new[]( std::size_t count)
{
    void *ptr = malloc(count);
    ::printf ("Allocating %ld bytes[] at %p\n", count, ptr);
    
    return ptr;
}

void operator delete(void *ptr) throw()
{
    ::printf ("deleting %p\n", ptr);
	free(ptr);
}

void operator delete[](void *ptr) throw()
{
    ::printf ("deleting[] %p\n", ptr);
    free(ptr);
}


int main() {
	{
	std::vector<boost::dynamic_bitset<>> m_nodes;
// 	m_nodes.resize(1000000 + 1);
	m_nodes.resize(10 + 1);
	uint64_t descriptor[4] = {1, 2, 3, 4};
	for(size_t i = 0; i < m_nodes.size(); i++)
		{
		m_nodes[i] = boost::dynamic_bitset<>(descriptor, descriptor + 4);
		}
	::printf("Exiting scope\n");
	}
}

@robot-kimbo
Copy link
Author

Hi, thanks for reply.

  1. system: ubuntu 18.04, boost 1.66.0 release version, gcc/g++ -7
  2. getMemoryUsage() is code of get the program memory, it like command "busybox top"
    here is the linux code of it:
    unsigned long getMemoryUsage()
    {
    unsigned long MEM = 0;
    FILE *f = ::fopen ("/proc/self/stat", "r");
    if (!f) return 0;
    if (!::fscanf(f,
    "%*d %*s %*c %*d %*d %*d %*d %*d%*u %*u %*u %*u %*u %*u %*u%*d %*d %*d %*d %*d %*d %*u %lu", &MEM))
    {
    // Error parsing:
    MEM=0;
    }
    ::fclose (f);
    return MEM/1024/1024;
    }
  3. For my test program, you can see after destruct of "std::vector<boost::dynamic_bitset<>>", the memory can not restore "13M", that why I mean "std::vector<boost::dynamic_bitset<>>" has memory leak.

@robot-kimbo
Copy link
Author

Hi, I also test std::bitset, the memory of this has not meomory leak, here is the test program

int main()
{
cout << "[ memory start ] " << getMemoryUsage() << std::endl;
{

    std::vector<std::bitset <256> > m_nodes;
    m_nodes.resize(1000000 + 1);
    uint64_t descriptor[4] = {1, 2, 3, 4};
    std::bitset <256> tmp(ULONG_MAX);
    for(size_t i = 0; i < m_nodes.size(); i++)
    {
        m_nodes[i] = tmp;
    }
}
cout << "[ memory end ] "  << getMemoryUsage() << endl;

}

The result is:
[ memory start ] 13
[ memory end ] 13

@swatanabe
Copy link
Contributor

swatanabe commented Nov 30, 2019 via email

@robot-kimbo
Copy link
Author

Ok, got it, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants