<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: memory code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 01:34:16 GMT</pubDate>
    <description>DZone Snippets: memory code</description>
    <item>
      <title>Reduce memory footprint of a .NET application</title>
      <link>http://snippets.dzone.com/posts/show/4093</link>
      <description>Don&#8217;t ask me anything. It just works :)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; try&lt;br /&gt;{&lt;br /&gt;Process process = Process.GetCurrentProcess();&lt;br /&gt;process.MaxWorkingSet = loProcess.MaxWorkingSet;&lt;br /&gt;process.Dispose();&lt;br /&gt;}&lt;br /&gt;catch { }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;p.s. you can even make a timer that could run this piece of code periodically. </description>
      <pubDate>Sat, 02 Jun 2007 10:39:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4093</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>ActiveRecord each_by_page</title>
      <link>http://snippets.dzone.com/posts/show/3557</link>
      <description>Perform an operation on a set of models including ActiveRecord callbacks, without instantiating all models at once.&lt;br /&gt;&lt;br /&gt;Step through and instantiate each member of the class and execute on it, but instantiate no more than per_page instances at any given time.&lt;br /&gt;Safe for destructive actions or actions that modify the fields your :order or :conditions clauses operate on.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  class Base&lt;br /&gt;    def each_by_page per_page, options = {}, &amp;block&lt;br /&gt;      # By-id for model-modifying blocks&lt;br /&gt;      # Build SQL to get ids of all matching records using the options provided by the user&lt;br /&gt;      sql = construct_finder_sql(options.dup.merge({ :select =&gt; 'id' }))&lt;br /&gt;      # Get the results as an array of tiny hashes { "id" =&gt; "1" } and flatten them out to just the ids&lt;br /&gt;      all_ids = connection.select_all(sql).map { |h| h['id'] }&lt;br /&gt;      at_a_time = 0..(per_page-1)&lt;br /&gt; &lt;br /&gt;      # chop apart the all_ids array a segment at a time&lt;br /&gt;      begin&lt;br /&gt;        ids = all_ids.slice!(at_a_time)&lt;br /&gt;        ids_cases = []&lt;br /&gt;        ids.each_with_index { |id, i| ids_cases &lt;&lt; "WHEN #{id} THEN #{i}" }&lt;br /&gt;        ids_cases = ids_cases.join(' ')&lt;br /&gt; &lt;br /&gt;        # Do the deed on this page of results&lt;br /&gt;        find(:all, options.merge(&lt;br /&gt;          :conditions =&gt; [ 'id IN (?)', ids ],&lt;br /&gt;          :order =&gt; "CASE id #{ids_cases} END"&lt;br /&gt;        )).each &amp;block&lt;br /&gt; &lt;br /&gt;      end until all_ids.empty?&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dweebd.com/ruby/activerecord-pagination-for-destructive-migrations/#comment-3"&gt;link to blog entry&lt;/a&gt;</description>
      <pubDate>Tue, 20 Feb 2007 23:59:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3557</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
    <item>
      <title>Unix Shared Memory</title>
      <link>http://snippets.dzone.com/posts/show/2585</link>
      <description>If you want to use multiple blocks of shared memory in a single process&lt;br /&gt;you might need to pass a different id to ftok() for each one.  In any case,&lt;br /&gt;the main entry point is open_shm().  It returns a block of shared memory&lt;br /&gt;of the specified size.  If justreading is 1, then you access what has been&lt;br /&gt;written to that block of shared mem.  If it's 0, then you create a new one&lt;br /&gt;with undefined contents.&lt;br /&gt;&lt;br /&gt;PDIE is left as an exercise to the reader...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* This code written by Nick Welch &lt;mack@incise.org&gt;, 2006.&lt;br /&gt; *&lt;br /&gt; * This code is in the public domain&lt;br /&gt; * and is provided AS IS, with NO WARRANTY. */&lt;br /&gt;&lt;br /&gt;static key_t get_shm_key(void)&lt;br /&gt;{&lt;br /&gt;    /* both arbitrary */&lt;br /&gt;    const char * KEY_PATH = "/dev/null";&lt;br /&gt;    const char KEY_ID = 0xc4; &lt;br /&gt;&lt;br /&gt;    key_t key = ftok(KEY_PATH, KEY_ID);&lt;br /&gt;    if (key == (key_t)-1)&lt;br /&gt;        PDIE("ftok");&lt;br /&gt;&lt;br /&gt;    return key;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static int get_shm_min(void)&lt;br /&gt;{&lt;br /&gt;    struct shminfo info;&lt;br /&gt;    if((shmctl(0, IPC_INFO, (struct shmid_ds *)(void *)&amp;info)) == -1)&lt;br /&gt;        PDIE("shmctl (shminfo)");&lt;br /&gt;    return info.shmmin;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void * open_shm(size_t bytes, int justreading)&lt;br /&gt;{&lt;br /&gt;    void * shm;&lt;br /&gt;    key_t key = get_shm_key();&lt;br /&gt;&lt;br /&gt;    int id; &lt;br /&gt;&lt;br /&gt;    if(!justreading)&lt;br /&gt;    {&lt;br /&gt;        /* we need to delete it in case we need to allocate more memory this&lt;br /&gt;         * time than last time.  if we try that without first deleting it, we&lt;br /&gt;         * will get EINVAL.&lt;br /&gt;         */&lt;br /&gt;&lt;br /&gt;        id = shmget(key, get_shm_min(), 0644|IPC_CREAT);&lt;br /&gt;&lt;br /&gt;        if(id == -1)&lt;br /&gt;            PDIE("shmget (first call)");&lt;br /&gt;&lt;br /&gt;        shm = shmat(id, NULL, 0);&lt;br /&gt;        if (shm == NULL)&lt;br /&gt;            PDIE("shmat(first call)");&lt;br /&gt;&lt;br /&gt;        if(shmctl(id, IPC_RMID, NULL) == -1)&lt;br /&gt;            PDIE("shmctl (marking shm segment for removal)");&lt;br /&gt;&lt;br /&gt;        if(shmdt(shm) == -1)&lt;br /&gt;            PDIE("shmdt (detaching shm segment to delete it)");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    id = shmget(key, bytes, justreading ? 0 : 0644|IPC_CREAT);&lt;br /&gt;    if(id == -1)&lt;br /&gt;        PDIE("shmget (second call)");&lt;br /&gt;&lt;br /&gt;    shm = shmat(id, NULL, 0);&lt;br /&gt;    if (shm == NULL)&lt;br /&gt;        PDIE("shmat (second call)");&lt;br /&gt;&lt;br /&gt;    return shm;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 11 Sep 2006 12:40:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2585</guid>
      <author>mackstann (Nick Welch)</author>
    </item>
    <item>
      <title>C memory manager "MemoryBlock "</title>
      <link>http://snippets.dzone.com/posts/show/437</link>
      <description>&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com&lt;br /&gt;&lt;br /&gt;#ifndef __MEMORYBLOCK__&lt;br /&gt;#define __MEMORYBLOCK__&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//-- includes -----------------------------------------------&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//-- data types ---------------------------------------------&lt;br /&gt;typedef struct _MemoryBlock {&lt;br /&gt;	void *data;&lt;br /&gt;	size_t capacity, used, dataSize;&lt;br /&gt;}* MemoryBlock;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//-- functions ----------------------------------------------&lt;br /&gt;MemoryBlock getMemoryBlock( register const size_t dataSize, register const size_t initialSize ){&lt;br /&gt;&lt;br /&gt;	MemoryBlock mb = (MemoryBlock)malloc( sizeof( struct _MemoryBlock ) );&lt;br /&gt;	&lt;br /&gt;	if( mb ) {&lt;br /&gt;		mb-&gt;used = 0;&lt;br /&gt;		mb-&gt;data = NULL;&lt;br /&gt;		mb-&gt;dataSize = dataSize;&lt;br /&gt;		mb-&gt;capacity = initialSize &amp;&amp; ( mb-&gt;data = (void *)malloc( initialSize * dataSize ) ) ? initialSize : 0;&lt;br /&gt;	}&lt;br /&gt;	return mb;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void freeMemoryBlock( register const MemoryBlock mb ){&lt;br /&gt;	free( mb-&gt;data );&lt;br /&gt;	free( mb );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int resizeMemoryBlock( register const size_t newSize, register const MemoryBlock mb ){&lt;br /&gt;&lt;br /&gt;	void *newData = (void *)realloc( mb-&gt;data, newSize * mb-&gt;dataSize );&lt;br /&gt;&lt;br /&gt;	if( newData ){&lt;br /&gt;		mb-&gt;data = newData;&lt;br /&gt;		return mb-&gt;capacity = newSize;&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int growMemoryBlock( register const MemoryBlock mb ){&lt;br /&gt;&lt;br /&gt;	size_t delta = mb-&gt;capacity &gt; 64 ? mb-&gt;capacity / 4 : mb-&gt;capacity &gt; 8 ? 16 : 4;&lt;br /&gt;	void *newData = (void *)realloc( mb-&gt;data, ( mb-&gt;capacity + delta ) * mb-&gt;dataSize );&lt;br /&gt;&lt;br /&gt;	if( newData ){&lt;br /&gt;		mb-&gt;data = newData;&lt;br /&gt;		return mb-&gt;capacity += delta;&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int growExMemoryBlock( register const size_t neededBlocks, register const MemoryBlock mb ){&lt;br /&gt;	while( mb-&gt;used+neededBlocks &gt; mb-&gt;capacity )&lt;br /&gt;		if( !growMemoryBlock( mb ) )&lt;br /&gt;			return 0;&lt;br /&gt;	return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int incMemoryBlock( register const MemoryBlock mb ){&lt;br /&gt;	return  mb-&gt;used+1 &lt;= mb-&gt;capacity || growMemoryBlock( mb );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#endif&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jul 2005 03:53:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/437</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
