-1

According to implicit sharing concept in following example we must experience low memory usage in Windows Task Manager.

We create 1000000 object in a for loop from Employee class and it might share its internal data (EmployeeData class) between created objects.

for (int i = 0; i < 1000000; ++i) {

    Employee *e1 = new Employee(); // calls default constructor
    Employee *e2 = e1; 
    Employee *e3 = e2;

    //e1->setName("Hans Holbein"); // ***
}

***: if uncomment mentioned line according to Qt documentation QSharedDataPointer it will be detaches from shared data and creates its own copy of EmployeeData.

EmployeeData and Employee class:

class EmployeeData : public QSharedData
{
  public:
    EmployeeData() : id(-1) { }
    EmployeeData(const EmployeeData &other)
        : QSharedData(other), id(other.id), name(other.name) { }
    ~EmployeeData() { }

    int id;
    QString name;
};

class Employee
{
  public:
    Employee() { d = new EmployeeData; }
    Employee(int id, const QString &name) {
        d = new EmployeeData;
        setId(id);
        setName(name);
    }
    Employee(const Employee &other)
          : d (other.d)
    {
    }
    void setId(int id) { d->id = id; }
    void setName(const QString &name) { d->name = name; }

    int id() const { return d->id; }
    QString name() const { return d->name; }

  private:
    QSharedDataPointer<EmployeeData> d;
};

Platform: Windows 10

Qt Version: 5.7

Compiler: MSVC 2015 32bit

The result is (from Task Manager):

  • Release: 40M RAM usage
  • Debug: 189M RAM usage

Question:

Based on provided loop assume EmployeeData size as 12 bytes, it must create one instance of EmployeeData and share it with another 999,999 object instances, and so memory usage must be reduced at least under 3M, is it Ok?

So if we uncomment the following line it must create 1000,000 unique instance of EmployeeData, so memory used by instances goes up, is that Ok?

//e1->setName("Hans Holbein");
Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
  • There's only one Employee object (copying plain pointers is just that, copying plain pointers) created and then modified and the leaked, per loop iteration. Your code does not take advantage of shared data in any way. Replace Employee with QString, and think how it would work then. You need to use the Employee variables as values, not as pointers. – hyde Sep 15 '16 at 16:30
  • @hyde I replaced `Employee` with `QString`, so in release mode I have `16M` RAM usage, if we assume QString shared data about `1KB`, so might have another extra non-shared data space, my question all about shared-data, QString may have many data members that not shared in each instance, but in `EmployeeData` we have two data members, so only one instance must be shared among another objects. – Reza Ebrahimi Sep 15 '16 at 17:26
  • But that's just it, you don't have multiple instances with shared data anywhere. To have that, you'd need something like: Employee e2 =*e1; – hyde Sep 15 '16 at 18:05
  • Also, just why do you use new and pointers here? One point of shared data objects is, you don't need to. Just use them like any values. – hyde Sep 15 '16 at 18:07
  • Ok, thanks, I got it, so the problem is we have a class that its data was same between many objects (duplicate data), what is the solution to share its data among another instances, with a `=` operator or a `copy constructor`? I think this could handle inside the object, not the outside to use such operators! – Reza Ebrahimi Sep 15 '16 at 18:11
  • Before understanding implicit sharing, you need to understand basic pointer usage. Your code above is totally wrong, it just copies pointer address and leaks memory – galinette Sep 15 '16 at 19:41
  • http://doc.qt.io/qt-5/implicit-sharing.html – hyde Sep 15 '16 at 20:20
  • @galinette I know above code leaks in memory, so thats not the problem, please read carefully the question and comment about it, its just about sharing a data structure between many objects, so its not a matter right now the objects on the heap leaks or not, whats the matter is how one object share its own data structure with another objects! its totally clear for you? – Reza Ebrahimi Sep 16 '16 at 12:51
  • @hyde Thanks, but I read all Qt documentation about sharing data both implicitly or explicitly, so I found my answer. – Reza Ebrahimi Sep 16 '16 at 12:52

1 Answers1

2

There is no implicit sharing going on here, you just have 2 pointers to the same object.

The implicit sharing only works when the copy constructor or assignment operator of the object are used.

Ignoring the huge memory leak of the new in the for loop, doing this will utilize implicit sharing

Employee *e2 = new Employee(e1); // copy constructor, implicit sharing
frogatto
  • 28,539
  • 11
  • 83
  • 129
David
  • 1,510
  • 14
  • 20