objects in memory

Jan 1, 2011 at 9:58pm
Hi all,

does anybody know anything about exactly how and instance of a class is stored in memory (RAM)?

I ask because I have a project to do for my degree and I am going to be saving the state of an object instance as it changes, so I can perform operations like 'undo'. I was thinking of pushing each instance of the object onto a stack but I am worried that my memory would soon fill up because maybe the functions/methods are also saved/duplicated in memory.

So really my question is: for each instance of a class, are the functions/methods/operations duplicated in memory, and NOT just the attributes/states?

By the way is there some standard way to acheive what I want (saving the history of an object's state)? I just thought since I made my own stack it would be nice to use here, and it seems appropriate.

Thanks for listening to my query,

James
Jan 1, 2011 at 10:15pm
How large is your class?

Must you save all of the data in the instance (or just small parts of it)?

Do you understand vectors?

How many steps do you want to save?
Jan 1, 2011 at 10:25pm
hi,

well I have 4 classes that form a system together (it's a back propagation neural network) about 600 lines or so.

I should save all the data really because (if you know about neural netowkrs) I want to be able to train the network, (the state changes on each iteration of the training) and then if I wish (in the application) i could revert back to another state and retrain using different training parameters.

I would like to save 500+ steps, though i suppose i dont really need that many.
Jan 1, 2011 at 10:30pm
actually I just thought, do you think I should just have every attribute in the classes be represented by a stack or some similar data structure? this would mean some fairly large modification of my work but it sounds much more efficient.
Jan 1, 2011 at 10:47pm
Well if you are saving vast amounts of learning data in your neural network is there a particular reason you want to store it in memory? I mean sure it would be quicker if you had to backtrack a ton, but why not store like 50 steps in a cyclical memory storage unit and store archived steps in your hard disk? That way you could go back to any state and you wouldn't be hogging all of the memory.

Neural networks are awfully cool, good on ya =)

Jan 1, 2011 at 10:58pm
"Neural networks are awfully cool, good on ya =)" - aren't they just! :D

I've never built a proper app before so i've never encountered an 'undo' system, however your suggestions sounds about right. Kind of sounds obvious when someone else says it...lol

thanks for your help.

just for interest the reason i want to backtrack is if my network encounters a 'local minima' the user should be able to backtrack and modify momentum, steepness, and learning rate terms that for part of the bp algorithm to possibly jump over the minima. So i suppose you're right, 50 steps or so would do it.

once again thanks.
Jan 1, 2011 at 11:04pm
Do you want to take the snapshot of the objects externally or you are prepared to alter the classes to support your "undo" infrastructure internally? Just something to consider.

I recently read something in the standard that may be relevant to you. The following code snippets are taken from there:
1
2
3
4
5
6
7
#define N sizeof(T)
char buf[N];
T obj; // obj initialized to its original value
std::memcpy(buf, &obj, N); // between these two calls to std::memcpy,
                           // obj might be modified
std::memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar type
                           // holds its original value 
, meaning that you can make snapshots of objects and restore them later. And the second:
1
2
3
4
5
6
T* t1p;
T* t2p;
// provided that t2p points to an initialized object ...
std::memcpy(t1p, t2p, sizeof(T));
// at this point, every subobject of trivially copyable type in *t1p contains
// the same value as the corresponding subobject in *t2p 
, meaning that bit-wise copy can be used to assign the objects.

However, this only applies for classes without complex structure. Self contained, with no references, no pointers, etc. If the object's state is interconnected with the rest of the system, binary snapshot is not enough.

The functions and static member variables are not part of the object data. This is class information. Additionally, virtual functions (if present in the class) require 4 bytes of additional memory in each object (single penalty for the entire set of such functions), few times that if there is multiple inheritance, and about 4 more bytes for each virtual base of the class.
Jan 1, 2011 at 11:43pm
hi simeonz,

I think I will probably be modifying the infrastructure of the classes, I would like my application to be robust and able to handle large amounts of data. If i do it the way i originally thought i will end up saving all attributes (even the ones that remain unchanged), and this would not be good for memory.

plus my classes are using a generic linked list structure that forms the graph/network, so it's full of pointers and really quite complex. It's good to know the standards though, and i will take note of your reply.

oh, and thanks for answering my original question about the object storage. rather amateurishly i don't have any virtual functions, but it's just as well i will be modifying the infrastructure so it has a self contained undo func anyway.
Topic archived. No new replies allowed.