Saving the most recent objects to avoid memory overflow

The goal here is to only save the most recent Objects created if maxMemoryOfObjects is exceeded. But the way I'm doing it ironically adds more memory to the Object class (its ID data member). Is there a better way to do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <list>
#include <algorithm>
#include <fstream>
#include <map>

int totalMemoryOfObjects = 0;
const int maxMemoryOfObjects = 200;
int lastObjectID = 0;

struct Object {
	int ID;  // This increases sizeof(Object) even more.
	Object() : ID(++lastObjectID) {}
};

struct Location {
	std::list<Object*> objectsLyingAround;
	Location() {
		for (int i = 0; i < 12; i++) {
			Object* object = new Object;
			objectsLyingAround.push_back(object);
			totalMemoryOfObjects += sizeof(*object);
		}
	}
};

struct Person {
	std::list<Object*> objectsSeen;
	void visitLocation (const Location* location) {
		std::copy (location->objectsLyingAround.begin(), location->objectsLyingAround.end(), std::back_inserter(objectsSeen));
	}
};

std::list<Location*> itinerary;

void saveObjects() {
//	std::ofstream ("Objects.txt");
	std::map<int, Object*/*, std::greater<int>*/> objectMap;
	for (Location* x : itinerary)
		for (Object* o : x->objectsLyingAround)
			objectMap.emplace (o->ID, o);
	std::cout << "Objects being saved (from oldest to newest) IDEALLY are:\n";
	for (const auto& x : objectMap)
		std::cout << x.first << ' ';
	if (totalMemoryOfObjects > maxMemoryOfObjects) {
		std::cout << "\nAlert!  totalMemoryOfObjects > maxMemoryOfObjects!  Will save only the newest objects.\n";
		auto it = objectMap.begin();
		while (totalMemoryOfObjects > maxMemoryOfObjects) {
			totalMemoryOfObjects -= sizeof(*it->second);
			std::cout << "totalMemoryOfObjects reduced to " << totalMemoryOfObjects << std::endl;
			it = objectMap.erase(it);
		}
		std::cout << "Objects being saved (from oldest to newest) NOW are:\n";
		for (const auto& x : objectMap)
			std::cout << x.first << ' ';		
	}
}

int main() {
	Person sam;		
	for (int i = 0; i < 5; i++) {
		Location* nextLocation = new Location;
		itinerary.push_back (nextLocation);
		sam.visitLocation (nextLocation);
	}
	saveObjects();
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Objects being saved (from oldest to newest) IDEALLY are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3
2 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

Alert!  totalMemoryOfObjects > maxMemoryOfObjects!  Will save only the newest objects.
totalMemoryOfObjects reduced to 236
totalMemoryOfObjects reduced to 232
totalMemoryOfObjects reduced to 228
totalMemoryOfObjects reduced to 224
totalMemoryOfObjects reduced to 220
totalMemoryOfObjects reduced to 216
totalMemoryOfObjects reduced to 212
totalMemoryOfObjects reduced to 208
totalMemoryOfObjects reduced to 204
totalMemoryOfObjects reduced to 200
Objects being saved (from oldest to newest) NOW are:
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 3
9 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
Last edited on
Why do you want to save the most recent objects? If they were just created, would it not make sense that they would need to be used soon? It would make more sense to save the older ones first.
We are assuming there that the old objects were created long ago from previous saves. Here I'm showing them recently created for simplicity. I want the older objects removed first because after however many gigabytes of storage (which will probably never be reached, but just in case), they are probably forgotten already anyways.
I think there is a difference between our uses of the word "save" - to me, "save" means "write to disk and drop from memory"
Topic archived. No new replies allowed.