Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7694829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:24:26+00:00 2026-05-31T21:24:26+00:00

I need to build a huge XML file, about 1-50 MB. I thought that

  • 0

I need to build a huge XML file, about 1-50 MB. I thought that using builder would be effective enough and, well it is, somewhat. The problem is, after the program reaches its last line it doesn’t end immediately, but Ruby is still doing something for several seconds, maybe garbage collection? After that the program finally ends.

To give a real example, I am measured the time of building an XML file. It outputs 55 seconds (there is a database behind so it takes long) when the XML was built, but Ruby still processes for about 15 more seconds and the processor is going crazy.

The pseudo/real code is as follows:

...
builder = Nokogiri::XML::Builder.with(doc) do |xml|
  build_node(xml)
end
...

def build_node(xml)
  ...
  xml["#{namespace}"] if namespace  
  xml.send("#{elem_name}", attrs_hash) do |elem_xml|
  ...
    if has_children
      if type
        case type
          when XML::TextContent::PLAIN
            elem_xml.text text_content
          when XML::TextContent::COMMENT
            elem_xml.comment text_content
          when XML::TextContent::CDATA
            elem_xml.cdata text_content
         end
       else
         build_node(elem_xml)
       end
    end
  end
end

Note that I was using a different approach using my own structure of classes, and the speed of the build was the same, but at the last line the program normally ended, but now I am forced to use Nokogiri so I have to find a solution.

What I can do to avoid that X seconds long overhead after the XML is built? Is it even possible?

UPDATE:

Thanks to a suggestion from Adiel Mittmann, during the creation of my minimal working example I was able to locate the problem. I now have a small (well not that small) example demonstrating the problem.

The following code is causing the problem:

xml.send("#{elem_name}_") do |elem_xml|
  ...
  elem_xml.text text_content #This line is the problem
  ...
end

So the line executes the following code based on Nokogiri’s documentation:

def create_text_node string, &block
  Nokogiri::XML::Text.new string.to_s, self, &block
end

Text node creation code gets executed then. So, what exactly is happening here?

UPDATE 2:

After some other tries, the problem can be easily reproduced by:

builder = Nokogiri::XML::Builder.new do |xml|
  0.upto(81900) do
    xml.text "test"
  end
end
puts "End"

So is it really Nokogiri itself? Is there any option for me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:24:28+00:00Added an answer on May 31, 2026 at 9:24 pm

    Your example also takes a long time to execute here. And you were right: it’s the garbage collector that’s taking so long to execute. Try this:

    require 'nokogiri'
    class A
      def a
        builder = Nokogiri::XML::Builder.new do |xml|
          0.upto(81900) do
            xml.text "test"
          end
        end
      end
    end
    A.new.a
    puts "End1"
    GC.start
    puts "End2"
    

    Here, the delay happens between "End1" and "End2". After "End2" is printed, the program closes immediately.

    Notice that I created an object to demonstrate it. Otherwise, the data generated by the builder can only be garbage collected when the program finishes.

    As for the best way to do what you’re trying to accomplish, I suggest you ask another question giving details of what exactly you’re trying to do with the XML files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have huge class that I need to build stub for. To give you
I need to build something that starts serving a H.264 encoded video to a
we have a big portal that build using SharePoint 2007 , asp.net 3.5 ,
I need a huge faver from you. I have build a website, his address
We have huge stack of xml files (around 5000+ files) possibly about 80 MB
I need to build a system to efficiently store & maintain a huge amount
For a personal project, I need to build a forum using PHP and MySQL.
I need to build a simple, single user database application for Windows. Main requirements
I need to build a managed DLL, targeted for x64, and expose it via
We need to build an administration portal website to support our client/server application. Since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.