Hi all,
I'm a python beginner, and I have a problem with no solution I can
see:
I want to index all instances of an object by an internal id (not the
hash) and be able to retrieve them later.
I created a class-level dictionary (Item._itemById) where I add every
new instance. My problem is:
How do I delete the instances when the class is no longer in use?
( having it in a map makes it 'in use' all the time, so my
implementation results in a memory leak :( ).
Here's the code:
class Item():
"""This is the base class for all data items"""
_idBase = None
_count = 0
_itemsById = {}
def __init__(self, title = ''):
Item._count += 1
self.id = Item._generateId(self)
Item._itemsById[self.id] = self
self.title = title
def __del__(self):
Item._count -= 1
# Should I do something here to delete the instance?
# Will this ever be called if the instance is in a static
dictionary?
def _generateId(self):
"""Generates an unique id for the instance. The id will be
unique
for both the current run and all runs in general.
"""
if not Item._idBase:
Item._idBase = str(hash(self))
return Item._idBase + '-' + str(Item._count).zfill(6)
def getItemById(id):
return _itemsById[id] 4 1097
On Oct 2, 7:54*am, Dan Barbus <dan.bar...@gmail.comwrote:
>
* * def getItemById(id):
* * * * return _itemsById[id]
I just saw that this won't compile. Still, ignoring this, I thing the
purpose of the code is pretty clear.
Thanks for any feedback.
On Oct 1, 10:01*pm, Dan Barbus <dan.bar...@gmail.comwrote:
On Oct 2, 7:54*am, Dan Barbus <dan.bar...@gmail.comwrote:
* * def getItemById(id):
* * * * return _itemsById[id]
I just saw that this won't compile. Still, ignoring this, I thing the
purpose of the code is pretty clear.
Thanks for any feedback.
Check the weakref module, you can create a WeakValueDictionary which
will not increment the refcount of the value, and will return None
after it's been garbage collected.
On Wed, 01 Oct 2008 21:54:26 -0700, Dan Barbus wrote:
def __del__(self):
Item._count -= 1
# Should I do something here to delete the instance?
# Will this ever be called if the instance is in a static
dictionary?
Don't use the `__del__()` method. It is not reliable as there are almost
no guarantees when or if at all it will be called. But its sole
existence may be the cause that objects of that class can't be garbage
collected. Read the documentation of `__del__()` for details.
Ciao,
Marc 'BlackJack' Rintsch
On Oct 1, 10:35*pm, Jason Scheirer <jason.schei...@gmail.comwrote:
On Oct 1, 10:01*pm, Dan Barbus <dan.bar...@gmail.comwrote:
On Oct 2, 7:54*am, Dan Barbus <dan.bar...@gmail.comwrote:
* * def getItemById(id):
* * * * return _itemsById[id]
I just saw that this won't compile. Still, ignoring this, I thing the
purpose of the code is pretty clear.
Thanks for any feedback.
Check the weakref module, you can create a WeakValueDictionary which
will not increment the refcount of the value, and will return None
after it's been garbage collected.
Another idea: have an AbstractFactory that keeps track of instances
created. When you done, delete the abstract factory, which will
delete the internal index. You'll have to figure out how to pass the
factory around, and when to delete it. Weakref sounds better, overall. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ranjeet.gupta |
last post by:
Dear All
Is the Root Cause of the Memory corruption is the Memory leak, ??
suppose If in the code there is Memory leak, Do this may lead to the
Memory Corruption while executing the program ?
...
|
by: Don Nell |
last post by:
Hello
Why is there a memory leak when this code is executed.
for(;;)
{
ManagementScope scope = new ManagementScope();
scope.Options.Username="username";
scope.Options.Password="password";...
|
by: jeevankodali |
last post by:
Hi
I have an .Net application which processes thousands of Xml nodes each
day and for each node I am using around 30-40 Regex matches to see if
they satisfy some conditions are not. These Regex...
|
by: James |
last post by:
The following code will create memory leaks!!!
using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
namespace MemoryLeak
|
by: Salvador |
last post by:
Hi,
I am using WMI to gather information about different computers (using win2K
and win 2K3), checking common classes and also WMI load balance. My
application runs every 1 minute and reports...
|
by: Jim Land |
last post by:
Jack Slocum claims here
http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript-
memory-leaks/
that "almost every site you visit that uses JavaScript is leaking memory".
...
|
by: klopter |
last post by:
I will describe what I want using the vector class and then point out the limitations of using vector.
vector <MyClass> MyContainer.
MyContainer.push( new MyClass(...) )
MyContainer now...
|
by: Ragnar Agustsson |
last post by:
Hi all
I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.
I have a 3rd party library, written in C++, that leaks a...
|
by: Peter |
last post by:
I am using VS2008.
I have a Windows Service application which creates Crystal Reports. This is
a multi theaded application which can run several reports at one time.
My problem - there is a...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |