A
A
Alexey2015-02-14 19:51:03
Programming
Alexey, 2015-02-14 19:51:03

How to "pin an object" on the heap?

There is a collection of elements that can be updated frequently and is constantly growing in size. I read Richter, the question arose of how to make an object non-portable on the heap and is it possible to immediately attribute it to the 2nd generation? Or avoid it being garbage collected? Collection as a source of news feed.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MrDywar Pichugin, 2015-02-22
@Priest512

Have you read Richter carefully?
The second generation is large objects (the specific size depends on the PC), immediately the 2nd generation.
An object can be staked at a specific address in memory, and this is also written in the book.
I don’t remember exactly, but it seems that volatile not only makes the object thread-safe, but also prohibits the compiler from optimizing a piece of code. (read again).
Echoing the notes, here is the answer:
Monitor and manage the garbage collector on an object.
We call the static Alloc method of the GCHandle object, pass a reference to the object and the GCHandleType in which:
1) Weak - monitoring, we find that the object is no longer available, the finalizer could be executed.
2) WeakTrackResurrection - monitoring, we find out that the object is no longer available, the finalizer has definitely been executed (if any).
3) Normal - control, forces to leave the object in memory, the memory occupied by this object can be compressed.
4) Pinned - control, forces the object to remain in memory, the memory occupied by this object cannot be compressed (ie moved).

S
Sergey Mozhaykin, 2015-02-14
@smozhaykin

If you want unused memory to be freed during incremental loading, look towards virtualization. Links provided here

C
CrazyHorse, 2015-02-14
@CrazyHorse

"And is it possible to immediately attribute it to the 2nd generation?" as they say, it smells bad and leads to performance problems in the long run.
In order for the garbage collector not to touch your collection, it can be, for example, static, but this in no way means that it should be done this way.
"Collection as a source of news feed." - if this is a web application, then I don’t see any problems in creating this collection every time based on a database query - on the page, then, as a rule, you have -10-20 news output from strength.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question