Reduce memory pressure on system malloc when allocating large items on the GC heap
authorSimon Hausmann <simon.hausmann@theqtcompany.com>
Mon, 27 Oct 2014 12:51:27 +0000 (13:51 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Mon, 27 Oct 2014 14:19:15 +0000 (15:19 +0100)
Try to free our large items for every 8 megabytes of additional large item
memory we allocate. This helps in particular on 32-bit builds and was noticable
in tst_qqmlecmascript::push_and_shift().

Change-Id: I4cc0b188e58ccaf32026e38c7aaf1cfadc83148b
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/jsruntime/qv4mm.cpp

index 442a276..b9a4a55 100644 (file)
@@ -104,6 +104,7 @@ struct MemoryManager::Data
     };
 
     LargeItem *largeItems;
+    std::size_t totalLargeItemsAllocated;
 
     GCDeletable *deletable;
 
@@ -120,6 +121,7 @@ struct MemoryManager::Data
         , maxShift(6)
         , maxChunkSize(32*1024)
         , largeItems(0)
+        , totalLargeItemsAllocated(0)
         , deletable(0)
     {
         memset(smallItems, 0, sizeof(smallItems));
@@ -185,6 +187,9 @@ Managed *MemoryManager::allocData(std::size_t size)
 
     // doesn't fit into a small bucket
     if (size >= MemoryManager::Data::MaxItemSize) {
+        if (m_d->totalLargeItemsAllocated > 8 * 1024 * 1024)
+            runGC();
+
         // we use malloc for this
         MemoryManager::Data::LargeItem *item = static_cast<MemoryManager::Data::LargeItem *>(
                 malloc(Q_V4_PROFILE_ALLOC(m_d->engine, size + sizeof(MemoryManager::Data::LargeItem),
@@ -193,6 +198,7 @@ Managed *MemoryManager::allocData(std::size_t size)
         item->next = m_d->largeItems;
         item->size = size;
         m_d->largeItems = item;
+        m_d->totalLargeItemsAllocated += size;
         return item->managed();
     }
 
@@ -476,6 +482,7 @@ void MemoryManager::runGC()
 
     memset(m_d->allocCount, 0, sizeof(m_d->allocCount));
     m_d->totalAlloc = 0;
+    m_d->totalLargeItemsAllocated = 0;
 }
 
 size_t MemoryManager::getUsedMem() const