as far as I know the well known Instrumentation Java method is unable to correctly calculate the deep size of an object.  Is there a reliable way to compute on the JVM the correct deep size of an object?  The use case I'm thinking about is a fixed (or upper bounded) memory size data structure, i.e. a cache.  Note: as far as possible, I would like an enterprise-ready solution, so either a "standard" coding practice or a well tested library
13148
I have an application that reads a CSV file with piles of data rows.  I give the user a summary of the number of rows based on types of data, but I want to make sure that I don't read in too many rows of data and cause OutOfMemoryErrors.  Each row translates into an object.  Is there an easy way to find out the size of that object programmatically?  Is there a reference that defines how large primitive types and object references are for a VM?  Right now, I have code that says read up to 32,000 rows, but I'd also like to have code that says read as many rows as possible until I've used 32MB of memory.  Maybe that is a different question, but I'd still like to know.
51785
I'd like to implement a big int class in C++ as a programming exercise&mdash;a class that can handle numbers bigger than a long int.  I know that there are several open source implementations out there already, but I'd like to write my own.  I'm trying to get a feel for what the right approach is.  I understand that the general strategy is get the number as a string, and then break it up into smaller numbers (single digits for example), and place them in an array.  At this point it should be relatively simple to implement the various comparison operators.  My main concern is how I would implement things like addition and multiplication.  I'm looking for a general approach and advice as opposed to actual working code.
77222
I have a really simple Java class that effectively decorates a Map with input validation, with the obvious void set() and String get() methods.  I'd like to be able to effectively call those methods and handle return values and exceptions from outside the JVM, but still on the same machine Update: the caller I have in mind is not another JVM; thanks @Dave Ray  My implementation considerations are typical    performance ease of implementation and maintenance (simplicity?) reliability flexibility (i.e. can I call from a remote machine, etc.)   Is there a 'right way?' If not, what are my options, and what are the pro/cons for each?  (Stuff people have actually done and can provide real-life feedback on would be great!)
114309
How to calculate the length (in pixels) of a string in Java?  Preferable without using Swing.  EDIT: I would like to draw the string using the drawString() in Java2D  and use the length for word wrapping.
114469
I was wondering what could be the size of an object of an empty class. It surely could not be 0 bytes since it should be possible to reference and point to it like any other object. But, how big is such an object?  I used this small program:  #include &lt;iostream&gt; using namespace std;  class Empty {};  int main() {     Empty e;     cerr &lt;&lt; sizeof(e) &lt;&lt; endl;     return 0; }   The output I got on both Visual C++ and Cygwin-g++ compilers was 1 byte! This was a little surprising to me since I was expecting it to be of the size of the machine word (32 bits or 4 bytes).  Can anyone explain why the size of 1 byte? Why not 4 bytes? Is this dependent on compiler or the machine too? Also, can someone give a more cogent reason for why an empty class object will not be of size 0 bytes?
45890
Question  I'm looking for a Java in-memory object caching API. Any recommendations? What solutions have you used in the past?  Current  Right now, I'm just using a Map:  Map cache = new HashMap&lt;String, Object&gt;(); cache.put("key", value);   Requirements  I need to extend the cache to include basic features like:   Max size Time to live   However, I don't need more sophisticated features like:   Access from multiple processes (caching server) Persistence (to disk)   Suggestions  In-Memory caching:    CacheBuilder - active development. See this .  - Config via API. No TTL. Not purpose built for caching.  - XML config. Mailing list. Last updated 2006.  - XML config. Documentation in Russian. Last updated 2006.   Enterprise caching:    - Properties config. Extensive documentation.  - XML config. Extensive documentation. By far the most popular according to Google hits.
14268
I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryError: Java heap space error because I am not being conservative on memory usage. The user can open unlimited number of files, and the program keeps the opened objects in the memory. After a quick research I found  and others saying on Windows machine the JVM defaults max heap size as 64MB.  Given this situation, how should I deal with this constraint?  I could increase the max heap size using command line option to java, but that would require figuring out available RAM and writing some launching program or script. Besides, increasing to some finite max does not ultimately get rid of the issue.  I could rewrite some of my code to persist objects to file system frequently (using database is the same thing) to free up the memory. It could work, but it's probably a lot work too.  If you could point me to details of above ideas or some alternatives like automatic virtual memory, extending heap size dynamically, that will be great.
81643
What is the purpose of the LongLength property for arrays in .Net.  Using a standard integer for length, you could accommodate up to 2 billion indices.  Are there really people using .Net to maintain a single array with more the 2 billion elements.  Even if each element was a single byte, that would still be 2 GB of data.   Is it feasible to use such a large array in .Net?
7206
I know that python has a len() function that is used to determine the size of a string, but I was wondering why it's not a method of the string object.  Update  Ok, I realized I was embarrassingly mistaken. __len__() is actually a method of a string object. It just seems weird to see object oriented code in Python using the len function on string objects. Furthermore, it's also weird to see __len__ as the name instead of just len.
7862
I want to know how to get size of objects like a string, integer, etc. in Python.  Related question:   I am using an XML file which contains size fields that specify the size of value. I must parse this XML and do my coding. When I want to change the value of a particular field, I will check the size field of that value. Here I want to compare whether the new value that I'm gong to enter is of the same size as in XML. I need to check the size of new value. In case of a string I can say its the length. But in case of int, float, etc. I am confused.
12769
Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to  , this is in fact implementation dependent as the standard doesn't say what the complexity has to be. The comment  says:     Actually, it depends on which STL you   are using. Microsoft Visual Studio V6   implements size() as {return (_Size);   } whereas gcc (at least in versions   3.3.2 and 4.1.0) do it as { return std::distance(begin(), end()); } The   first has constant speed, the second   has o(N) speed    So my guess is that for the VC++ crowd size() has constant complexity as Dinkumware probably won't have changed that fact since VC6. Am I right there? What does it look like currently in gcc? If it is really O(n), why did the developers choose to do so?
9476
Imagine you have a large array of floating point numbers, of all kinds of sizes. What is the most correct way to calculate the sum, with the least error? For example, when the array looks like this:  [1.0, 1e-10, 1e-10, ... 1e-10.0]   and you add up from left to right with a simple loop, like  sum = 0 numbers.each do |val|     sum += val end   whenever you add up the smaller numbers might fall below the precision threshold so the error gets bigger and bigger. As far as I know the best way is to sort the array and start adding up numbers from lowest to highest, but I am wondering if there is an even better way (faster, more precise)?  EDIT: Thanks for the answer, I now have a working code that perfectly sums up double values in Java. It is a straight port from the Python post of the winning answer. The solution passes all of my unit tests. (A longer but optimized version of this is available here )  /**  * Adds up numbers in an array with perfect precision, and in O(n).  *   * @see http://code.activestate.com/recipes/393090/  */ public class Summarizer {      /**      * Perfectly sums up numbers, without rounding errors (if at all possible).      *       * @param values      *            The values to sum up.      * @return The sum.      */     public static double msum(double... values) {         List&lt;Double&gt; partials = new ArrayList&lt;Double&gt;();         for (double x : values) {             int i = 0;             for (double y : partials) {                 if (Math.abs(x) &lt; Math.abs(y)) {                     double tmp = x;                     x = y;                     y = tmp;                 }                 double hi = x + y;                 double lo = y - (hi - x);                 if (lo != 0.0) {                     partials.set(i, lo);                     ++i;                 }                 x = hi;             }             if (i &lt; partials.size()) {                 partials.set(i, x);                 partials.subList(i + 1, partials.size()).clear();             } else {                 partials.add(x);             }         }         return sum(partials);     }      /**      * Sums up the rest of the partial numbers which cannot be summed up without      * loss of precision.      */     public static double sum(Collection&lt;Double&gt; values) {         double s = 0.0;         for (Double d : values) {             s += d;         }         return s;     } }
222
In several C++ examples I see a use of the type size_t where I would have used a simple int. What's the difference, and why size_t should be better?
38175
For example: sizeof(char*) returns 4.  As does int*, long long*, everything that I've tried.  Are there any exceptions to this?
26439
Is there any performance difference between the for loops on a primitive array?   Assume:  double[] doubleArray = new double[300000];   for (double var: doubleArray)     someComplexCalculation(var);   or :  for ( int i = 0, y = doubleArray.length; i &lt; y; i++)    someComplexCalculation(doubleArray[i]);   Test result  I actually profiled it:  Total timeused for modern loop= 13269ms Total timeused for old loop   = 15370ms   So the modern loop actually runs faster, at least on my Mac OSX JVM 1.5.
26977
In Java, an array IS AN Object. My question is... is an Object constructor called when new arrays is being created? We would like to use this fact to instrument Object constructor with some extra bytecode which checks length of array being constructed. Would that work?
95816
DML is not allowed in a constructor. Is there a known reason behind this limit?  I have been searching for the answer &amp; haven't found anything which addresses the question. May be it's not documented anywhere(Or google doesn't know where it is).