473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Which uses less memory?

I'm not sure if this is as easy a question as I'd like it to be, but
here goes....

I'm working on an application that is very memory intensive, so we're
trying to reduce the memory footprint of classes wherever possible. I
have a need for a class which is able to have a type identifier which
can be examined at run-time to determine whether that class (called
Domain) contains data I care about or not.

I've thought of two ways to implement this:

1. Add a type attribute and set it to a descriptive string.
2. Create marker classes and use multiple inheritance to "attach"
these markers to specific Domains.

Here's the kicker: I need to serialize these Domains and send them to/
from Java code as well as work on them using Python. We're looking to
use Hessian and pyactivemq to handle the Java/Python interfaces.
Which, I guess, leads to the following group of questions:

1. Which method has the smaller footprint within the Python engine?
2. Do these protocols (Hessian and Stomp) preserve the class
information when the class is serialized?

Any input would be most welcome. Thanks!
Bret Wortman
Nov 15 '07 #1
2 1353
br**********@gmail.com <br**********@gmail.comwrote:
I'm not sure if this is as easy a question as I'd like it to be, but
here goes....

I'm working on an application that is very memory intensive, so we're
trying to reduce the memory footprint of classes wherever possible. I
have a need for a class which is able to have a type identifier which
can be examined at run-time to determine whether that class (called
Domain) contains data I care about or not.

I've thought of two ways to implement this:

1. Add a type attribute and set it to a descriptive string.
2. Create marker classes and use multiple inheritance to "attach"
these markers to specific Domains.

Here's the kicker: I need to serialize these Domains and send them to/
from Java code as well as work on them using Python. We're looking to
use Hessian and pyactivemq to handle the Java/Python interfaces.
Which, I guess, leads to the following group of questions:

1. Which method has the smaller footprint within the Python engine?
2. Do these protocols (Hessian and Stomp) preserve the class
information when the class is serialized?

Any input would be most welcome. Thanks!
I don't know the answers to your specific questions but we managed to
1/3 the memory requirement of our app by identifying the class having
the most instances (about 1,000,000 in our case) and adding __slots__
to it!

I'd guess that if you __slot__-ed the Domain class then you'll find
the overhead of a type attribute is minimal (4 bytes per instance I
think).

No idea about Hessian or Stomp (never heard of them!) but classes with
__slot__s are normal classes which would pickle or unpickle.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 17 '07 #2
En Sat, 17 Nov 2007 19:30:04 -0300, Nick Craig-Wood <ni**@craig-wood.com>
escribi�:
> I'm working on an application that is very memory intensive, so we're
trying to reduce the memory footprint of classes wherever possible. I

I'd guess that if you __slot__-ed the Domain class then you'll find
the overhead of a type attribute is minimal (4 bytes per instance I
think).

No idea about Hessian or Stomp (never heard of them!) but classes with
__slot__s are normal classes which would pickle or unpickle.
Actually classes with __slots__ require that you write your own
__getstate__ and __setstate__. The following implementation may be enough
in simple cases:

pyclass X(object):
.... __slots__ = ('foo','bar')
....
pyx = X()
pyx.foo = 123
pydumps(x)
Traceback (most recent call last):
....
TypeError: a class that defines __slots__ without defining __getstate__
cannot b
e pickled

class X(object):
__slots__ = ('foo','bar')
def __getstate__(self):
return dict((name, getattr(self, name))
for name in self.__slots__
if hasattr(self, name))
def __setstate__(self, state):
for name,value in state.iteritems():
setattr(self, name, value)

pyx = X()
pyx.foo = 123
pyp = dumps(x)
pyx2 = loads(p)
pytype(x2)
<class '__main__.X'>
pyx2.foo
123
pyx2.bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: bar

--
Gabriel Genellina

Nov 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
25
by: CodeCracker | last post by:
Problem details below: I have few items(simple values or objects) to be put into an array and I implement it through a set rather than just an array of items. This is because every time I get a...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
4
by: **Developer** | last post by:
If I need to allocate memory, maybe because I'm going to: Marshal.StructureToPtr Does it matter which of the following I use? Marshal.AllocCoTaskMem Marshal.AllocHGlobal I know the arguments...
5
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character...
11
by: Divick | last post by:
Hi, can somebody help me figure out how can I make write a function which inturn uses malloc routine to allocate memory which is 2^k aligned? The condition is such that there should not be any...
20
by: khan | last post by:
Since we did not want to make an error in counting of bytes, we used the code char *p; ... p = malloc(strlen("hello")+1); strcpy(p,"hello"); instead of the intended
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.