#๐ Actually clearing memory on demand
44 messages ยท Page 1 of 1 (latest)
@gentle hill
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
no, afaik
-# From what I know, python uses an arena allocator, so for something to actually be freed memory, the whole arena has to be empty and that's quite rare
Might've found a lib that handles this. "gc" from the standard python c installation. Maybe use "gc.collect()" after "del {variable}"?
no that's not the same
gc.collect attemps to deallocate objects that for example, is holding a reference to itself
like a list containing itself
Do you have a specific use case where this is required?
!e
In CPython, as soon as nobody if referencing an object, it is automatically destroyed:
class Banana:
def __init__(self, length):
self.length = length
def __del__(self):
print(f"Banana({self.length!r}) is dying")
def some_function():
banana1 = Banana(420)
banana2 = Banana(69)
banana3 = banana2
print("created the bananas...")
banana2 = None
print("set banana2 to None")
banana3 = None
print("set banana3 to None")
some_function()
No, gc.collect does run the collection for everything it can. The docs for it literally start with "run the full collection"
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | created the bananas...
002 | set banana2 to None
003 | Banana(69) is dying
004 | set banana3 to None
005 | Banana(420) is dying
for everything it can.
wdym? Python objects are deallocated whenever their refcounts have dropped to 0, so what I'm saying is running gc.collect won't help if it's refcount isn't 0, even after del 'x'. Where gc.collect can help is for 'unreachable' objects, e.g a list holding itself
python should handle cyclic references, gc is just for controlling that operation
done automatically specifically for types without an implemented __del__ function
iirc
gc is specifically for cyclic references + other things, ref counting is separate (?)
It's for a really sensetive app. Need to get the data out of memory as soon as the work is done
They are not deallocated exactly at that moment. They are only queued to be cleared and gc runs periodically.
If you create a lot of objects and then drop the reference, gc might not be able to keep up (you allocate new objects faster than gc is clearing). That's what gc.collect() is for - force that cleanup to complete fully before you do next command
already looked into it doesn't actually clear the data
How are you checking whether it "clear the data"? Because if you still have a reference to that object to check it, then of course it won't clear - you still have a reference!
Do you have the code somewhere?
And if you use something like weakref, you gotta remember not keep a strong reference to what weakref is referencing (don't save it as another variable, always check it via weakref)
oh does it actually clear it? I only read it and gave up. Didn't look far into it. I could be wrong
so if I get rid of all references, gc.collect will wipe the data from memory? My goal here is to make sure the physical mamory doesn't have any data on it. If it just marks it as empty tht won't do
If an object literally has no references to it, it will be immediately collected
I don't really have anything related to this particular issue written yet. It will just be object I need to get rid of as soon as work is done
so if I don't have it linked to anything else,
var = None
gc.collect()
will do the trick?
gc.collect() is unnecessary
garbage collector doesn't empty unreferenced memory instantly last I checked
As you can read in https://docs.python.org/3/library/gc.html:
This module provides an interface to the optional garbage collector. It provides the ability to disable the collector, tune the collection frequency, and set debugging options. It also provides access to unreachable objects that the collector found but cannot free. Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles.
Python uses two different mechanisms for removing unreachable objects. One is reference counting and the other is the cyclic garbage collector.
Every object has a counter that is incremented when a new reference to it is created, and decremented when that reference is no longer used. When decrementing the reference counter of an object, if it becomes 0, the object is immediately destroyed.
When you have e.g. two objects that store each other in an attribute (a.foo is b and b.foo is a), they will never reach a refcount of 0, even if you can't actually reach any of them outside of this cycle. This is what the second mechanism is for. The gc module is only for this kind of collection.
(If you are familiar with other GC languages, this is unusual, and also the worst way of doing garbage collection as far as performance goes apparently (having both refcounting and the cyclic collector))
yes but it only marks the space as free no? I need it to be overwritten actully. Like write 0s to physical memory.. (Do excuse me if I am mistaken)
Doesn't do it instantly, but does it fast enough for most programs not to have to care about it.
As I said above, it only matters if you're creating objects faster than gc can collect them.
See: https://docs.python.org/3/c-api/memory.html#object-allocators
There's an arena allocator which is used for small allocations (<512 bytes). It doesn't say how these arenas themselves are collected. It's theoretically possible for you to get unlucky and have a large number of these arenas hang around. If your objects are 512 bytes in size or less at some point, the best way to check if this is a problem would be to try it and see what happens.
For larger objects, the default allocator (malloc/free) is used.
If you do get issues with fragmentation or something like that, you can disable arenas at build time and potentially try enabling mimalloc instead of the default allocator.
I think I will just offload it to C++ and do the rest in python. I don't see any way to reliably claer up physical memory in python
If it's important to have as small of a memory footprint as possible, then doing it in C/C++/Rust/Go/etc. could be worth it.
it's not about how much memory I take up. Just need to get sensetive data outta there
Ah, I see what you mean now. You want to literally overwrite the bytes of memory so that they don't contain some sort of secret anymore.
In that case yes, it would be really difficult to control in Python
You could, theoretically, operate on a bytearray and then clear it out once you're done with it. But it's difficult to image what you'll do with it without creating temporary copies of some of the data when passing stuff to libraries.
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.